Skip to content

Commit 8bedb10

Browse files
matthew-mcallisterandrewrk
authored andcommitted
Fix runtime assignment to comptime aggregate field
This was causing a segfault
1 parent ae1ebe0 commit 8bedb10

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/ir.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16533,7 +16533,8 @@ static IrInstruction *ir_analyze_container_init_fields_union(IrAnalyze *ira, IrI
1653316533
if ((err = type_resolve(ira->codegen, casted_field_value->value.type, ResolveStatusZeroBitsKnown)))
1653416534
return ira->codegen->invalid_instruction;
1653516535

16536-
bool is_comptime = ir_should_inline(ira->new_irb.exec, instruction->scope);
16536+
bool is_comptime = ir_should_inline(ira->new_irb.exec, instruction->scope)
16537+
|| type_requires_comptime(ira->codegen, container_type) == ReqCompTimeYes;
1653716538
if (is_comptime || casted_field_value->value.special != ConstValSpecialRuntime ||
1653816539
!type_has_bits(casted_field_value->value.type))
1653916540
{
@@ -16584,7 +16585,8 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc
1658416585

1658516586
IrInstructionStructInitField *new_fields = allocate<IrInstructionStructInitField>(actual_field_count);
1658616587

16587-
bool is_comptime = ir_should_inline(ira->new_irb.exec, instruction->scope);
16588+
bool is_comptime = ir_should_inline(ira->new_irb.exec, instruction->scope)
16589+
|| type_requires_comptime(ira->codegen, container_type) == ReqCompTimeYes;
1658816590

1658916591
ConstExprValue const_val = {};
1659016592
const_val.special = ConstValSpecialStatic;

test/compile_errors.zig

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5367,4 +5367,32 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
53675367
,
53685368
".tmp_source.zig:2:35: error: expected sized integer or sized float, found comptime_float",
53695369
);
5370+
5371+
cases.add(
5372+
"runtime assignment to comptime struct type",
5373+
\\const Foo = struct {
5374+
\\ Bar: u8,
5375+
\\ Baz: type,
5376+
\\};
5377+
\\export fn f() void {
5378+
\\ var x: u8 = 0;
5379+
\\ const foo = Foo { .Bar = x, .Baz = u8 };
5380+
\\}
5381+
,
5382+
".tmp_source.zig:7:30: error: unable to evaluate constant expression",
5383+
);
5384+
5385+
cases.add(
5386+
"runtime assignment to comptime union type",
5387+
\\const Foo = union {
5388+
\\ Bar: u8,
5389+
\\ Baz: type,
5390+
\\};
5391+
\\export fn f() void {
5392+
\\ var x: u8 = 0;
5393+
\\ const foo = Foo { .Bar = x };
5394+
\\}
5395+
,
5396+
".tmp_source.zig:7:30: error: unable to evaluate constant expression",
5397+
);
53705398
}

0 commit comments

Comments
 (0)