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
1739417394 }
1739517395 ZigType *parent_ptr_type = parent_result_loc->value->type;
1739617396 assert(parent_ptr_type->id == ZigTypeIdPointer);
17397 if ((err = type_resolve(ira->codegen, parent_ptr_type->data.pointer.child_type,
17398 ResolveStatusAlignmentKnown)))
17399 {
17397 ZigType *child_type = parent_ptr_type->data.pointer.child_type;
17398
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))) {
1740017411 return ira->codegen->invalid_instruction;
1740117412 }
17413
1740217414 uint64_t parent_ptr_align = get_ptr_align(ira->codegen, parent_ptr_type);
1740317415 if ((err = type_resolve(ira->codegen, value_type, ResolveStatusAlignmentKnown))) {
1740417416 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" {
146146 };
147147 expect(@bitCast(u8, Foo{ .value = 0xF }) == 0xf);
148148}
149
150test "bitcast result to _" {
151 _ = @bitCast(u8, @as(i8, 1));
152}