This commit is contained in:
agra
2026-02-24 18:17:33 +02:00
parent 630e76c319
commit 7a381e1b4c
4 changed files with 90 additions and 2 deletions

View File

@@ -2694,6 +2694,17 @@ pub const CodeGen = struct {
.struct_literal => |sl| {
return self.evalConstantStruct(sl, target_ty);
},
.enum_literal => |el| {
if (target_ty.isEnum()) {
const variants = self.lookupEnumVariants(target_ty.enum_type) orelse return null;
for (variants, 0..) |vname, i| {
if (std.mem.eql(u8, vname, el.name)) {
return c.LLVMConstInt(llvm_ty, @intCast(i), 0);
}
}
}
return null;
},
else => return null,
}
}
@@ -2730,8 +2741,10 @@ pub const CodeGen = struct {
/// Register a top-level value constant (e.g., `SPECIAL_VALUE :u8: 42;`) as an LLVM global.
fn registerTopLevelConstant(self: *CodeGen, cd: ast.ConstDecl) !void {
const ta = cd.type_annotation orelse return; // need explicit type for top-level constants
const sx_ty = self.resolveType(ta);
const sx_ty = if (cd.type_annotation) |ta|
self.resolveType(ta)
else
self.inferType(cd.value);
if (sx_ty == .void_type) return;
const const_val = self.evalConstant(cd.value, sx_ty) orelse return;
@@ -6106,6 +6119,18 @@ pub const CodeGen = struct {
return self.wrapOptional(val, target_ty);
}
// Match/if expression with enum/union target: propagate type context into arms
// so enum literals like .red can resolve their type from the assignment target
if ((node.data == .match_expr or node.data == .if_expr) and
(target_ty.isEnum() or target_ty.isUnion()))
{
const saved = self.current_return_type;
self.current_return_type = target_ty;
const val = try self.genExpr(node);
self.current_return_type = saved;
return val;
}
// Enum literal assigned to enum type: resolve variant value
if (node.data == .enum_literal and target_ty.isEnum()) {
return self.genEnumLiteral(node.data.enum_literal.name, target_ty.enum_type);