From 2a5c622e65a07db95859beabf46f36d3a62c785a Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Tue, 7 Jan 2020 19:58:31 +0100 Subject: [PATCH] Fix crash with unresolved loc Fixes #4099 --- src/ir.cpp | 14 +++++++------- test/stage1/behavior/bitcast.zig | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index 5fae82ceeb..e841da35d0 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -19278,7 +19278,7 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh return new_incoming_values.at(0); } - ZigType *resolved_type; + ZigType *resolved_type = nullptr; if (peer_parent != nullptr) { bool peer_parent_has_type; 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 resolved_type = ira->explicit_return_type; } else if (peer_parent->parent->id == ResultLocIdCast) { resolved_type = ir_resolve_type(ira, peer_parent->parent->source_instruction->child); - if (type_is_invalid(resolved_type)) - return ira->codegen->invalid_instruction; - } else { + } else if (peer_parent->parent->resolved_loc) { ZigType *resolved_loc_ptr_type = peer_parent->parent->resolved_loc->value->type; ir_assert(resolved_loc_ptr_type->id == ZigTypeIdPointer, &phi_instruction->base); resolved_type = resolved_loc_ptr_type->data.pointer.child_type; } - goto skip_resolve_peer_types; + + if (resolved_type != nullptr && type_is_invalid(resolved_type)) + return ira->codegen->invalid_instruction; } } - { + + if (resolved_type == nullptr) { resolved_type = ir_resolve_peer_types(ira, phi_instruction->base.source_node, nullptr, new_incoming_values.items, new_incoming_values.length); if (type_is_invalid(resolved_type)) return ira->codegen->invalid_instruction; } -skip_resolve_peer_types: switch (type_has_one_possible_value(ira->codegen, resolved_type)) { case OnePossibleValueInvalid: diff --git a/test/stage1/behavior/bitcast.zig b/test/stage1/behavior/bitcast.zig index 87b69d0e25..fe3dd6902d 100644 --- a/test/stage1/behavior/bitcast.zig +++ b/test/stage1/behavior/bitcast.zig @@ -150,3 +150,20 @@ test "comptime bitcast used in expression has the correct type" { test "bitcast result to _" { _ = @bitCast(u8, @as(i8, 1)); } + +test "nested bitcast" { + const S = struct { + fn moo(x: isize) void { + @import("std").testing.expectEqual(@intCast(isize, 42), x); + } + + fn foo(x: isize) void { + @This().moo( + @bitCast(isize, if (x != 0) @bitCast(usize, x) else @bitCast(usize, x)), + ); + } + }; + + S.foo(42); + comptime S.foo(42); +}