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

feat: don't eagerly error on cast expressions #5635

Merged
merged 9 commits into from
Jul 31, 2024
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/elaborator/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ impl<'context> Elaborator<'context> {
fn elaborate_cast(&mut self, cast: CastExpression, span: Span) -> (HirExpression, Type) {
let (lhs, lhs_type) = self.elaborate_expression(cast.lhs);
let r#type = self.resolve_type(cast.r#type);
let result = self.check_cast(lhs_type, &r#type, span);
let result = self.check_cast(&lhs_type, &r#type, span);
let expr = HirExpression::Cast(HirCastExpression { lhs, r#type });
(expr, result)
}
Expand Down
11 changes: 8 additions & 3 deletions compiler/noirc_frontend/src/elaborator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ impl<'context> Elaborator<'context> {
}
}

pub(super) fn check_cast(&mut self, from: Type, to: &Type, span: Span) -> Type {
pub(super) fn check_cast(&mut self, from: &Type, to: &Type, span: Span) -> Type {
match from.follow_bindings() {
Type::Integer(..)
| Type::FieldElement
Expand All @@ -788,8 +788,13 @@ impl<'context> Elaborator<'context> {
| Type::Bool => (),

Type::TypeVariable(_, _) => {
self.push_err(TypeCheckError::TypeAnnotationsNeeded { span });
return Type::Error;
// NOTE: in reality the expected type can also include bool, but for the compiler's simplicity
// we only allow integer types. If a bool is in `from` it will need an explicit type annotation.
let expected = &&Type::polymorphic_integer_or_field(self.interner);
self.unify(from, expected, || TypeCheckError::InvalidCast {
jfecher marked this conversation as resolved.
Show resolved Hide resolved
from: from.clone(),
span,
});
}
Type::Error => return Type::Error,
from => {
Expand Down
37 changes: 37 additions & 0 deletions compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2809,3 +2809,40 @@ fn uses_self_type_in_trait_where_clause() {

assert_eq!(method_name, "trait_func");
}

#[test]
fn do_not_eagerly_error_on_cast_on_type_variable() {
let src = r#"
pub fn foo<T, U>(x: T, f: fn(T) -> U) -> U {
f(x)
}

fn main() {
let x: u8 = 1;
let _: Field = foo(x, |x| x as Field);
}
"#;
assert_no_errors(src);
}

#[test]
fn error_on_cast_over_type_variable() {
let src = r#"
pub fn foo<T, U>(x: T, f: fn(T) -> U) -> U {
f(x)
}

fn main() {
let x = "a";
let _: Field = foo(x, |x| x as Field);
}
"#;

let errors = get_program_errors(src);
assert_eq!(errors.len(), 1);

assert!(matches!(
errors[0].0,
CompilationError::TypeError(TypeCheckError::TypeMismatch { .. })
));
}
Loading