From d72ec09cee1d2c71e577a6acea5b710dd74e31dd Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Fri, 17 Jan 2025 11:55:56 +0100 Subject: [PATCH] Fix lack of location for reporting lambdas with missing return statement #1857. --- releasenotes.md | 1 + src/compiler/parse_expr.c | 3 ++- src/compiler/sema_stmts.c | 5 ++--- test/test_suite/functions/missing_return_lambda.c3 | 9 +++++++++ 4 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 test/test_suite/functions/missing_return_lambda.c3 diff --git a/releasenotes.md b/releasenotes.md index e39b8ea1f..1785beaf7 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -8,6 +8,7 @@ ### Fixes - Fix issue requiring prefix on a generic interface declaration. - Fix bug in SHA1 for longer blocks #1854. +- Fix lack of location for reporting lambdas with missing return statement #1857. ### Stdlib changes - Added '%h' and '%H' for printing out binary data in hexadecimal using the formatter. diff --git a/src/compiler/parse_expr.c b/src/compiler/parse_expr.c index e4ae01c71..0c19d6356 100644 --- a/src/compiler/parse_expr.c +++ b/src/compiler/parse_expr.c @@ -408,6 +408,7 @@ static Expr *parse_lambda(ParseContext *c, Expr *left) Expr *expr = EXPR_NEW_TOKEN(EXPR_LAMBDA); advance_and_verify(c, TOKEN_FN); Decl *func = decl_calloc(); + func->span = c->prev_span; func->decl_kind = DECL_FUNC; func->visibility = VISIBLE_LOCAL; func->func_decl.generated_lambda = NULL; @@ -429,7 +430,7 @@ static Expr *parse_lambda(ParseContext *c, Expr *left) sig->rtype = return_type ? type_infoid(return_type) : 0; sig->variadic = variadic; if (!parse_attributes(c, &func->attributes, NULL, NULL, NULL)) return poisoned_expr; - + RANGE_EXTEND_PREV(func); if (tok_is(c, TOKEN_IMPLIES)) { ASSIGN_ASTID_OR_RET(func->func_decl.body, diff --git a/src/compiler/sema_stmts.c b/src/compiler/sema_stmts.c index 0ad808017..d9a369249 100644 --- a/src/compiler/sema_stmts.c +++ b/src/compiler/sema_stmts.c @@ -3315,11 +3315,10 @@ bool sema_analyse_function_body(SemaContext *context, Decl *func) if (!is_naked) sema_append_contract_asserts(assert_first, body); Type *canonical_rtype = type_no_optional(prototype->rtype)->canonical; if (!sema_analyse_compound_statement_no_scope(context, body)) return false; - ASSERT0(context->active_scope.depth == 1); + ASSERT(func,context->active_scope.depth == 1); if (!context->active_scope.jump_end && canonical_rtype != type_void) { - SEMA_ERROR(func, "Missing return statement at the end of the function."); - return false; + RETURN_SEMA_ERROR(func, "Missing return statement at the end of the function."); } SCOPE_END; if (lambda_params) diff --git a/test/test_suite/functions/missing_return_lambda.c3 b/test/test_suite/functions/missing_return_lambda.c3 new file mode 100644 index 000000000..c3f897e43 --- /dev/null +++ b/test/test_suite/functions/missing_return_lambda.c3 @@ -0,0 +1,9 @@ +import std::io::path; + +fn void! process_dir(String dir_name) +{ + path::Path p = path::temp_new(dir_name)!; + path::PathWalker fnwalk = fn bool!(Path p, bool is_dir, void*) { // #error: issing return statement at the end + io::printfn("path is %s", p); + }; +} \ No newline at end of file