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: check infix expression is valid in program input #6450

Merged
merged 2 commits into from
Nov 5, 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
5 changes: 4 additions & 1 deletion compiler/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,6 @@
| Type::Forall(_, _)
| Type::Quoted(_)
| Type::Slice(_)
| Type::InfixExpr(_, _, _)
| Type::TraitAsType(..) => false,

Type::Alias(alias, generics) => {
Expand All @@ -1205,6 +1204,10 @@
.get_fields(generics)
.into_iter()
.all(|(_, field)| field.is_valid_for_program_input()),

Type::InfixExpr(lhs, _, rhs) => {
lhs.is_valid_for_program_input() && rhs.is_valid_for_program_input()
}
}
}

Expand Down Expand Up @@ -2053,7 +2056,7 @@

/// Instantiates a type with the given types.
/// This differs from substitute in that only the quantified type variables
/// are matched against the type list and are eligible for substitution - similar

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Canonicalization)
/// to normal instantiation. This function is used when the turbofish operator
/// is used and generic substitutions are provided manually by users.
///
Expand Down Expand Up @@ -2231,7 +2234,7 @@
arg.substitute_helper(type_bindings, substitute_bound_typevars)
});
let named = vecmap(&generics.named, |arg| {
let typ = arg.typ.substitute_helper(type_bindings, substitute_bound_typevars);

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (recuring)
NamedType { name: arg.name.clone(), typ }
});
Type::TraitAsType(*s, name.clone(), TraitGenerics { ordered, named })
Expand Down Expand Up @@ -2316,7 +2319,7 @@
String(size) => String(Box::new(size.follow_bindings())),
FmtString(size, args) => {
let size = Box::new(size.follow_bindings());
let args = Box::new(args.follow_bindings());

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (typevarsfined)
FmtString(size, args)
}
Struct(def, args) => {
Expand Down Expand Up @@ -2488,7 +2491,7 @@
},
Type::MutableReference(typ) => typ.integral_maximum_size(),
Type::InfixExpr(lhs, _op, rhs) => lhs.infix_kind(rhs).integral_maximum_size(),
Type::Constant(_, kind) => kind.integral_maximum_size(),

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (unbindable)

Type::Array(..)
| Type::Slice(..)
Expand Down
38 changes: 38 additions & 0 deletions compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1988,7 +1988,7 @@
fn main() {
let _ = foo::<A>();
}
"#;

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Isnt)
let errors = get_program_errors(src);
assert_eq!(errors.len(), 2);
assert!(matches!(
Expand All @@ -2005,7 +2005,7 @@
// The EvaluatedGlobalIsntU32 warning is a stopgap
// (originally from https://github.com/noir-lang/noir/issues/6126)
#[test]
fn numeric_generic_field_arithmetic_larger_than_u32() {

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Isnt)
let src = r#"
struct Foo<let F: Field> {}

Expand All @@ -2014,7 +2014,7 @@
F
}
}

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Isnt)
// 2^32 - 1
global A: Field = 4294967295;

Expand Down Expand Up @@ -3623,3 +3623,41 @@
"#;
assert_no_errors(src);
}

#[test]
fn allows_struct_with_generic_infix_type_as_main_input_1() {
let src = r#"
struct Foo<let N: u32> {
x: [u64; N * 2],
}

fn main(_x: Foo<18>) {}
asterite marked this conversation as resolved.
Show resolved Hide resolved
"#;
assert_no_errors(src);
}

#[test]
fn allows_struct_with_generic_infix_type_as_main_input_2() {
let src = r#"
struct Foo<let N: u32> {
x: [u64; N * 2],
}

fn main(_x: Foo<2 * 9>) {}
"#;
assert_no_errors(src);
}

#[test]
fn allows_struct_with_generic_infix_type_as_main_input_3() {
let src = r#"
struct Foo<let N: u32> {
x: [u64; N * 2],
}

global N = 9;

fn main(_x: Foo<N * 2>) {}
"#;
assert_no_errors(src);
}
Loading