Commit 8fa88c88c2

Veikka Tuominen <git@vexu.eu>
2022-06-05 19:08:02
AstGen: fix coercion scope type when stores are eliminated
1 parent cb5d2b6
Changed files (2)
src
test
behavior
src/AstGen.zig
@@ -9875,6 +9875,9 @@ const GenZir = struct {
         errdefer as_scope.unstack();
         as_scope.rl_ptr = try as_scope.addBin(.coerce_result_ptr, dest_type, result_ptr);
 
+        // `rl_ty_inst` needs to be set in case the stores to `rl_ptr` are eliminated.
+        as_scope.rl_ty_inst = dest_type;
+
         return as_scope;
     }
 
test/behavior/basic.zig
@@ -1062,3 +1062,15 @@ comptime {
     s = S{ .a = 1 };
     assert(s.a == 1);
 }
+
+test "switch inside @as gets correct type" {
+    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
+
+    var a: u32 = 0;
+    var b: [2]u32 = undefined;
+    b[0] = @as(u32, switch (a) {
+        1 => 1,
+        else => 0,
+    });
+}