fix const result loc, runtime if cond, else unreachable

Closes #2791. See that issue for more details; I documented the
debugging process quite thoroughly on this one.
master
Andrew Kelley 2019-09-02 20:28:25 -04:00
parent ab4cba14c8
commit 4a5b0cde13
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
2 changed files with 20 additions and 0 deletions

View File

@ -3522,6 +3522,15 @@ static LLVMValueRef ir_render_store_ptr(CodeGen *g, IrExecutable *executable, Ir
assert(ptr_type->id == ZigTypeIdPointer);
if (!type_has_bits(ptr_type))
return nullptr;
if (instruction->ptr->ref_count == 0) {
// In this case, this StorePtr instruction should be elided. Something happened like this:
// var t = true;
// const x = if (t) Num.Two else unreachable;
// The if condition is a runtime value, so the StorePtr for `x = Num.Two` got generated
// (this instruction being rendered) but because of `else unreachable` the result ended
// up being a comptime const value.
return nullptr;
}
bool have_init_expr = !value_is_all_undef(&instruction->value->value);
if (have_init_expr) {

View File

@ -63,3 +63,14 @@ test "labeled break inside comptime if inside runtime if" {
}
expect(answer == 42);
}
test "const result loc, runtime if cond, else unreachable" {
const Num = enum {
One,
Two,
};
var t = true;
const x = if (t) Num.Two else unreachable;
if (x != .Two) @compileError("bad");
}