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: consolidate usage of dummy_id #4362

Closed
wants to merge 5 commits into from
Closed
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
7 changes: 3 additions & 4 deletions compiler/noirc_frontend/src/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ pub enum CrateId {
Root(usize),
Crate(usize),
Stdlib(usize),
Dummy,
}

impl CrateId {
pub fn dummy_id() -> CrateId {
CrateId::Dummy
}
// pub fn dummy_id() -> CrateId {
// CrateId::Dummy
// }

pub fn is_stdlib(&self) -> bool {
matches!(self, CrateId::Stdlib(_))
Expand Down
22 changes: 11 additions & 11 deletions compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl UnresolvedFunctions {
pub fn resolve_trait_bounds_trait_ids(
&mut self,
def_maps: &BTreeMap<CrateId, CrateDefMap>,
crate_id: CrateId,
crate_id: Option<CrateId>,
) -> Vec<DefCollectorErrorKind> {
let mut errors = Vec::new();

Expand Down Expand Up @@ -80,7 +80,7 @@ pub struct UnresolvedStruct {
pub struct UnresolvedTrait {
pub file_id: FileId,
pub module_id: LocalModuleId,
pub crate_id: CrateId,
pub crate_id: Option<CrateId>,
pub trait_def: NoirTrait,
pub method_ids: HashMap<String, FuncId>,
pub fns_with_default_impl: UnresolvedFunctions,
Expand Down Expand Up @@ -228,7 +228,7 @@ impl DefCollector {

let dep_def_root =
context.def_map(&dep.crate_id).expect("ice: def map was just created").root;
let module_id = ModuleId { krate: dep.crate_id, local_id: dep_def_root };
let module_id = ModuleId { krate: Some(dep.crate_id), local_id: dep_def_root };
// Add this crate as a dependency by linking it's root module
def_map.extern_prelude.insert(dep.as_name(), module_id);
}
Expand All @@ -248,18 +248,18 @@ impl DefCollector {
ast,
root_file_id,
crate_root,
crate_id,
Some(crate_id),
context,
));

let submodules = vecmap(def_collector.def_map.modules().iter(), |(index, _)| index);
// Add the current crate to the collection of DefMaps
context.def_maps.insert(crate_id, def_collector.def_map);

inject_prelude(crate_id, context, crate_root, &mut def_collector.collected_imports);
inject_prelude(Some(crate_id), context, crate_root, &mut def_collector.collected_imports);
for submodule in submodules {
inject_prelude(
crate_id,
Some(crate_id),
context,
LocalModuleId(submodule),
&mut def_collector.collected_imports,
Expand Down Expand Up @@ -310,7 +310,7 @@ impl DefCollector {
errors.extend(resolve_type_aliases(
context,
def_collector.collected_type_aliases,
crate_id,
Some(crate_id),
));

errors.extend(resolve_traits(context, def_collector.collected_traits, crate_id));
Expand Down Expand Up @@ -361,7 +361,7 @@ impl DefCollector {
functions.extend(resolve_trait_impls(
context,
def_collector.collected_traits_impls,
crate_id,
Some(crate_id),
&mut errors,
));

Expand All @@ -382,7 +382,7 @@ impl DefCollector {
}

fn inject_prelude(
crate_id: CrateId,
crate_id: Option<CrateId>,
context: &Context,
crate_root: LocalModuleId,
collected_imports: &mut Vec<ImportDirective>,
Expand All @@ -395,14 +395,14 @@ fn inject_prelude(
let path =
Path { segments: segments.clone(), kind: crate::PathKind::Dep, span: Span::default() };

if !crate_id.is_stdlib() {
if !crate_id.map(|id| id.is_stdlib()).unwrap_or(false) {
if let Ok(module_def) = path_resolver::resolve_path(
&context.def_maps,
ModuleId { krate: crate_id, local_id: crate_root },
path,
) {
let module_id = module_def.as_module().expect("std::prelude should be a module");
let prelude = context.module(module_id).scope().names();
let prelude = context.module(module_id).map(scope).unwrap_or_else(|| ItemScope::default()).names();

for path in prelude {
let mut segments = segments.clone();
Expand Down
20 changes: 10 additions & 10 deletions compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn collect_defs(
ast: SortedModule,
file_id: FileId,
module_id: LocalModuleId,
crate_id: CrateId,
crate_id: Option<CrateId>,
context: &mut Context,
) -> Vec<(CompilationError, FileId)> {
let mut collector = ModCollector { def_collector, file_id, module_id };
Expand Down Expand Up @@ -115,7 +115,7 @@ impl<'a> ModCollector<'a> {
errors
}

fn collect_impls(&mut self, context: &mut Context, impls: Vec<TypeImpl>, krate: CrateId) {
fn collect_impls(&mut self, context: &mut Context, impls: Vec<TypeImpl>, krate: Option<CrateId>) {
let module_id = ModuleId { krate, local_id: self.module_id };

for r#impl in impls {
Expand All @@ -142,7 +142,7 @@ impl<'a> ModCollector<'a> {
&mut self,
context: &mut Context,
impls: Vec<NoirTraitImpl>,
krate: CrateId,
krate: Option<CrateId>,
) {
for trait_impl in impls {
let trait_name = trait_impl.trait_name.clone();
Expand Down Expand Up @@ -178,7 +178,7 @@ impl<'a> ModCollector<'a> {
&mut self,
context: &mut Context,
trait_impl: &NoirTraitImpl,
krate: CrateId,
krate: Option<CrateId>,
) -> UnresolvedFunctions {
let mut unresolved_functions =
UnresolvedFunctions { file_id: self.file_id, functions: Vec::new(), trait_id: None };
Expand All @@ -201,7 +201,7 @@ impl<'a> ModCollector<'a> {
&mut self,
context: &mut Context,
functions: Vec<NoirFunction>,
krate: CrateId,
krate: Option<CrateId>,
) -> Vec<(CompilationError, FileId)> {
let mut unresolved_functions =
UnresolvedFunctions { file_id: self.file_id, functions: Vec::new(), trait_id: None };
Expand Down Expand Up @@ -257,7 +257,7 @@ impl<'a> ModCollector<'a> {
&mut self,
context: &mut Context,
types: Vec<NoirStruct>,
krate: CrateId,
krate: Option<CrateId>,
) -> Vec<(CompilationError, FileId)> {
let mut definition_errors = vec![];
for struct_definition in types {
Expand Down Expand Up @@ -343,7 +343,7 @@ impl<'a> ModCollector<'a> {
&mut self,
context: &mut Context,
traits: Vec<NoirTrait>,
krate: CrateId,
krate: Option<CrateId>,
) -> Vec<(CompilationError, FileId)> {
let mut errors: Vec<(CompilationError, FileId)> = vec![];
for trait_definition in traits {
Expand Down Expand Up @@ -491,7 +491,7 @@ impl<'a> ModCollector<'a> {
fn collect_submodules(
&mut self,
context: &mut Context,
crate_id: CrateId,
crate_id: Option<CrateId>,
submodules: Vec<SortedSubModule>,
file_id: FileId,
) -> Vec<(CompilationError, FileId)> {
Expand Down Expand Up @@ -523,7 +523,7 @@ impl<'a> ModCollector<'a> {
&mut self,
context: &mut Context,
mod_name: &Ident,
crate_id: CrateId,
crate_id: Option<CrateId>,
) -> Vec<(CompilationError, FileId)> {
let mut errors: Vec<(CompilationError, FileId)> = vec![];
let child_file_id =
Expand Down Expand Up @@ -612,7 +612,7 @@ impl<'a> ModCollector<'a> {
// the struct name.
if add_to_parent_scope {
let mod_id = ModuleId {
krate: self.def_collector.def_map.krate,
krate: Some(self.def_collector.def_map.krate),
local_id: LocalModuleId(module_id),
};

Expand Down
14 changes: 9 additions & 5 deletions compiler/noirc_frontend/src/hir/def_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,23 @@ impl LocalModuleId {

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ModuleId {
pub krate: CrateId,
pub krate: Option<CrateId>,
pub local_id: LocalModuleId,
}

impl ModuleId {
pub fn dummy_id() -> ModuleId {
ModuleId { krate: CrateId::dummy_id(), local_id: LocalModuleId::dummy_id() }
ModuleId { krate: None, local_id: LocalModuleId::dummy_id() }
}
}

impl ModuleId {
pub fn module(self, def_maps: &BTreeMap<CrateId, CrateDefMap>) -> &ModuleData {
&def_maps[&self.krate].modules()[self.local_id.0]
pub fn module(self, def_maps: &BTreeMap<CrateId, CrateDefMap>) -> Option<&ModuleData> {
if let Some(krate) = self.krate {
Some(&def_maps[&krate].modules()[self.local_id.0])
} else {
None
}
}
}

Expand Down Expand Up @@ -81,7 +85,7 @@ impl CrateDefMap {
// expect the same crate to be processed twice. It would not
// make the implementation wrong, if the same crate was processed twice, it just makes it slow.
let mut errors: Vec<(CompilationError, FileId)> = vec![];
if context.def_map(&crate_id).is_some() {
if context.def_map(Some(&crate_id)).is_some() {
return errors;
}

Expand Down
38 changes: 19 additions & 19 deletions compiler/noirc_frontend/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ impl Context<'_, '_> {
/// It is perfectly valid for the compiler to look
/// up a CrateDefMap and it is not available.
/// This is how the compiler knows to compile a Crate.
pub fn def_map(&self, crate_id: &CrateId) -> Option<&CrateDefMap> {
self.def_maps.get(crate_id)
pub fn def_map(&self, crate_id: Option<&CrateId>) -> Option<&CrateDefMap> {
crate_id.and_then(|id| self.def_maps.get(id))
}

/// Return the CrateId for each crate that has been compiled
Expand All @@ -110,13 +110,13 @@ impl Context<'_, '_> {
self.def_interner.function_name(id)
}

pub fn fully_qualified_function_name(&self, crate_id: &CrateId, id: &FuncId) -> String {
pub fn fully_qualified_function_name(&self, crate_id: Option<&CrateId>, id: &FuncId) -> String {
let def_map = self.def_map(crate_id).expect("The local crate should be analyzed already");

let name = self.def_interner.function_name(id);

let module_id = self.def_interner.function_module(*id);
let module = self.module(module_id);
let module = self.module(module_id).expect("The local module should be analyzed already");

let parent =
def_map.get_module_path_with_separator(module_id.local_id.0, module.parent, "::");
Expand All @@ -133,21 +133,21 @@ impl Context<'_, '_> {
///
/// For example, if you project contains a `main.nr` and `foo.nr` and you provide the `main_crate_id` and the
/// `bar_struct_id` where the `Bar` struct is inside `foo.nr`, this function would return `foo::Bar` as a [String].
pub fn fully_qualified_struct_path(&self, crate_id: &CrateId, id: StructId) -> String {
pub fn fully_qualified_struct_path(&self, crate_id: Option<&CrateId>, id: StructId) -> String {
let module_id = id.module_id();
let child_id = module_id.local_id.0;
let def_map =
self.def_map(&module_id.krate).expect("The local crate should be analyzed already");
self.def_map((&module_id.krate).as_ref()).expect("The local crate should be analyzed already");

let module = self.module(module_id);
let module = self.module(module_id).expect("The local module should be analyzed already");

let module_path = def_map.get_module_path_with_separator(child_id, module.parent, "::");

if &module_id.krate == crate_id {
if (&module_id.krate).as_ref() == crate_id {
module_path
} else {
let crates = self
.find_dependencies(crate_id, &module_id.krate)
.find_dependencies(crate_id, (&module_id.krate).as_ref())
.expect("The Struct was supposed to be defined in a dependency");
crates.join("::") + "::" + &module_path
}
Expand All @@ -160,14 +160,14 @@ impl Context<'_, '_> {
/// Returns the path from crate_id to target_crate_id
fn find_dependencies(
&self,
crate_id: &CrateId,
target_crate_id: &CrateId,
crate_id: Option<&CrateId>,
target_crate_id: Option<&CrateId>,
) -> Option<Vec<String>> {
for dep in &self.crate_graph[crate_id].dependencies {
if &dep.crate_id == target_crate_id {
for dep in crate_id.map(|id| &self.crate_graph[id].dependencies).unwrap_or(&vec![]) {
if Some(&dep.crate_id) == target_crate_id {
return Some(vec![dep.name.to_string()]);
}
if let Some(mut path) = self.find_dependencies(&dep.crate_id, target_crate_id) {
if let Some(mut path) = self.find_dependencies(Some(&dep.crate_id), target_crate_id) {
path.insert(0, dep.name.to_string());
return Some(path);
}
Expand All @@ -182,7 +182,7 @@ impl Context<'_, '_> {
/// Returns the FuncId of the 'main' function in a crate.
/// - Expects check_crate to be called beforehand
/// - Panics if no main function is found
pub fn get_main_function(&self, crate_id: &CrateId) -> Option<FuncId> {
pub fn get_main_function(&self, crate_id: Option<&CrateId>) -> Option<FuncId> {
// Find the local crate, one should always be present
let local_crate = self.def_map(crate_id).unwrap();

Expand All @@ -194,7 +194,7 @@ impl Context<'_, '_> {
/// will return all functions marked with #[test].
pub fn get_all_test_functions_in_crate_matching(
&self,
crate_id: &CrateId,
crate_id: Option<&CrateId>,
pattern: FunctionNameMatch,
) -> Vec<(String, TestFunction)> {
let interner = &self.def_interner;
Expand All @@ -217,7 +217,7 @@ impl Context<'_, '_> {
.collect()
}

pub fn get_all_exported_functions_in_crate(&self, crate_id: &CrateId) -> Vec<(String, FuncId)> {
pub fn get_all_exported_functions_in_crate(&self, crate_id: Option<&CrateId>) -> Vec<(String, FuncId)> {
let interner = &self.def_interner;
let def_map = self.def_map(crate_id).expect("The local crate should be analyzed already");

Expand All @@ -231,13 +231,13 @@ impl Context<'_, '_> {
}

/// Return a Vec of all `contract` declarations in the source code and the functions they contain
pub fn get_all_contracts(&self, crate_id: &CrateId) -> Vec<Contract> {
pub fn get_all_contracts(&self, crate_id: Option<&CrateId>) -> Vec<Contract> {
self.def_map(crate_id)
.expect("The local crate should be analyzed already")
.get_all_contracts(&self.def_interner)
}

fn module(&self, module_id: def_map::ModuleId) -> &def_map::ModuleData {
fn module(&self, module_id: def_map::ModuleId) -> Option<&def_map::ModuleData> {
module_id.module(&self.def_maps)
}
}
2 changes: 2 additions & 0 deletions compiler/noirc_frontend/src/hir/resolution/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ pub enum ResolverError {
LowLevelFunctionOutsideOfStdlib { ident: Ident },
#[error("Dependency cycle found, '{item}' recursively depends on itself: {cycle} ")]
DependencyCycle { span: Span, item: String, cycle: String },
#[error("Internal error: missing crate ID")]
MissingCrateId(ModuleId),
}

impl ResolverError {
Expand Down
4 changes: 2 additions & 2 deletions compiler/noirc_frontend/src/hir/resolution/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use super::{path_resolver::StandardPathResolver, resolver::Resolver};
#[allow(clippy::too_many_arguments)]
pub(crate) fn resolve_function_set(
interner: &mut NodeInterner,
crate_id: CrateId,
crate_id: Option<CrateId>,
def_maps: &BTreeMap<CrateId, CrateDefMap>,
mut unresolved_functions: UnresolvedFunctions,
self_type: Option<Type>,
Expand Down Expand Up @@ -61,7 +61,7 @@ pub(crate) fn resolve_function_set(

pub(crate) fn resolve_free_functions(
interner: &mut NodeInterner,
crate_id: CrateId,
crate_id: Option<CrateId>,
def_maps: &BTreeMap<CrateId, CrateDefMap>,
collected_functions: Vec<UnresolvedFunctions>,
self_type: Option<Type>,
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/hir/resolution/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl ResolvedGlobals {
pub(crate) fn resolve_globals(
context: &mut Context,
globals: Vec<UnresolvedGlobal>,
crate_id: CrateId,
crate_id: Option<CrateId>,
) -> ResolvedGlobals {
let mut errors: Vec<(CompilationError, FileId)> = vec![];
let globals = vecmap(globals, |global| {
Expand Down
Loading
Loading