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(frontend): Continue type check if we are missing an unsafe block #5720

Merged
merged 6 commits into from
Aug 14, 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
26 changes: 12 additions & 14 deletions compiler/noirc_frontend/src/elaborator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1328,22 +1328,20 @@ impl<'context> Elaborator<'context> {
if crossing_runtime_boundary {
if !self.in_unsafe_block {
self.push_err(TypeCheckError::Unsafe { span });
return Type::Error;
}

let called_func_id = self
.interner
.lookup_function_from_expr(&call.func)
.expect("Called function should exist");
self.run_lint(|elaborator| {
lints::oracle_called_from_constrained_function(
elaborator.interner,
&called_func_id,
is_current_func_constrained,
span,
)
.map(Into::into)
});
if let Some(called_func_id) = self.interner.lookup_function_from_expr(&call.func) {
self.run_lint(|elaborator| {
lints::oracle_called_from_constrained_function(
elaborator.interner,
&called_func_id,
is_current_func_constrained,
span,
)
.map(Into::into)
});
}

let errors = lints::unconstrained_function_args(&args);
for error in errors {
self.push_err(error);
Expand Down
12 changes: 6 additions & 6 deletions compiler/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,13 @@
/// Stores the [Location] of a [Type] reference
pub(crate) type_ref_locations: Vec<(Type, Location)>,

/// In Noir's metaprogramming, a noir type has the type `Type`. When these are spliced

Check warning on line 197 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (metaprogramming)
/// into `quoted` expressions, we preserve the original type by assigning it a unique id
/// and creating a `Token::QuotedType(id)` from this id. We cannot create a token holding
/// the actual type since types do not implement Send or Sync.
quoted_types: noirc_arena::Arena<Type>,

/// Determins whether to run in LSP mode. In LSP mode references are tracked.

Check warning on line 203 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Determins)
pub(crate) lsp_mode: bool,

/// Store the location of the references in the graph.
Expand Down Expand Up @@ -951,12 +951,12 @@
/// Returns the [`FuncId`] corresponding to the function referred to by `expr_id`
pub fn lookup_function_from_expr(&self, expr: &ExprId) -> Option<FuncId> {
if let HirExpression::Ident(HirIdent { id, .. }, _) = self.expression(expr) {
if let Some(DefinitionKind::Function(func_id)) =
self.try_definition(id).map(|def| &def.kind)
{
Some(*func_id)
} else {
None
match self.try_definition(id).map(|def| &def.kind) {
Some(DefinitionKind::Function(func_id)) => Some(*func_id),
Some(DefinitionKind::Local(Some(expr_id))) => {
self.lookup_function_from_expr(expr_id)
}
_ => None,
}
} else {
None
Expand Down
51 changes: 51 additions & 0 deletions compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2310,7 +2310,7 @@
}

#[test]
fn underflowing_u8() {

Check warning on line 2313 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (underflowing)
let src = r#"
fn main() {
let _: u8 = -1;
Expand Down Expand Up @@ -2348,7 +2348,7 @@
}

#[test]
fn underflowing_i8() {

Check warning on line 2351 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (underflowing)
let src = r#"
fn main() {
let _: i8 = -129;
Expand Down Expand Up @@ -2482,12 +2482,63 @@
let src = r#"
fn main() {
let func = foo;
// Warning should trigger here
func();
inner(func);
}

fn inner(x: unconstrained fn() -> ()) {
// Warning should trigger here
x();
}

unconstrained fn foo() {}
"#;
let errors = get_program_errors(src);
assert_eq!(errors.len(), 2);

for error in &errors {
let CompilationError::TypeError(TypeCheckError::Unsafe { .. }) = &error.0 else {
panic!("Expected an 'unsafe' error, got {:?}", errors[0].0);
};
}
}

#[test]
fn missing_unsafe_block_when_needing_type_annotations() {
// This test is a regression check that even when an unsafe block is missing
// that we still appropriately continue type checking and infer type annotations.
let src = r#"
fn main() {
let z = BigNum { limbs: [2, 0, 0] };
assert(z.__is_zero() == false);
}

struct BigNum<let N: u32> {
limbs: [u64; N],
}

impl<let N: u32> BigNum<N> {
unconstrained fn __is_zero_impl(self) -> bool {
let mut result: bool = true;
for i in 0..N {
result = result & (self.limbs[i] == 0);
}
result
}
}

trait BigNumTrait {
fn __is_zero(self) -> bool;
}

impl<let N: u32> BigNumTrait for BigNum<N> {
fn __is_zero(self) -> bool {
self.__is_zero_impl()
}
}
"#;
let errors = get_program_errors(src);
assert_eq!(errors.len(), 1);

let CompilationError::TypeError(TypeCheckError::Unsafe { .. }) = &errors[0].0 else {
Expand Down
Loading