Fix crash in struct initializer evaluation

Closes #4100
This commit is contained in:
LemonBoy 2020-01-07 19:20:57 +01:00 committed by Andrew Kelley
parent 688d02176c
commit 3607d9ee68
2 changed files with 14 additions and 0 deletions

View File

@ -17479,6 +17479,8 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s
field->decl_node = value ? value->source_node : suspend_source_instr->source_node;
if (value && instr_is_comptime(value)) {
ZigValue *val = ir_resolve_const(ira, value, UndefOk);
if (!val)
return ira->codegen->invalid_instruction;
field->is_comptime = true;
field->init_val = create_const_vals(1);
copy_const_val(field->init_val, val);

View File

@ -2,6 +2,18 @@ const tests = @import("tests.zig");
const builtin = @import("builtin");
pub fn addCases(cases: *tests.CompileErrorContext) void {
cases.addTest("error in struct initializer doesn't crash the compiler",
\\pub export fn entry() void {
\\ const bitfield = struct {
\\ e: u8,
\\ e: u8,
\\ };
\\ var a = .{@sizeOf(bitfield)};
\\}
, &[_][]const u8{
"tmp.zig:4:9: error: duplicate struct field: 'e'",
});
cases.addTest("repeated invalid field access to generic function returning type crashes compiler. #2655",
\\pub fn A() type {
\\ return Q;