Skip to content

Commit

Permalink
chore: let add_definition_location take a Location (#7185)
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored Jan 24, 2025
1 parent b6fa180 commit c44b626
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 27 deletions.
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 @@ impl<'a> ModCollector<'a> {
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 @@ impl<'a> ModCollector<'a> {
context.def_interner.register_type_alias(
type_alias_id,
name,
location,
visibility,
parent_module_id,
);
Expand All @@ -440,6 +442,7 @@ impl<'a> ModCollector<'a> {
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 @@ impl<'a> ModCollector<'a> {
.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 @@ impl<'a> ModCollector<'a> {
context.def_interner.register_trait(
trait_id,
name.to_string(),
location,
visibility,
parent_module_id,
);
Expand Down Expand Up @@ -1106,7 +1113,7 @@ pub fn collect_struct(
}

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 @@ pub fn collect_enum(
}

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 @@ -954,7 +954,7 @@ impl NodeInterner {
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 @@ impl NodeInterner {
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

0 comments on commit c44b626

Please sign in to comment.