diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-01-15 23:13:44 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2022-01-15 23:13:44 -0700 |
| commit | 7f41e20802fd8f9eb19c0218f1e2000e2751592a (patch) | |
| tree | 535a39fbae2c811e4c7af03b36cf52b3f164ad73 /test/behavior/array_llvm.zig | |
| parent | 7c6f5d26eadb137ab3fb2be340485ebd860a85fe (diff) | |
| download | zig-7f41e20802fd8f9eb19c0218f1e2000e2751592a.tar.gz zig-7f41e20802fd8f9eb19c0218f1e2000e2751592a.zip | |
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.
Diffstat (limited to 'test/behavior/array_llvm.zig')
| -rw-r--r-- | test/behavior/array_llvm.zig | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/test/behavior/array_llvm.zig b/test/behavior/array_llvm.zig index 5bbb06cffe..b4b0f1f268 100644 --- a/test/behavior/array_llvm.zig +++ b/test/behavior/array_llvm.zig @@ -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(); +} |
