diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 77e5e8b5f..bccc61586 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -4898,7 +4898,13 @@ static inline bool sema_expr_analyse_expr_block(Context *context, Type *to, Expr Ast **saved_returns = context_push_returns(context); context->expected_block_type = to; + SCOPE_START_WITH_FLAGS(SCOPE_EXPR_BLOCK) + + PUSH_CONTINUE(NULL); + PUSH_BREAK(NULL); + PUSH_NEXT(NULL, NULL); + VECEACH(expr->expr_block.stmts, i) { if (!sema_analyse_statement(context, expr->expr_block.stmts[i])) @@ -4925,7 +4931,11 @@ static inline bool sema_expr_analyse_expr_block(Context *context, Type *to, Expr goto EXIT; } expr_set_type(expr, left_canonical); -EXIT: + + EXIT: + POP_BREAKCONT(); + POP_NEXT(); + SCOPE_END; context_pop_returns(context, saved_returns); expr->failable = context->expr_failable_return; diff --git a/src/compiler/sema_stmts.c b/src/compiler/sema_stmts.c index 74ac547bd..30c559bb9 100644 --- a/src/compiler/sema_stmts.c +++ b/src/compiler/sema_stmts.c @@ -895,7 +895,7 @@ static bool sema_analyse_break_stmt(Context *context, Ast *statement) } else { - SEMA_ERROR(statement, "'break' is not allowed here."); + SEMA_ERROR(statement, "There is no valid target for 'break', did you make a mistake?"); } return false; } diff --git a/test/test_suite/expression_block/expression_block_break.c3 b/test/test_suite/expression_block/expression_block_break.c3 new file mode 100644 index 000000000..603dde3da --- /dev/null +++ b/test/test_suite/expression_block/expression_block_break.c3 @@ -0,0 +1,13 @@ +module fe; +import std::io; +extern func int printf(char *str, ...); + +func int main() +{ + while(true) + { + return {| break; return 0; |}; // #error: There is no valid target for 'break', did you make a mistake + } + io::println("I didn't return."); + return 0; +} \ No newline at end of file