authorgravatar for twostepted@gmail.comTravis Staloch <twostepted@gmail.com> 2024-01-16 08:22:44-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-01-16 18:22:44+02:00
logf3353708d8df3b522c3ace618283f17a1733557b
tree21af01d50ae1e59dacf3a26c9f6b3592d1386981
parentda506aaf6ea731c72daaac649dba788407db0d6c
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

AstGen: use correct token_src for switch, if and while exprs

fixes #18579

5 files changed, 48 insertions(+), 8 deletions(-)

src/AstGen.zig+7-8
......@@ -6174,7 +6174,7 @@ fn ifExpr(
61746174 .gen_zir = &then_scope,
61756175 .name = ident_name,
61766176 .inst = payload_inst,
6177 .token_src = payload_token,
6177 .token_src = token_name_index,
61786178 .id_cat = .capture,
61796179 };
61806180 try then_scope.addDbgVar(.dbg_var_val, ident_name, payload_inst);
......@@ -6415,19 +6415,18 @@ fn whileExpr(
64156415 // will add this instruction to then_scope.instructions below
64166416 const payload_inst = try then_scope.makeUnNode(tag, cond.inst, while_full.ast.cond_expr);
64176417 opt_payload_inst = payload_inst.toOptional();
6418 const ident_token = if (payload_is_ref) payload_token + 1 else payload_token;
6418 const ident_token = payload_token + @intFromBool(payload_is_ref);
64196419 const ident_bytes = tree.tokenSlice(ident_token);
64206420 if (mem.eql(u8, "_", ident_bytes))
64216421 break :s &then_scope.base;
6422 const payload_name_loc = payload_token + @intFromBool(payload_is_ref);
6423 const ident_name = try astgen.identAsString(payload_name_loc);
6424 try astgen.detectLocalShadowing(&then_scope.base, ident_name, payload_name_loc, ident_bytes, .capture);
6422 const ident_name = try astgen.identAsString(ident_token);
6423 try astgen.detectLocalShadowing(&then_scope.base, ident_name, ident_token, ident_bytes, .capture);
64256424 payload_val_scope = .{
64266425 .parent = &then_scope.base,
64276426 .gen_zir = &then_scope,
64286427 .name = ident_name,
64296428 .inst = payload_inst.toRef(),
6430 .token_src = payload_token,
6429 .token_src = ident_token,
64316430 .id_cat = .capture,
64326431 };
64336432 dbg_var_name = ident_name;
......@@ -7107,7 +7106,7 @@ fn switchExprErrUnion(
71077106 .gen_zir = &case_scope,
71087107 .name = ident_name,
71097108 .inst = unwrapped_payload,
7110 .token_src = payload_token,
7109 .token_src = token_name_index,
71117110 .id_cat = .capture,
71127111 };
71137112 try case_scope.addDbgVar(.dbg_var_val, ident_name, unwrapped_payload);
......@@ -7667,7 +7666,7 @@ fn switchExpr(
76677666 .gen_zir = &case_scope,
76687667 .name = capture_name,
76697668 .inst = switch_block.toRef(),
7670 .token_src = payload_token,
7669 .token_src = ident,
76717670 .id_cat = .capture,
76727671 };
76737672 dbg_var_name = capture_name;
test/cases/compile_errors/capture_by_ref_if.zig created+10
......@@ -0,0 +1,10 @@
1test {
2 if (undefined) |*ident| {} else |err| {}
3}
4
5// error
6// backend=stage2
7// target=native
8//
9// :2:22: error: unused capture
10// :2:38: error: unused capture
test/cases/compile_errors/capture_by_ref_if_err_switch.zig created+10
......@@ -0,0 +1,10 @@
1test {
2 const e: error{A}!u32 = error.A;
3 if (e) |*ptr| {} else |err| switch (err) {}
4}
5
6// error
7// backend=stage2
8// target=native
9//
10// :3:14: error: unused capture
test/cases/compile_errors/capture_by_ref_switch.zig created+11
......@@ -0,0 +1,11 @@
1test {
2 switch (undefined) {
3 .a => |*ident| {},
4 }
5}
6
7// error
8// backend=stage2
9// target=native
10//
11// :3:17: error: unused capture
test/cases/compile_errors/capture_by_ref_while.zig created+10
......@@ -0,0 +1,10 @@
1test {
2 while (undefined) |*foo| {} else |err| {}
3}
4
5// error
6// backend=stage2
7// target=native
8//
9// :2:25: error: unused capture
10// :2:39: error: unused capture
\ No newline at end of file