-
-
Notifications
You must be signed in to change notification settings - Fork 14.3k
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
|
The job Click to see the possible cause of the failure (guessed by this bot) |
|
|
| 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 |
|
|
|
Nominated for @rust-lang/lang since this adds a new lint. |
In the PR lint for black_boxing ZSTs is implemented 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_box specifically to prevent optimizations. When passed a ZST black_box effectively does nothing and the user receives no feedback. Creating confusion and flaws.
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