Skip to content
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

Diagnostics sometimes suggest items from rustc_private crates #135232

Closed
tgross35 opened this issue Jan 8, 2025 · 3 comments · Fixed by #135278
Closed

Diagnostics sometimes suggest items from rustc_private crates #135232

tgross35 opened this issue Jan 8, 2025 · 3 comments · Fixed by #135278
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tgross35
Copy link
Contributor

tgross35 commented Jan 8, 2025

Code

This requires a compiler_builtins that includes a public or possibly public (i.e. feature gated) trait. The most recent v0.1.142 has this on Windows only (attempted update in #135180), I have a patched branch that makes this trait available on all platforms https://github.com/tgross35/rust/tree/experiment-builtins-math.

trait Trait { type Bar; }
type Foo = dyn Trait<F = i32, Bar = i32>;

Current output

Running rustc +stage0 foo.rs --crate-type=lib

--> foo.rs:6:22
  |
6 | type Foo = dyn Trait<F = i32, Bar = i32>;
  |                      ^ there is a similarly named associated type `H` in the trait `compiler_builtins::math::libm::support::int_traits::DInt`

Desired output

Don't suggest using compiler_builtins unless the crate is #![feature(rustc_private)]

Rationale and extra context

My investigation on Zulip https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/Why.20do.20diagnostics.20suggest.20compiler_builtins

Rust Version

Current stage0

rustc 1.84.0-beta.1 (b4297a573 2024-11-26)
binary: rustc
commit-hash: b4297a573b4eefacd62e7ea1ba071536282d3254
commit-date: 2024-11-26
host: x86_64-unknown-linux-gnu
release: 1.84.0-beta.1
LLVM version: 19.1.4

Anything else?

No response

@tgross35 tgross35 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 8, 2025
@tgross35
Copy link
Contributor Author

tgross35 commented Jan 8, 2025

The context is that compiler_builtins has a lot of traits that are usually private but are public with a feature for testing. Currently everything is wrapped in a cfged macro so the traits don't even make it to AST unless the relevant config is enabled. It would be much nicer to instead just make the relevant module conditionally private/public, but since diagnostics ignore module visibility, that means they can show up in diagnostics if the crate is accessible.

I did try making that change before rust-lang/compiler-builtins#656, which had to be reverted because of the failure at #128691 (comment). Didn't look into the cause too much at that time. But in any case, the current situation isn't very robust, and finding problems only at sync time is inconvenient.

I can try to figure this out but I'm not sure where to start. @compiler-errors this seems like your territory, any suggestions?

@tgross35
Copy link
Contributor Author

tgross35 commented Jan 8, 2025

Also this only seems to apply to trait-related diagnostics, e.g. calling a function from compiler_builtins doesn't suggest anything.

fn main() {
    __adddf3(1.0, 2.0);
}

@tgross35
Copy link
Contributor Author

tgross35 commented Jan 8, 2025

More reliable repro that works on stable and refers to addr2line from the sysroot

trait Trait { type Bar; }
type Foo = dyn Trait<Buf = i32, Bar = i32>;
error[E0220]: associated type `Buf` not found for `Trait`
 --> src/lib.rs:2:22
  |
2 | type Foo = dyn Trait<Buf = i32, Bar = i32>;
  |                      ^^^ there is an associated type `Buf` in the trait `addr2line::LookupContinuation`

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f137a5a714aff5759deb28283947f9c6

Looks like this might be easier than expected if this is the only kind of error, maybe just a filter at

if let [best_trait] = visible_traits
.iter()
.copied()
.filter(|trait_def_id| {
tcx.associated_items(trait_def_id)
.filter_by_name_unhygienic(suggested_name)
.any(|item| item.kind == assoc_kind)
})
.collect::<Vec<_>>()[..]
.

@jieyouxu jieyouxu added the D-confusing Diagnostics: Confusing error or lint that should be reworked. label Jan 8, 2025
@tgross35 tgross35 self-assigned this Jan 9, 2025
tgross35 added a commit to tgross35/rust that referenced this issue Jan 9, 2025
For places where we don't want stdlib-private crates to be accessible,
use `.visible_crates()` rather than `.crates()`. The current effect is
that items in stdlib-private crates will no longer show up in
diagnostics related to trait selection.

Fixes: rust-lang#135232
tgross35 added a commit to tgross35/rust that referenced this issue Jan 12, 2025
For places where we don't want stdlib-private crates to be accessible,
use `.visible_crates()` rather than `.crates()`. The current effect is
that items in stdlib-private crates will no longer show up in
diagnostics related to trait selection.

Fixes: rust-lang#135232
tgross35 added a commit to tgross35/rust that referenced this issue Jan 13, 2025
For places where we don't want stdlib-private crates to be accessible,
use `.visible_crates()` rather than `.crates()`. The current effect is
that items in stdlib-private crates will no longer show up in
diagnostics related to trait selection.

Fixes: rust-lang#135232
tgross35 added a commit to tgross35/rust that referenced this issue Jan 13, 2025
For places where we don't want stdlib-private crates to be accessible,
use `.visible_crates()` rather than `.crates()`. The current effect is
that items in stdlib-private crates will no longer show up in
diagnostics related to trait selection.

Fixes: rust-lang#135232
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 13, 2025
Exclude dependencies of `std` for diagnostics

Currently crates in the sysroot can show up in diagnostic suggestions, such as in rust-lang#135232. To prevent this, introduce a query `visible_crates` that excludes crates marked `is_private_dep` and use this query in user-facing areas of code.

Setting `#![feature(rustc_private)]` overrides this to just use all crates, since `rustc_private` enables use of `std`'s private dependencies.

Notably, this changes `all_traits` to use `.visible_crates(())` rather than `.crates(())`. This is not strictly required and filtering could happen later as-needed; however, I cannot think of any cases where traits from private dependencies should be relevant in trait selection or diagnostics, so filtering at this earlier point seems more likely to avoid similar issues in the future.

This may be reviewed per-commit.

Fixes: rust-lang#135232
tgross35 added a commit to tgross35/rust that referenced this issue Jan 13, 2025
For places where we don't want stdlib-private crates to be accessible,
use `.visible_crates()` rather than `.crates()`. The current effect is
that items in stdlib-private crates will no longer show up in
diagnostics related to trait selection.

Fixes: rust-lang#135232
tgross35 added a commit to tgross35/rust that referenced this issue Jan 13, 2025
Add an alternative to `tcx.all_traits()` that only shows traits that the
user might be able to use, for diagnostic purposes. With this available,
make use of it for diagnostics including associated type errors which
fixes [1].

Includes a few comment updates for related API.

Fixes: rust-lang#135232 [1]
tgross35 added a commit to tgross35/rust that referenced this issue Jan 13, 2025
Add an alternative to `tcx.all_traits()` that only shows traits that the
user might be able to use, for diagnostic purposes. With this available,
make use of it for diagnostics including associated type errors which
fixes [1].

Includes a few comment updates for related API.

Fixes: rust-lang#135232 [1]
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 13, 2025
Exclude dependencies of `std` for diagnostics

Currently crates in the sysroot can show up in diagnostic suggestions, such as in rust-lang#135232. To prevent this, duplicate `all_traits` into `visible_traits` which only shows traits in non-private crates.

Setting `#![feature(rustc_private)]` overrides this and makes items in private crates visible as well, since `rustc_private` enables use of `std`'s private dependencies.

This may be reviewed per-commit.

Fixes: rust-lang#135232
tgross35 added a commit to tgross35/rust that referenced this issue Jan 14, 2025
Add an alternative to `tcx.all_traits()` that only shows traits that the
user might be able to use, for diagnostic purposes. With this available,
make use of it for diagnostics including associated type errors which
fixes [1].

Includes a few comment updates for related API.

Fixes: rust-lang#135232 [1]
tgross35 added a commit to tgross35/rust that referenced this issue Jan 14, 2025
Add an alternative to `tcx.all_traits()` that only shows traits that the
user might be able to use, for diagnostic purposes. With this available,
make use of it for diagnostics including associated type errors, which
is part of the problem with [1].

Includes a few comment updates for related API.

[1]: rust-lang#135232
tgross35 added a commit to tgross35/rust that referenced this issue Jan 14, 2025
In order to avoid diagnostics suggesting stdlib-private dependencies,
make everything that is a direct dependency of any `std` crates private
by default. Note that this will be overridden, if the same crate is
public elsewhere in the crate graph then that overrides the private
default.

It may also be feasible to do this in the library crate, marking `std`'s
dependencies private via Cargo. However, given that the feature is still
rather unstable, doing this within the compiler seems more
straightforward.

Fixes: rust-lang#135232 [1]
tgross35 added a commit to tgross35/rust that referenced this issue Jan 14, 2025
Add an alternative to `tcx.all_traits()` that only shows traits that the
user might be able to use, for diagnostic purposes. With this available,
make use of it for diagnostics including associated type errors, which
is part of the problem with [1].

Includes a few comment updates for related API.

[1]: rust-lang#135232
tgross35 added a commit to tgross35/rust that referenced this issue Jan 14, 2025
In order to avoid diagnostics suggesting stdlib-private dependencies,
make everything that is a direct dependency of any `std` crates private
by default. Note that this will be overridden, if the same crate is
public elsewhere in the crate graph then that overrides the private
default.

It may also be feasible to do this in the library crate, marking `std`'s
dependencies private via Cargo. However, given that the feature is still
rather unstable, doing this within the compiler seems more
straightforward.

Fixes: rust-lang#135232 [1]
tgross35 added a commit to tgross35/rust that referenced this issue Jan 14, 2025
Add an alternative to `tcx.all_traits()` that only shows traits that the
user might be able to use, for diagnostic purposes. With this available,
make use of it for diagnostics including associated type errors, which
is part of the problem with [1].

Includes a few comment updates for related API.

[1]: rust-lang#135232
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 14, 2025
…arrowLii

Exclude dependencies of `std` for diagnostics

Currently crates in the sysroot can show up in diagnostic suggestions, such as in rust-lang#135232. To prevent this, duplicate `all_traits` into `visible_traits` which only shows traits in non-private crates.

Setting `#![feature(rustc_private)]` overrides this and makes items in private crates visible as well, since `rustc_private` enables use of `std`'s private dependencies.

This may be reviewed per-commit.

Fixes: rust-lang#135232
@bors bors closed this as completed in ed63539 Jan 14, 2025
github-actions bot pushed a commit to rust-lang/miri that referenced this issue Jan 15, 2025
Exclude dependencies of `std` for diagnostics

Currently crates in the sysroot can show up in diagnostic suggestions, such as in rust-lang/rust#135232. To prevent this, duplicate `all_traits` into `visible_traits` which only shows traits in non-private crates.

Setting `#![feature(rustc_private)]` overrides this and makes items in private crates visible as well, since `rustc_private` enables use of `std`'s private dependencies.

This may be reviewed per-commit.

Fixes: rust-lang/rust#135232
Kobzol pushed a commit to Kobzol/rustc-dev-guide that referenced this issue Jan 20, 2025
Exclude dependencies of `std` for diagnostics

Currently crates in the sysroot can show up in diagnostic suggestions, such as in rust-lang/rust#135232. To prevent this, duplicate `all_traits` into `visible_traits` which only shows traits in non-private crates.

Setting `#![feature(rustc_private)]` overrides this and makes items in private crates visible as well, since `rustc_private` enables use of `std`'s private dependencies.

This may be reviewed per-commit.

Fixes: rust-lang/rust#135232
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants