Skip to content

Commit

Permalink
feat: lsp rename/find-all-references for globals (#5415)
Browse files Browse the repository at this point in the history
# Description

## Problem

Resolves #5359

## Summary

Based on top of #5414


https://github.com/noir-lang/noir/assets/209371/c4b75068-5fdf-4eb5-93b7-69e4a52db2d5

## Additional Context

None

## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
asterite authored Jul 9, 2024
1 parent e153ecb commit fa9b444
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 39 deletions.
2 changes: 2 additions & 0 deletions compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,8 @@ impl<'context> Elaborator<'context> {
self.elaborate_comptime_global(global_id);
}

self.interner.add_definition_location(ReferenceId::Global(global_id));

self.local_module = old_module;
self.file = old_file;
self.current_item = old_item;
Expand Down
15 changes: 12 additions & 3 deletions compiler/noirc_frontend/src/elaborator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,6 @@ impl<'context> Elaborator<'context> {
};

if let Some(unresolved_span) = typ.span {
let reference =
ReferenceId::Variable(Location::new(unresolved_span, self.file), is_self_type_name);

match resolved_type {
Type::Struct(ref struct_type, _) => {
// Record the location of the type reference
Expand All @@ -167,11 +164,19 @@ impl<'context> Elaborator<'context> {

if !is_synthetic {
let referenced = ReferenceId::Struct(struct_type.borrow().id);
let reference = ReferenceId::Variable(
Location::new(unresolved_span, self.file),
is_self_type_name,
);
self.interner.add_reference(referenced, reference);
}
}
Type::Alias(ref alias_type, _) => {
let referenced = ReferenceId::Alias(alias_type.borrow().id);
let reference = ReferenceId::Variable(
Location::new(unresolved_span, self.file),
is_self_type_name,
);
self.interner.add_reference(referenced, reference);
}
_ => (),
Expand Down Expand Up @@ -364,6 +369,10 @@ impl<'context> Elaborator<'context> {
self.interner.add_global_dependency(current_item, id);
}

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

Some(Type::Constant(self.eval_global_as_array_length(id, path)))
}
_ => None,
Expand Down
27 changes: 10 additions & 17 deletions compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,25 +511,18 @@ fn add_import_reference(
return;
}

match def_id {
crate::macros_api::ModuleDefId::FunctionId(func_id) => {
let variable = ReferenceId::Variable(Location::new(name.span(), file_id), false);
interner.add_reference(ReferenceId::Function(func_id), variable);
}
crate::macros_api::ModuleDefId::TypeId(struct_id) => {
let variable = ReferenceId::Variable(Location::new(name.span(), file_id), false);
interner.add_reference(ReferenceId::Struct(struct_id), variable);
}
crate::macros_api::ModuleDefId::TraitId(trait_id) => {
let variable = ReferenceId::Variable(Location::new(name.span(), file_id), false);
interner.add_reference(ReferenceId::Trait(trait_id), variable);
}
let referenced = match def_id {
crate::macros_api::ModuleDefId::ModuleId(module_id) => ReferenceId::Module(module_id),
crate::macros_api::ModuleDefId::FunctionId(func_id) => ReferenceId::Function(func_id),
crate::macros_api::ModuleDefId::TypeId(struct_id) => ReferenceId::Struct(struct_id),
crate::macros_api::ModuleDefId::TraitId(trait_id) => ReferenceId::Trait(trait_id),
crate::macros_api::ModuleDefId::TypeAliasId(type_alias_id) => {
let variable = ReferenceId::Variable(Location::new(name.span(), file_id), false);
interner.add_reference(ReferenceId::Alias(type_alias_id), variable);
ReferenceId::Alias(type_alias_id)
}
_ => (),
}
crate::macros_api::ModuleDefId::GlobalId(global_id) => ReferenceId::Global(global_id),
};
let reference = ReferenceId::Variable(Location::new(name.span(), file_id), false);
interner.add_reference(referenced, reference);
}

fn inject_prelude(
Expand Down
30 changes: 11 additions & 19 deletions compiler/noirc_frontend/src/locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,26 +117,18 @@ impl NodeInterner {
let node_index = self.location_indices.get_node_from_location(location)?;

let reference_node = self.reference_graph[node_index];
let found_locations: Vec<Location> = match reference_node {
ReferenceId::Global(_) | ReferenceId::Module(_) => todo!(),
ReferenceId::Function(_)
| ReferenceId::Struct(_)
| ReferenceId::Trait(_)
| ReferenceId::Alias(_) => self.find_all_references_for_index(
node_index,
include_referenced,
include_self_type_name,
),

ReferenceId::Variable(_, _) => {
let referenced_node_index = self.referenced_index(node_index)?;
self.find_all_references_for_index(
referenced_node_index,
include_referenced,
include_self_type_name,
)
}
let referenced_node_index = if let ReferenceId::Variable(_, _) = reference_node {
self.referenced_index(node_index)?
} else {
node_index
};

let found_locations = self.find_all_references_for_index(
referenced_node_index,
include_referenced,
include_self_type_name,
);

Some(found_locations)
}

Expand Down
5 changes: 5 additions & 0 deletions tooling/lsp/src/requests/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,9 @@ mod rename_tests {
async fn test_rename_type_alias() {
check_rename_succeeds("rename_type_alias", "Bar").await;
}

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

[dependencies]
22 changes: 22 additions & 0 deletions tooling/lsp/test_programs/rename_global/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
mod foo {
global FOO = 1;
}

use foo::FOO;

fn main() {
let _ = foo::FOO;
let _ = FOO;
let _: [Field; FOO] = [1];
}

trait WithNumber<N> {

}

struct Some {
}

impl WithNumber<FOO> for Some {

}
7 changes: 7 additions & 0 deletions tooling/lsp/test_programs/rename_type_alias/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ mod foo {
}

type Bar = Foo;

mod bar {
struct Baz {

}
}
}

use foo::Foo;
use foo::Bar;
use foo::bar;

fn main() {
let x: Bar = Foo {};
Expand Down

0 comments on commit fa9b444

Please sign in to comment.