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 rename struct #5380

Merged
merged 58 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 57 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
f941c04
feat(lsp): allow function rename
kobyhallx Feb 7, 2024
8456a58
Merge remote-tracking branch 'origin/master' into kh-rename-functions
kobyhallx Feb 7, 2024
daf83ed
Merge branch 'master' into kh-rename-functions
kobyhallx Feb 8, 2024
e685934
chore: move collection point
kobyhallx Feb 8, 2024
c9ba252
feat: add func rename with imports
kobyhallx Feb 11, 2024
df38fb6
chore: cleanup
kobyhallx Feb 12, 2024
d73f177
Merge remote-tracking branch 'origin/master' into kh-rename-functions
kobyhallx Feb 12, 2024
3cef0a4
Merge branch 'master' into kh-rename-functions
kobyhallx Feb 12, 2024
39d7d56
chore: remove globals for now
kobyhallx Feb 12, 2024
d77236c
chore: clippy
kobyhallx Feb 12, 2024
2d124bd
chore: clippy - unnecesary ret
kobyhallx Feb 12, 2024
09e898f
chore: clippy - map instead and_then
kobyhallx Feb 12, 2024
78913ac
Merge branch 'master' into kh-rename-functions
kobyhallx Feb 12, 2024
9718234
Merge branch 'master' into kh-rename-functions
kobyhallx Feb 12, 2024
ab455b1
chore: review nits
kobyhallx Feb 12, 2024
d7cd372
chore: spells
kobyhallx Feb 12, 2024
4421a2d
chore: name change
kobyhallx Feb 13, 2024
d564ac8
chore: name change
kobyhallx Feb 13, 2024
f0b81a2
Fix bug with renaming imported functions from submodules
jfecher Feb 13, 2024
0b0eca7
Add unreachable cases
jfecher Feb 13, 2024
7cb7bc7
Merge branch 'master' into kh-rename-functions
jfecher Feb 13, 2024
33b50dc
Fix merge
jfecher Feb 13, 2024
7280c9d
Merge branch 'master' into kh-rename-functions
jfecher Feb 22, 2024
0802086
clippy
jfecher Feb 22, 2024
8122dc8
Merge branch 'kh-rename-functions' of https://github.com/noir-lang/no…
jfecher Feb 22, 2024
a9b785c
Fmt and clippy
jfecher Feb 22, 2024
62c5cad
Merge branch 'master' into kh-rename-functions
TomAFrench Feb 22, 2024
a34e4db
Merge branch 'master' into kh-rename-functions
TomAFrench Jun 25, 2024
d598adf
chore: fix merge
TomAFrench Jun 25, 2024
9d5577c
Merge branch 'master' into kh-rename-functions
asterite Jun 27, 2024
7724b01
Add tests for lsp rename, and use the legacy resolver to make them work
asterite Jun 27, 2024
d18e0a9
Implement lsp rename using elaborator
asterite Jun 27, 2024
52ce934
Check that matches actually match the name being renamed
asterite Jun 27, 2024
4db9e6c
Use separate programs for lsp tests
asterite Jun 27, 2024
aee04c7
clippy
asterite Jun 27, 2024
58bc455
Merge branch 'master' into kh-rename-functions
asterite Jun 27, 2024
2d89ab4
Merge branch 'master' into kh-rename-functions
asterite Jun 27, 2024
2d92617
Undo changes to resolver
asterite Jun 27, 2024
a2fc465
Don't use legacy resolver in another place either
asterite Jun 27, 2024
8b5aaae
Skip empty location spans (otherwise they produce a panic)
asterite Jun 27, 2024
33a695c
Merge branch 'master' into kh-rename-functions
asterite Jun 27, 2024
4da3335
Remove some code duplication in lsp rename tests
asterite Jun 28, 2024
b9f3d76
Extract test_utils::init_lsp_server
asterite Jun 28, 2024
bf87231
process_rename_request only needs TextDocumentPositionParams
asterite Jun 28, 2024
07b2ccb
chore: refactor tests to make it easier to check all cases
TomAFrench Jun 28, 2024
b8dbd04
chore: fix type
TomAFrench Jun 28, 2024
684389a
chore: fmt
TomAFrench Jun 28, 2024
7a2f0f7
chore: add instance of qualified path to test program
TomAFrench Jun 28, 2024
7532717
Let lsp rename function work well with qualified paths
asterite Jun 28, 2024
303dc9b
Remove extra tests, and move consts inside functions
asterite Jun 28, 2024
641c74f
Merge branch 'master' into kh-rename-functions
asterite Jun 28, 2024
2181263
Rename test programs
asterite Jul 1, 2024
8347496
feat: lsp rename now works for structs
asterite Jul 1, 2024
4e75387
Easier way to test renames
asterite Jul 2, 2024
41de529
Test that struct rename works in `use` statements
asterite Jul 2, 2024
86644d4
Check that renaming a function works in a `use` statement
asterite Jul 2, 2024
1d01781
Only track references when running LSP
asterite Jul 2, 2024
a403340
Merge branch 'master' into ab/lsp-rename-struct
asterite Jul 2, 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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions compiler/noirc_frontend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ regex = "1.9.1"
cfg-if = "1.0.0"
tracing.workspace = true
petgraph = "0.6"
rangemap = "1.4.0"
lalrpop-util = { version = "0.20.2", features = ["lexer"] }


Expand Down
7 changes: 6 additions & 1 deletion compiler/noirc_frontend/src/elaborator/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
HirLiteral, HirStatement, Ident, IndexExpression, Literal, MemberAccessExpression,
MethodCallExpression, PrefixExpression,
},
node_interner::{DefinitionKind, ExprId, FuncId},
node_interner::{DefinitionKind, DependencyId, ExprId, FuncId},
token::Tokens,
Kind, QuotedType, Shared, StructType, Type,
};
Expand Down Expand Up @@ -431,6 +431,11 @@ impl<'context> Elaborator<'context> {
r#type,
struct_generics,
});

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

(expr, Type::Struct(struct_type, generics))
}

Expand Down
22 changes: 18 additions & 4 deletions compiler/noirc_frontend/src/elaborator/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
stmt::HirPattern,
},
macros_api::{HirExpression, Ident, Path, Pattern},
node_interner::{DefinitionId, DefinitionKind, ExprId, GlobalId, TraitImplKind},
node_interner::{DefinitionId, DefinitionKind, DependencyId, ExprId, GlobalId, TraitImplKind},
Shared, StructType, Type, TypeBindings,
};

Expand Down Expand Up @@ -157,6 +157,8 @@ impl<'context> Elaborator<'context> {
mutable: Option<Span>,
new_definitions: &mut Vec<HirIdent>,
) -> HirPattern {
let name_span = name.last_segment().span();

let error_identifier = |this: &mut Self| {
// Must create a name here to return a HirPattern::Identifier. Allowing
// shadowing here lets us avoid further errors if we define ERROR_IDENT
Expand Down Expand Up @@ -196,6 +198,10 @@ impl<'context> Elaborator<'context> {
new_definitions,
);

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

HirPattern::Struct(expected_type, fields, location)
}

Expand Down Expand Up @@ -418,10 +424,14 @@ impl<'context> Elaborator<'context> {

if hir_ident.id != DefinitionId::dummy_id() {
match self.interner.definition(hir_ident.id).kind {
DefinitionKind::Function(id) => {
DefinitionKind::Function(func_id) => {
if let Some(current_item) = self.current_item {
self.interner.add_function_dependency(current_item, id);
self.interner.add_function_dependency(current_item, func_id);
}

let variable = DependencyId::Variable(hir_ident.location);
let function = DependencyId::Function(func_id);
self.interner.add_reference(function, variable);
}
DefinitionKind::Global(global_id) => {
if let Some(global) = self.unresolved_globals.remove(&global_id) {
Expand All @@ -430,6 +440,10 @@ impl<'context> Elaborator<'context> {
if let Some(current_item) = self.current_item {
self.interner.add_global_dependency(current_item, global_id);
}

let variable = DependencyId::Variable(hir_ident.location);
let global = DependencyId::Global(global_id);
self.interner.add_reference(global, variable);
}
DefinitionKind::GenericType(_) => {
// Initialize numeric generics to a polymorphic integer type in case
Expand Down Expand Up @@ -575,7 +589,7 @@ impl<'context> Elaborator<'context> {
}

pub fn get_ident_from_path(&mut self, path: Path) -> (HirIdent, usize) {
let location = Location::new(path.span(), self.file);
let location = Location::new(path.last_segment().span(), self.file);

let error = match path.as_ident().map(|ident| self.use_variable(ident)) {
Some(Ok(found)) => return found,
Expand Down
11 changes: 10 additions & 1 deletion compiler/noirc_frontend/src/elaborator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ use crate::{
HirExpression, HirLiteral, HirStatement, Path, PathKind, SecondaryAttribute, Signedness,
UnaryOp, UnresolvedType, UnresolvedTypeData,
},
node_interner::{DefinitionKind, ExprId, GlobalId, TraitId, TraitImplKind, TraitMethodId},
node_interner::{
DefinitionKind, DependencyId, ExprId, GlobalId, TraitId, TraitImplKind, TraitMethodId,
},
Generics, Kind, ResolvedGeneric, Type, TypeBinding, TypeVariable, TypeVariableKind,
};

Expand Down Expand Up @@ -242,6 +244,8 @@ impl<'context> Elaborator<'context> {
return Type::Alias(alias, args);
}

let last_segment = path.last_segment();

match self.lookup_struct_or_error(path) {
Some(struct_type) => {
if self.resolving_ids.contains(&struct_type.borrow().id) {
Expand Down Expand Up @@ -279,6 +283,11 @@ impl<'context> Elaborator<'context> {
self.interner.add_type_dependency(current_item, dependency_id);
}

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

Type::Struct(struct_type, args)
}
None => Type::Error,
Expand Down
33 changes: 31 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 @@ -20,17 +20,18 @@ use crate::hir::Context;

use crate::macros_api::{MacroError, MacroProcessor};
use crate::node_interner::{
FuncId, GlobalId, NodeInterner, StructId, TraitId, TraitImplId, TypeAliasId,
DependencyId, FuncId, GlobalId, NodeInterner, StructId, TraitId, TraitImplId, TypeAliasId,
};

use crate::ast::{
ExpressionKind, Ident, LetStatement, Literal, NoirFunction, NoirStruct, NoirTrait,
NoirTypeAlias, Path, PathKind, UnresolvedGenerics, UnresolvedTraitConstraint, UnresolvedType,
};

use crate::parser::{ParserError, SortedModule};
use fm::FileId;
use iter_extended::vecmap;
use noirc_errors::{CustomDiagnostic, Span};
use noirc_errors::{CustomDiagnostic, Location, Span};
use std::collections::{BTreeMap, HashMap};

use std::vec;
Expand Down Expand Up @@ -327,6 +328,7 @@ impl DefCollector {

// Resolve unresolved imports collected from the crate, one by one.
for collected_import in std::mem::take(&mut def_collector.imports) {
let module_id = collected_import.module_id;
match resolve_import(crate_id, &collected_import, &context.def_maps) {
Ok(resolved_import) => {
if let Some(error) = resolved_import.error {
Expand All @@ -344,6 +346,9 @@ impl DefCollector {
let result = current_def_map.modules[resolved_import.module_scope.0]
.import(name.clone(), ns, resolved_import.is_prelude);

let file_id = current_def_map.file_id(module_id);
add_import_reference(ns, &name, &mut context.def_interner, file_id);

if let Err((first_def, second_def)) = result {
let err = DefCollectorErrorKind::Duplicate {
typ: DuplicateType::Import,
Expand Down Expand Up @@ -467,6 +472,30 @@ impl DefCollector {
}
}

fn add_import_reference(
def_id: crate::macros_api::ModuleDefId,
name: &Ident,
interner: &mut NodeInterner,
file_id: FileId,
) {
if name.span() == Span::empty(0) {
// We ignore empty spans at 0 location, this must be Stdlib
return;
}

match def_id {
crate::macros_api::ModuleDefId::FunctionId(func_id) => {
let variable = DependencyId::Variable(Location::new(name.span(), file_id));
interner.add_reference_for(DependencyId::Function(func_id), variable);
}
crate::macros_api::ModuleDefId::TypeId(struct_id) => {
let variable = DependencyId::Variable(Location::new(name.span(), file_id));
interner.add_reference_for(DependencyId::Struct(struct_id), variable);
}
_ => (),
}
}

fn inject_prelude(
crate_id: CrateId,
context: &Context,
Expand Down
8 changes: 7 additions & 1 deletion compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
TypeImpl,
};
use crate::macros_api::NodeInterner;
use crate::node_interner::DependencyId;
use crate::{
graph::CrateId,
hir::def_collector::dc_crate::{UnresolvedStruct, UnresolvedTrait},
Expand Down Expand Up @@ -265,6 +266,7 @@
let mut definition_errors = vec![];
for struct_definition in types {
let name = struct_definition.name.clone();
let name_location = Location::new(name.span(), self.file_id);

let unresolved = UnresolvedStruct {
file_id: self.file_id,
Expand Down Expand Up @@ -308,6 +310,9 @@

// And store the TypeId -> StructType mapping somewhere it is reachable
self.def_collector.items.types.insert(id, unresolved);

context.def_interner.add_struct_location(id, name_location);
context.def_interner.add_definition_location(DependencyId::Struct(id));
}
definition_errors
}
Expand Down Expand Up @@ -413,6 +418,7 @@
let func_id = context.def_interner.push_empty_fn();
method_ids.insert(name.to_string(), func_id);

let location = Location::new(name.span(), self.file_id);
let modifiers = FunctionModifiers {
name: name.to_string(),
visibility: ItemVisibility::Public,
Expand All @@ -421,9 +427,9 @@
is_unconstrained: false,
generic_count: generics.len(),
is_comptime: false,
name_location: location,
};

let location = Location::new(name.span(), self.file_id);
context
.def_interner
.push_function_definition(func_id, modifiers, trait_id.0, location);
Expand Down Expand Up @@ -481,7 +487,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 490 in compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (nickysn)

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (alexvitkov)
if let Err((first_def, second_def)) = self.def_collector.def_map.modules
[trait_id.0.local_id.0]
.declare_type_alias(name.clone(), TypeAliasId::dummy_id())
Expand Down
5 changes: 5 additions & 0 deletions compiler/noirc_frontend/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,9 @@ impl Context<'_, '_> {
ResolvedGeneric { name, type_var, kind, span }
})
}

// Enables reference tracking (useful for tools like LSP).
pub fn track_references(&mut self) {
self.def_interner.track_references = true;
}
}
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1512,7 +1512,7 @@ impl<'a> Resolver<'a> {
// 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 (hir_ident, var_scope_index) = self.get_ident_from_path(path);
let (hir_ident, var_scope_index) = self.get_ident_from_path(path.clone());

if hir_ident.id != DefinitionId::dummy_id() {
match self.interner.definition(hir_ident.id).kind {
Expand Down
1 change: 1 addition & 0 deletions compiler/noirc_frontend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod debug;
pub mod elaborator;
pub mod graph;
pub mod lexer;
pub mod locations;
pub mod monomorphization;
pub mod node_interner;
pub mod parser;
Expand Down
Loading
Loading