From 4911d39769e3b9ccf07062751106aa388bc97e48 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Fri, 15 Apr 2022 10:41:35 +0300 Subject: [PATCH] AstGen: handle rl_ty_inst for mutable variables --- src/AstGen.zig | 5 +++++ test/behavior/basic.zig | 20 +++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/AstGen.zig b/src/AstGen.zig index ccce4b0bc8..a5351f8769 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -2851,6 +2851,9 @@ fn varDecl( return &sub_scope.base; }, .keyword_var => { + const old_rl_ty_inst = gz.rl_ty_inst; + defer gz.rl_ty_inst = old_rl_ty_inst; + const is_comptime = var_decl.comptime_token != null or gz.force_comptime; var resolve_inferred_alloc: Zir.Inst.Ref = .none; const var_data: struct { @@ -2875,6 +2878,7 @@ fn varDecl( }); } }; + gz.rl_ty_inst = type_inst; break :a .{ .alloc = alloc, .result_loc = .{ .ptr = alloc } }; } else a: { const alloc = alloc: { @@ -2894,6 +2898,7 @@ fn varDecl( }); } }; + gz.rl_ty_inst = .none; resolve_inferred_alloc = alloc; break :a .{ .alloc = alloc, .result_loc = .{ .inferred_ptr = alloc } }; }; diff --git a/test/behavior/basic.zig b/test/behavior/basic.zig index 134ca1a235..96aa6900ee 100644 --- a/test/behavior/basic.zig +++ b/test/behavior/basic.zig @@ -859,7 +859,6 @@ test "catch in block has correct result location" { if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; const S = struct { fn open() error{A}!@This() { @@ -887,3 +886,22 @@ test "labeled block with runtime branch forwards its result location type to bre }; try expect(e == .b); } + +test "try in labeled block doesn't cast to wrong type" { + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; + + const S = struct { + a: u32, + fn foo() anyerror!u32 { + return 1; + } + }; + const s: ?*S = blk: { + var a = try S.foo(); + + _ = a; + break :blk null; + }; + _ = s; +}