Commit 3607d9ee68

LemonBoy <thatlemon@gmail.com>
2020-01-07 19:20:57
Fix crash in struct initializer evaluation
Closes #4100
1 parent 688d021
Changed files (2)
src/ir.cpp
@@ -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);
test/compile_errors.zig
@@ -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;