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

chore: let add_definition_location take a Location #7185

Merged
merged 1 commit into from
Jan 24, 2025
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
32 changes: 20 additions & 12 deletions compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1760,17 +1760,17 @@ impl<'context> Elaborator<'context> {
}
}

let fields_len = fields.len();
if self.interner.is_in_lsp_mode() {
for (field_index, field) in fields.iter().enumerate() {
let location = Location::new(field.name.span(), self.file);
let reference_id = ReferenceId::StructMember(*type_id, field_index);
self.interner.add_definition_location(reference_id, location, None);
}
}

self.interner.update_type(*type_id, |struct_def| {
struct_def.set_fields(fields);
});

for field_index in 0..fields_len {
self.interner.add_definition_location(
ReferenceId::StructMember(*type_id, field_index),
None,
);
}
}

// Check whether the struct fields have nested slices
Expand Down Expand Up @@ -1858,7 +1858,8 @@ impl<'context> Elaborator<'context> {
);

let reference_id = ReferenceId::EnumVariant(*type_id, i);
self.interner.add_definition_location(reference_id, Some(module_id));
let location = Location::new(variant.item.name.span(), self.file);
self.interner.add_definition_location(reference_id, location, Some(module_id));
}
}
}
Expand All @@ -1878,15 +1879,15 @@ impl<'context> Elaborator<'context> {
None
};

let span = let_stmt.pattern.span();

if !self.in_contract()
&& let_stmt.attributes.iter().any(|attr| matches!(attr, SecondaryAttribute::Abi(_)))
{
let span = let_stmt.pattern.span();
self.push_err(ResolverError::AbiAttributeOutsideContract { span });
}

if !let_stmt.comptime && matches!(let_stmt.pattern, Pattern::Mutable(..)) {
let span = let_stmt.pattern.span();
self.push_err(ResolverError::MutableGlobal { span });
}

Expand All @@ -1899,7 +1900,14 @@ impl<'context> Elaborator<'context> {
self.elaborate_comptime_global(global_id);

if let Some(name) = name {
self.interner.register_global(global_id, name, global.visibility, self.module_id());
let location = Location::new(span, self.file);
self.interner.register_global(
global_id,
name,
location,
global.visibility,
self.module_id(),
);
}

self.local_module = old_module;
Expand Down
13 changes: 10 additions & 3 deletions compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@
let doc_comments = type_alias.doc_comments;
let type_alias = type_alias.item;
let name = type_alias.name.clone();
let location = Location::new(name.span(), self.file_id);
let visibility = type_alias.visibility;

// And store the TypeId -> TypeAlias mapping somewhere it is reachable
Expand Down Expand Up @@ -419,6 +420,7 @@
context.def_interner.register_type_alias(
type_alias_id,
name,
location,
visibility,
parent_module_id,
);
Expand All @@ -440,6 +442,7 @@
let doc_comments = trait_definition.doc_comments;
let trait_definition = trait_definition.item;
let name = trait_definition.name.clone();
let location = Location::new(trait_definition.name.span(), self.file_id);

// Create the corresponding module for the trait namespace
let trait_id = match self.push_child_module(
Expand Down Expand Up @@ -533,7 +536,10 @@
.push_function_definition(func_id, modifiers, trait_id.0, location);

let referenced = ReferenceId::Function(func_id);
context.def_interner.add_definition_location(referenced, Some(trait_id.0));
let module_id = Some(trait_id.0);
context
.def_interner
.add_definition_location(referenced, location, module_id);

if !trait_item.doc_comments.is_empty() {
context.def_interner.set_doc_comments(
Expand Down Expand Up @@ -663,6 +669,7 @@
context.def_interner.register_trait(
trait_id,
name.to_string(),
location,
visibility,
parent_module_id,
);
Expand Down Expand Up @@ -904,7 +911,7 @@
// if it's an inline module, or the first char of a the file if it's an external module.
// - `location` will always point to the token "foo" in `mod foo` regardless of whether
// it's inline or external.
// Eventually the location put in `ModuleData` is used for codelenses about `contract`s,

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (codelenses)
// so we keep using `location` so that it continues to work as usual.
let location = Location::new(mod_name.span(), mod_location.file);
let new_module = ModuleData::new(
Expand Down Expand Up @@ -1106,7 +1113,7 @@
}

if interner.is_in_lsp_mode() {
interner.register_type(id, name.to_string(), visibility, parent_module_id);
interner.register_type(id, name.to_string(), location, visibility, parent_module_id);
}

Some((id, unresolved))
Expand Down Expand Up @@ -1201,7 +1208,7 @@
}

if interner.is_in_lsp_mode() {
interner.register_type(id, name.to_string(), visibility, parent_module_id);
interner.register_type(id, name.to_string(), location, visibility, parent_module_id);
}

Some((id, unresolved))
Expand Down
14 changes: 9 additions & 5 deletions compiler/noirc_frontend/src/locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,14 @@ impl NodeInterner {
pub(crate) fn add_definition_location(
&mut self,
referenced: ReferenceId,
referenced_location: Location,
module_id: Option<ModuleId>,
) {
if !self.lsp_mode {
return;
}

let referenced_index = self.get_or_insert_reference(referenced);
let referenced_location = self.reference_location(referenced);
self.location_indices.add_location(referenced_location, referenced_index);
if let Some(module_id) = module_id {
self.reference_modules.insert(referenced, module_id);
Expand Down Expand Up @@ -319,43 +319,47 @@ impl NodeInterner {
&mut self,
id: GlobalId,
name: String,
location: Location,
visibility: ItemVisibility,
parent_module_id: ModuleId,
) {
self.add_definition_location(ReferenceId::Global(id), Some(parent_module_id));
self.add_definition_location(ReferenceId::Global(id), location, Some(parent_module_id));
self.register_name_for_auto_import(name, ModuleDefId::GlobalId(id), visibility, None);
}

pub(crate) fn register_type(
&mut self,
id: TypeId,
name: String,
location: Location,
visibility: ItemVisibility,
parent_module_id: ModuleId,
) {
self.add_definition_location(ReferenceId::Type(id), Some(parent_module_id));
self.add_definition_location(ReferenceId::Type(id), location, Some(parent_module_id));
self.register_name_for_auto_import(name, ModuleDefId::TypeId(id), visibility, None);
}

pub(crate) fn register_trait(
&mut self,
id: TraitId,
name: String,
location: Location,
visibility: ItemVisibility,
parent_module_id: ModuleId,
) {
self.add_definition_location(ReferenceId::Trait(id), Some(parent_module_id));
self.add_definition_location(ReferenceId::Trait(id), location, Some(parent_module_id));
self.register_name_for_auto_import(name, ModuleDefId::TraitId(id), visibility, None);
}

pub(crate) fn register_type_alias(
&mut self,
id: TypeAliasId,
name: String,
location: Location,
visibility: ItemVisibility,
parent_module_id: ModuleId,
) {
self.add_definition_location(ReferenceId::Alias(id), Some(parent_module_id));
self.add_definition_location(ReferenceId::Alias(id), location, Some(parent_module_id));
self.register_name_for_auto_import(name, ModuleDefId::TypeAliasId(id), visibility, None);
}

Expand Down
11 changes: 4 additions & 7 deletions compiler/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,12 @@
interned_statement_kinds: noirc_arena::Arena<StatementKind>,

// Interned `UnresolvedTypeData`s during comptime code.
interned_unresolved_type_datas: noirc_arena::Arena<UnresolvedTypeData>,

Check warning on line 217 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (datas)

// Interned `Pattern`s during comptime code.
interned_patterns: noirc_arena::Arena<Pattern>,

/// Determins whether to run in LSP mode. In LSP mode references are tracked.

Check warning on line 222 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Determins)
pub(crate) lsp_mode: bool,

/// Store the location of the references in the graph.
Expand Down Expand Up @@ -675,7 +675,7 @@
quoted_types: Default::default(),
interned_expression_kinds: Default::default(),
interned_statement_kinds: Default::default(),
interned_unresolved_type_datas: Default::default(),

Check warning on line 678 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (datas)
interned_patterns: Default::default(),
lsp_mode: false,
location_indices: LocationIndices::default(),
Expand Down Expand Up @@ -954,7 +954,7 @@
self.definitions.push(DefinitionInfo { name, mutable, comptime, kind, location });

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

id
Expand All @@ -979,21 +979,18 @@
module: ModuleId,
location: Location,
) -> DefinitionId {
let name_location = Location::new(function.name.span(), location.file);
let modifiers = FunctionModifiers {
name: function.name.0.contents.clone(),
visibility: function.visibility,
attributes: function.attributes.clone(),
is_unconstrained: function.is_unconstrained,
generic_count: function.generics.len(),
is_comptime: function.is_comptime,
name_location: Location::new(function.name.span(), location.file),
name_location,
};
let definition_id = self.push_function_definition(id, modifiers, module, location);

// This needs to be done after pushing the definition since it will reference the
// location that was stored
self.add_definition_location(ReferenceId::Function(id), Some(module));

self.add_definition_location(ReferenceId::Function(id), name_location, Some(module));
definition_id
}

Expand Down Expand Up @@ -2148,11 +2145,11 @@
&mut self,
typ: UnresolvedTypeData,
) -> InternedUnresolvedTypeData {
InternedUnresolvedTypeData(self.interned_unresolved_type_datas.insert(typ))

Check warning on line 2148 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (datas)
}

pub fn get_unresolved_type_data(&self, id: InternedUnresolvedTypeData) -> &UnresolvedTypeData {
&self.interned_unresolved_type_datas[id.0]

Check warning on line 2152 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (datas)
}

/// Returns the type of an operator (which is always a function), along with its return type.
Expand Down
Loading