Commit a76558db26

Andrew Kelley <superjoe30@gmail.com>
2017-03-15 02:38:27
fix behavior with reinterpreting constant memory
1 parent 7bc0145
Changed files (2)
src
test
cases
src/ir.cpp
@@ -8538,11 +8538,13 @@ static TypeTableEntry *ir_analyze_dereference(IrAnalyze *ira, IrInstructionUnOp
     // this dereference is always an rvalue because in the IR gen we identify lvalue and emit
     // one of the ptr instructions
 
-    if (value->value.special != ConstValSpecialRuntime) {
-        ConstExprValue *out_val = ir_build_const_from(ira, &un_op_instruction->base);
+    if (instr_is_comptime(value)) {
         ConstExprValue *pointee = const_ptr_pointee(&value->value);
-        *out_val = *pointee;
-        return child_type;
+        if (pointee->type == child_type) {
+            ConstExprValue *out_val = ir_build_const_from(ira, &un_op_instruction->base);
+            *out_val = *pointee;
+            return child_type;
+        }
     }
 
     ir_build_load_ptr_from(&ira->new_irb, &un_op_instruction->base, value);
test/cases/cast.zig
@@ -15,3 +15,13 @@ fn numLitIntToPtrCast() {
     const vga_mem = (&u16)(0xB8000);
     assert(usize(vga_mem) == 0xB8000);
 }
+
+fn pointerReinterpretConstFloatToInt() {
+    @setFnTest(this);
+
+    const float: f64 = 5.99999999999994648725e-01;
+    const float_ptr = &float;
+    const int_ptr = (&i32)(float_ptr);
+    const int_val = *int_ptr;
+    assert(int_val == 858993411);
+}