Skip to content

Lint unused pub items in binary crates#149509

Merged
rust-bors[bot] merged 3 commits intorust-lang:mainfrom
mu001999-contrib:flag/pub-as-pub-crate
May 7, 2026
Merged

Lint unused pub items in binary crates#149509
rust-bors[bot] merged 3 commits intorust-lang:mainfrom
mu001999-contrib:flag/pub-as-pub-crate

Conversation

@mu001999
Copy link
Copy Markdown
Member

@mu001999 mu001999 commented Dec 1, 2025

View all comments

This PR adds a new unstable flag -Ztreat-pub-as-pub-crate as @Kobzol suggested.
When compiling binary crates with this flag, the seed worklist will only contain the entry fn and won't contain other reachable items. Then we can do the dead code analysis for pub items just like they are pub(crate).

Related zulip thread #general > pub/pub(crate) within a binary is a footgun.


Updated:

Adds a new lint unused_pub_items_in_binary (crate-level, default allow for now) instead of the previous unstable flag to lint unused pub items for binary crates.

See more details of implementation in #149509 (comment).

This lint is allowed by default, but I believe this has been better than the unstable flag. Making it warn-by-default will lead to a lot of noise for this PR (like bless many tests). So I'd like to make it warn-by-default in a separate PR in the future.

@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 Dec 1, 2025
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Dec 1, 2025

r? @jdonszelmann

rustbot has assigned @jdonszelmann.
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

@mu001999 mu001999 changed the title Add new unstable flag -Ztreat-pub-as-pub-crate Add new unstable flag -Ztreat-pub-as-pub-crate Dec 1, 2025
@mu001999 mu001999 changed the title Add new unstable flag -Ztreat-pub-as-pub-crate Add a new unstable flag -Ztreat-pub-as-pub-crate Dec 1, 2025
@Noratrieb
Copy link
Copy Markdown
Member

Noratrieb commented Dec 1, 2025

What are the plans for this flag? Being made the default? As just a flag, it's essentially useless, because no one will know about it. So I'd be opposed to just adding it without any plan for making it useful for everyone.

@mu001999 mu001999 marked this pull request as draft December 2, 2025 01:13
@rustbot rustbot 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-review Status: Awaiting review from the assignee but also interested parties. labels Dec 2, 2025
@mu001999 mu001999 force-pushed the flag/pub-as-pub-crate branch 2 times, most recently from 6541f80 to 8ac52ba Compare December 2, 2025 02:12
@mu001999 mu001999 marked this pull request as ready for review December 2, 2025 02:13
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 2, 2025
@mu001999
Copy link
Copy Markdown
Member Author

mu001999 commented Dec 2, 2025

Maybe this could be a separate lint, I haven't thought too clearly yet

@jdonszelmann
Copy link
Copy Markdown
Contributor

Yea I do agree with nora here, maybe open a (specific) zulip thread to make a proper plan for this?

@jdonszelmann
Copy link
Copy Markdown
Contributor

I guess I'd prefer this as a lint (attribute at the crate root) myself

@jdonszelmann
Copy link
Copy Markdown
Contributor

@rustbot author

@rustbot rustbot 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-review Status: Awaiting review from the assignee but also interested parties. labels Dec 3, 2025
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Dec 3, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@mu001999 mu001999 marked this pull request as draft December 3, 2025 14:34
@Kobzol
Copy link
Copy Markdown
Member

Kobzol commented Dec 3, 2025

I guess that the use-case that I'm envisioning is to have an opt-in way to treat pub stuff as pub(crate) in binary crates. If it was an allow-by-default lint, which could be manually denied, that would serve that use-case, I think.

@GrantGryczan
Copy link
Copy Markdown

Why would the end goal be for this to be opt-in? I and others want to see #74970 become a default. I wouldn't opt into this, because that would make my codebase less idiomatic, diverging from how everyone else who hasn't opted in uses pub.

The reason this is useful as a default is because it doesn't semantically make sense to distinguish between pub and pub(crate) in binary crates. This creates a footgun because, for example, pub items can't receive dead_code warnings. If it's just opt-in, then that doesn't remove the footgun for anyone who hasn't opted in, and I don't see a benefit to pub and pub(crate) being distinct in binary crates.

The only reason I can see that someone might not want to opt into this is that one might use pub to prevent dead_code warnings as opposed to pub(crate). But I think this intent might be better communicated using #[expect(dead_code)] rather than the implicit behavior of pub. Dead code detection is useful as a default. (The only downside to that is that you then have to remove the #[expect(dead_code)] if you later use it, but I'll leave it to others to decide if that coupling is an important consideration.)

@mu001999 mu001999 force-pushed the flag/pub-as-pub-crate branch from 8ac52ba to 923596e Compare December 28, 2025 15:22
@mu001999 mu001999 force-pushed the flag/pub-as-pub-crate branch from 923596e to 1d89354 Compare March 19, 2026 12:06
@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the flag/pub-as-pub-crate branch from 1d89354 to e116866 Compare March 19, 2026 13:41
@traviscross
Copy link
Copy Markdown
Contributor

We talked about this at the tail end of the lang call today. The three of us present were OK with the new name dead_code_pub_in_binary. This is good to go as far as lang is concerned.

@traviscross traviscross removed I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang labels Apr 29, 2026
@traviscross traviscross dismissed their stale review April 29, 2026 16:07

Name changed.

@mu001999 mu001999 requested a review from jdonszelmann May 2, 2026 14:27
@jdonszelmann
Copy link
Copy Markdown
Contributor

Already said I was okay with the implementation, don't think anything changed since. @bors r+

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 6, 2026

📌 Commit 93abcca has been approved by jdonszelmann

It is now in the queue for this repository.

@JonathanBrouwer
Copy link
Copy Markdown
Contributor

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 6, 2026

This pull request was unapproved.

This PR was contained in a rollup (#156232), which was unapproved.

View changes since this unapproval

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 6, 2026

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.

@mu001999
Copy link
Copy Markdown
Member Author

mu001999 commented May 6, 2026

@bors r=jdonszelmann

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 6, 2026

📌 Commit 26bfcf3 has been approved by jdonszelmann

It is now in the queue for this repository.

@rust-bors

This comment has been minimized.

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 7, 2026

☀️ Test successful - CI
Approved by: jdonszelmann
Duration: 3h 9m 40s
Pushing 0e5924a to main...

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 7, 2026

What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing 4ddd453 (parent) -> 0e5924a (this PR)

Test differences

Show 814 test diffs

Stage 1

  • [ui] tests/ui/lint/dead-code-pub-in-binary/allow-dead-code-transitive.rs: [missing] -> pass (J1)
  • [ui] tests/ui/lint/dead-code-pub-in-binary/allow-lint.rs: [missing] -> pass (J1)
  • [ui] tests/ui/lint/dead-code-pub-in-binary/basic.rs: [missing] -> pass (J1)
  • [ui] tests/ui/lint/dead-code-pub-in-binary/deny-lint.rs: [missing] -> pass (J1)
  • [ui] tests/ui/lint/dead-code-pub-in-binary/interaction-allow-dead-code.rs: [missing] -> pass (J1)
  • [ui] tests/ui/lint/dead-code-pub-in-binary/interaction-allow-unused-pub.rs: [missing] -> pass (J1)
  • [ui] tests/ui/lint/dead-code-pub-in-binary/interaction-deny-dead-code-only.rs: [missing] -> pass (J1)
  • [ui] tests/ui/lint/dead-code-pub-in-binary/library.rs: [missing] -> pass (J1)
  • [ui] tests/ui/lint/dead-code-pub-in-binary/no-mangle-items.rs: [missing] -> pass (J1)

Stage 2

  • [ui] tests/ui/lint/dead-code-pub-in-binary/allow-dead-code-transitive.rs: [missing] -> pass (J0)
  • [ui] tests/ui/lint/dead-code-pub-in-binary/allow-lint.rs: [missing] -> pass (J0)
  • [ui] tests/ui/lint/dead-code-pub-in-binary/basic.rs: [missing] -> pass (J0)
  • [ui] tests/ui/lint/dead-code-pub-in-binary/deny-lint.rs: [missing] -> pass (J0)
  • [ui] tests/ui/lint/dead-code-pub-in-binary/interaction-allow-dead-code.rs: [missing] -> pass (J0)
  • [ui] tests/ui/lint/dead-code-pub-in-binary/interaction-allow-unused-pub.rs: [missing] -> pass (J0)
  • [ui] tests/ui/lint/dead-code-pub-in-binary/interaction-deny-dead-code-only.rs: [missing] -> pass (J0)
  • [ui] tests/ui/lint/dead-code-pub-in-binary/library.rs: [missing] -> pass (J0)
  • [ui] tests/ui/lint/dead-code-pub-in-binary/no-mangle-items.rs: [missing] -> pass (J0)

Additionally, 796 doctest diffs were found. These are ignored, as they are noisy.

Job group index

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard 0e5924a4a0b57f2c6bc8cac93f59281e68541a91 --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. x86_64-gnu-llvm-21: 1h 1m -> 1h 22m (+34.1%)
  2. dist-apple-various: 1h 41m -> 2h 6m (+24.1%)
  3. dist-i586-gnu-i586-i686-musl: 1h 36m -> 1h 16m (-20.8%)
  4. armhf-gnu: 1h 33m -> 1h 14m (-20.7%)
  5. x86_64-rust-for-linux: 41m 19s -> 49m 46s (+20.5%)
  6. dist-loongarch64-linux: 1h 51m -> 1h 30m (-18.5%)
  7. dist-armv7-linux: 1h 30m -> 1h 14m (-18.1%)
  8. dist-x86_64-apple: 1h 55m -> 2h 12m (+14.1%)
  9. dist-aarch64-msvc: 1h 40m -> 1h 52m (+11.4%)
  10. x86_64-gnu-llvm-22-2: 1h 39m -> 1h 27m (-11.3%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@rust-timer
Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (0e5924a): comparison URL.

Overall result: ❌ regressions - please read:

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.3% [0.1%, 0.4%] 4
Regressions ❌
(secondary)
0.1% [0.1%, 0.1%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.3% [0.1%, 0.4%] 4

Max RSS (memory usage)

Results (primary 0.1%, secondary -0.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
1.9% [1.9%, 1.9%] 1
Regressions ❌
(secondary)
2.4% [2.3%, 2.5%] 2
Improvements ✅
(primary)
-1.7% [-1.7%, -1.7%] 1
Improvements ✅
(secondary)
-2.7% [-6.2%, -0.4%] 3
All ❌✅ (primary) 0.1% [-1.7%, 1.9%] 2

Cycles

Results (primary -5.5%, secondary 0.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
1.8% [0.7%, 3.9%] 3
Improvements ✅
(primary)
-5.5% [-8.4%, -2.6%] 2
Improvements ✅
(secondary)
-0.6% [-0.9%, -0.4%] 3
All ❌✅ (primary) -5.5% [-8.4%, -2.6%] 2

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 499.861s -> 498.18s (-0.34%)
Artifact size: 394.98 MiB -> 394.99 MiB (0.00%)

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

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team to-announce Announce this issue on triage meeting

Projects

None yet

Development

Successfully merging this pull request may close these issues.