-
-
Notifications
You must be signed in to change notification settings - Fork 14.5k
Implement lint for black_boxing ZSTs #150037
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
|
|
compiler/rustc_lint/messages.ftl
Outdated
| lint_builtin_black_box_zst_call = `black_box` on zero-sized callable `{$ty}` has no effect on call opacity | ||
| .label = zero-sized callable passed here | ||
|
|
||
| lint_builtin_black_box_zst_help = coerce to a function pointer and call `black_box` on that pointer instead |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be cool if we were actually able to suggest a code fix here! But, I don't think that's necessary for this PR, just a nice future improvement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like? any example or idea?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
guess the error message already says to coerce the object to a pointer, but we could also show a suggested code snippet. I think that would also let rust-analyzer make the change automatically. Given an error message like:
error: `black_box` on zero-sized callable `fn(u32, u32) -> u32 {add}` has no effect on call opacity
--> $DIR/lint-black-box-zst-call.rs:10:18
|
LL | let add_bb = black_box(add);
| ^^^^^^^^^^---^
| |
| zero-sized callable passed here
|
= note: zero-sized callable values have no runtime representation, so the call still targets the original function directly
= help: coerce to a function pointer and call `black_box` on that pointer instead
The suggestion would be something like:
let add_ptr: fn(u32, u32) -> u32 = add;
let add_bb = black_box(add_ptr);
We have some other errors and warnings that make suggestions like this, so I'd try to look at what they do and see if you can reuse that infrastructure.
|
@bors r+ |
|
Lints are under the purview of t-lang. This is insta-stable as far as I can see. Has there been a t-lang FCP? |
|
@bors r- |
|
Ah, good call! Thanks for stepping in here. |
Thanks for flagging this. I am happy to follow whatever process is needed. |
|
r? eholk |
|
|
|
🔔 This is now entering its final comment period, as per the review above. 🔔 |
This comment has been minimized.
This comment has been minimized.
ce4318a to
c00ca79
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
This comment has been minimized.
This comment has been minimized.
compiler/rustc_lint/messages.ftl
Outdated
| lint_builtin_anonymous_params = anonymous parameters are deprecated and will be removed in the next edition | ||
| .suggestion = try naming the parameter or explicitly ignoring it | ||
|
|
||
| lint_builtin_black_box_zst_call = `black_box` on zero-sized callable `{$ty}` has no effect on call opacity |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the terminology "zero-sized callable" appears anywhere else. Should we be more specific and say "function item" or "closure" here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is another footgun potential here. For the specific case of closures, using black_box is probably wrong even if the closure's hidden type is has non-zero size.
After all, such use of black_box will apply the black box effect to the values of captured variables, but like here, it has no effect on call opacity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if I used "function item" in the message it would look confusing if a user triggered it with black_box(()) as my implementation checks layout.is_zst(). If you like I can update the message to "zero-sized type" to be more readable and accurate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And for the footgun I agree but this PR is scoped to ZSTs so I think to address it in a followup PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's not limited to callables, then yeah this should be reworded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the error message to zero-sized type making it more readable and accurate. Thanks!
|
TBH I'm -0 on this because But I also don't feel a need to try to block it 🤷 |
I stumbled into this when black-boxing a function for a benchmark (which is why I opened the issue). Of course it's probably ideal practice to do so, but I don't think most benchmark authors look at the assembly every time - I didn't until noticing weird results. |
This comment has been minimized.
This comment has been minimized.
|
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. |
|
The job Click to see the possible cause of the failure (guessed by this bot) |
|
☔ The latest upstream changes (presumably #152308) made this pull request unmergeable. Please resolve the merge conflicts. |
Implement a lint for black_boxing ZSTs as discussed in #137658
This PR implements a new lint which warns when
std::hint::black_boxis called with a Zero-Sized Type (ZST).Why the lint is needed?
The compiler is currently "silently failing" to do what the user asked. A warning closes the gap between what the user thinks is happening and what is actually happening. Users reach for
black_boxspecifically to prevent optimizations. When passed a ZSTblack_boxeffectively does nothing, yet the user receives no feedback, which creates confusion.Changes:
Added
#[rustc_diagnostic_item = "black_box"]tolibrary/core/src/hint.rs.Implemented a
LateLintPassto detect calls toblack_boxwhere the argument's layout is a ZST.Closes: #137658