Skip to content

Conversation

@xonx4l
Copy link
Contributor

@xonx4l xonx4l commented Dec 15, 2025

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_box is 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"] to library/core/src/hint.rs.
Implemented a LateLintPass to detect calls to black_box where the argument's layout is a ZST.

Closes: #137658

@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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Dec 15, 2025
@rustbot
Copy link
Collaborator

rustbot commented Dec 15, 2025

r? @eholk

rustbot has assigned @eholk.
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
Copy link
Collaborator

The job aarch64-gnu-llvm-20-2 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
  |
  = note: using it is strongly discouraged
  = note: `#[warn(internal_features)]` on by default

warning: `black_box` on zero-sized callable `fn(u32, u32) -> u32 {add}` has no effect on call opacity
    --> compiler/rustc_lint/src/builtin.rs:3132:18
     |
3132 |     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
     = note: `#[warn(black_box_zst_calls)]` on by default

warning: 2 warnings emitted

Test compiled successfully, but it's marked `compile_fail`.
\ (no newline at end of output)

@rustbot rustbot assigned tgross35 and unassigned eholk Dec 22, 2025
@rustbot
Copy link
Collaborator

rustbot commented Dec 22, 2025

tgross35 is currently at their maximum review capacity.
They may take a while to respond.

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
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor

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.

@eholk
Copy link
Contributor

eholk commented Dec 22, 2025

@bors r+

@bors
Copy link
Collaborator

bors commented Dec 22, 2025

📌 Commit a02a37f has been approved by eholk

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 22, 2025
@RalfJung
Copy link
Member

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?

@RalfJung
Copy link
Member

@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Dec 22, 2025
@eholk
Copy link
Contributor

eholk commented Dec 22, 2025

Ah, good call! Thanks for stepping in here.

@xonx4l
Copy link
Contributor Author

xonx4l commented Dec 23, 2025

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?

Thanks for flagging this. I am happy to follow whatever process is needed.

@tgross35
Copy link
Contributor

r? eholk
@rustbot label +I-lang-nominated

@rustbot
Copy link
Collaborator

rustbot commented Dec 28, 2025

eholk is currently at their maximum review capacity.
They may take a while to respond.

@rustbot rustbot added the I-lang-nominated Nominated for discussion during a lang team meeting. label Dec 28, 2025
@tgross35 tgross35 added T-lang Relevant to the language team and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. I-lang-nominated Nominated for discussion during a lang team meeting. labels Dec 28, 2025
@tgross35 tgross35 added the I-lang-nominated Nominated for discussion during a lang team meeting. label Dec 28, 2025
@tgross35
Copy link
Contributor

Nominated for @rust-lang/lang since this adds a new lint.

@traviscross traviscross added needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang labels Dec 28, 2025
@traviscross traviscross added the I-lang-radar Items that are on lang's radar and will need eventual work or consideration. label Jan 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

I-lang-nominated Nominated for discussion during a lang team meeting. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lint on black_boxing ZSTs

8 participants