Commit f3353708d8

travisstaloch <1562827+travisstaloch@users.noreply.github.com>
2024-01-16 17:22:44
AstGen: use correct token_src for switch, if and while exprs
fixes #18579
1 parent da506aa
src/AstGen.zig
@@ -6174,7 +6174,7 @@ fn ifExpr(
                     .gen_zir = &then_scope,
                     .name = ident_name,
                     .inst = payload_inst,
-                    .token_src = payload_token,
+                    .token_src = token_name_index,
                     .id_cat = .capture,
                 };
                 try then_scope.addDbgVar(.dbg_var_val, ident_name, payload_inst);
@@ -6415,19 +6415,18 @@ fn whileExpr(
                 // will add this instruction to then_scope.instructions below
                 const payload_inst = try then_scope.makeUnNode(tag, cond.inst, while_full.ast.cond_expr);
                 opt_payload_inst = payload_inst.toOptional();
-                const ident_token = if (payload_is_ref) payload_token + 1 else payload_token;
+                const ident_token = payload_token + @intFromBool(payload_is_ref);
                 const ident_bytes = tree.tokenSlice(ident_token);
                 if (mem.eql(u8, "_", ident_bytes))
                     break :s &then_scope.base;
-                const payload_name_loc = payload_token + @intFromBool(payload_is_ref);
-                const ident_name = try astgen.identAsString(payload_name_loc);
-                try astgen.detectLocalShadowing(&then_scope.base, ident_name, payload_name_loc, ident_bytes, .capture);
+                const ident_name = try astgen.identAsString(ident_token);
+                try astgen.detectLocalShadowing(&then_scope.base, ident_name, ident_token, ident_bytes, .capture);
                 payload_val_scope = .{
                     .parent = &then_scope.base,
                     .gen_zir = &then_scope,
                     .name = ident_name,
                     .inst = payload_inst.toRef(),
-                    .token_src = payload_token,
+                    .token_src = ident_token,
                     .id_cat = .capture,
                 };
                 dbg_var_name = ident_name;
@@ -7107,7 +7106,7 @@ fn switchExprErrUnion(
                             .gen_zir = &case_scope,
                             .name = ident_name,
                             .inst = unwrapped_payload,
-                            .token_src = payload_token,
+                            .token_src = token_name_index,
                             .id_cat = .capture,
                         };
                         try case_scope.addDbgVar(.dbg_var_val, ident_name, unwrapped_payload);
@@ -7667,7 +7666,7 @@ fn switchExpr(
                     .gen_zir = &case_scope,
                     .name = capture_name,
                     .inst = switch_block.toRef(),
-                    .token_src = payload_token,
+                    .token_src = ident,
                     .id_cat = .capture,
                 };
                 dbg_var_name = capture_name;
test/cases/compile_errors/capture_by_ref_if.zig
@@ -0,0 +1,10 @@
+test {
+    if (undefined) |*ident| {} else |err| {}
+}
+
+// error
+// backend=stage2
+// target=native
+//
+// :2:22: error: unused capture
+// :2:38: error: unused capture
test/cases/compile_errors/capture_by_ref_if_err_switch.zig
@@ -0,0 +1,10 @@
+test {
+    const e: error{A}!u32 = error.A;
+    if (e) |*ptr| {} else |err| switch (err) {}
+}
+
+// error
+// backend=stage2
+// target=native
+//
+// :3:14: error: unused capture
test/cases/compile_errors/capture_by_ref_switch.zig
@@ -0,0 +1,11 @@
+test {
+    switch (undefined) {
+        .a => |*ident| {},
+    }
+}
+
+// error
+// backend=stage2
+// target=native
+//
+// :3:17: error: unused capture
test/cases/compile_errors/capture_by_ref_while.zig
@@ -0,0 +1,10 @@
+test {
+    while (undefined) |*foo| {} else |err| {}
+}
+
+// error
+// backend=stage2
+// target=native
+//
+// :2:25: error: unused capture
+// :2:39: error: unused capture
\ No newline at end of file