authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-01-07 19:58:31+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-07 18:16:17-05:00
log2a5c622e65a07db95859beabf46f36d3a62c785a
tree401b1fd8c0796cadabadcc355840ccaf8150e62f
parent9f064bcf74ec0246630c0e5ed8df83df5c46aaaa

Fix crash with unresolved loc

Fixes #4099

2 files changed, 24 insertions(+), 7 deletions(-)

src/ir.cpp+7-7
...@@ -19278,7 +19278,7 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh...@@ -19278,7 +19278,7 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh
19278 return new_incoming_values.at(0);19278 return new_incoming_values.at(0);
19279 }19279 }
1928019280
19281 ZigType *resolved_type;19281 ZigType *resolved_type = nullptr;
19282 if (peer_parent != nullptr) {19282 if (peer_parent != nullptr) {
19283 bool peer_parent_has_type;19283 bool peer_parent_has_type;
19284 if ((err = ir_result_has_type(ira, peer_parent->parent, &peer_parent_has_type)))19284 if ((err = ir_result_has_type(ira, peer_parent->parent, &peer_parent_has_type)))
...@@ -19288,23 +19288,23 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh...@@ -19288,23 +19288,23 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh
19288 resolved_type = ira->explicit_return_type;19288 resolved_type = ira->explicit_return_type;
19289 } else if (peer_parent->parent->id == ResultLocIdCast) {19289 } else if (peer_parent->parent->id == ResultLocIdCast) {
19290 resolved_type = ir_resolve_type(ira, peer_parent->parent->source_instruction->child);19290 resolved_type = ir_resolve_type(ira, peer_parent->parent->source_instruction->child);
19291 if (type_is_invalid(resolved_type))19291 } else if (peer_parent->parent->resolved_loc) {
19292 return ira->codegen->invalid_instruction;
19293 } else {
19294 ZigType *resolved_loc_ptr_type = peer_parent->parent->resolved_loc->value->type;19292 ZigType *resolved_loc_ptr_type = peer_parent->parent->resolved_loc->value->type;
19295 ir_assert(resolved_loc_ptr_type->id == ZigTypeIdPointer, &phi_instruction->base);19293 ir_assert(resolved_loc_ptr_type->id == ZigTypeIdPointer, &phi_instruction->base);
19296 resolved_type = resolved_loc_ptr_type->data.pointer.child_type;19294 resolved_type = resolved_loc_ptr_type->data.pointer.child_type;
19297 }19295 }
19298 goto skip_resolve_peer_types;19296
19297 if (resolved_type != nullptr && type_is_invalid(resolved_type))
19298 return ira->codegen->invalid_instruction;
19299 }19299 }
19300 }19300 }
19301 {19301
19302 if (resolved_type == nullptr) {
19302 resolved_type = ir_resolve_peer_types(ira, phi_instruction->base.source_node, nullptr,19303 resolved_type = ir_resolve_peer_types(ira, phi_instruction->base.source_node, nullptr,
19303 new_incoming_values.items, new_incoming_values.length);19304 new_incoming_values.items, new_incoming_values.length);
19304 if (type_is_invalid(resolved_type))19305 if (type_is_invalid(resolved_type))
19305 return ira->codegen->invalid_instruction;19306 return ira->codegen->invalid_instruction;
19306 }19307 }
19307skip_resolve_peer_types:
1930819308
19309 switch (type_has_one_possible_value(ira->codegen, resolved_type)) {19309 switch (type_has_one_possible_value(ira->codegen, resolved_type)) {
19310 case OnePossibleValueInvalid:19310 case OnePossibleValueInvalid:
test/stage1/behavior/bitcast.zig+17
...@@ -150,3 +150,20 @@ test "comptime bitcast used in expression has the correct type" {...@@ -150,3 +150,20 @@ test "comptime bitcast used in expression has the correct type" {
150test "bitcast result to _" {150test "bitcast result to _" {
151 _ = @bitCast(u8, @as(i8, 1));151 _ = @bitCast(u8, @as(i8, 1));
152}152}
153
154test "nested bitcast" {
155 const S = struct {
156 fn moo(x: isize) void {
157 @import("std").testing.expectEqual(@intCast(isize, 42), x);
158 }
159
160 fn foo(x: isize) void {
161 @This().moo(
162 @bitCast(isize, if (x != 0) @bitCast(usize, x) else @bitCast(usize, x)),
163 );
164 }
165 };
166
167 S.foo(42);
168 comptime S.foo(42);
169}