Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lsp): add goto definition for locals #3705

Merged
merged 7 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@

for method in r#impl.methods {
let func_id = context.def_interner.push_empty_fn();
context.def_interner.push_function(func_id, &method.def, module_id);
let location = Location::new(method.span(), self.file_id);
context.def_interner.push_function(func_id, &method.def, module_id, location);
unresolved_functions.push_fn(self.module_id, func_id, method);
}

Expand All @@ -152,7 +153,8 @@

for (_, func_id, noir_function) in &mut unresolved_functions.functions {
noir_function.def.where_clause.append(&mut trait_impl.where_clause.clone());
context.def_interner.push_function(*func_id, &noir_function.def, module);
let location = Location::new(noir_function.def.span, self.file_id);
context.def_interner.push_function(*func_id, &noir_function.def, module, location);
}

let unresolved_trait_impl = UnresolvedTraitImpl {
Expand Down Expand Up @@ -185,7 +187,8 @@
for item in &trait_impl.items {
if let TraitImplItem::Function(impl_method) = item {
let func_id = context.def_interner.push_empty_fn();
context.def_interner.push_function(func_id, &impl_method.def, module);
let location = Location::new(impl_method.span(), self.file_id);
context.def_interner.push_function(func_id, &impl_method.def, module, location);
unresolved_functions.push_fn(self.module_id, func_id, impl_method.clone());
}
}
Expand Down Expand Up @@ -218,7 +221,8 @@

// First create dummy function in the DefInterner
// So that we can get a FuncId
context.def_interner.push_function(func_id, &function.def, module);
let location = Location::new(function.span(), self.file_id);
context.def_interner.push_function(func_id, &function.def, module, location);

// Now link this func_id to a crate level map with the noir function and the module id
// Encountering a NoirFunction, we retrieve it's module_data to get the namespace
Expand Down Expand Up @@ -266,7 +270,9 @@

// Create the corresponding module for the struct namespace
let id = match self.push_child_module(&name, self.file_id, false, false) {
Ok(local_id) => context.def_interner.new_struct(&unresolved, krate, local_id),
Ok(local_id) => {
context.def_interner.new_struct(&unresolved, krate, local_id, self.file_id)
}
Err(error) => {
definition_errors.push((error.into(), self.file_id));
continue;
Expand Down Expand Up @@ -384,14 +390,17 @@
let modifiers = FunctionModifiers {
name: name.to_string(),
visibility: crate::FunctionVisibility::Public,
// TODO(Maddiaa): Investigate trait implementations with attributes see: https://github.com/noir-lang/noir/issues/2629

Check warning on line 393 in compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (Maddiaa)
attributes: crate::token::Attributes::empty(),
is_unconstrained: false,
contract_function_type: None,
is_internal: None,
};

context.def_interner.push_function_definition(func_id, modifiers, id.0);
let location = Location::new(name.span(), self.file_id);
context
.def_interner
.push_function_definition(func_id, modifiers, id.0, location);

match self.def_collector.def_map.modules[id.0.local_id.0]
.declare_function(name.clone(), func_id)
Expand Down Expand Up @@ -440,7 +449,7 @@
}
}
TraitItem::Type { name } => {
// TODO(nickysn or alexvitkov): implement context.def_interner.push_empty_type_alias and get an id, instead of using TypeAliasId::dummy_id()

Check warning on line 452 in compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (nickysn)

Check warning on line 452 in compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (alexvitkov)
if let Err((first_def, second_def)) = self.def_collector.def_map.modules
[id.0.local_id.0]
.declare_type_alias(name.clone(), TypeAliasId::dummy_id())
Expand Down
6 changes: 4 additions & 2 deletions compiler/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@

/// True if the current module is a contract.
/// This is usually determined by self.path_resolver.module_id(), but it can
/// be overriden for impls. Impls are an odd case since the methods within resolve

Check warning on line 98 in compiler/noirc_frontend/src/hir/resolution/resolver.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (overriden)
/// as if they're in the parent module, but should be placed in a child module.
/// Since they should be within a child module, in_contract is manually set to false
/// for these so we can still resolve them in the parent module without them being in a contract.
Expand Down Expand Up @@ -256,8 +256,9 @@
return self.add_global_variable_decl(name, definition);
}

let id = self.interner.push_definition(name.0.contents.clone(), mutable, definition);
let location = Location::new(name.span(), self.file);
let id =
self.interner.push_definition(name.0.contents.clone(), mutable, definition, location);
let ident = HirIdent { location, id };
let resolver_meta = ResolverMeta { num_times_used: 0, ident, warn_if_unused };

Expand Down Expand Up @@ -300,8 +301,9 @@
ident = hir_let_stmt.ident();
resolver_meta = ResolverMeta { num_times_used: 0, ident, warn_if_unused: true };
} else {
let id = self.interner.push_definition(name.0.contents.clone(), false, definition);
let location = Location::new(name.span(), self.file);
let id =
self.interner.push_definition(name.0.contents.clone(), false, definition, location);
ident = HirIdent { location, id };
resolver_meta = ResolverMeta { num_times_used: 0, ident, warn_if_unused: true };
}
Expand Down
5 changes: 3 additions & 2 deletions compiler/noirc_frontend/src/hir/resolution/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::{BTreeMap, HashSet};

use fm::FileId;
use iter_extended::vecmap;
use noirc_errors::Span;
use noirc_errors::{Location, Span};

use crate::{
graph::CrateId,
Expand Down Expand Up @@ -176,7 +176,8 @@ fn collect_trait_impl_methods(
if let Some(default_impl) = &method.default_impl {
let func_id = interner.push_empty_fn();
let module = ModuleId { local_id: trait_impl.module_id, krate: crate_id };
interner.push_function(func_id, &default_impl.def, module);
let location = Location::new(default_impl.def.span, trait_impl.file_id);
interner.push_function(func_id, &default_impl.def, module, location);
func_ids_in_trait.insert(func_id);
ordered_methods.push((
method.default_impl_module_id,
Expand Down
24 changes: 16 additions & 8 deletions compiler/noirc_frontend/src/hir/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,24 +255,27 @@ mod test {
fn basic_let() {
let mut interner = NodeInterner::default();

// Safety: The FileId in a location isn't used for tests
let file = FileId::default();
let location = Location::new(Span::default(), file);

// Add a simple let Statement into the interner
// let z = x + y;
//
// Push x variable
let x_id = interner.push_definition("x".into(), false, DefinitionKind::Local(None));

// Safety: The FileId in a location isn't used for tests
let file = FileId::default();
let location = Location::new(Span::default(), file);
let x_id =
interner.push_definition("x".into(), false, DefinitionKind::Local(None), location);

let x = HirIdent { id: x_id, location };

// Push y variable
let y_id = interner.push_definition("y".into(), false, DefinitionKind::Local(None));
let y_id =
interner.push_definition("y".into(), false, DefinitionKind::Local(None), location);
let y = HirIdent { id: y_id, location };

// Push z variable
let z_id = interner.push_definition("z".into(), false, DefinitionKind::Local(None));
let z_id =
interner.push_definition("z".into(), false, DefinitionKind::Local(None), location);
let z = HirIdent { id: z_id, location };

// Push x and y as expressions
Expand Down Expand Up @@ -304,7 +307,12 @@ mod test {

let name = HirIdent {
location,
id: interner.push_definition("test_func".into(), false, DefinitionKind::Local(None)),
id: interner.push_definition(
"test_func".into(),
false,
DefinitionKind::Local(None),
location,
),
};

// Add function meta
Expand Down
9 changes: 5 additions & 4 deletions compiler/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
node_interner::{ExprId, NodeInterner, TypeAliasId},
};
use iter_extended::vecmap;
use noirc_errors::Span;
use noirc_errors::{Location, Span};
use noirc_printable_type::PrintableType;

use crate::{node_interner::StructId, Ident, Signedness};
Expand Down Expand Up @@ -166,7 +166,7 @@ pub struct StructType {
fields: Vec<(Ident, Type)>,

pub generics: Generics,
pub span: Span,
pub location: Location,
}

/// Corresponds to generic lists such as `<T, U>` in the source
Expand All @@ -191,11 +191,12 @@ impl StructType {
pub fn new(
id: StructId,
name: Ident,
span: Span,

location: Location,
fields: Vec<(Ident, Type)>,
generics: Generics,
) -> StructType {
StructType { id, fields, name, span, generics }
StructType { id, fields, name, location, generics }
}

/// To account for cyclic references between structs, a struct's
Expand Down
18 changes: 13 additions & 5 deletions compiler/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ pub struct DefinitionInfo {
pub name: String,
pub mutable: bool,
pub kind: DefinitionKind,
pub location: Location,
}

impl DefinitionInfo {
Expand Down Expand Up @@ -518,6 +519,7 @@ impl NodeInterner {
typ: &UnresolvedStruct,
krate: CrateId,
local_id: LocalModuleId,
file_id: FileId,
) -> StructId {
let struct_id = StructId(ModuleId { krate, local_id });
let name = typ.struct_def.name.clone();
Expand All @@ -533,7 +535,8 @@ impl NodeInterner {
(id, Shared::new(TypeBinding::Unbound(id)))
});

let new_struct = StructType::new(struct_id, name, typ.struct_def.span, no_fields, generics);
let location = Location::new(typ.struct_def.span, file_id);
let new_struct = StructType::new(struct_id, name, location, no_fields, generics);
self.structs.insert(struct_id, Shared::new(new_struct));
self.struct_attributes.insert(struct_id, typ.struct_def.attributes.clone());
struct_id
Expand Down Expand Up @@ -662,13 +665,14 @@ impl NodeInterner {
name: String,
mutable: bool,
definition: DefinitionKind,
location: Location,
) -> DefinitionId {
let id = DefinitionId(self.definitions.len());
if let DefinitionKind::Function(func_id) = definition {
self.function_definition_ids.insert(func_id, id);
}

self.definitions.push(DefinitionInfo { name, mutable, kind: definition });
self.definitions.push(DefinitionInfo { name, mutable, kind: definition, location });
id
}

Expand All @@ -679,7 +683,8 @@ impl NodeInterner {
let mut modifiers = FunctionModifiers::new();
modifiers.name = name;
let module = ModuleId::dummy_id();
self.push_function_definition(id, modifiers, module);
let location = Location::dummy();
self.push_function_definition(id, modifiers, module, location);
id
}

Expand All @@ -688,6 +693,7 @@ impl NodeInterner {
id: FuncId,
function: &FunctionDefinition,
module: ModuleId,
location: Location,
) -> DefinitionId {
use ContractFunctionType::*;

Expand All @@ -701,19 +707,20 @@ impl NodeInterner {
contract_function_type: Some(if function.is_open { Open } else { Secret }),
is_internal: Some(function.is_internal),
};
self.push_function_definition(id, modifiers, module)
self.push_function_definition(id, modifiers, module, location)
}

pub fn push_function_definition(
&mut self,
func: FuncId,
modifiers: FunctionModifiers,
module: ModuleId,
location: Location,
) -> DefinitionId {
let name = modifiers.name.clone();
self.function_modifiers.insert(func, modifiers);
self.function_modules.insert(func, module);
self.push_definition(name, false, DefinitionKind::Function(func))
self.push_definition(name, false, DefinitionKind::Function(func), location)
}

pub fn set_function_trait(&mut self, func: FuncId, self_type: Type, trait_id: TraitId) {
Expand Down Expand Up @@ -1242,6 +1249,7 @@ impl NodeInterner {
DefinitionKind::Function(func_id) => {
Some(self.function_meta(&func_id).location)
}
DefinitionKind::Local(_local_id) => Some(definition_info.location),
_ => None,
}
}
Expand Down
Loading