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(...@@ -6174,7 +6174,7 @@ fn ifExpr(
6174 .gen_zir = &then_scope,6174 .gen_zir = &then_scope,
6175 .name = ident_name,6175 .name = ident_name,
6176 .inst = payload_inst,6176 .inst = payload_inst,
6177 .token_src = payload_token,6177 .token_src = token_name_index,
6178 .id_cat = .capture,6178 .id_cat = .capture,
6179 };6179 };
6180 try then_scope.addDbgVar(.dbg_var_val, ident_name, payload_inst);6180 try then_scope.addDbgVar(.dbg_var_val, ident_name, payload_inst);
...@@ -6415,19 +6415,18 @@ fn whileExpr(...@@ -6415,19 +6415,18 @@ fn whileExpr(
6415 // will add this instruction to then_scope.instructions below6415 // will add this instruction to then_scope.instructions below
6416 const payload_inst = try then_scope.makeUnNode(tag, cond.inst, while_full.ast.cond_expr);6416 const payload_inst = try then_scope.makeUnNode(tag, cond.inst, while_full.ast.cond_expr);
6417 opt_payload_inst = payload_inst.toOptional();6417 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);
6419 const ident_bytes = tree.tokenSlice(ident_token);6419 const ident_bytes = tree.tokenSlice(ident_token);
6420 if (mem.eql(u8, "_", ident_bytes))6420 if (mem.eql(u8, "_", ident_bytes))
6421 break :s &then_scope.base;6421 break :s &then_scope.base;
6422 const payload_name_loc = payload_token + @intFromBool(payload_is_ref);6422 const ident_name = try astgen.identAsString(ident_token);
6423 const ident_name = try astgen.identAsString(payload_name_loc);6423 try astgen.detectLocalShadowing(&then_scope.base, ident_name, ident_token, ident_bytes, .capture);
6424 try astgen.detectLocalShadowing(&then_scope.base, ident_name, payload_name_loc, ident_bytes, .capture);
6425 payload_val_scope = .{6424 payload_val_scope = .{
6426 .parent = &then_scope.base,6425 .parent = &then_scope.base,
6427 .gen_zir = &then_scope,6426 .gen_zir = &then_scope,
6428 .name = ident_name,6427 .name = ident_name,
6429 .inst = payload_inst.toRef(),6428 .inst = payload_inst.toRef(),
6430 .token_src = payload_token,6429 .token_src = ident_token,
6431 .id_cat = .capture,6430 .id_cat = .capture,
6432 };6431 };
6433 dbg_var_name = ident_name;6432 dbg_var_name = ident_name;
...@@ -7107,7 +7106,7 @@ fn switchExprErrUnion(...@@ -7107,7 +7106,7 @@ fn switchExprErrUnion(
7107 .gen_zir = &case_scope,7106 .gen_zir = &case_scope,
7108 .name = ident_name,7107 .name = ident_name,
7109 .inst = unwrapped_payload,7108 .inst = unwrapped_payload,
7110 .token_src = payload_token,7109 .token_src = token_name_index,
7111 .id_cat = .capture,7110 .id_cat = .capture,
7112 };7111 };
7113 try case_scope.addDbgVar(.dbg_var_val, ident_name, unwrapped_payload);7112 try case_scope.addDbgVar(.dbg_var_val, ident_name, unwrapped_payload);
...@@ -7667,7 +7666,7 @@ fn switchExpr(...@@ -7667,7 +7666,7 @@ fn switchExpr(
7667 .gen_zir = &case_scope,7666 .gen_zir = &case_scope,
7668 .name = capture_name,7667 .name = capture_name,
7669 .inst = switch_block.toRef(),7668 .inst = switch_block.toRef(),
7670 .token_src = payload_token,7669 .token_src = ident,
7671 .id_cat = .capture,7670 .id_cat = .capture,
7672 };7671 };
7673 dbg_var_name = capture_name;7672 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