default struct fields

This commit is contained in:
2026-07-18 16:23:47 +02:00
parent e889a99e55
commit 9eb7476522
10 changed files with 191 additions and 11 deletions
+17 -1
View File
@@ -1505,9 +1505,25 @@ ct_eval_struct_expr :: proc(state: ^Ct_State, expr: ast.Expr, expected: types.Ty
}
if !union_record {
for field, index in fields {
if values[index] == INVALID_CT_VALUE {
if values[index] != INVALID_CT_VALUE {
continue
}
field_default, has_default := find_struct_field_default(checker, struct_type, symbol.Id(field.name))
if !has_default {
return INVALID_CT_VALUE, ct_flow(.Normal), ct_failf(state, .Not_Comptime, expr.span, "missing initializer for struct field '%s'", symbol_text(checker, symbol.Id(field.name)))
}
previous_pkg, previous_file := state.pkg, state.file
state.pkg, state.file = field_default.pkg, field_default.file
value, flow, ok := ct_eval_expr(state, field_default.expr, field.type, depth+1)
state.pkg, state.file = previous_pkg, previous_file
if !ok || flow.kind != .Normal {
return INVALID_CT_VALUE, flow, ok
}
value, ok = ct_coerce_value(state, value, field.type, checker.ast_module.exprs[field_default.expr].span)
if !ok {
return INVALID_CT_VALUE, ct_flow(.Normal), false
}
values[index] = value
}
}
start := u32(len(state.children))