authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-03-28 12:23:19+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-28 13:05:08-07:00
loga415fe0bc083159cf4bca6984636a564589e9789
tree037e4548b04cbdde0eca8d07c3210a83fa1f4dec
parent3c4ac47e00d961186c8728df964a29a33275514c

AstGen: clear rl_ty_inst in setBreakResultLoc if one is not provided


2 files changed, 26 insertions(+), 2 deletions(-)

src/AstGen.zig+5-2
......@@ -2731,6 +2731,7 @@ fn varDecl(
27312731 };
27322732 resolve_inferred_alloc = alloc;
27332733 init_scope.rl_ptr = alloc;
2734 init_scope.rl_ty_inst = .none;
27342735 }
27352736 const init_result_loc: ResultLoc = .{ .block_ptr = &init_scope };
27362737 const init_inst = try reachableExpr(&init_scope, &init_scope.base, init_result_loc, var_decl.ast.init_node, node);
......@@ -4860,7 +4861,7 @@ fn orelseCatchExpr(
48604861 // block_scope unstacked now, can add new instructions to parent_gz
48614862 try parent_gz.instructions.append(astgen.gpa, block);
48624863
4863 var then_scope = parent_gz.makeSubBlock(scope);
4864 var then_scope = block_scope.makeSubBlock(scope);
48644865 defer then_scope.unstack();
48654866
48664867 // This could be a pointer or value depending on `unwrap_op`.
......@@ -4870,7 +4871,7 @@ fn orelseCatchExpr(
48704871 else => try rvalue(&then_scope, block_scope.break_result_loc, unwrapped_payload, node),
48714872 };
48724873
4873 var else_scope = parent_gz.makeSubBlock(scope);
4874 var else_scope = block_scope.makeSubBlock(scope);
48744875 defer else_scope.unstack();
48754876
48764877 var err_val_scope: Scope.LocalVal = undefined;
......@@ -9850,10 +9851,12 @@ const GenZir = struct {
98509851 },
98519852
98529853 .discard, .none, .ptr, .ref => {
9854 gz.rl_ty_inst = .none;
98539855 gz.break_result_loc = parent_rl;
98549856 },
98559857
98569858 .inferred_ptr => |ptr| {
9859 gz.rl_ty_inst = .none;
98579860 gz.rl_ptr = ptr;
98589861 gz.break_result_loc = .{ .block_ptr = gz };
98599862 },
test/behavior/basic.zig+21
......@@ -854,3 +854,24 @@ test "labeled block implicitly ends in a break" {
854854 if (a) break :blk;
855855 }
856856}
857
858test "catch in block has correct result location" {
859 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
860 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
861 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
862 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
863
864 const S = struct {
865 fn open() error{A}!@This() {
866 return @This(){};
867 }
868 fn foo(_: @This()) u32 {
869 return 1;
870 }
871 };
872 const config_h_text: u32 = blk: {
873 var dir = S.open() catch unreachable;
874 break :blk dir.foo();
875 };
876 try expect(config_h_text == 1);
877}