better comptime match-statement support

This commit is contained in:
2026-07-16 08:57:54 +02:00
parent 3cc750b3b2
commit 1f25e6cd1d
4 changed files with 159 additions and 119 deletions
+12 -28
View File
@@ -8637,39 +8637,23 @@ specialization_match_body :: proc(
if !ok || flow.kind != .Normal || subject == INVALID_CT_VALUE || int(subject) >= len(state.values) {
return nil, {}, false, false
}
value := state.values[subject]
if value.kind != .Struct || !types.is_tagged_union(value.type, &checker.module.types) {
selection, selected := ct_select_match_arm(&state, statement, subject, 0)
if !selected {
return nil, {}, false, false
}
for arm_id in statement.body {
arm := checker.ast_module.statements[arm_id]
if arm.kind != .Match_Arm || arm.pointer_capture {
continue
arm := checker.ast_module.statements[selection.arm]
if arm.pointer_capture {
return nil, {}, false, false
}
if len(arm.captures) > 0 {
if selection.payload == INVALID_CT_VALUE || types.is_void(selection.payload_type) {
return nil, {}, false, false
}
if len(arm.patterns) == 0 {
return arm.body, {}, false, true
}
for pattern_id in arm.patterns {
if pattern_id == ast.INVALID_EXPR || int(pattern_id) >= len(checker.ast_module.exprs) {
continue
}
pattern := checker.ast_module.exprs[pattern_id]
if pattern.kind != .Enum_Literal {
continue
}
if index, field, found := find_struct_field(checker, value.type, pattern.name); found && index == int(value.active) {
if len(arm.captures) > 0 && arm.captures[0] != checker.sink_symbol && !types.is_void(field.type) {
children := ct_child_slice(&state, value)
if len(children) == 0 {
return nil, {}, false, false
}
return arm.body, store_static_binding(checker, &state, children[0], arm.captures[0]), true, true
}
return arm.body, {}, false, true
}
if arm.captures[0] != checker.sink_symbol {
return arm.body, store_static_binding(checker, &state, selection.payload, arm.captures[0]), true, true
}
}
return nil, {}, false, false
return arm.body, {}, false, true
}
Inline_Binding_Error :: enum u8 {