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: Clean error when attemping to return a slice from Brillig to ACIR #4280

Merged
merged 3 commits into from
Feb 6, 2024
Merged
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
16 changes: 15 additions & 1 deletion compiler/noirc_evaluator/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub enum RuntimeError {
NestedSlice { call_stack: CallStack },
#[error("Big Integer modulus do no match")]
BigIntModulus { call_stack: CallStack },
#[error("Slices cannot be returned from an unconstrained runtime to a constrained runtime")]
UnconstrainedSliceReturnToConstrained { call_stack: CallStack },
}

// We avoid showing the actual lhs and rhs since most of the time they are just 0
Expand Down Expand Up @@ -135,7 +137,8 @@ impl RuntimeError {
| RuntimeError::IntegerOutOfBounds { call_stack, .. }
| RuntimeError::UnsupportedIntegerSize { call_stack, .. }
| RuntimeError::NestedSlice { call_stack, .. }
| RuntimeError::BigIntModulus { call_stack, .. } => call_stack,
| RuntimeError::BigIntModulus { call_stack, .. }
| RuntimeError::UnconstrainedSliceReturnToConstrained { call_stack } => call_stack,
}
}
}
Expand Down Expand Up @@ -171,6 +174,17 @@ impl RuntimeError {
location.span,
)
}
RuntimeError::UnconstrainedSliceReturnToConstrained { .. } => {
let primary_message = self.to_string();
let location =
self.call_stack().back().expect("Expected RuntimeError to have a location");

Diagnostic::simple_error(
primary_message,
"If attempting to return a `Vec` type, `Vec` contains a slice internally.".to_string(),
Copy link
Contributor

@jfecher jfecher Feb 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of these hyper-specific taglines which might not apply to the actual cause of the error from the user. If we're not specifically checking for Vec, we shouldn't mention it in the error message imo. Otherwise it makes the message sound more confusing / imprecise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that is a fair point. I tried to mitigate with the language "If attempting". I did it this way as this issues an error when we do codegen on the call which isn't super clear about the return type itself. I figured if a user wrote a struct with a slice in it they would know, but for Vec they may be unsure as it is one of our stdlib types.

location.span,
)
}
_ => {
let message = self.to_string();
let location =
Expand Down
8 changes: 8 additions & 0 deletions compiler/noirc_evaluator/src/ssa/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,14 @@ impl Context {
"expected an intrinsic/brillig call, but found {func:?}. All ACIR methods should be inlined"
),
RuntimeType::Brillig => {
// Check that we are not attempting to return a slice from
// an unconstrained runtime to a constrained runtime
for result_id in result_ids {
if dfg.type_of_value(*result_id).contains_slice_element() {
return Err(RuntimeError::UnconstrainedSliceReturnToConstrained { call_stack: self.acir_context.get_call_stack() })
}
}

let inputs = vecmap(arguments, |arg| self.convert_value(*arg, dfg));

let code = self.gen_brillig_for(func, brillig)?;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "brillig_slice_to_acir"
type = "bin"
authors = [""]
compiler_version = ">=0.23.0"

[dependencies]
14 changes: 14 additions & 0 deletions test_programs/compile_failure/brillig_slice_to_acir/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
global DEPTH: Field = 40000;

fn main(x: [u32; DEPTH], y: u32) {
let mut new_x = [];
new_x = clear(x, y);
}

unconstrained fn clear(x: [u32; DEPTH], y: u32) -> [u32] {
let mut a = [];
for i in 0..y {
a = a.push_back(x[i]);
}
a
}
7 changes: 7 additions & 0 deletions test_programs/compile_failure/brillig_vec_to_acir/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "brillig_vec_to_acir"
type = "bin"
authors = [""]
compiler_version = ">=0.23.0"

[dependencies]
14 changes: 14 additions & 0 deletions test_programs/compile_failure/brillig_vec_to_acir/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
global DEPTH: Field = 40000;

fn main(x: [u32; DEPTH], y: u32) {
let mut new_x = Vec::new();
new_x = clear(x, y);
}

unconstrained fn clear(x: [u32; DEPTH], y: u32) -> Vec<u32> {
let mut a = Vec::new();
for i in 0..y {
a.push(x[i]);
}
a
}
Loading