From cf9200b81594071d6378df2294da1c737c780c05 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 17 Sep 2018 18:13:38 -0400 Subject: [PATCH] dereferencing a *u0 is comptime-known to be 0 --- src/ir.cpp | 7 +++++++ test/cases/eval.zig | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/src/ir.cpp b/src/ir.cpp index ce044aa0f3..f4b727412e 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -11187,6 +11187,13 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc } } } + // dereferencing a *u0 is comptime known to be 0 + if (child_type->id == ZigTypeIdInt && child_type->data.integral.bit_count == 0) { + IrInstruction *result = ir_create_const(&ira->new_irb, source_instruction->scope, + source_instruction->source_node, child_type); + init_const_unsigned_negative(&result->value, child_type, 0, false); + return result; + } // TODO if the instruction is a const ref instruction we can skip it IrInstruction *load_ptr_instruction = ir_build_load_ptr(&ira->new_irb, source_instruction->scope, source_instruction->source_node, ptr); diff --git a/test/cases/eval.zig b/test/cases/eval.zig index 7e9c9a739d..7f8ad8ed07 100644 --- a/test/cases/eval.zig +++ b/test/cases/eval.zig @@ -682,3 +682,9 @@ test "refer to the type of a generic function" { } fn doNothingWithType(comptime T: type) void {} + +test "zero extend from u0 to u1" { + var zero_u0: u0 = 0; + var zero_u1: u1 = zero_u0; + assert(zero_u1 == 0); +}