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

fix: 'cannot eval non-comptime global' error #5586

Merged
merged 3 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions aztec_macros/src/utils/hir_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
let global_id = context.def_interner.push_empty_global(
name.clone(),
module_id,
*crate_id,
file_id,
global.attributes.clone(),
false,
Expand Down Expand Up @@ -334,7 +335,7 @@
}
}

pub fn get_global_numberic_const(

Check warning on line 338 in aztec_macros/src/utils/hir_utils.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (numberic)
context: &HirContext,
const_name: &str,
) -> Result<u128, MacroError> {
Expand Down
1 change: 1 addition & 0 deletions compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1617,6 +1617,7 @@ impl<'context> Elaborator<'context> {
global,
self.file,
self.local_module,
self.crate_id,
);

generated_items.globals.push(global);
Expand Down
4 changes: 2 additions & 2 deletions compiler/noirc_frontend/src/hir/comptime/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
}
} else {
let name = self.elaborator.interner.function_name(&function);
unreachable!("Non-builtin, lowlevel or oracle builtin fn '{name}'")

Check warning on line 158 in compiler/noirc_frontend/src/hir/comptime/interpreter.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lowlevel)
}
}

Expand Down Expand Up @@ -326,7 +326,6 @@
Err(InterpreterError::VariableNotInScope { location })
} else {
let name = self.elaborator.interner.definition_name(id).to_string();
eprintln!("{name} not in scope");
Err(InterpreterError::NonComptimeVarReferenced { name, location })
}
}
Expand Down Expand Up @@ -400,6 +399,7 @@
if let Ok(value) = self.lookup(&ident) {
Ok(value)
} else {
let global_crate = self.elaborator.interner.get_global(*global_id).crate_id;
jfecher marked this conversation as resolved.
Show resolved Hide resolved
let let_ =
self.elaborator.interner.get_global_let_statement(*global_id).ok_or_else(
|| {
Expand All @@ -408,7 +408,7 @@
},
)?;

if let_.comptime {
if let_.comptime || global_crate != self.crate_id {
jfecher marked this conversation as resolved.
Show resolved Hide resolved
self.evaluate_let(let_.clone())?;
}
self.lookup(&ident)
Expand Down
7 changes: 6 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 @@ -84,7 +84,7 @@
});
}

errors.extend(collector.collect_globals(context, ast.globals));
errors.extend(collector.collect_globals(context, ast.globals, crate_id));

errors.extend(collector.collect_traits(context, ast.traits, crate_id));

Expand All @@ -106,6 +106,7 @@
&mut self,
context: &mut Context,
globals: Vec<LetStatement>,
crate_id: CrateId,
) -> Vec<(CompilationError, fm::FileId)> {
let mut errors = vec![];
for global in globals {
Expand All @@ -115,6 +116,7 @@
global,
self.file_id,
self.module_id,
crate_id,
);

if let Some(error) = error {
Expand Down Expand Up @@ -492,6 +494,7 @@
let global_id = context.def_interner.push_empty_global(
name.clone(),
trait_id.0.local_id,
krate,
self.file_id,
vec![],
false,
Expand All @@ -511,7 +514,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 517 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 517 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 Expand Up @@ -697,7 +700,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 703 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(parent, location, is_contract);
Expand Down Expand Up @@ -862,12 +865,14 @@
global: LetStatement,
file_id: FileId,
module_id: LocalModuleId,
crate_id: CrateId,
) -> (UnresolvedGlobal, Option<(CompilationError, FileId)>) {
let name = global.pattern.name_ident().clone();

let global_id = interner.push_empty_global(
name.clone(),
module_id,
crate_id,
file_id,
global.attributes.clone(),
matches!(global.pattern, Pattern::Mutable { .. }),
Expand Down
8 changes: 7 additions & 1 deletion compiler/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
/// Stores the [Location] of a [Type] reference
pub(crate) type_ref_locations: Vec<(Type, Location)>,

/// In Noir's metaprogramming, a noir type has the type `Type`. When these are spliced

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (metaprogramming)
/// into `quoted` expressions, we preserve the original type by assigning it a unique id
/// and creating a `Token::QuotedType(id)` from this id. We cannot create a token holding
/// the actual type since types do not implement Send or Sync.
Expand Down Expand Up @@ -543,6 +543,7 @@
pub definition_id: DefinitionId,
pub ident: Ident,
pub local_id: LocalModuleId,
pub crate_id: CrateId,
pub location: Location,
pub let_statement: StmtId,
pub value: Option<comptime::Value>,
Expand Down Expand Up @@ -756,6 +757,7 @@
&mut self,
ident: Ident,
local_id: LocalModuleId,
crate_id: CrateId,
let_statement: StmtId,
file: FileId,
attributes: Vec<SecondaryAttribute>,
Expand All @@ -773,6 +775,7 @@
definition_id,
ident,
local_id,
crate_id,
let_statement,
location,
value: None,
Expand All @@ -786,18 +789,21 @@
}

/// Intern an empty global. Used for collecting globals before they're defined
#[allow(clippy::too_many_arguments)]
pub fn push_empty_global(
&mut self,
name: Ident,
local_id: LocalModuleId,
crate_id: CrateId,
file: FileId,
attributes: Vec<SecondaryAttribute>,
mutable: bool,
comptime: bool,
) -> GlobalId {
let statement = self.push_stmt(HirStatement::Error);
let span = name.span();
let id = self.push_global(name, local_id, statement, file, attributes, mutable, comptime);
let id = self
.push_global(name, local_id, crate_id, statement, file, attributes, mutable, comptime);
self.push_stmt_location(statement, span, file);
id
}
Expand Down
Loading