Skip to content

Commit 9387fa3

Browse files
authored
Rollup merge of rust-lang#148279 - IntegralPilot:master, r=hkBst
rustc_builtin_macros: rename bench parameter to avoid collisions with user-defined function names Resolves rust-lang#148275 by preventing name collisions in the `#[bench]` macro. Previously, a user-defined function named "b" could not be benchmarked because the macro-generated lambda identity collided with the same name. We now generate the lambda ident as `__bench_<function_name>`, ensuring it is always distinct from the user’s function. Because the prefix is applied recursively (e.g. benchmarking `__bench_b` produces a lambda ident `__bench___bench_b`), there is no possible function name that can equal its corresponding lambda ident. This guarantees that the user can safely bench a function of any valid name without risk of identifier collision.
2 parents a1b89b5 + 67802e0 commit 9387fa3

File tree

1 file changed

+7
-7
lines changed
  • compiler/rustc_builtin_macros/src

1 file changed

+7
-7
lines changed

compiler/rustc_builtin_macros/src/test.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,30 +207,30 @@ pub(crate) fn expand_test_or_bench(
207207
};
208208

209209
let test_fn = if is_bench {
210-
// A simple ident for a lambda
211-
let b = Ident::from_str_and_span("b", attr_sp);
212-
210+
// avoid name collisions by using the function name within the identifier, see bug #148275
211+
let bencher_param =
212+
Ident::from_str_and_span(&format!("__bench_{}", fn_.ident.name), attr_sp);
213213
cx.expr_call(
214214
sp,
215215
cx.expr_path(test_path("StaticBenchFn")),
216216
thin_vec![
217217
// #[coverage(off)]
218-
// |b| self::test::assert_test_result(
218+
// |__bench_fn_name| self::test::assert_test_result(
219219
coverage_off(cx.lambda1(
220220
sp,
221221
cx.expr_call(
222222
sp,
223223
cx.expr_path(test_path("assert_test_result")),
224224
thin_vec![
225-
// super::$test_fn(b)
225+
// super::$test_fn(__bench_fn_name)
226226
cx.expr_call(
227227
ret_ty_sp,
228228
cx.expr_path(cx.path(sp, vec![fn_.ident])),
229-
thin_vec![cx.expr_ident(sp, b)],
229+
thin_vec![cx.expr_ident(sp, bencher_param)],
230230
),
231231
],
232232
),
233-
b,
233+
bencher_param,
234234
)), // )
235235
],
236236
)

0 commit comments

Comments
 (0)