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: added error messages for passing oracles and references from unconstrained to constrained functions #4570

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
3 changes: 3 additions & 0 deletions compiler/noirc_evaluator/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub enum RuntimeError {
BigIntModulus { call_stack: CallStack },
#[error("Slices cannot be returned from an unconstrained runtime to a constrained runtime")]
UnconstrainedSliceReturnToConstrained { call_stack: CallStack },
#[error("All `oracle` methods should be wrapped in an unconstrained fn")]
UnconstrainedOracleReturnToConstrained { call_stack: CallStack },
}

// We avoid showing the actual lhs and rhs since most of the time they are just 0
Expand Down Expand Up @@ -139,6 +141,7 @@ impl RuntimeError {
| RuntimeError::NestedSlice { call_stack, .. }
| RuntimeError::BigIntModulus { call_stack, .. }
| RuntimeError::UnconstrainedSliceReturnToConstrained { call_stack } => call_stack,
RuntimeError::UnconstrainedOracleReturnToConstrained { call_stack } => call_stack,
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions compiler/noirc_evaluator/src/ssa/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,11 @@ impl Context {
self.ssa_values.insert(*result, output);
}
}
Value::ForeignFunction(_) => unreachable!(
"All `oracle` methods should be wrapped in an unconstrained fn"
),
Value::ForeignFunction(_) => {
return Err(RuntimeError::UnconstrainedOracleReturnToConstrained {
call_stack: self.acir_context.get_call_stack(),
})
}
_ => unreachable!("expected calling a function but got {function_value:?}"),
}
}
Expand Down
5 changes: 5 additions & 0 deletions compiler/noirc_frontend/src/hir/type_check/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ pub enum TypeCheckError {
"Cannot pass a mutable reference from a constrained runtime to an unconstrained runtime"
)]
ConstrainedReferenceToUnconstrained { span: Span },
#[error(
"Cannot pass a mutable reference from a unconstrained runtime to an constrained runtime"
)]
UnconstrainedReferenceToConstrained { span: Span },
#[error("Slices cannot be returned from an unconstrained runtime to a constrained runtime")]
UnconstrainedSliceReturnToConstrained { span: Span },
#[error("Only sized types may be used in the entry point to a program")]
Expand Down Expand Up @@ -213,6 +217,7 @@ impl From<TypeCheckError> for Diagnostic {
| TypeCheckError::OverflowingAssignment { span, .. }
| TypeCheckError::FieldModulo { span }
| TypeCheckError::ConstrainedReferenceToUnconstrained { span }
| TypeCheckError::UnconstrainedReferenceToConstrained { span }
| TypeCheckError::UnconstrainedSliceReturnToConstrained { span } => {
Diagnostic::simple_error(error.to_string(), String::new(), span)
}
Expand Down
11 changes: 11 additions & 0 deletions compiler/noirc_frontend/src/hir/type_check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,17 @@ impl<'interner> TypeChecker<'interner> {
return Type::Error;
}

// Check that we are not passing a reference from an unconstrained runtime to a constrained runtime
if is_current_func_constrained
&& is_unconstrained_call
&& matches!(&return_type.follow_bindings(), Type::MutableReference(_))
Andy53 marked this conversation as resolved.
Show resolved Hide resolved
{
self.errors.push(TypeCheckError::UnconstrainedReferenceToConstrained {
span: self.interner.expr_span(expr_id),
});
return Type::Error;
}

return_type
}
HirExpression::MethodCall(mut method_call) => {
Expand Down
5 changes: 5 additions & 0 deletions test_programs/compile_failure/unconstrained_oracle/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "unconstrained_oracle"
type = "bin"
authors = [""]
[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[oracle(getNoun)]
unconstrained fn external_fn() -> Field {
100 / 5
}

fn main() {
let x = anon();
assert(x * 5 == 100);
}
5 changes: 5 additions & 0 deletions test_programs/compile_failure/unconstrained_ref/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "unconstrained_ref"
type = "bin"
authors = [""]
[dependencies]
8 changes: 8 additions & 0 deletions test_programs/compile_failure/unconstrained_ref/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
unconstrained fn uncon_ref() -> &mut Field {
let lr = &mut 7;
lr
}

fn main() {
let e = uncon_ref();
}
5 changes: 5 additions & 0 deletions test_programs/compile_failure/unconstrained_slice/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "unconstrained_slice"
type = "bin"
authors = [""]
[dependencies]
11 changes: 11 additions & 0 deletions test_programs/compile_failure/unconstrained_slice/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
unconstrained fn uncon_slice() -> [Field] {
Andy53 marked this conversation as resolved.
Show resolved Hide resolved
let mut slice: [Field] = [0; 2];

let mut new_slice = slice.push_back(6);
new_slice
}

fn main() {
let e = uncon_slice();
e
}
Loading