dereferencing a *u0 is comptime-known to be 0

This commit is contained in:
Andrew Kelley 2018-09-17 18:13:38 -04:00
parent 4c6f1e614a
commit cf9200b815
No known key found for this signature in database
GPG Key ID: 4E7CD66038A4D47C
2 changed files with 13 additions and 0 deletions

View File

@ -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);

View File

@ -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);
}