Skip to content

avoid suggesting traits from private dependencies #143038

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Qelxiros
Copy link
Contributor

@Qelxiros Qelxiros commented Jun 26, 2025

fixes #142676
fixes #138191

r? @tgross35

@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 Jun 26, 2025
@Qelxiros Qelxiros force-pushed the 142676-private-dependency-traits branch from 8dd63b7 to 2f3d599 Compare June 26, 2025 04:05
@tgross35
Copy link
Contributor

tgross35 commented Jun 26, 2025

Oh that's a wonderfully small fix.

Were you able to reproduce this with something other than object and compiler_builtins? The test here is good to have, but it would also be good to extend it with an aux build that puts some known trait into scope (in case object/builtins ever wind up with a different API or not being deps). As an example of that, check out the tests at https://github.com/rust-lang/rust/tree/bc4376fa73b636eb6f2c7d48b1f731d70f022c4b/tests/ui/privacy/pub-priv-dep.

Just make sure it actually fails without the fix since I know this is a bit touchy.

explain: bool,
) -> bool {
valid_out_of_scope_traits.retain(|id| !self.tcx.is_private_dep(id.krate));
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it might be better to rename all_traits to all_visible_traits (and update its usage) here https://github.com/rust-lang/rust/blob/2f3d599989883d0be932df3dceed65a6ec3274f8/compiler/rustc_hir_typeck/src/method/suggest.rs#L4387C15-L4389, then have that function call visible_traits

/// All traits in the crate graph, including those not visible to the user.
pub fn all_traits(self) -> impl Iterator<Item = DefId> {
iter::once(LOCAL_CRATE)
.chain(self.crates(()).iter().copied())
.flat_map(move |cnum| self.traits(cnum).iter().copied())
}
/// All traits that are visible within the crate graph (i.e. excluding private dependencies).
pub fn visible_traits(self) -> impl Iterator<Item = DefId> {
let visible_crates =
self.crates(()).iter().copied().filter(move |cnum| self.is_user_visible_dep(*cnum));
iter::once(LOCAL_CRATE)
.chain(visible_crates)
.flat_map(move |cnum| self.traits(cnum).iter().copied())
}
. That seems likely to catch future problems that don't go through suggest_valid_traits.

@estebank any opinions here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe renaming all_traits to all_traits_inclusing_private or something else verbose? visible_traits is what most people want/need so it should have the shorter, more findable name.

Copy link
Contributor

Choose a reason for hiding this comment

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

The actual tcx methods you mean? That seems reasonable to me to avoid the footgun. It's only named in ~10 places so @Qelxiros feel free to either do that in a second commit in this PR, or as a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm happy to add it here. As a side note, visible_traits above uses TyCtxt::is_user_visible_dep, which allows imports from direct private dependencies. Using that instead of is_private_dep still removes the erroneous suggestions, is more in line with my mental model of what I'm trying to do here, and aligns with the documentation of is_user_visible_dep:

/// dependency, or a [direct] private dependency. This is used to decide whether the crate can
/// be shown in `impl` suggestions.
I also think that this will preserve the compiler's ability to make suggestions like these for library developers. Am I missing anything here?

@rustbot
Copy link
Collaborator

rustbot commented Jun 26, 2025

HIR ty lowering was modified

cc @fmease

@Qelxiros
Copy link
Contributor Author

I'm assuming that ping is due to the function rename, see #143038 (comment) for context

@rust-log-analyzer

This comment has been minimized.

@Qelxiros Qelxiros force-pushed the 142676-private-dependency-traits branch from 1ac98c8 to a9d5951 Compare June 26, 2025 23:20
@Qelxiros
Copy link
Contributor Author

@estebank @tgross35 anything preventing merging this?

Copy link
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

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

LGTM as well, thank you for the changes!

One request if possible, are you able to adjust the history a bit? Split commits like:

  1. Add the tests and run+bless it so we have the current failures in history (this is purely nice to have, feel free to disregard this if it's tricky)
  2. Make compiler-builtins a private dep
  3. Add the fix and update the test (everything else)

This is so we will be able to backport only the library changes (second commit) to beta, which should fix #143215.

@Qelxiros Qelxiros force-pushed the 142676-private-dependency-traits branch from 0bd4396 to 92e5103 Compare June 30, 2025 22:05
@Qelxiros Qelxiros requested a review from tgross35 June 30, 2025 22:07
@tgross35
Copy link
Contributor

help: trait CastFrom which provides cast_from_lossy is implemented but not in scope; perhaps you want to import it

Awesome to see the failure reproduced in the first commit since I was having trouble getting it to work locally. Thank you for all the work here!

@bors r+

@bors
Copy link
Collaborator

bors commented Jun 30, 2025

📌 Commit 92e5103 has been approved by tgross35

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 Jun 30, 2025
@tgross35
Copy link
Contributor

tgross35 commented Jun 30, 2025

@rust-lang/libs nominating commit 208ab58 1e6e4bb for backport, which should fix #143215. The remaining commits aren't needed in beta since they are diagnostic changes.

@rustbot label +I-libs-nominated +beta-nominated

(updated team labels for the backport)

@rustbot rustbot added beta-nominated Nominated for backporting to the compiler in the beta channel. I-libs-nominated Nominated for discussion during a libs team meeting. labels Jun 30, 2025
@tgross35
Copy link
Contributor

@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 Jun 30, 2025
@tgross35 tgross35 removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 30, 2025
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 2, 2025
…-traits, r=tgross35

avoid suggesting traits from private dependencies

fixes rust-lang#142676
fixes rust-lang#138191

r? ``@tgross35``
bors added a commit that referenced this pull request Jul 2, 2025
Rollup of 11 pull requests

Successful merges:

 - #141829 (Specialize sleep_until implementation for unix (except mac))
 - #141847 (Explain `TOCTOU` on the top of `std::fs`, and reference it in functions)
 - #142138 (Add `Vec::into_chunks`)
 - #142321 (Expose elf abi on ppc64 targets)
 - #142886 (ci: aarch64-gnu: Stop skipping `panic_abort_doc_tests`)
 - #143038 (avoid suggesting traits from private dependencies)
 - #143194 (fix bitcast of single-element SIMD vectors)
 - #143206 (Align attr fixes)
 - #143258 (Don't recompute `DisambiguatorState` for every RPITIT in trait definition)
 - #143260 (Use the correct export kind for __rust_alloc_error_handler_should_panic)
 - #143274 (ci: support optional jobs)

r? `@ghost`
`@rustbot` modify labels: rollup
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 2, 2025
…-traits, r=tgross35

avoid suggesting traits from private dependencies

fixes rust-lang#142676
fixes rust-lang#138191

r? ```@tgross35```
bors added a commit that referenced this pull request Jul 2, 2025
Rollup of 12 pull requests

Successful merges:

 - #141847 (Explain `TOCTOU` on the top of `std::fs`, and reference it in functions)
 - #142138 (Add `Vec::into_chunks`)
 - #142321 (Expose elf abi on ppc64 targets)
 - #142886 (ci: aarch64-gnu: Stop skipping `panic_abort_doc_tests`)
 - #143038 (avoid suggesting traits from private dependencies)
 - #143194 (fix bitcast of single-element SIMD vectors)
 - #143206 (Align attr fixes)
 - #143231 (Suggest use another lifetime specifier instead of underscore lifetime)
 - #143232 ([COMPILETEST-UNTANGLE 3/N] Use "directives" consistently within compiletest)
 - #143258 (Don't recompute `DisambiguatorState` for every RPITIT in trait definition)
 - #143260 (Use the correct export kind for __rust_alloc_error_handler_should_panic)
 - #143274 (ci: support optional jobs)

r? `@ghost`
`@rustbot` modify labels: rollup
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 2, 2025
…-traits, r=tgross35

avoid suggesting traits from private dependencies

fixes rust-lang#142676
fixes rust-lang#138191

r? ````@tgross35````
bors added a commit that referenced this pull request Jul 2, 2025
Rollup of 11 pull requests

Successful merges:

 - #141847 (Explain `TOCTOU` on the top of `std::fs`, and reference it in functions)
 - #142138 (Add `Vec::into_chunks`)
 - #142321 (Expose elf abi on ppc64 targets)
 - #142886 (ci: aarch64-gnu: Stop skipping `panic_abort_doc_tests`)
 - #143038 (avoid suggesting traits from private dependencies)
 - #143194 (fix bitcast of single-element SIMD vectors)
 - #143231 (Suggest use another lifetime specifier instead of underscore lifetime)
 - #143232 ([COMPILETEST-UNTANGLE 3/N] Use "directives" consistently within compiletest)
 - #143258 (Don't recompute `DisambiguatorState` for every RPITIT in trait definition)
 - #143260 (Use the correct export kind for __rust_alloc_error_handler_should_panic)
 - #143274 (ci: support optional jobs)

r? `@ghost`
`@rustbot` modify labels: rollup
@matthiaskrgr
Copy link
Member

@bors r-
#143317 (comment)

@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 Jul 2, 2025
@Amanieu Amanieu added the beta-accepted Accepted for backporting to the compiler in the beta channel. label Jul 2, 2025
@tgross35
Copy link
Contributor

tgross35 commented Jul 2, 2025

Since read_at is only available on Unix via FileExt, I guess that makes the rest of the diagnostic not cross-platform (which is preexisting). I think you could make this cross-platform by adding something like

// By default, the `read` diagnostic suggests `std::os::unix::fs::FileExt::read_at`. Add 
// something more likely to be recommended to make the diagnostic cross-platform. 
trait DecoyRead {
    fn read1(&self) {}
}
impl<T> DecoyRead for Vec<T> {}

@Qelxiros Qelxiros force-pushed the 142676-private-dependency-traits branch from 51aa76f to 6b824e8 Compare July 3, 2025 02:05
@tgross35
Copy link
Contributor

tgross35 commented Jul 3, 2025

@bors2 try jobs=test-various

@rust-bors
Copy link

rust-bors bot commented Jul 3, 2025

⌛ Trying commit 6b824e8 with merge 9508371

To cancel the try build, run the command @bors2 try cancel.

rust-bors bot added a commit that referenced this pull request Jul 3, 2025
…<try>

avoid suggesting traits from private dependencies

<!-- homu-ignore:start -->
<!--
If this PR is related to an unstable feature or an otherwise tracked effort,
please link to the relevant tracking issue here. If you don't know of a related
tracking issue or there are none, feel free to ignore this.

This PR will get automatically assigned to a reviewer. In case you would like
a specific user to review your work, you can assign it to them by using

    r? <reviewer name>
-->
<!-- homu-ignore:end -->

fixes #142676
fixes #138191

r? `@tgross35`

try-job: test-various
@rust-bors
Copy link

rust-bors bot commented Jul 3, 2025

☀️ Try build successful (CI)
Build commit: 9508371 (9508371619dabdc5448e5949c21860d8f0afee61, parent: 6677875279b560442a07a08d5119b4cd6b3c5593)

@tgross35
Copy link
Contributor

tgross35 commented Jul 3, 2025

@bors r+

@bors
Copy link
Collaborator

bors commented Jul 3, 2025

📌 Commit 6b824e8 has been approved by tgross35

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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 3, 2025
jdonszelmann added a commit to jdonszelmann/rust that referenced this pull request Jul 3, 2025
…-traits, r=tgross35

avoid suggesting traits from private dependencies

fixes rust-lang#142676
fixes rust-lang#138191

r? `@tgross35`
jdonszelmann added a commit to jdonszelmann/rust that referenced this pull request Jul 3, 2025
…-traits, r=tgross35

avoid suggesting traits from private dependencies

fixes rust-lang#142676
fixes rust-lang#138191

r? ``@tgross35``
bors added a commit that referenced this pull request Jul 3, 2025
Rollup of 6 pull requests

Successful merges:

 - #134006 (setup typos check in CI)
 - #142876 (Port `#[target_feature]` to new attribute parsing infrastructure)
 - #143038 (avoid suggesting traits from private dependencies)
 - #143083 (Fix rustdoc not correctly showing attributes on re-exports)
 - #143283 (document optional jobs)
 - #143329 (minicore: use core's `diagnostic::on_unimplemented` messages)

Failed merges:

 - #143237 (Port `#[no_implicit_prelude]` to the new attribute parsing infrastructure)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beta-accepted Accepted for backporting to the compiler in the beta channel. beta-nominated Nominated for backporting to the compiler in the beta channel. I-libs-nominated Nominated for discussion during a libs team meeting. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rustc suggest adding an import of compiler_builtins Imports from private std dependency object shouldn't be suggested
8 participants