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

chore: Brillig call return type check #4282

Merged
merged 8 commits into from
Feb 7, 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
11 changes: 0 additions & 11 deletions compiler/noirc_evaluator/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,6 @@ 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(),
location.span,
)
}
_ => {
let message = self.to_string();
let location =
Expand Down
5 changes: 4 additions & 1 deletion compiler/noirc_frontend/src/hir/type_check/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ pub enum TypeCheckError {
"Cannot pass a mutable reference from a constrained runtime to an unconstrained runtime"
)]
ConstrainedReferenceToUnconstrained { span: Span },
#[error("Slices cannot be returned from an unconstrained runtime to a constrained runtime")]
UnconstrainedSliceReturnToConstrained { span: Span },
}

impl TypeCheckError {
Expand Down Expand Up @@ -207,7 +209,8 @@ impl From<TypeCheckError> for Diagnostic {
| TypeCheckError::IntegerAndFieldBinaryOperation { span }
| TypeCheckError::OverflowingAssignment { span, .. }
| TypeCheckError::FieldModulo { span }
| TypeCheckError::ConstrainedReferenceToUnconstrained { span } => {
| TypeCheckError::ConstrainedReferenceToUnconstrained { span }
| TypeCheckError::UnconstrainedSliceReturnToConstrained { span } => {
Diagnostic::simple_error(error.to_string(), String::new(), span)
}
TypeCheckError::PublicReturnType { typ, span } => Diagnostic::simple_error(
Expand Down
16 changes: 15 additions & 1 deletion compiler/noirc_frontend/src/hir/type_check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ impl<'interner> TypeChecker<'interner> {
(typ, *arg, self.interner.expr_span(arg))
});

// Check that we are not passing a mutable reference from a constrained runtime to an unconstrained runtime
for (typ, _, _) in args.iter() {
if is_current_func_constrained
&& is_unconstrained_call
Expand All @@ -182,7 +183,20 @@ impl<'interner> TypeChecker<'interner> {
}

let span = self.interner.expr_span(expr_id);
self.bind_function_type(function, args, span)
let return_type = self.bind_function_type(function, args, span);

// Check that we are not passing a slice from an unconstrained runtime to a constrained runtime
if is_current_func_constrained
&& is_unconstrained_call
&& return_type.contains_slice()
{
self.errors.push(TypeCheckError::UnconstrainedSliceReturnToConstrained {
span: self.interner.expr_span(expr_id),
});
return Type::Error;
}

return_type
}
HirExpression::MethodCall(mut method_call) => {
let mut object_type = self.check_expression(&method_call.object).follow_bindings();
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
}
}

fn contains_slice(&self) -> bool {
pub(crate) fn contains_slice(&self) -> bool {
match self {
Type::Array(size, _) => matches!(size.as_ref(), Type::NotConstant),
Type::Struct(struct_typ, generics) => {
Expand Down Expand Up @@ -1408,7 +1408,7 @@
Type::NamedGeneric(binding, _) | Type::TypeVariable(binding, _) => {
substitute_binding(binding)
}
// Do not substitute_helper fields, it ca, substitute_bound_typevarsn lead to infinite recursion

Check warning on line 1411 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (typevarsn)
// and we should not match fields when type checking anyway.
Type::Struct(fields, args) => {
let args = vecmap(args, |arg| {
Expand All @@ -1423,7 +1423,7 @@
Type::Tuple(fields)
}
Type::Forall(typevars, typ) => {
// Trying to substitute_helper a variable de, substitute_bound_typevarsfined within a nested Forall

Check warning on line 1426 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (typevarsfined)
// is usually impossible and indicative of an error in the type checker somewhere.
for var in typevars {
assert!(!type_bindings.contains_key(&var.id()));
Expand Down
Loading