rename expand to inline

This commit is contained in:
2026-08-02 20:31:05 +02:00
parent c92723bc14
commit 9e79d6692b
13 changed files with 123 additions and 82 deletions
+1 -1
View File
@@ -221,7 +221,7 @@ Stmt :: struct {
// list (empty marks the `else` arm; more than one is a multi-pattern arm),
// `captures` for the optional payload capture (0 or 1 name, tagged-union variants
// only) with `pointer_capture` distinguishing `|@cap|` from `|cap|`, and `body`
// as the arm body. An expanded arm has `expand` set, no patterns, and one or two
// as the arm body. An `inline` arm has `expand` set, no patterns, and one or two
// captures for its specialized value/payload and optional tagged-union tag.
captures: []symbol.Id,
// `Match_Arm` pattern list; empty ⇒ the `else` arm.
+14 -14
View File
@@ -10574,7 +10574,7 @@ expand_field_bindings :: proc(
value := state.values[value_id]
if ct_value_contains_undefined(&state, value_id) {
if diagnose {
_ = ct_fail(&state, .Not_Comptime, checker.ast_module.exprs[expr].span, "expand for cannot expand an undefined comptime value")
_ = ct_fail(&state, .Not_Comptime, checker.ast_module.exprs[expr].span, "inline for cannot iterate an undefined comptime value")
}
return nil, .Diagnosed if state.diagnostic != source.INVALID_DIAGNOSTIC else .Invalid
}
@@ -10712,7 +10712,7 @@ contains_expand_control :: proc(
}
case .For, .While:
// Unlabelled control belongs to the nested loop. A labelled jump can still
// name the surrounding expand loop and is therefore relevant here.
// name the surrounding inline loop and is therefore relevant here.
if contains_expand_control(checker, statement.body, target_label, false) {
return true
}
@@ -10817,7 +10817,7 @@ flatten_expand_iteration :: proc(
if diagnostic != nil {
diagnostic^ = source.add(
checker.diagnostics, statement.span,
"break or continue targeting an expand loop must be compile-time-resolvable",
"break or continue targeting an inline loop must be compile-time-resolvable",
)
}
return .Invalid
@@ -10856,7 +10856,7 @@ flatten_expand_iteration :: proc(
if diagnostic != nil {
diagnostic^ = source.add(
checker.diagnostics, statement.span,
"break or continue targeting an expand loop must be compile-time-resolvable",
"break or continue targeting an inline loop must be compile-time-resolvable",
)
}
return .Invalid
@@ -10882,7 +10882,7 @@ flatten_expand_iteration :: proc(
if diagnostic != nil {
diagnostic^ = source.add(
checker.diagnostics, statement.span,
"break or continue targeting an expand loop must be compile-time-resolvable",
"break or continue targeting an inline loop must be compile-time-resolvable",
)
}
return .Invalid
@@ -11814,7 +11814,7 @@ build_block :: proc(
case .For:
if statement.expand {
if statement.pointer_capture {
id := source.add(checker.diagnostics, statement.span, "expand for does not support pointer captures")
id := source.add(checker.diagnostics, statement.span, "inline for does not support pointer captures")
append(&body, hir.stmt_id(len(checker.module.statements)))
append(&checker.module.statements, hir.Stmt{kind=.Trap, span=statement.span, diagnostic=id})
ctx.problematic^ = true
@@ -11822,9 +11822,9 @@ build_block :: proc(
}
bindings, expand_error := expand_field_bindings(checker, statement.expr, statement.name, ctx.pkg, ctx.file, true)
if expand_error != .None && expand_error != .Diagnosed {
message := "expand for requires a comptime tuple, fixed array, range, slice, or reflection value"
message := "inline for requires a comptime tuple, fixed array, range, slice, or reflection value"
if expand_error == .Quota {
message = "expand for expansion exceeds the compile-time evaluation quota"
message = "inline for expansion exceeds the compile-time evaluation quota"
}
id := source.add(
checker.diagnostics, statement.span,
@@ -12869,28 +12869,28 @@ emit_match :: proc(
continue
}
if has_else || has_expand {
message := "arms after 'else' are unreachable" if has_else else "arms after 'expand' are unreachable"
message := "arms after 'else' are unreachable" if has_else else "arms after 'inline' are unreachable"
source.add(checker.diagnostics, arm.span, message)
ok = false
}
if arm.expand {
if !is_tagged && !is_enum_subject {
source.add(checker.diagnostics, arm.span, "'expand' requires an enum or tagged-union match subject")
source.add(checker.diagnostics, arm.span, "'inline' requires an enum or tagged-union match subject")
ok = false
continue
}
expected_captures := 1 if is_enum_subject else 2
if len(arm.captures) == 0 || len(arm.captures) > expected_captures {
description := "exactly one capture" if is_enum_subject else "one or two captures"
source.addf(checker.diagnostics, arm.span, "expanded match on '%s' requires %s", type_label(checker, subject_type), description)
source.addf(checker.diagnostics, arm.span, "inlined match on '%s' requires %s", type_label(checker, subject_type), description)
ok = false
}
if is_enum_subject && arm.pointer_capture {
source.add(checker.diagnostics, arm.span, "enum expansion does not support pointer captures")
source.add(checker.diagnostics, arm.span, "enum inlining does not support pointer captures")
ok = false
}
if len(arm.captures) > 1 && arm.captures[0] != checker.sink_symbol && arm.captures[0] == arm.captures[1] {
source.add(checker.diagnostics, arm.span, "expand captures must have distinct names")
source.add(checker.diagnostics, arm.span, "inline captures must have distinct names")
ok = false
}
remaining := 0
@@ -12963,7 +12963,7 @@ emit_match :: proc(
}
}
if remaining == 0 {
source.add(checker.diagnostics, arm.span, "redundant 'expand': the 'match' already covers every variant")
source.add(checker.diagnostics, arm.span, "redundant 'inline': the 'match' already covers every variant")
ok = false
}
has_expand = true
+1 -1
View File
@@ -40,7 +40,7 @@ keyword_kind :: proc(text: string) -> token.Kind {
case "if": return .Keyword_If
case "while": return .Keyword_While
case "for": return .Keyword_For
case "expand": return .Keyword_Expand
case "inline": return .Keyword_Inline
case "break": return .Keyword_Break
case "continue": return .Keyword_Continue
case "defer": return .Keyword_Defer
+8 -17
View File
@@ -1813,16 +1813,7 @@ parse_value_control_flow :: proc(parser: ^Parser) -> (ast.Stmt_Id, bool) {
}
parse_statement :: proc(parser: ^Parser) -> ast.Stmt_Id {
if current(parser).kind == .Identifier && token_text(parser, current(parser)) == "inline" &&
peek(parser).kind == .Keyword_For {
start := advance(parser)
id := parse_for(parser)
parser.module.statements[id].expand = true
parser.module.statements[id].span = span_from(start.span, parser.module.statements[id].span)
source.add(parser.diagnostics, start.span, "'inline for' was renamed to 'expand for'")
return id
}
if current(parser).kind == .Keyword_Expand && peek(parser).kind == .Keyword_For {
if current(parser).kind == .Keyword_Inline && peek(parser).kind == .Keyword_For {
start := advance(parser)
id := parse_for(parser)
parser.module.statements[id].expand = true
@@ -2266,7 +2257,7 @@ parse_arm_body :: proc(parser: ^Parser) -> []ast.Stmt_Id {
}
// parse_match_arm parses one `<pattern,...> [|[@]capture|]: <body>`, `else: <body>`,
// or `expand |[@]value[, tag]|: <body>` arm.
// or `inline |[@]value[, tag]|: <body>` arm.
parse_match_arm :: proc(parser: ^Parser) -> ast.Stmt_Id {
start := current(parser).span
patterns: [dynamic]ast.Expr_Id
@@ -2275,10 +2266,10 @@ parse_match_arm :: proc(parser: ^Parser) -> ast.Stmt_Id {
captures.allocator = parser.module.allocator
pointer_capture := false
expand := false
if _, is_expand := allow(parser, .Keyword_Expand); is_expand {
if _, is_expand := allow(parser, .Keyword_Inline); is_expand {
expand = true
if _, ok := allow(parser, .Pipe); !ok {
source.add(parser.diagnostics, current(parser).span, "expected '|' before expand captures")
source.add(parser.diagnostics, current(parser).span, "expected '|' before inline captures")
} else {
if _, at_ok := allow(parser, .At); at_ok {
pointer_capture = true
@@ -2288,7 +2279,7 @@ parse_match_arm :: proc(parser: ^Parser) -> ast.Stmt_Id {
advance(parser)
append(&captures, name_tok.symbol)
} else {
source.add(parser.diagnostics, current(parser).span, "expected an expand value capture")
source.add(parser.diagnostics, current(parser).span, "expected an inline value capture")
}
if _, comma_ok := allow(parser, .Comma); comma_ok {
tag_tok := current(parser)
@@ -2296,10 +2287,10 @@ parse_match_arm :: proc(parser: ^Parser) -> ast.Stmt_Id {
advance(parser)
append(&captures, tag_tok.symbol)
} else {
source.add(parser.diagnostics, current(parser).span, "expected an expand tag capture")
source.add(parser.diagnostics, current(parser).span, "expected an inline tag capture")
}
if _, extra := allow(parser, .Comma); extra {
source.add(parser.diagnostics, current(parser).span, "'expand' accepts at most two captures")
source.add(parser.diagnostics, current(parser).span, "'inline' accepts at most two captures")
for current(parser).kind != .Pipe && current(parser).kind != .Colon &&
current(parser).kind != .Newline && current(parser).kind != .Eof {
advance(parser)
@@ -2307,7 +2298,7 @@ parse_match_arm :: proc(parser: ^Parser) -> ast.Stmt_Id {
}
}
if _, close_ok := allow(parser, .Pipe); !close_ok {
source.add(parser.diagnostics, current(parser).span, "expected '|' to close expand captures")
source.add(parser.diagnostics, current(parser).span, "expected '|' to close inline captures")
}
}
} else if _, is_else := allow(parser, .Keyword_Else); !is_else {
+1 -1
View File
@@ -85,7 +85,7 @@ Kind :: enum u8 {
Keyword_If,
Keyword_While,
Keyword_For,
Keyword_Expand,
Keyword_Inline,
Keyword_Break,
Keyword_Continue,
Keyword_Defer,