favor return over return _ (void return); newline/closing terminates

This commit is contained in:
2026-07-14 19:07:37 +02:00
parent eac5b32738
commit 471896b48a
19 changed files with 92266 additions and 94923 deletions
+17 -10
View File
@@ -1282,14 +1282,11 @@ finish_statement :: proc(parser: ^Parser, allow_closing_brace := false) -> sourc
parse_return :: proc(parser: ^Parser) -> ast.Stmt_Id {
start := advance(parser)
skip_newlines(parser)
if current(parser).kind == .Underscore {
end := advance(parser)
if current(parser).kind == .Newline || current(parser).kind == .Right_Brace || current(parser).kind == .Eof {
id := ast.stmt_id(len(parser.module.statements))
append(&parser.module.statements, ast.Stmt{
kind=.Return,
span=span_from(start.span, end.span),
name=end.symbol,
span=start.span,
expr=ast.INVALID_EXPR,
diagnostic=source.INVALID_DIAGNOSTIC,
})
@@ -1321,12 +1318,10 @@ parse_return :: proc(parser: ^Parser) -> ast.Stmt_Id {
return id
}
// `yield <expr>` supplies the value of the enclosing value block. The checker
// only accepts it as the final statement of a value block (a `{ ... }` on the
// right of a declaration/assignment); it is the block analogue of `return`.
// `yield <expr>` supplies a non-void value to an enclosing value construct.
// The expression must start on the same line; `break` handles valueless exits.
parse_yield :: proc(parser: ^Parser) -> ast.Stmt_Id {
start := advance(parser) // consume 'yield'
skip_newlines(parser)
// `yield :blk x` targets the loop labeled `blk`; a bare `yield x` targets
// the directly-enclosing value block / if branch. No expression starts with
// ':', so a leading colon is unambiguously a label.
@@ -1335,9 +1330,21 @@ parse_yield :: proc(parser: ^Parser) -> ast.Stmt_Id {
if name, name_ok := allow(parser, .Identifier); name_ok {
label = name.symbol
} else {
source.add(parser.diagnostics, current(parser).span, "expected a loop label after ':'")
source.add(parser.diagnostics, current(parser).span, "expected a yield target label after ':'")
}
}
if current(parser).kind == .Newline || current(parser).kind == .Right_Brace || current(parser).kind == .Eof {
expr := invalid_expr(parser, start.span, "'yield' must produce a value")
id := ast.stmt_id(len(parser.module.statements))
append(&parser.module.statements, ast.Stmt{
kind=.Yield,
span=start.span,
label=label,
expr=expr,
diagnostic=source.INVALID_DIAGNOSTIC,
})
return id
}
if cf, is_cf := parse_value_control_flow(parser); is_cf {
cf_span := parser.module.statements[cf].span
body := make([]ast.Stmt_Id, 1, parser.module.allocator)