Skip to content

Commit

Permalink
feat: Sync from noir (#12556)
Browse files Browse the repository at this point in the history
Automated pull of development from the
[noir](https://github.com/noir-lang/noir) programming language, a
dependency of Aztec.
BEGIN_COMMIT_OVERRIDE
chore!: convert `TraitMethodNotInScope` to error
(noir-lang/noir#7427)
chore: bump ring to address advisory
(noir-lang/noir#7619)
chore: bump external pinned commits
(noir-lang/noir#7618)
feat: relate errors to macro built-ins errors
(noir-lang/noir#7609)
END_COMMIT_OVERRIDE

---------

Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
  • Loading branch information
AztecBot and TomAFrench authored Mar 7, 2025
1 parent fd9f145 commit fbaa634
Show file tree
Hide file tree
Showing 21 changed files with 123 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .noir-sync-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b3443c124b19a909bf9cb370b4e0ebc151bb6aa3
37be49fd081f33dc7256d23cee8ccc0719c50a82
2 changes: 1 addition & 1 deletion noir/noir-repo/.github/benchmark_projects.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define: &AZ_COMMIT 3b981f9217f9b859bdfbcdba2f5c080392c98da6
define: &AZ_COMMIT fd9f1458557f2d67bcf2d58ba4194e000f58600c
projects:
private-kernel-inner:
repo: AztecProtocol/aztec-packages
Expand Down
8 changes: 4 additions & 4 deletions noir/noir-repo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion noir/noir-repo/EXTERNAL_NOIR_LIBRARIES.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define: &AZ_COMMIT 3b981f9217f9b859bdfbcdba2f5c080392c98da6
define: &AZ_COMMIT fd9f1458557f2d67bcf2d58ba4194e000f58600c
libraries:
noir_check_shuffle:
repo: noir-lang/noir_check_shuffle
Expand Down
67 changes: 34 additions & 33 deletions noir/noir-repo/compiler/noirc_frontend/src/elaborator/comptime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ impl<'context> Elaborator<'context> {
pub fn elaborate_item_from_comptime_in_function<'a, T>(
&'a mut self,
current_function: Option<FuncId>,
reason: Option<ElaborateReason>,
f: impl FnOnce(&mut Elaborator<'a>) -> T,
) -> T {
self.elaborate_item_from_comptime(f, |elaborator| {
self.elaborate_item_from_comptime(reason, f, |elaborator| {
if let Some(function) = current_function {
let meta = elaborator.interner.function_meta(&function);
elaborator.current_item = Some(DependencyId::Function(function));
Expand All @@ -65,9 +66,10 @@ impl<'context> Elaborator<'context> {
pub fn elaborate_item_from_comptime_in_module<'a, T>(
&'a mut self,
module: ModuleId,
reason: Option<ElaborateReason>,
f: impl FnOnce(&mut Elaborator<'a>) -> T,
) -> T {
self.elaborate_item_from_comptime(f, |elaborator| {
self.elaborate_item_from_comptime(reason, f, |elaborator| {
elaborator.current_item = None;
elaborator.crate_id = module.krate;
elaborator.local_module = module.local_id;
Expand All @@ -76,6 +78,7 @@ impl<'context> Elaborator<'context> {

fn elaborate_item_from_comptime<'a, T>(
&'a mut self,
reason: Option<ElaborateReason>,
f: impl FnOnce(&mut Elaborator<'a>) -> T,
setup: impl FnOnce(&mut Elaborator<'a>),
) -> T {
Expand Down Expand Up @@ -104,7 +107,14 @@ impl<'context> Elaborator<'context> {
let result = f(&mut elaborator);
elaborator.check_and_pop_function_context();

self.errors.append(&mut elaborator.errors);
let mut errors = std::mem::take(&mut elaborator.errors);
if let Some(reason) = reason {
errors = vecmap(errors, |error| {
CompilationError::ComptimeError(reason.to_macro_error(error))
});
};

self.errors.extend(errors);
result
}

Expand Down Expand Up @@ -342,14 +352,11 @@ impl<'context> Elaborator<'context> {
generated_items: &mut CollectedItems,
location: Location,
) {
let previous_errors =
self.push_elaborate_reason_and_take_errors(ElaborateReason::RunningAttribute, location);

for item in items {
self.add_item(item, generated_items, location);
}

self.pop_elaborate_reason(previous_errors);
self.with_elaborate_reason(ElaborateReason::RunningAttribute(location), |elaborator| {
for item in items {
elaborator.add_item(item, generated_items, location);
}
});
}

pub(crate) fn add_item(
Expand Down Expand Up @@ -558,14 +565,10 @@ impl<'context> Elaborator<'context> {
});

if !generated_items.is_empty() {
let previous_errors = self.push_elaborate_reason_and_take_errors(
ElaborateReason::RunningAttribute,
location,
);

self.elaborate_items(generated_items);

self.pop_elaborate_reason(previous_errors);
let reason = ElaborateReason::RunningAttribute(location);
self.with_elaborate_reason(reason, |elaborator| {
elaborator.elaborate_items(generated_items);
});
}
}
}
Expand Down Expand Up @@ -652,33 +655,31 @@ impl<'context> Elaborator<'context> {
}
}

/// Pushes an ElaborateReason but also `std::mem::take`s the current errors and returns them.
pub(crate) fn push_elaborate_reason_and_take_errors(
&mut self,
reason: ElaborateReason,
location: Location,
) -> Vec<CompilationError> {
self.elaborate_reasons.push_back((reason, location));
std::mem::take(&mut self.errors)
}
pub(crate) fn with_elaborate_reason<F, T>(&mut self, reason: ElaborateReason, f: F) -> T
where
F: FnOnce(&mut Elaborator) -> T,
{
self.elaborate_reasons.push_back(reason);
let previous_errors = std::mem::take(&mut self.errors);

let value = f(self);

/// Pops en ElaborateREason. Receives the errors that were returned by `push_elaborate_reason`
/// so they are restored, while also wrapping errors in the current Elaborator in a ComptimeError.
pub(crate) fn pop_elaborate_reason(&mut self, previous_errors: Vec<CompilationError>) {
let new_errors = std::mem::take(&mut self.errors);
let new_errors = self.wrap_errors_in_macro_error(new_errors);
self.errors = previous_errors;
self.push_errors(new_errors);
self.elaborate_reasons.pop_back();

value
}

fn wrap_errors_in_macro_error(&self, errors: Vec<CompilationError>) -> Vec<CompilationError> {
vecmap(errors, |error| self.wrap_error_in_macro_error(error))
}

fn wrap_error_in_macro_error(&self, mut error: CompilationError) -> CompilationError {
for (reason, location) in self.elaborate_reasons.iter().rev() {
error = CompilationError::ComptimeError(reason.to_macro_error(error, *location));
for reason in self.elaborate_reasons.iter().rev() {
error = CompilationError::ComptimeError(reason.to_macro_error(error));
}
error
}
Expand Down
19 changes: 10 additions & 9 deletions noir/noir-repo/compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,25 +199,26 @@ pub struct Elaborator<'context> {
/// Sometimes items are elaborated because a function attribute ran and generated items.
/// The Elaborator keeps track of these reasons so that when an error is produced it will
/// be wrapped in another error that will include this reason.
pub(crate) elaborate_reasons: im::Vector<(ElaborateReason, Location)>,
pub(crate) elaborate_reasons: im::Vector<ElaborateReason>,
}

#[derive(Copy, Clone)]
pub enum ElaborateReason {
/// A function attribute generated an item that's being elaborated.
RunningAttribute,
/// Evaluating `Module::add_item`
AddingItemToModule,
RunningAttribute(Location),
/// Evaluating a comptime call like `Module::add_item`
EvaluatingComptimeCall(&'static str, Location),
}

impl ElaborateReason {
fn to_macro_error(self, error: CompilationError, location: Location) -> ComptimeError {
fn to_macro_error(self, error: CompilationError) -> ComptimeError {
match self {
ElaborateReason::RunningAttribute => {
ElaborateReason::RunningAttribute(location) => {
ComptimeError::ErrorRunningAttribute { error: Box::new(error), location }
}
ElaborateReason::AddingItemToModule => {
ComptimeError::ErrorAddingItemToModule { error: Box::new(error), location }
ElaborateReason::EvaluatingComptimeCall(method_name, location) => {
let error = Box::new(error);
ComptimeError::ErrorEvaluatingComptimeCall { method_name, error, location }
}
}
}
Expand Down Expand Up @@ -251,7 +252,7 @@ impl<'context> Elaborator<'context> {
crate_id: CrateId,
interpreter_call_stack: im::Vector<Location>,
options: ElaboratorOptions<'context>,
elaborate_reasons: im::Vector<(ElaborateReason, Location)>,
elaborate_reasons: im::Vector<ElaborateReason>,
) -> Self {
Self {
scopes: ScopeForest::default(),
Expand Down
17 changes: 12 additions & 5 deletions noir/noir-repo/compiler/noirc_frontend/src/hir/comptime/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,15 +703,22 @@ impl<'a> From<&'a InterpreterError> for CustomDiagnostic {
/// comptime call or macro "something" that eventually led to that error.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ComptimeError {
ErrorRunningAttribute { error: Box<CompilationError>, location: Location },
ErrorAddingItemToModule { error: Box<CompilationError>, location: Location },
ErrorRunningAttribute {
error: Box<CompilationError>,
location: Location,
},
ErrorEvaluatingComptimeCall {
method_name: &'static str,
error: Box<CompilationError>,
location: Location,
},
}

impl ComptimeError {
pub fn location(&self) -> Location {
match self {
ComptimeError::ErrorRunningAttribute { location, .. }
| ComptimeError::ErrorAddingItemToModule { location, .. } => *location,
| ComptimeError::ErrorEvaluatingComptimeCall { location, .. } => *location,
}
}
}
Expand All @@ -724,9 +731,9 @@ impl<'a> From<&'a ComptimeError> for CustomDiagnostic {
diagnostic.add_secondary("While running this function attribute".into(), *location);
diagnostic
}
ComptimeError::ErrorAddingItemToModule { error, location } => {
ComptimeError::ErrorEvaluatingComptimeCall { method_name, error, location } => {
let mut diagnostic = CustomDiagnostic::from(&**error);
diagnostic.add_secondary("While interpreting `Module::add_item`".into(), *location);
diagnostic.add_secondary(format!("While evaluating `{method_name}`"), *location);
diagnostic
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_hash::FxHashMap as HashMap;

use crate::TypeVariable;
use crate::ast::{BinaryOpKind, FunctionKind, IntegerBitSize, Signedness, UnaryOp};
use crate::elaborator::Elaborator;
use crate::elaborator::{ElaborateReason, Elaborator};
use crate::graph::CrateId;
use crate::hir::def_map::ModuleId;
use crate::hir::type_check::TypeCheckError;
Expand Down Expand Up @@ -191,7 +191,7 @@ impl<'local, 'interner> Interpreter<'local, 'interner> {
Some(body) => Ok(body),
None => {
if matches!(&meta.function_body, FunctionBody::Unresolved(..)) {
self.elaborate_in_function(None, |elaborator| {
self.elaborate_in_function(None, None, |elaborator| {
elaborator.elaborate_function(function);
});

Expand All @@ -207,21 +207,23 @@ impl<'local, 'interner> Interpreter<'local, 'interner> {
fn elaborate_in_function<T>(
&mut self,
function: Option<FuncId>,
reason: Option<ElaborateReason>,
f: impl FnOnce(&mut Elaborator) -> T,
) -> T {
self.unbind_generics_from_previous_function();
let result = self.elaborator.elaborate_item_from_comptime_in_function(function, f);
let result = self.elaborator.elaborate_item_from_comptime_in_function(function, reason, f);
self.rebind_generics_from_previous_function();
result
}

fn elaborate_in_module<T>(
&mut self,
module: ModuleId,
reason: Option<ElaborateReason>,
f: impl FnOnce(&mut Elaborator) -> T,
) -> T {
self.unbind_generics_from_previous_function();
let result = self.elaborator.elaborate_item_from_comptime_in_module(module, f);
let result = self.elaborator.elaborate_item_from_comptime_in_module(module, reason, f);
self.rebind_generics_from_previous_function();
result
}
Expand Down Expand Up @@ -1325,9 +1327,10 @@ impl<'local, 'interner> Interpreter<'local, 'interner> {
let mut result = self.call_function(function_id, arguments, bindings, location)?;
if call.is_macro_call {
let expr = result.into_expression(self.elaborator, location)?;
let expr = self.elaborate_in_function(self.current_function, |elaborator| {
elaborator.elaborate_expression(expr).0
});
let expr =
self.elaborate_in_function(self.current_function, None, |elaborator| {
elaborator.elaborate_expression(expr).0
});
result = self.evaluate(expr)?;

// Macro calls are typed as type variables during type checking.
Expand Down
Loading

0 comments on commit fbaa634

Please sign in to comment.