Skip to content

Commit

Permalink
fix compiler crash in a nullable if after an if in...
Browse files Browse the repository at this point in the history
...a switch prong of a switch with 2 prongs in an else

closes #656
  • Loading branch information
andrewrk committed Dec 14, 2017
1 parent f55fdc0 commit c9e0141
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions test/behavior.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
30 changes: 30 additions & 0 deletions test/cases/bugs/656.zig
Original file line number Diff line number Diff line change
@@ -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 => {},
}
}
}

0 comments on commit c9e0141

Please sign in to comment.