authorgravatar for matthew.mcallister.0@gmail.comMatthew McAllister <matthew.mcallister.0@gmail.com> 2019-02-01 03:12:56-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-01 17:14:12-05:00
log8bedb10939b511e39787b2693249d2d3e8102854
treea9c1149afe28219dba160000c86af06396efe6fe
parentae1ebe09b7c1258bfa8de37244fd9b510b1447a4

Fix runtime assignment to comptime aggregate field

This was causing a segfault

2 files changed, 32 insertions(+), 2 deletions(-)

src/ir.cpp+4-2
...@@ -16533,7 +16533,8 @@ static IrInstruction *ir_analyze_container_init_fields_union(IrAnalyze *ira, IrI...@@ -16533,7 +16533,8 @@ static IrInstruction *ir_analyze_container_init_fields_union(IrAnalyze *ira, IrI
16533 if ((err = type_resolve(ira->codegen, casted_field_value->value.type, ResolveStatusZeroBitsKnown)))16533 if ((err = type_resolve(ira->codegen, casted_field_value->value.type, ResolveStatusZeroBitsKnown)))
16534 return ira->codegen->invalid_instruction;16534 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;
16537 if (is_comptime || casted_field_value->value.special != ConstValSpecialRuntime ||16538 if (is_comptime || casted_field_value->value.special != ConstValSpecialRuntime ||
16538 !type_has_bits(casted_field_value->value.type))16539 !type_has_bits(casted_field_value->value.type))
16539 {16540 {
...@@ -16584,7 +16585,8 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc...@@ -16584,7 +16585,8 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc
1658416585
16585 IrInstructionStructInitField *new_fields = allocate<IrInstructionStructInitField>(actual_field_count);16586 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
16589 ConstExprValue const_val = {};16591 ConstExprValue const_val = {};
16590 const_val.special = ConstValSpecialStatic;16592 const_val.special = ConstValSpecialStatic;
test/compile_errors.zig+28
...@@ -5367,4 +5367,32 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -5367,4 +5367,32 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
5367 ,5367 ,
5368 ".tmp_source.zig:2:35: error: expected sized integer or sized float, found comptime_float",5368 ".tmp_source.zig:2:35: error: expected sized integer or sized float, found comptime_float",
5369 );5369 );
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 );
5370}5398}