Commit 3b4a6679a6

Timon Kruiper <timonkruiper@gmail.com>
2019-08-24 12:16:46
Fix comptime bitcast inside an expression
1 parent 695695c
Changed files (3)
src
test
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) ||
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);
+}
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 {