Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5617aa0
fix: lsp struct reference in return position
asterite Jul 3, 2024
e1ebd06
fix: lsp struct reference in Path
asterite Jul 3, 2024
c3e9c30
Introduce `ReferenceId` as an enum exclusive to the reference graph
asterite Jul 3, 2024
668bc86
feat: lsp "go to definition" for module in call path
asterite Jul 3, 2024
6923fec
feat: lsp "go to" inline module from call path
asterite Jul 3, 2024
c6af4c6
feat: lsp "go to" on `use` module path
asterite Jul 3, 2024
5f8f9b4
feat: lsp "go to" by clicking on module definition ("mod foo")
asterite Jul 3, 2024
9930c11
clippy
asterite Jul 3, 2024
76995b1
feat: lsp rename/find-all-references for traits
asterite Jul 4, 2024
30d0ee4
fix: (lsp) don't rename `self` pointing to struct
asterite Jul 4, 2024
3530c4e
Make sure `Self` isn't renamed but found as a reference
asterite Jul 4, 2024
65a5ad0
Disallow renaming "Self"
asterite Jul 4, 2024
c2d2183
Merge branch 'master' into ab/lsp-struct-more-fixes
asterite Jul 4, 2024
afd6626
clippy
asterite Jul 4, 2024
e68d25a
WIP
asterite Jul 4, 2024
3ff7b2c
feat: lsp rename/find-all-references for type aliases
asterite Jul 4, 2024
c97464a
No need to separately keep track of struct name locations
asterite Jul 4, 2024
5465f6a
No need to separately keep track of alias name locations
asterite Jul 4, 2024
d164f52
No need to separately keep track of trait locations
asterite Jul 4, 2024
b914df0
Merge branch 'master' into ab/lsp-struct-more-fixes
asterite Jul 4, 2024
ab97382
Merge branch 'master' into ab/lsp-struct-more-fixes
asterite Jul 4, 2024
ab3a9a9
Merge branch 'ab/lsp-struct-more-fixes' into ab/lsp-alias-references
asterite Jul 4, 2024
9e097aa
feat: lsp rename/find-all-references for globals
asterite Jul 4, 2024
4968178
Rename ReferenceId::Variable to ReferenceId::Reference
asterite Jul 8, 2024
56b7ea6
lsp rename/find-all-references for local variables
asterite Jul 8, 2024
1abfd18
Merge branch 'master' into ab/lsp-local-variables
asterite Jul 9, 2024
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
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/elaborator/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ impl<'context> Elaborator<'context> {
});

let referenced = ReferenceId::Struct(struct_type.borrow().id);
let reference = ReferenceId::Variable(Location::new(span, self.file), is_self_type);
let reference = ReferenceId::Reference(Location::new(span, self.file), is_self_type);
self.interner.add_reference(referenced, reference);

(expr, Type::Struct(struct_type, generics))
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,7 @@ impl<'context> Elaborator<'context> {
let trait_name = trait_impl.trait_path.last_segment();

let referenced = ReferenceId::Trait(trait_id);
let reference = ReferenceId::Variable(
let reference = ReferenceId::Reference(
Location::new(trait_name.span(), trait_impl.file_id),
trait_name.is_self_type_name(),
);
Expand Down
14 changes: 11 additions & 3 deletions compiler/noirc_frontend/src/elaborator/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl<'context> Elaborator<'context> {
);

let referenced = ReferenceId::Struct(struct_type.borrow().id);
let reference = ReferenceId::Variable(Location::new(name_span, self.file), is_self_type);
let reference = ReferenceId::Reference(Location::new(name_span, self.file), is_self_type);
self.interner.add_reference(referenced, reference);

HirPattern::Struct(expected_type, fields, location)
Expand Down Expand Up @@ -438,6 +438,7 @@ impl<'context> Elaborator<'context> {
// Otherwise, then it is referring to an Identifier
// This lookup allows support of such statements: let x = foo::bar::SOME_GLOBAL + 10;
// If the expression is a singular indent, we search the resolver's current scope as normal.
let span = path.span();
let is_self_type_name = path.last_segment().is_self_type_name();
let (hir_ident, var_scope_index) = self.get_ident_from_path(path);

Expand All @@ -448,7 +449,8 @@ impl<'context> Elaborator<'context> {
self.interner.add_function_dependency(current_item, func_id);
}

let variable = ReferenceId::Variable(hir_ident.location, is_self_type_name);
let variable =
ReferenceId::Reference(hir_ident.location, is_self_type_name);
let function = ReferenceId::Function(func_id);
self.interner.add_reference(function, variable);
}
Expand All @@ -460,7 +462,8 @@ impl<'context> Elaborator<'context> {
self.interner.add_global_dependency(current_item, global_id);
}

let variable = ReferenceId::Variable(hir_ident.location, is_self_type_name);
let variable =
ReferenceId::Reference(hir_ident.location, is_self_type_name);
let global = ReferenceId::Global(global_id);
self.interner.add_reference(global, variable);
}
Expand All @@ -477,6 +480,11 @@ impl<'context> Elaborator<'context> {
DefinitionKind::Local(_) => {
// only local variables can be captured by closures.
self.resolve_local_variable(hir_ident.clone(), var_scope_index);

let referenced = ReferenceId::Local(hir_ident.id);
let reference =
ReferenceId::Reference(Location::new(span, self.file), false);
self.interner.add_reference(referenced, reference);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/elaborator/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl<'context> Elaborator<'context> {
resolver.resolve(self.def_maps, path.clone(), &mut Some(&mut references))?;

for (referenced, ident) in references.iter().zip(path.segments) {
let reference = ReferenceId::Variable(
let reference = ReferenceId::Reference(
Location::new(ident.span(), self.file),
ident.is_self_type_name(),
);
Expand Down
6 changes: 5 additions & 1 deletion compiler/noirc_frontend/src/elaborator/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
macros_api::{
ForLoopStatement, ForRange, HirStatement, LetStatement, Path, Statement, StatementKind,
},
node_interner::{DefinitionId, DefinitionKind, GlobalId, StmtId},
node_interner::{DefinitionId, DefinitionKind, GlobalId, ReferenceId, StmtId},
Type,
};

Expand Down Expand Up @@ -256,6 +256,10 @@ impl<'context> Elaborator<'context> {
typ.follow_bindings()
};

let referenced = ReferenceId::Local(ident.id);
let reference = ReferenceId::Reference(Location::new(span, self.file), false);
self.interner.add_reference(referenced, reference);

(HirLValue::Ident(ident.clone(), typ.clone()), typ, mutable)
}
LValue::MemberAccess { object, field_name, span } => {
Expand Down
7 changes: 4 additions & 3 deletions compiler/noirc_frontend/src/elaborator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl<'context> Elaborator<'context> {

if !is_synthetic {
let referenced = ReferenceId::Struct(struct_type.borrow().id);
let reference = ReferenceId::Variable(
let reference = ReferenceId::Reference(
Location::new(unresolved_span, self.file),
is_self_type_name,
);
Expand All @@ -173,7 +173,7 @@ impl<'context> Elaborator<'context> {
}
Type::Alias(ref alias_type, _) => {
let referenced = ReferenceId::Alias(alias_type.borrow().id);
let reference = ReferenceId::Variable(
let reference = ReferenceId::Reference(
Location::new(unresolved_span, self.file),
is_self_type_name,
);
Expand Down Expand Up @@ -370,7 +370,8 @@ impl<'context> Elaborator<'context> {
}

let referenced = ReferenceId::Global(id);
let reference = ReferenceId::Variable(Location::new(path.span(), self.file), false);
let reference =
ReferenceId::Reference(Location::new(path.span(), self.file), false);
self.interner.add_reference(referenced, reference);

Some(Type::Constant(self.eval_global_as_array_length(id, path)))
Expand Down
4 changes: 2 additions & 2 deletions compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ impl DefCollector {

for (referenced, ident) in references.iter().zip(&collected_import.path.segments) {
let reference =
ReferenceId::Variable(Location::new(ident.span(), file_id), false);
ReferenceId::Reference(Location::new(ident.span(), file_id), false);
context.def_interner.add_reference(*referenced, reference);
}

Expand Down Expand Up @@ -521,7 +521,7 @@ fn add_import_reference(
}
crate::macros_api::ModuleDefId::GlobalId(global_id) => ReferenceId::Global(global_id),
};
let reference = ReferenceId::Variable(Location::new(name.span(), file_id), false);
let reference = ReferenceId::Reference(Location::new(name.span(), file_id), false);
interner.add_reference(referenced, reference);
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ impl<'a> ModCollector<'a> {
Ok(child_mod_id) => {
// Track that the "foo" in `mod foo;` points to the module "foo"
let referenced = ReferenceId::Module(child_mod_id);
let reference = ReferenceId::Variable(location, false);
let reference = ReferenceId::Reference(location, false);
context.def_interner.add_reference(referenced, reference);

errors.extend(collect_defs(
Expand Down
5 changes: 3 additions & 2 deletions compiler/noirc_frontend/src/locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ impl NodeInterner {
let alias_type = alias_type.borrow();
Location::new(alias_type.name.span(), alias_type.location.file)
}
ReferenceId::Variable(location, _) => location,
ReferenceId::Local(id) => self.definition(id).location,
ReferenceId::Reference(location, _) => location,
}
}

Expand Down Expand Up @@ -117,7 +118,7 @@ impl NodeInterner {
let node_index = self.location_indices.get_node_from_location(location)?;

let reference_node = self.reference_graph[node_index];
let referenced_node_index = if let ReferenceId::Variable(_, _) = reference_node {
let referenced_node_index = if let ReferenceId::Reference(_, _) = reference_node {
self.referenced_index(node_index)?
} else {
node_index
Expand Down
12 changes: 10 additions & 2 deletions compiler/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,13 @@ pub enum ReferenceId {
Global(GlobalId),
Function(FuncId),
Alias(TypeAliasId),
Variable(Location, bool /* is Self */),
Local(DefinitionId),
Reference(Location, bool /* is Self */),
}

impl ReferenceId {
pub fn is_self_type_name(&self) -> bool {
matches!(self, Self::Variable(_, true))
matches!(self, Self::Reference(_, true))
}
}

Expand Down Expand Up @@ -836,12 +837,19 @@ impl NodeInterner {
location: Location,
) -> DefinitionId {
let id = DefinitionId(self.definitions.len());
let is_local = matches!(definition, DefinitionKind::Local(_));

if let DefinitionKind::Function(func_id) = definition {
self.function_definition_ids.insert(func_id, id);
}

let kind = definition;
self.definitions.push(DefinitionInfo { name, mutable, comptime, kind, location });

if is_local {
self.add_definition_location(ReferenceId::Local(id));
}

id
}

Expand Down
5 changes: 5 additions & 0 deletions tooling/lsp/src/requests/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,9 @@ mod goto_definition_tests {
)
.await;
}

#[test]
async fn goto_for_local_variable() {
expect_goto_for_all_references("local_variable", "some_var", 0).await;
}
}
7 changes: 6 additions & 1 deletion tooling/lsp/src/requests/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub(crate) fn on_prepare_rename_request(
let reference_id = interner.reference_at_location(location);
let rename_possible = match reference_id {
// Rename shouldn't be possible when triggered on top of "Self"
Some(ReferenceId::Variable(_, true /* is self type name */)) => false,
Some(ReferenceId::Reference(_, true /* is self type name */)) => false,
Some(_) => true,
None => false,
};
Expand Down Expand Up @@ -194,4 +194,9 @@ mod rename_tests {
async fn test_rename_global() {
check_rename_succeeds("rename_global", "FOO").await;
}

#[test]
async fn test_rename_local_variable() {
check_rename_succeeds("local_variable", "some_var").await;
}
}
6 changes: 6 additions & 0 deletions tooling/lsp/test_programs/local_variable/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "local_variable"
type = "bin"
authors = [""]

[dependencies]
5 changes: 5 additions & 0 deletions tooling/lsp/test_programs/local_variable/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
let mut some_var = 1;
some_var = 2;
let _ = some_var;
}