Skip to content

Commit

Permalink
Lambdas were not type checked thoroughly #1185.
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Apr 25, 2024
1 parent 1cf97df commit 1af3c7b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- Fixed issue in safe mode when converting enums.
- Better checking of operator methods.
- Bug when assigning an optional from an optional.
- Lambdas were not type checked thoroughly #1185.

### Stdlib changes
- "init_new/init_temp" removed.
Expand Down
9 changes: 7 additions & 2 deletions src/compiler/sema_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -7691,8 +7691,7 @@ static inline bool sema_expr_analyse_lambda(SemaContext *context, Type *target_t
}
if (to_sig && vec_size(to_sig->params) != vec_size(sig->params))
{
SEMA_ERROR(expr, "The lambda doesn't match the required type %s.", type_quoted_error_string(target_type));
return false;
RETURN_SEMA_ERROR(expr, "The lambda doesn't match the required type %s.", type_quoted_error_string(target_type));
}
FOREACH_BEGIN_IDX(i, Decl *param, sig->params)
if (param->var.type_info) continue;
Expand Down Expand Up @@ -7730,6 +7729,12 @@ static inline bool sema_expr_analyse_lambda(SemaContext *context, Type *target_t
decl->extname = decl->name = scratch_buffer_copy();
decl->type = type_new_func(decl, sig);
if (!sema_analyse_function_signature(context, decl, sig->abi, sig)) return false;
if (target_type && flat->pointer->function.prototype->raw_type != decl->type->function.prototype->raw_type)
{
RETURN_SEMA_ERROR(expr, "The lambda has type %s, which doesn't match the required type %s.",
type_quoted_error_string(decl->type),
type_quoted_error_string(target_type));
}
decl->func_decl.lambda_ct_parameters = ct_lambda_parameters;
decl->func_decl.is_lambda = true;
decl->alignment = type_alloca_alignment(decl->type);
Expand Down
14 changes: 14 additions & 0 deletions test/test_suite/lambda/lambda_checks.c3
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module test;
def Func = fn void (bool);

fn bool foo (String) => true;
fn void bar(Func func) => func(false);

fn void main()
{
Func funcX = &foo; // #error: Implicitly casting
bar(&foo); // #error: Implicitly casting

Func func = fn bool (String) { return true; }; // #error: which doesn't match
bar(fn bool (String) { return true; }); // #error: which doesn't match
}

0 comments on commit 1af3c7b

Please sign in to comment.