Skip to content

Conversation

@IntegralPilot
Copy link

@IntegralPilot IntegralPilot commented Oct 30, 2025

Resolves #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.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 30, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 30, 2025

r? @jackh726

rustbot has assigned @jackh726.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rust-log-analyzer

This comment has been minimized.


// 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(&param_name), attr_sp);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you change it from from_str_and_span to new + intern?

Copy link
Author

@IntegralPilot IntegralPilot Oct 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s mostly a style preference, @oli-obk. Since format! gives us a String (unlike the old str), we still need either a conversion or an interning step - from_str_and_span would need a .as_str() call, while new needs a Symbol::intern call. So from_str_and_span doesn’t really simplify anything, and I think the explicit intern call makes it flow a bit clearer and cleaner than a conversion, and also separates the logical steps of building the string and then interning it. That said, if you’d rather keep from_str_and_span and just add .as_str() for consistency, I’m good with that too, I really don't mind. Thank you for your time in reviewing! 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use &format...?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can't #[bench] a function named b

6 participants