Skip to content

Commit

Permalink
No need to track module parent in ModuleAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Jul 12, 2024
1 parent 3fc052b commit 70dabfa
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 26 deletions.
6 changes: 1 addition & 5 deletions compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,11 +733,7 @@ impl<'a> ModCollector<'a> {

context.def_interner.add_module_attributes(
mod_id,
ModuleAttributes {
name: mod_name.0.contents.clone(),
location: mod_location,
parent: self.module_id,
},
ModuleAttributes { name: mod_name.0.contents.clone(), location: mod_location },
);
}

Expand Down
5 changes: 0 additions & 5 deletions compiler/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ const IMPL_SEARCH_RECURSION_LIMIT: u32 = 10;
pub struct ModuleAttributes {
pub name: String,
pub location: Location,
pub parent: LocalModuleId,
}

type StructAttributes = Vec<SecondaryAttribute>;
Expand Down Expand Up @@ -1017,10 +1016,6 @@ impl NodeInterner {
self.module_attributes.get(module_id)
}

pub fn try_module_parent(&self, module_id: &ModuleId) -> Option<LocalModuleId> {
self.try_module_attributes(module_id).map(|attrs| attrs.parent)
}

pub fn global_attributes(&self, global_id: &GlobalId) -> &[SecondaryAttribute] {
&self.global_attributes[global_id]
}
Expand Down
37 changes: 23 additions & 14 deletions tooling/lsp/src/requests/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,17 @@ fn format_reference(reference: ReferenceId, args: &ProcessRequestCallbackArgs) -
}
fn format_module(id: ModuleId, args: &ProcessRequestCallbackArgs) -> String {
let module_attributes = args.interner.module_attributes(&id);
let parent_module_id = args.def_maps[&id.krate].modules()[id.local_id.0].parent;

let mut string = String::new();
if format_parent_module_from_module_id(
&ModuleId { krate: id.krate, local_id: module_attributes.parent },
args,
&mut string,
) {
string.push('\n');
if let Some(parent_module_id) = parent_module_id {
if format_parent_module_from_module_id(
&ModuleId { krate: id.krate, local_id: parent_module_id },
args,
&mut string,
) {
string.push('\n');
}
}
string.push_str(" ");
string.push_str("mod ");
Expand Down Expand Up @@ -316,7 +319,6 @@ fn format_parent_module_from_module_id(
CrateId::Stdlib(_) => Some("std".to_string()),
CrateId::Dummy => None,
};

let wrote_crate = if let Some(crate_name) = crate_name {
string.push_str(" ");
string.push_str(&crate_name);
Expand All @@ -329,20 +331,27 @@ fn format_parent_module_from_module_id(
return wrote_crate;
};

let modules = args.def_maps[&module.krate].modules();
let module_data = &modules[module.local_id.0];

if wrote_crate {
string.push_str("::");
} else {
string.push_str(" ");
}

let mut segments = Vec::new();
let mut current_attributes = module_attributes;
while let Some(parent_attributes) = args.interner.try_module_attributes(&ModuleId {
krate: module.krate,
local_id: current_attributes.parent,
}) {
segments.push(&parent_attributes.name);
current_attributes = parent_attributes;
let mut current_parent = module_data.parent;
while let Some(parent) = current_parent {
let parent_attributes = args
.interner
.try_module_attributes(&ModuleId { krate: module.krate, local_id: parent });
if let Some(parent_attributes) = parent_attributes {
segments.push(&parent_attributes.name);
current_parent = modules[module.local_id.0].parent;
} else {
break;
}
}

for segment in segments.iter().rev() {
Expand Down
13 changes: 11 additions & 2 deletions tooling/lsp/src/requests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{collections::HashMap, future::Future};
use std::{
collections::{BTreeMap, HashMap},
future::Future,
};

use crate::{
parse_diff, resolve_workspace_for_source_path,
Expand All @@ -14,7 +17,11 @@ use lsp_types::{
use nargo::insert_all_files_for_workspace_into_file_manager;
use nargo_fmt::Config;
use noirc_driver::file_manager_with_stdlib;
use noirc_frontend::{graph::Dependency, macros_api::NodeInterner};
use noirc_frontend::{
graph::{CrateId, Dependency},
hir::def_map::CrateDefMap,
macros_api::NodeInterner,
};
use serde::{Deserialize, Serialize};

use crate::{
Expand Down Expand Up @@ -278,6 +285,7 @@ pub(crate) struct ProcessRequestCallbackArgs<'a> {
interners: &'a HashMap<String, NodeInterner>,
root_crate_name: String,
root_crate_dependencies: &'a Vec<Dependency>,
def_maps: &'a BTreeMap<CrateId, CrateDefMap>,
}

pub(crate) fn process_request<F, T>(
Expand Down Expand Up @@ -332,6 +340,7 @@ where
interners: &state.cached_definitions,
root_crate_name: package.name.to_string(),
root_crate_dependencies: &context.crate_graph[context.root_crate_id()].dependencies,
def_maps: &context.def_maps,
}))
}
pub(crate) fn find_all_references_in_workspace(
Expand Down

0 comments on commit 70dabfa

Please sign in to comment.