authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-01-02 12:56:46+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-02 10:41:11-05:00
log1e61e5f4041859859dc5b6f1936cccfd32e9c3a5
tree2e9f59b449f11ba743c9c81ab3880ec0c6a591a7
parentf35a963ac5a2ea053801c37be1271153f35b7df1

Don't ptrCast a result-location assignment to _

After #4010 doing `_ = @bitCast(...)` triggered a nonsensical compiler error.

2 files changed, 19 insertions(+), 3 deletions(-)

src/ir.cpp+15-3
...@@ -17394,11 +17394,23 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe...@@ -17394,11 +17394,23 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
17394 }17394 }
17395 ZigType *parent_ptr_type = parent_result_loc->value->type;17395 ZigType *parent_ptr_type = parent_result_loc->value->type;
17396 assert(parent_ptr_type->id == ZigTypeIdPointer);17396 assert(parent_ptr_type->id == ZigTypeIdPointer);
17397 if ((err = type_resolve(ira->codegen, parent_ptr_type->data.pointer.child_type,17397 ZigType *child_type = parent_ptr_type->data.pointer.child_type;
17398 ResolveStatusAlignmentKnown)))17398
17399 {17399 bool has_bits;
17400 if ((err = type_has_bits2(ira->codegen, child_type, &has_bits))) {
17401 return ira->codegen->invalid_instruction;
17402 }
17403
17404 // This happens when the bitCast result is assigned to _
17405 if (!has_bits) {
17406 assert(allow_discard);
17407 return parent_result_loc;
17408 }
17409
17410 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusAlignmentKnown))) {
17400 return ira->codegen->invalid_instruction;17411 return ira->codegen->invalid_instruction;
17401 }17412 }
17413
17402 uint64_t parent_ptr_align = get_ptr_align(ira->codegen, parent_ptr_type);17414 uint64_t parent_ptr_align = get_ptr_align(ira->codegen, parent_ptr_type);
17403 if ((err = type_resolve(ira->codegen, value_type, ResolveStatusAlignmentKnown))) {17415 if ((err = type_resolve(ira->codegen, value_type, ResolveStatusAlignmentKnown))) {
17404 return ira->codegen->invalid_instruction;17416 return ira->codegen->invalid_instruction;
test/stage1/behavior/bitcast.zig+4
...@@ -146,3 +146,7 @@ test "comptime bitcast used in expression has the correct type" {...@@ -146,3 +146,7 @@ test "comptime bitcast used in expression has the correct type" {
146 };146 };
147 expect(@bitCast(u8, Foo{ .value = 0xF }) == 0xf);147 expect(@bitCast(u8, Foo{ .value = 0xF }) == 0xf);
148}148}
149
150test "bitcast result to _" {
151 _ = @bitCast(u8, @as(i8, 1));
152}