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(...@@ -2731,6 +2731,7 @@ fn varDecl(
2731 };2731 };
2732 resolve_inferred_alloc = alloc;2732 resolve_inferred_alloc = alloc;
2733 init_scope.rl_ptr = alloc;2733 init_scope.rl_ptr = alloc;
2734 init_scope.rl_ty_inst = .none;
2734 }2735 }
2735 const init_result_loc: ResultLoc = .{ .block_ptr = &init_scope };2736 const init_result_loc: ResultLoc = .{ .block_ptr = &init_scope };
2736 const init_inst = try reachableExpr(&init_scope, &init_scope.base, init_result_loc, var_decl.ast.init_node, node);2737 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(...@@ -4860,7 +4861,7 @@ fn orelseCatchExpr(
4860 // block_scope unstacked now, can add new instructions to parent_gz4861 // block_scope unstacked now, can add new instructions to parent_gz
4861 try parent_gz.instructions.append(astgen.gpa, block);4862 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);
4864 defer then_scope.unstack();4865 defer then_scope.unstack();
48654866
4866 // This could be a pointer or value depending on `unwrap_op`.4867 // This could be a pointer or value depending on `unwrap_op`.
...@@ -4870,7 +4871,7 @@ fn orelseCatchExpr(...@@ -4870,7 +4871,7 @@ fn orelseCatchExpr(
4870 else => try rvalue(&then_scope, block_scope.break_result_loc, unwrapped_payload, node),4871 else => try rvalue(&then_scope, block_scope.break_result_loc, unwrapped_payload, node),
4871 };4872 };
48724873
4873 var else_scope = parent_gz.makeSubBlock(scope);4874 var else_scope = block_scope.makeSubBlock(scope);
4874 defer else_scope.unstack();4875 defer else_scope.unstack();
48754876
4876 var err_val_scope: Scope.LocalVal = undefined;4877 var err_val_scope: Scope.LocalVal = undefined;
...@@ -9850,10 +9851,12 @@ const GenZir = struct {...@@ -9850,10 +9851,12 @@ const GenZir = struct {
9850 },9851 },
98519852
9852 .discard, .none, .ptr, .ref => {9853 .discard, .none, .ptr, .ref => {
9854 gz.rl_ty_inst = .none;
9853 gz.break_result_loc = parent_rl;9855 gz.break_result_loc = parent_rl;
9854 },9856 },
98559857
9856 .inferred_ptr => |ptr| {9858 .inferred_ptr => |ptr| {
9859 gz.rl_ty_inst = .none;
9857 gz.rl_ptr = ptr;9860 gz.rl_ptr = ptr;
9858 gz.break_result_loc = .{ .block_ptr = gz };9861 gz.break_result_loc = .{ .block_ptr = gz };
9859 },9862 },
test/behavior/basic.zig+21
...@@ -854,3 +854,24 @@ test "labeled block implicitly ends in a break" {...@@ -854,3 +854,24 @@ test "labeled block implicitly ends in a break" {
854 if (a) break :blk;854 if (a) break :blk;
855 }855 }
856}856}
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}