Skip to content

Commit

Permalink
Use coercions in more places to match rust
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher committed Jul 28, 2023
1 parent 0bfa557 commit 752ae8e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crates/noirc_frontend/src/hir/type_check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ impl<'interner> TypeChecker<'interner> {
let arg_type = self.check_expression(&arg);

let span = self.interner.expr_span(expr_id);
self.make_subtype_of(&arg_type, &param_type, span, || {
self.make_subtype_of(&arg_type, &param_type, arg, || {
TypeCheckError::TypeMismatch {
expected_typ: param_type.to_string(),
expr_typ: arg_type.to_string(),
Expand Down
22 changes: 16 additions & 6 deletions crates/noirc_frontend/src/hir/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,17 @@ pub fn type_check_func(interner: &mut NodeInterner, func_id: FuncId) -> Vec<Type
// Check declared return type and actual return type
if !can_ignore_ret {
let func_span = interner.expr_span(function_body_id); // XXX: We could be more specific and return the span of the last stmt, however stmts do not have spans yet
function_last_type.make_subtype_of(&declared_return_type, func_span, &mut errors, || {
TypeCheckError::TypeMismatch {
function_last_type.make_subtype_with_coercions(
&declared_return_type,
*function_body_id,
interner,
&mut errors,
|| TypeCheckError::TypeMismatch {
expected_typ: declared_return_type.to_string(),
expr_typ: function_last_type.to_string(),
expr_span: func_span,
}
});
},
);
}

errors
Expand Down Expand Up @@ -130,10 +134,16 @@ impl<'interner> TypeChecker<'interner> {
&mut self,
actual: &Type,
expected: &Type,
span: Span,
expression: ExprId,
make_error: impl FnOnce() -> TypeCheckError,
) {
actual.make_subtype_of(expected, span, &mut self.errors, make_error);
actual.make_subtype_with_coercions(
expected,
expression,
self.interner,
&mut self.errors,
make_error,
);
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/noirc_frontend/src/hir/type_check/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl<'interner> TypeChecker<'interner> {
});

let span = self.interner.expr_span(&assign_stmt.expression);
self.make_subtype_of(&expr_type, &lvalue_type, span, || {
self.make_subtype_of(&expr_type, &lvalue_type, assign_stmt.expression, || {
TypeCheckError::TypeMismatchWithSource {
rhs: expr_type.clone(),
lhs: lvalue_type.clone(),
Expand Down Expand Up @@ -259,7 +259,7 @@ impl<'interner> TypeChecker<'interner> {
// Now check if LHS is the same type as the RHS
// Importantly, we do not coerce any types implicitly
let expr_span = self.interner.expr_span(&rhs_expr);
self.make_subtype_of(&expr_type, &annotated_type, expr_span, || {
self.make_subtype_of(&expr_type, &annotated_type, rhs_expr, || {
TypeCheckError::TypeMismatch {
expected_typ: annotated_type.to_string(),
expr_typ: expr_type.to_string(),
Expand Down

0 comments on commit 752ae8e

Please sign in to comment.