AstGen: emit as instructions for branching expressions

There is a mechanism to avoid redundant `as` ZIR instructions which is
to pass `ResultLoc.coerced_ty` instead of `ResultLoc.ty` when it is
known by AstGen that Sema will do the coercion.

This commit downgrades `coerced_ty` to `ty` when a result location
passes through an expression that branches, such as `if`, `switch`,
`while`, and `for`, causing the `as` ZIR instruction to be emitted.

This ensures that the type of a result location will be applied to, e.g.
a `comptime_int` on either side of a branch on a runtime condition.
This commit is contained in:
Andrew Kelley
2022-01-15 23:13:44 -07:00
parent 7c6f5d26ea
commit 7f41e20802
3 changed files with 31 additions and 22 deletions

View File

@@ -179,3 +179,17 @@ test "access the null element of a null terminated array" {
try S.doTheTest();
comptime try S.doTheTest();
}
test "type deduction for array subscript expression" {
const S = struct {
fn doTheTest() !void {
var array = [_]u8{ 0x55, 0xAA };
var v0 = true;
try expect(@as(u8, 0xAA) == array[if (v0) 1 else 0]);
var v1 = false;
try expect(@as(u8, 0x55) == array[if (v1) 1 else 0]);
}
};
try S.doTheTest();
comptime try S.doTheTest();
}