From c92aefc2997a6fb6a0c8938c2ec4b69f2f47f94d Mon Sep 17 00:00:00 2001 From: MolecularPilot Date: Thu, 30 Oct 2025 17:36:11 +1100 Subject: [PATCH 1/5] rustc_builtin_macros: rename bench parameter to avoid collisions with user-defined function names --- compiler/rustc_builtin_macros/src/test.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/test.rs b/compiler/rustc_builtin_macros/src/test.rs index 7a189ee1f4d05..40b348c05b06e 100644 --- a/compiler/rustc_builtin_macros/src/test.rs +++ b/compiler/rustc_builtin_macros/src/test.rs @@ -207,9 +207,9 @@ pub(crate) fn expand_test_or_bench( }; let test_fn = if is_bench { - // A simple ident for a lambda - let b = Ident::from_str_and_span("b", attr_sp); - + // A simple ident for a lambda, using the user's function name within it to avoid collisons. + let param_name = format!("__bench_{}", fn_.ident.name); + let bencher_param = Ident::new(Symbol::intern(¶m_name), attr_sp); cx.expr_call( sp, cx.expr_path(test_path("StaticBenchFn")), @@ -226,11 +226,11 @@ pub(crate) fn expand_test_or_bench( cx.expr_call( ret_ty_sp, cx.expr_path(cx.path(sp, vec![fn_.ident])), - thin_vec![cx.expr_ident(sp, b)], + thin_vec![cx.expr_ident(sp, bencher_param)], ), ], ), - b, + bencher_param, )), // ) ], ) From ebe155a549c24dd04a532e1edcc1736d46488549 Mon Sep 17 00:00:00 2001 From: MolecularPilot Date: Thu, 30 Oct 2025 17:51:49 +1100 Subject: [PATCH 2/5] fix typo in comment --- compiler/rustc_builtin_macros/src/test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_builtin_macros/src/test.rs b/compiler/rustc_builtin_macros/src/test.rs index 40b348c05b06e..2891c1095cf4a 100644 --- a/compiler/rustc_builtin_macros/src/test.rs +++ b/compiler/rustc_builtin_macros/src/test.rs @@ -207,7 +207,7 @@ pub(crate) fn expand_test_or_bench( }; let test_fn = if is_bench { - // A simple ident for a lambda, using the user's function name within it to avoid collisons. + // A simple ident for a lambda, using the user's function name within it to avoid collisions. let param_name = format!("__bench_{}", fn_.ident.name); let bencher_param = Ident::new(Symbol::intern(¶m_name), attr_sp); cx.expr_call( From 7f382acfd2af3e420b5629aa9622d8b20cadc222 Mon Sep 17 00:00:00 2001 From: MolecularPilot Date: Fri, 31 Oct 2025 19:03:54 +1100 Subject: [PATCH 3/5] nit: continue using from_str_and_span as requested --- compiler/rustc_builtin_macros/src/test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_builtin_macros/src/test.rs b/compiler/rustc_builtin_macros/src/test.rs index 2891c1095cf4a..6163ed12fcd8d 100644 --- a/compiler/rustc_builtin_macros/src/test.rs +++ b/compiler/rustc_builtin_macros/src/test.rs @@ -209,7 +209,7 @@ pub(crate) fn expand_test_or_bench( let test_fn = if is_bench { // A simple ident for a lambda, using the user's function name within it to avoid collisions. let param_name = format!("__bench_{}", fn_.ident.name); - let bencher_param = Ident::new(Symbol::intern(¶m_name), attr_sp); + let bencher_param = Ident::from_str_and_span(¶m_name, attr_sp); cx.expr_call( sp, cx.expr_path(test_path("StaticBenchFn")), From 595f084dba4b0a91ced793006a3774b989a386d1 Mon Sep 17 00:00:00 2001 From: MolecularPilot Date: Wed, 5 Nov 2025 22:13:20 +1100 Subject: [PATCH 4/5] inline param_name as requested --- compiler/rustc_builtin_macros/src/test.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/test.rs b/compiler/rustc_builtin_macros/src/test.rs index 6163ed12fcd8d..0e0e93cad4e61 100644 --- a/compiler/rustc_builtin_macros/src/test.rs +++ b/compiler/rustc_builtin_macros/src/test.rs @@ -208,8 +208,8 @@ pub(crate) fn expand_test_or_bench( let test_fn = if is_bench { // A simple ident for a lambda, using the user's function name within it to avoid collisions. - let param_name = format!("__bench_{}", fn_.ident.name); - let bencher_param = Ident::from_str_and_span(¶m_name, attr_sp); + let bencher_param = + Ident::from_str_and_span(&format!("__bench_{}", fn_.ident.name), attr_sp); cx.expr_call( sp, cx.expr_path(test_path("StaticBenchFn")), From 50e1b4b496c1f63aa3e44b5385871fcc76ed47d9 Mon Sep 17 00:00:00 2001 From: MolecularPilot Date: Wed, 5 Nov 2025 22:22:57 +1100 Subject: [PATCH 5/5] update comment as requested. also change outdated b references in other comments --- compiler/rustc_builtin_macros/src/test.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/test.rs b/compiler/rustc_builtin_macros/src/test.rs index 0e0e93cad4e61..1d4d9e2128f1b 100644 --- a/compiler/rustc_builtin_macros/src/test.rs +++ b/compiler/rustc_builtin_macros/src/test.rs @@ -207,7 +207,7 @@ pub(crate) fn expand_test_or_bench( }; let test_fn = if is_bench { - // A simple ident for a lambda, using the user's function name within it to avoid collisions. + // A collision-resistant ident for a lambda, using the user's function name within it to avoid collisions. let bencher_param = Ident::from_str_and_span(&format!("__bench_{}", fn_.ident.name), attr_sp); cx.expr_call( @@ -215,14 +215,14 @@ pub(crate) fn expand_test_or_bench( cx.expr_path(test_path("StaticBenchFn")), thin_vec![ // #[coverage(off)] - // |b| self::test::assert_test_result( + // |__bench_fn_name| self::test::assert_test_result( coverage_off(cx.lambda1( sp, cx.expr_call( sp, cx.expr_path(test_path("assert_test_result")), thin_vec![ - // super::$test_fn(b) + // super::$test_fn(__bench_fn_name) cx.expr_call( ret_ty_sp, cx.expr_path(cx.path(sp, vec![fn_.ident])),