Commit c9e01412a4

Andrew Kelley <superjoe30@gmail.com>
2017-12-14 07:07:23
fix compiler crash in a nullable if after an if in...
...a switch prong of a switch with 2 prongs in an else closes #656
1 parent f55fdc0
Changed files (3)
src
test
src/ir.cpp
@@ -1191,6 +1191,8 @@ static IrInstruction *ir_build_var_decl(IrBuilder *irb, Scope *scope, AstNode *s
     if (align_value) ir_ref_instruction(align_value, irb->current_basic_block);
     ir_ref_instruction(init_value, irb->current_basic_block);
 
+    var->decl_instruction = &decl_var_instruction->base;
+
     return &decl_var_instruction->base;
 }
 
@@ -5108,9 +5110,7 @@ static IrInstruction *ir_gen_var_decl(IrBuilder *irb, Scope *scope, AstNode *nod
     if (init_value == irb->codegen->invalid_instruction)
         return init_value;
 
-    IrInstruction *result = ir_build_var_decl(irb, scope, node, var, type_instruction, align_value, init_value);
-    var->decl_instruction = result;
-    return result;
+    return ir_build_var_decl(irb, scope, node, var, type_instruction, align_value, init_value);
 }
 
 static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *node) {
test/cases/bugs/656.zig
@@ -0,0 +1,30 @@
+const assert = @import("std").debug.assert;
+
+const PrefixOp = union(enum) {
+    Return,
+    AddrOf: Value,
+};
+
+const Value = struct {
+    align_expr: ?u32,
+};
+
+test "nullable if after an if in a switch prong of a switch with 2 prongs in an else" {
+    foo(false, true);
+}
+
+fn foo(a: bool, b: bool) {
+    var prefix_op = PrefixOp { .AddrOf = Value { .align_expr = 1234 } };
+    if (a) {
+    } else {
+        switch (prefix_op) {
+            PrefixOp.AddrOf => |addr_of_info| {
+                if (b) { }
+                if (addr_of_info.align_expr) |align_expr| {
+                    assert(align_expr == 1234);
+                }
+            },
+            PrefixOp.Return => {},
+        }
+    }
+}
test/behavior.zig
@@ -8,6 +8,7 @@ comptime {
     _ = @import("cases/bool.zig");
     _ = @import("cases/bugs/394.zig");
     _ = @import("cases/bugs/655.zig");
+    _ = @import("cases/bugs/656.zig");
     _ = @import("cases/cast.zig");
     _ = @import("cases/const_slice_child.zig");
     _ = @import("cases/defer.zig");