Skip to content

Do not use effective_visibilities query for Adt types of a local trait while proving a where-clause #145642

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

Merged
merged 1 commit into from
Aug 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2878,7 +2878,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
// we check if `TraitB` can be reachable from `S`
// to determine whether to note `TraitA` is sealed trait.
if let ty::Adt(adt, _) = ty.kind() {
let visibilities = tcx.effective_visibilities(());
let visibilities = &tcx.resolutions(()).effective_visibilities;
Copy link
Contributor

Choose a reason for hiding this comment

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

okay 🤔 what does the effective_visibilities query actually do with the effective_visibilities computed by by nameres? I generally feel like it should have some more comments somewhere.

cc @petrochenkov maybe?

Copy link
Member Author

@xizheyin xizheyin Aug 20, 2025

Choose a reason for hiding this comment

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

I noticed that in versions prior to #143431, &tcx.resolutions(()).effective_visibilities was used in this way. As for what exactly it does, I'm not quite sure. At the time, I used this API as suggested.🤔

https://github.com/rust-lang/rust/pull/143431/files#diff-c712eee556d0df70dfc5c8c66c34fa13fc94a30853854f632f5dd84acfd9c973L2778

Copy link
Contributor

Choose a reason for hiding this comment

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

okay 🤔 what does the effective_visibilities query actually do with the effective_visibilities computed by by nameres?

Actually calculates them.
Name resolutions can only fill the "directly public" and "exported" levels, based on modules, reexports and nominal visibility.
The query uses type information and calculates the remaining "reachable" levels too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Here we are only checking Level::Reexported so it's ok to use the incomplete effective visibilities from name resolver.

visibilities.effective_vis(local).is_none_or(|v| {
v.at_level(Level::Reexported)
.is_accessible_from(adt.did(), tcx)
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/trait-bounds/trait-bound-adt-issue-145611.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This test is for regression of issue #145611
// There should not be cycle error in effective_visibilities query.

trait LocalTrait {}
struct SomeType;
fn impls_trait<T: LocalTrait>() {}
fn foo() -> impl Sized {
impls_trait::<SomeType>(); //~ ERROR the trait bound `SomeType: LocalTrait` is not satisfied [E0277]
}

fn main() {}
20 changes: 20 additions & 0 deletions tests/ui/trait-bounds/trait-bound-adt-issue-145611.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error[E0277]: the trait bound `SomeType: LocalTrait` is not satisfied
--> $DIR/trait-bound-adt-issue-145611.rs:8:19
|
LL | impls_trait::<SomeType>();
| ^^^^^^^^ the trait `LocalTrait` is not implemented for `SomeType`
|
help: this trait has no implementations, consider adding one
--> $DIR/trait-bound-adt-issue-145611.rs:4:1
|
LL | trait LocalTrait {}
| ^^^^^^^^^^^^^^^^
note: required by a bound in `impls_trait`
--> $DIR/trait-bound-adt-issue-145611.rs:6:19
|
LL | fn impls_trait<T: LocalTrait>() {}
| ^^^^^^^^^^ required by this bound in `impls_trait`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
Loading