diff --git a/src/ir.cpp b/src/ir.cpp index 0c48a2f982..4147fcd599 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -15418,6 +15418,10 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe bitcasted_value = nullptr; } + if (bitcasted_value == nullptr || type_is_invalid(bitcasted_value->value.type)) { + return bitcasted_value; + } + IrInstruction *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, result_bit_cast->parent, dest_type, bitcasted_value, force_runtime, non_null_comptime, true); if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value.type) || diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 034800fd4c..1cc26bfe59 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -1229,6 +1229,15 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:2:25: error: destination type 'u7' has 7 bits but source type 'u8' has 8 bits", ); + cases.add( + "@bitCast with different sizes inside an expression", + \\export fn entry() void { + \\ var foo = (@bitCast(u8, f32(1.0)) == 0xf); + \\} + , + "tmp.zig:2:25: error: destination type 'u8' has size 1 but source type 'f32' has size 4", + ); + cases.add( "attempted `&&`", \\export fn entry(a: bool, b: bool) i32 { diff --git a/test/stage1/behavior/bitcast.zig b/test/stage1/behavior/bitcast.zig index 125e4cce54..ceb538c094 100644 --- a/test/stage1/behavior/bitcast.zig +++ b/test/stage1/behavior/bitcast.zig @@ -139,3 +139,10 @@ test "bitcast packed struct literal to byte" { const casted = @bitCast(u8, Foo{ .value = 0xF }); expect(casted == 0xf); } + +test "comptime bitcast used in expression has the correct type" { + const Foo = packed struct { + value: u8, + }; + expect(@bitCast(u8, Foo{ .value = 0xF }) == 0xf); +}