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

feat: show backtrace on comptime assertion failures #5842

Merged
merged 12 commits into from
Aug 29, 2024
18 changes: 12 additions & 6 deletions compiler/noirc_errors/src/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl CustomDiagnostic {
) -> CustomDiagnostic {
CustomDiagnostic {
message: primary_message,
secondaries: vec![CustomLabel::new(secondary_message, secondary_span)],
secondaries: vec![CustomLabel::new(secondary_message, secondary_span, None)],
notes: Vec::new(),
kind,
}
Expand Down Expand Up @@ -98,7 +98,7 @@ impl CustomDiagnostic {
) -> CustomDiagnostic {
CustomDiagnostic {
message: primary_message,
secondaries: vec![CustomLabel::new(secondary_message, secondary_span)],
secondaries: vec![CustomLabel::new(secondary_message, secondary_span, None)],
notes: Vec::new(),
kind: DiagnosticKind::Bug,
}
Expand All @@ -113,7 +113,11 @@ impl CustomDiagnostic {
}

pub fn add_secondary(&mut self, message: String, span: Span) {
self.secondaries.push(CustomLabel::new(message, span));
self.secondaries.push(CustomLabel::new(message, span, None));
}

pub fn add_secondary_with_file(&mut self, message: String, span: Span, file: fm::FileId) {
self.secondaries.push(CustomLabel::new(message, span, Some(file)));
}

pub fn is_error(&self) -> bool {
Expand Down Expand Up @@ -153,11 +157,12 @@ impl std::fmt::Display for CustomDiagnostic {
pub struct CustomLabel {
pub message: String,
pub span: Span,
pub file: Option<fm::FileId>,
}

impl CustomLabel {
fn new(message: String, span: Span) -> CustomLabel {
CustomLabel { message, span }
fn new(message: String, span: Span, file: Option<fm::FileId>) -> CustomLabel {
CustomLabel { message, span, file }
}
}

Expand Down Expand Up @@ -234,7 +239,8 @@ fn convert_diagnostic(
.map(|sl| {
let start_span = sl.span.start() as usize;
let end_span = sl.span.end() as usize;
Label::secondary(file_id, start_span..end_span).with_message(&sl.message)
let file = sl.file.unwrap_or(file_id);
Label::secondary(file, start_span..end_span).with_message(&sl.message)
})
.collect()
} else {
Expand Down
3 changes: 3 additions & 0 deletions compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ pub struct Elaborator<'context> {

/// Temporary flag to enable the experimental arithmetic generics feature
enable_arithmetic_generics: bool,

pub(crate) interpreter_call_stack: im::Vector<Location>,
}

#[derive(Default)]
Expand Down Expand Up @@ -214,6 +216,7 @@ impl<'context> Elaborator<'context> {
unresolved_globals: BTreeMap::new(),
enable_arithmetic_generics,
current_trait: None,
interpreter_call_stack: im::Vector::new(),
}
}

Expand Down
13 changes: 11 additions & 2 deletions compiler/noirc_frontend/src/hir/comptime/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
FailingConstraint {
message: Option<String>,
location: Location,
call_stack: im::Vector<Location>,
},
NoMethodFound {
name: String,
Expand Down Expand Up @@ -353,12 +354,20 @@
let msg = format!("Expected a `bool` but found `{typ}`");
CustomDiagnostic::simple_error(msg, String::new(), location.span)
}
InterpreterError::FailingConstraint { message, location } => {
InterpreterError::FailingConstraint { message, location, call_stack } => {
let (primary, secondary) = match message {
Some(msg) => (msg.clone(), "Assertion failed".into()),
None => ("Assertion failed".into(), String::new()),
};
CustomDiagnostic::simple_error(primary, secondary, location.span)
let mut diagnostic =
CustomDiagnostic::simple_error(primary, secondary, location.span);

// Only take at most 3 frames starting from the top of the stack to avoid producing too much output
for frame in call_stack.iter().rev().take(3) {
diagnostic.add_secondary_with_file("".to_string(), frame.span, frame.file);
}

diagnostic
}
InterpreterError::NoMethodFound { name, typ, location } => {
let msg = format!("No method named `{name}` found for type `{typ}`");
Expand Down Expand Up @@ -455,7 +464,7 @@
let message = format!("Failed to parse macro's token stream into {rule}");
let tokens = vecmap(tokens.iter(), ToString::to_string).join(" ");

// 10 is an aribtrary number of tokens here chosen to fit roughly onto one line

Check warning on line 467 in compiler/noirc_frontend/src/hir/comptime/errors.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (aribtrary)
let token_stream = if tokens.len() > 10 {
format!("The resulting token stream was: {tokens}")
} else {
Expand Down
9 changes: 7 additions & 2 deletions compiler/noirc_frontend/src/hir/comptime/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
current_function: Option<FuncId>,
) -> Self {
let bound_generics = Vec::new();
Self { elaborator, crate_id, current_function, bound_generics, in_loop: false }
let in_loop = false;
Self { elaborator, crate_id, current_function, bound_generics, in_loop }
}

pub(crate) fn call_function(
Expand Down Expand Up @@ -99,8 +100,11 @@
}

self.remember_bindings(&instantiation_bindings, &impl_bindings);
self.elaborator.interpreter_call_stack.push_back(location);

let result = self.call_function_inner(function, arguments, location);

self.elaborator.interpreter_call_stack.pop_back();
undo_instantiation_bindings(impl_bindings);
undo_instantiation_bindings(instantiation_bindings);
self.rebind_generics_from_previous_function();
Expand Down Expand Up @@ -219,7 +223,7 @@
}
} else {
let name = self.elaborator.interner.function_name(&function);
unreachable!("Non-builtin, lowlevel or oracle builtin fn '{name}'")

Check warning on line 226 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 @@ -1462,7 +1466,8 @@
let message = constrain.2.and_then(|expr| self.evaluate(expr).ok());
let message =
message.map(|value| value.display(self.elaborator.interner).to_string());
Err(InterpreterError::FailingConstraint { location, message })
let call_stack = self.elaborator.interpreter_call_stack.clone();
Err(InterpreterError::FailingConstraint { location, message, call_stack })
}
value => {
let location = self.elaborator.interner.expr_location(&constrain.0);
Expand Down
30 changes: 21 additions & 9 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
location: Location,
) -> IResult<Value> {
let interner = &mut self.elaborator.interner;
let call_stack = &self.elaborator.interpreter_call_stack;
match name {
"array_as_str_unchecked" => array_as_str_unchecked(interner, arguments, location),
"array_len" => array_len(interner, arguments, location),
Expand Down Expand Up @@ -102,11 +103,11 @@
"quoted_as_type" => quoted_as_type(self, arguments, location),
"quoted_eq" => quoted_eq(arguments, location),
"slice_insert" => slice_insert(interner, arguments, location),
"slice_pop_back" => slice_pop_back(interner, arguments, location),
"slice_pop_front" => slice_pop_front(interner, arguments, location),
"slice_pop_back" => slice_pop_back(interner, arguments, location, call_stack),
"slice_pop_front" => slice_pop_front(interner, arguments, location, call_stack),
"slice_push_back" => slice_push_back(interner, arguments, location),
"slice_push_front" => slice_push_front(interner, arguments, location),
"slice_remove" => slice_remove(interner, arguments, location),
"slice_remove" => slice_remove(interner, arguments, location, call_stack),
"struct_def_as_type" => struct_def_as_type(interner, arguments, location),
"struct_def_fields" => struct_def_fields(interner, arguments, location),
"struct_def_generics" => struct_def_generics(interner, arguments, location),
Expand Down Expand Up @@ -145,8 +146,16 @@
}
}

fn failing_constraint<T>(message: impl Into<String>, location: Location) -> IResult<T> {
Err(InterpreterError::FailingConstraint { message: Some(message.into()), location })
fn failing_constraint<T>(
message: impl Into<String>,
location: Location,
call_stack: &im::Vector<Location>,
) -> IResult<T> {
Err(InterpreterError::FailingConstraint {
message: Some(message.into()),
location,
call_stack: call_stack.clone(),
})
}

fn array_len(
Expand Down Expand Up @@ -278,22 +287,23 @@
interner: &mut NodeInterner,
arguments: Vec<(Value, Location)>,
location: Location,
call_stack: &im::Vector<Location>,
) -> IResult<Value> {
let (slice, index) = check_two_arguments(arguments, location)?;

let (mut values, typ) = get_slice(interner, slice)?;
let index = get_u32(index)? as usize;

if values.is_empty() {
return failing_constraint("slice_remove called on empty slice", location);
return failing_constraint("slice_remove called on empty slice", location, call_stack);
}

if index >= values.len() {
let message = format!(
"slice_remove: index {index} is out of bounds for a slice of length {}",
values.len()
);
return failing_constraint(message, location);
return failing_constraint(message, location, call_stack);
}

let element = values.remove(index);
Expand All @@ -316,27 +326,29 @@
interner: &mut NodeInterner,
arguments: Vec<(Value, Location)>,
location: Location,
call_stack: &im::Vector<Location>,
) -> IResult<Value> {
let argument = check_one_argument(arguments, location)?;

let (mut values, typ) = get_slice(interner, argument)?;
match values.pop_front() {
Some(element) => Ok(Value::Tuple(vec![element, Value::Slice(values, typ)])),
None => failing_constraint("slice_pop_front called on empty slice", location),
None => failing_constraint("slice_pop_front called on empty slice", location, call_stack),
}
}

fn slice_pop_back(
interner: &mut NodeInterner,
arguments: Vec<(Value, Location)>,
location: Location,
call_stack: &im::Vector<Location>,
) -> IResult<Value> {
let argument = check_one_argument(arguments, location)?;

let (mut values, typ) = get_slice(interner, argument)?;
match values.pop_back() {
Some(element) => Ok(Value::Tuple(vec![Value::Slice(values, typ), element])),
None => failing_constraint("slice_pop_back called on empty slice", location),
None => failing_constraint("slice_pop_back called on empty slice", location, call_stack),
}
}

Expand Down Expand Up @@ -413,7 +425,7 @@
let argument = check_one_argument(arguments, location)?;
let typ = parse(argument, parser::parse_type(), "a type")?;
let typ =
interpreter.elaborate_item(interpreter.current_function, |elab| elab.resolve_type(typ));

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elab)

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elab)
Ok(Value::Type(typ))
}

Expand Down
Loading