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: 17 additions & 1 deletion compiler/noirc_errors/src/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct CustomDiagnostic {
pub secondaries: Vec<CustomLabel>,
notes: Vec<String>,
pub kind: DiagnosticKind,
call_stack: Option<Vec<Location>>,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
Expand All @@ -35,6 +36,7 @@ impl CustomDiagnostic {
secondaries: Vec::new(),
notes: Vec::new(),
kind: DiagnosticKind::Error,
call_stack: None,
}
}

Expand All @@ -49,6 +51,7 @@ impl CustomDiagnostic {
secondaries: vec![CustomLabel::new(secondary_message, secondary_span)],
notes: Vec::new(),
kind,
call_stack: None,
}
}

Expand Down Expand Up @@ -101,6 +104,7 @@ impl CustomDiagnostic {
secondaries: vec![CustomLabel::new(secondary_message, secondary_span)],
notes: Vec::new(),
kind: DiagnosticKind::Bug,
call_stack: None,
}
}

Expand All @@ -116,6 +120,10 @@ impl CustomDiagnostic {
self.secondaries.push(CustomLabel::new(message, span));
}

pub fn set_call_stack(&mut self, call_stack: Vec<Location>) {
self.call_stack = Some(call_stack);
}

pub fn is_error(&self) -> bool {
matches!(self.kind, DiagnosticKind::Error)
}
Expand Down Expand Up @@ -228,7 +236,7 @@ fn convert_diagnostic(
_ => Diagnostic::error(),
};

let secondary_labels = if let Some(file_id) = file {
let mut secondary_labels = if let Some(file_id) = file {
cd.secondaries
.iter()
.map(|sl| {
Expand All @@ -241,6 +249,14 @@ fn convert_diagnostic(
vec![]
};

if let Some(call_stack) = &cd.call_stack {
secondary_labels.extend(call_stack.iter().map(|frame| {
let start_span = frame.span.start() as usize;
let end_span = frame.span.end() as usize;
Label::secondary(frame.file, start_span..end_span)
}));
}

let mut notes = cd.notes.clone();
notes.push(stack_trace);

Expand Down
8 changes: 6 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: Vec<Location>,
},
NoMethodFound {
name: String,
Expand Down Expand Up @@ -353,12 +354,15 @@
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);
diagnostic.set_call_stack(call_stack.clone());
diagnostic
}
InterpreterError::NoMethodFound { name, typ, location } => {
let msg = format!("No method named `{name}` found for type `{typ}`");
Expand Down Expand Up @@ -455,7 +459,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 462 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
12 changes: 10 additions & 2 deletions compiler/noirc_frontend/src/hir/comptime/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
/// multiple times. Without this map, when one of these inner functions exits we would
/// unbind the generic completely instead of resetting it to its previous binding.
bound_generics: Vec<HashMap<TypeVariable, Type>>,

call_stack: Vec<Location>,
}

#[allow(unused)]
Expand All @@ -70,7 +72,9 @@
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;
let call_stack = Vec::new();
asterite marked this conversation as resolved.
Show resolved Hide resolved
Self { elaborator, crate_id, current_function, bound_generics, in_loop, call_stack }
}

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

self.remember_bindings(&instantiation_bindings, &impl_bindings);
self.call_stack.push(location);

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

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

Check warning on line 229 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 +1469,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.call_stack.clone();
Err(InterpreterError::FailingConstraint { location, message, call_stack })
}
value => {
let location = self.elaborator.interner.expr_location(&constrain.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@
}

fn failing_constraint<T>(message: impl Into<String>, location: Location) -> IResult<T> {
Err(InterpreterError::FailingConstraint { message: Some(message.into()), location })
let call_stack = Vec::new();
Err(InterpreterError::FailingConstraint { message: Some(message.into()), location, call_stack })
asterite marked this conversation as resolved.
Show resolved Hide resolved
}

fn array_len(
Expand Down Expand Up @@ -413,7 +414,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 417 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 417 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