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

Exclude dependencies of std for diagnostics #135278

Merged
merged 5 commits into from
Jan 14, 2025

Conversation

tgross35
Copy link
Contributor

@tgross35 tgross35 commented Jan 9, 2025

Currently crates in the sysroot can show up in diagnostic suggestions, such as in #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: #135232

@rustbot
Copy link
Collaborator

rustbot commented Jan 9, 2025

r? @SparrowLii

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

@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 Jan 9, 2025
@tgross35
Copy link
Contributor Author

tgross35 commented Jan 9, 2025

Unsure whether adding a query is okay or if this should just be a filtering method on tcx. I am still figuring out a good way to test this so

@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 Jan 9, 2025
@tgross35
Copy link
Contributor Author

tgross35 commented Jan 9, 2025

Another option is to drop stdlib_private_dep and just use private_dep in the query. I didn't do this originally because I thought private_dep was more tightly coupled to the public_private_dependencies feature, but maybe this isn't the case.

Open to suggestions here.

@tgross35 tgross35 force-pushed the ignore-std-dep-crates branch 3 times, most recently from 6a3e3c6 to a04ed09 Compare January 13, 2025 02:45
@tgross35
Copy link
Contributor Author

Okay, I think I have something workable here. I dropped stdlib_private_dep in favor of reusing is_private_dep, and added a repro.

@rustbot ready

@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 Jan 13, 2025
Copy link
Member

@SparrowLii SparrowLii left a comment

Choose a reason for hiding this comment

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

This change makes sense, but I'm not sure it is the best for diagnostics' design.
@compiler-errors Could you help have a review?

pub fn all_traits(self) -> impl Iterator<Item = DefId> + 'tcx {
iter::once(LOCAL_CRATE)
.chain(self.crates(()).iter().copied())
.chain(self.visible_crates(()).iter().copied())
Copy link
Member

Choose a reason for hiding this comment

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

Do we have to change this? It makes sense to analyze trait candidates from private dependencies IMO, we just need to exclude it from diagnostics

And if we not change it here, we can avoid to add a new query since visible_crates only be used in the query all_diagnostic_items

Copy link
Member

Choose a reason for hiding this comment

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

yeah, this seems wrong. all_traits is also used by things like SMIR and rustdoc in ways that are not just diagnostics. if you want to distinguish "all visible traits" from "all traits", and then use the former in diagnostics, then it should be more explicit.

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 wasn't sure about this one, but yeah that makes sense. I guess there are a couple questions - should the query be kept or should it just filter as-needed? My original thought was that there could be other existing or new places where crates is used but visible_crates would be more correct, but maybe this isn't very true.

Then either way, should all_traits be split into visible_traits, or is filtering in all_diagnostic_items sufficient to cover this case? (I didn't realize that would affect the output here, so didn't actually test that on its own)

Copy link
Member

Choose a reason for hiding this comment

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

I think the filtering should be done as needed. if we expose visible_traits as just a function, it doesn't matter if the filtering is expensive b/c we only call it on the diagnostic codepath (i.e. failed compilation).

Copy link
Member

Choose a reason for hiding this comment

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

Then either way, should all_traits be split into visible_traits, or is filtering in all_diagnostic_items sufficient to cover this case? (I didn't realize that would affect the output here, so didn't actually test that on its own)

probably worth just testing yourself, idk off the top of my head lol

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just tested, seems like all_diagnostic_items doesn't have any effect on the relevant output here so it has to filter somewhere else too.

So I got rid of the query and split all_traits into all_traits and visible_traits that does the filtering on the fly. I also replaced all_traits in a few other places that deal with errors that seem reasonable (?), still waiting for my tests to finish and see if that affects much.

@compiler-errors
Copy link
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jan 13, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request 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
@bors
Copy link
Contributor

bors commented Jan 13, 2025

⌛ Trying commit a04ed09 with merge dbe10d0...

@tgross35 tgross35 force-pushed the ignore-std-dep-crates branch from a04ed09 to 871d75a Compare January 13, 2025 05:00
@rustbot
Copy link
Collaborator

rustbot commented Jan 13, 2025

HIR ty lowering was modified

cc @fmease

@tgross35 tgross35 force-pushed the ignore-std-dep-crates branch 2 times, most recently from 35ad309 to c0eb4af Compare January 13, 2025 05:26
@rust-log-analyzer

This comment has been minimized.

@tgross35
Copy link
Contributor Author

(^ spurious CI failure)

@SparrowLii
Copy link
Member

@bors try @rust-timer queue

@bors
Copy link
Contributor

bors commented Jan 14, 2025

📌 Commit ed63539 has been approved by SparrowLii

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 Jan 14, 2025
@bors
Copy link
Contributor

bors commented Jan 14, 2025

⌛ Testing commit ed63539 with merge 8c39ce5...

@bjorn3
Copy link
Member

bjorn3 commented Jan 14, 2025

Alright, figured it out. addr2line, which I was using for my tests, was apparently already a private dependency. I am not sure why this is (possibly related to it being optional in std?)

#111076 marked all dependencies of libstd as private except for those that are actually part of the public interface of libstd to fix a similar diagnostic issue. This wasn't done for liballoc and other sysroot crates however. Should probably be fixed.

@@ -876,6 +876,11 @@ impl<'tcx> TyCtxt<'tcx> {
/// [public]: TyCtxt::is_private_dep
/// [direct]: rustc_session::cstore::ExternCrate::is_direct
pub fn is_user_visible_dep(self, key: CrateNum) -> bool {
// `#![rustc_private]` overrides defaults to make private dependencies usable.
if self.features().enabled(sym::rustc_private) {
Copy link
Member

Choose a reason for hiding this comment

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

However, this is a very internal feature, so it doesn't seem worth doing any additional filtering here.

Not really. There are a lot of external custom drivers that need to enable this feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you suggesting that it is worth filtering here to only expose crates that are dependencies of std? Or just that the message is inaccurate.

Copy link
Member

Choose a reason for hiding this comment

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

I think the override for rustc_private should be removed. Even with rustc_private enabled you generally shouldn't do something like extern crate libc; from the sysroot.

@bors
Copy link
Contributor

bors commented Jan 14, 2025

☀️ Test successful - checks-actions
Approved by: SparrowLii
Pushing 8c39ce5 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jan 14, 2025
@bors bors merged commit 8c39ce5 into rust-lang:master Jan 14, 2025
7 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Jan 14, 2025
@tgross35
Copy link
Contributor Author

#111076 marked all dependencies of libstd as private except for those that are actually part of the public interface of libstd to fix a similar diagnostic issue. This wasn't done for liballoc and other sysroot crates however. Should probably be fixed.

I was not aware of this history but that makes sense, I will update the others. In that case, do you think it makes sense to also revert ed63539? Assuming yes, since it would no longer be needed.

@tgross35 tgross35 deleted the ignore-std-dep-crates branch January 14, 2025 18:36
@bjorn3
Copy link
Member

bjorn3 commented Jan 14, 2025

If the Cargo.toml changes are enough, then yes. It would make sense to me to revert that commit.

@tgross35
Copy link
Contributor Author

Opened #135501 as an experiment, but I have been unsuccessful keeping the test fixed just by setting public = false.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (8c39ce5): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results (primary -1.6%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.6% [-3.1%, -0.8%] 4
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -1.6% [-3.1%, -0.8%] 4

Cycles

Results (primary 2.6%, secondary -3.4%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
2.6% [2.3%, 3.0%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.4% [-4.6%, -2.6%] 6
All ❌✅ (primary) 2.6% [2.3%, 3.0%] 2

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 763.379s -> 762.502s (-0.11%)
Artifact size: 326.08 MiB -> 326.12 MiB (0.01%)

tgross35 added a commit to tgross35/compiler-builtins that referenced this pull request Jan 15, 2025
Replace `public_test_dep!` by placing optionally public items into new
modules, then controlling what is exported with the `public-test-deps`
feature.

This is nicer for automatic formatting and diagnostics.

This is a reland of 2e2a925 ("Eliminate the use of
`public_test_dep!`"), which was reverted in 47e50fd ('Revert "Eliminate
the use of..."') due to a bug exposed at [1]. This was fixed in [2], so
the cleanup should be able to be applied again.

[1]: rust-lang/rust#128691
[1]: rust-lang/rust#135278
tgross35 added a commit to tgross35/compiler-builtins that referenced this pull request Jan 15, 2025
Replace `public_test_dep!` by placing optionally public items into new
modules, then controlling what is exported with the `public-test-deps`
feature.

This is nicer for automatic formatting and diagnostics.

This is a reland of 2e2a925 ("Eliminate the use of
`public_test_dep!`"), which was reverted in 47e50fd ('Revert "Eliminate
the use of..."') due to a bug exposed at [1]. This was fixed in [2], so
the cleanup should be able to be applied again.

[1]: rust-lang/rust#128691
[2]: rust-lang/rust#135278
tgross35 added a commit to tgross35/compiler-builtins that referenced this pull request Jan 15, 2025
Replace `public_test_dep!` by placing optionally public items into new
modules, then controlling what is exported with the `public-test-deps`
feature.

This is nicer for automatic formatting and diagnostics.

This is a reland of 2e2a925 ("Eliminate the use of
`public_test_dep!`"), which was reverted in 47e50fd ('Revert "Eliminate
the use of..."') due to a bug exposed at [1]. This was fixed in [2], so
the cleanup should be able to be applied again.

[1]: rust-lang/rust#128691
[2]: rust-lang/rust#135278
tgross35 added a commit to rust-lang/compiler-builtins that referenced this pull request Jan 15, 2025
Replace `public_test_dep!` by placing optionally public items into new
modules, then controlling what is exported with the `public-test-deps`
feature.

This is nicer for automatic formatting and diagnostics.

This is a reland of 2e2a925 ("Eliminate the use of
`public_test_dep!`"), which was reverted in 47e50fd ('Revert "Eliminate
the use of..."') due to a bug exposed at [1]. This was fixed in [2], so
the cleanup should be able to be applied again.

[1]: rust-lang/rust#128691
[2]: rust-lang/rust#135278
tgross35 added a commit to tgross35/rust that referenced this pull request Jan 16, 2025
The change between 0.1.143 and 0.1.144 includes refactoring that was in
compiler-builtins before, but had to be reverted before landing in
rust-lang/rust because the traits were leaking into diagnostics [1].
Recently a fix for this issue was merged [2] so the cleanup is reapplied
here.

This also acts as a regression test for [2].

[1]: rust-lang#128691 (comment)
[2]: rust-lang#135278
jhpratt added a commit to jhpratt/rust that referenced this pull request Jan 16, 2025
Update `compiler-builtins` to 0.1.144

The change between 0.1.143 and 0.1.144 includes refactoring that was in compiler-builtins before, but had to be reverted before landing in rust-lang/rust because the traits were leaking into diagnostics [1]. Recently a fix for this issue was merged [2] so the cleanup is reapplied here.

This also acts as a regression test for [2].

[1]: rust-lang#128691 (comment)
[2]: rust-lang#135278
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 16, 2025
Update `compiler-builtins` to 0.1.144

The change between 0.1.143 and 0.1.144 includes refactoring that was in compiler-builtins before, but had to be reverted before landing in rust-lang/rust because the traits were leaking into diagnostics [1]. Recently a fix for this issue was merged [2] so the cleanup is reapplied here.

This also acts as a regression test for [2].

[1]: rust-lang#128691 (comment)
[2]: rust-lang#135278
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 16, 2025
Update `compiler-builtins` to 0.1.144

The change between 0.1.143 and 0.1.144 includes refactoring that was in compiler-builtins before, but had to be reverted before landing in rust-lang/rust because the traits were leaking into diagnostics [1]. Recently a fix for this issue was merged [2] so the cleanup is reapplied here.

This also acts as a regression test for [2].

[1]: rust-lang#128691 (comment)
[2]: rust-lang#135278
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 16, 2025
Update `compiler-builtins` to 0.1.144

The change between 0.1.143 and 0.1.144 includes refactoring that was in compiler-builtins before, but had to be reverted before landing in rust-lang/rust because the traits were leaking into diagnostics [1]. Recently a fix for this issue was merged [2] so the cleanup is reapplied here.

This also acts as a regression test for [2].

[1]: rust-lang#128691 (comment)
[2]: rust-lang#135278
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 16, 2025
Update `compiler-builtins` to 0.1.144

The change between 0.1.143 and 0.1.144 includes refactoring that was in compiler-builtins before, but had to be reverted before landing in rust-lang/rust because the traits were leaking into diagnostics [1]. Recently a fix for this issue was merged [2] so the cleanup is reapplied here.

This also acts as a regression test for [2].

[1]: rust-lang#128691 (comment)
[2]: rust-lang#135278
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 16, 2025
Update `compiler-builtins` to 0.1.144

The change between 0.1.143 and 0.1.144 includes refactoring that was in compiler-builtins before, but had to be reverted before landing in rust-lang/rust because the traits were leaking into diagnostics [1]. Recently a fix for this issue was merged [2] so the cleanup is reapplied here.

This also acts as a regression test for [2].

[1]: rust-lang#128691 (comment)
[2]: rust-lang#135278

try-job: test-various
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Diagnostics sometimes suggest items from rustc_private crates
8 participants