commit 3607d9ee6821f524e2866f8ca44a5d577495d762 (tree)
parent 688d02176c05763fc8dcb9e5f5b1e5a19dc98c50
Author: LemonBoy <thatlemon@gmail.com>
Date: Tue, 7 Jan 2020 19:20:57 +0100
Fix crash in struct initializer evaluation
Closes #4100
Diffstat:
2 files changed, 14 insertions(+), 0 deletions(-)
diff --git a/src/ir.cpp b/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);
diff --git a/test/compile_errors.zig b/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;