diff --git a/src/AstGen.zig b/src/AstGen.zig index 6ef69e2c6b..ad7cb813ef 100644 --- a/src/AstGen.zig +++ b/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; diff --git a/test/cases/compile_errors/capture_by_ref_if.zig b/test/cases/compile_errors/capture_by_ref_if.zig new file mode 100644 index 0000000000..58ac0a99b4 --- /dev/null +++ b/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 diff --git a/test/cases/compile_errors/capture_by_ref_if_err_switch.zig b/test/cases/compile_errors/capture_by_ref_if_err_switch.zig new file mode 100644 index 0000000000..a210f7cf8d --- /dev/null +++ b/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 diff --git a/test/cases/compile_errors/capture_by_ref_switch.zig b/test/cases/compile_errors/capture_by_ref_switch.zig new file mode 100644 index 0000000000..c1e9399e5d --- /dev/null +++ b/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 diff --git a/test/cases/compile_errors/capture_by_ref_while.zig b/test/cases/compile_errors/capture_by_ref_while.zig new file mode 100644 index 0000000000..916662c180 --- /dev/null +++ b/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