From a76558db26ea175504830ae4aa840a1aa82b16d0 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 14 Mar 2017 21:38:27 -0400 Subject: [PATCH] fix behavior with reinterpreting constant memory --- src/ir.cpp | 10 ++++++---- test/cases/cast.zig | 10 ++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index 677bef308c..0539851454 100644 --- a/src/ir.cpp +++ b/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); diff --git a/test/cases/cast.zig b/test/cases/cast.zig index 3ab5834d4f..a7bef9ded5 100644 --- a/test/cases/cast.zig +++ b/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); +}