- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
Description
We should treat unexpected query cycles as bugs, not user-facing errors. Ideally we'd add an attribute to queries to indicate that they are expected to cycle and otherwise ICE on cycles.
Affected queries are  crate_variances or effective_visibilities.
original issue:
when fetching effective_visibilities for Adt self types of a local trait while proving a where-clause.
rust/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs
Lines 2871 to 2889 in 8c32e31
| if let ty::PredicateKind::Clause(clause) = predicate.kind().skip_binder() { | |
| match clause { | |
| ty::ClauseKind::Trait(trait_pred) => { | |
| let def_id = trait_pred.def_id(); | |
| let visible_item = if let Some(local) = def_id.as_local() { | |
| let ty = trait_pred.self_ty(); | |
| // when `TraitA: TraitB` and `S` only impl TraitA, | |
| // 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(()); | |
| visibilities.effective_vis(local).is_none_or(|v| { | |
| v.at_level(Level::Reexported) | |
| .is_accessible_from(adt.did(), tcx) | |
| }) | |
| } else { | |
| // FIXME(xizheyin): if the type is not ADT, we should not suggest it | |
| true | |
| } | 
This query fetches the hidden type of all opaque types
rust/compiler/rustc_privacy/src/lib.rs
Lines 1841 to 1846 in 8c32e31
| let pub_ev = EffectiveVisibility::from_vis(ty::Visibility::Public); | |
| visitor | |
| .reach_through_impl_trait(opaque.def_id, pub_ev) | |
| .generics() | |
| .predicates() | |
| .ty(); | 
introduced in #143431, cc @xizheyin @compiler-errors
trait LocalTrait {}
struct SomeType;
fn impls_trait<T: LocalTrait>() {}
fn foo() -> impl Sized {
    impls_trait::<SomeType>();
}results in
error[E0391]: cycle detected when computing type of `foo::{opaque#0}`
 --> src/lib.rs:4:13
  |
4 | fn foo() -> impl Sized {
  |             ^^^^^^^^^^
  |
note: ...which requires computing type of opaque `foo::{opaque#0}`...
 --> src/lib.rs:4:13
  |
4 | fn foo() -> impl Sized {
  |             ^^^^^^^^^^
note: ...which requires borrow-checking `foo`...
 --> src/lib.rs:4:1
  |
4 | fn foo() -> impl Sized {
  | ^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires promoting constants in MIR for `foo`...
 --> src/lib.rs:4:1
  |
4 | fn foo() -> impl Sized {
  | ^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires checking if `foo` contains FFI-unwind calls...
 --> src/lib.rs:4:1
  |
4 | fn foo() -> impl Sized {
  | ^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires building MIR for `foo`...
 --> src/lib.rs:4:1
  |
4 | fn foo() -> impl Sized {
  | ^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires match-checking `foo`...
 --> src/lib.rs:4:1
  |
4 | fn foo() -> impl Sized {
  | ^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires type-checking `foo`...
 --> src/lib.rs:4:1
  |
4 | fn foo() -> impl Sized {
  | ^^^^^^^^^^^^^^^^^^^^^^
  = note: ...which requires checking effective visibilities...
  = note: ...which again requires computing type of `foo::{opaque#0}`, completing the cycle
note: cycle used when checking that `foo::{opaque#0}` is well-formed
 --> src/lib.rs:4:13
  |
4 | fn foo() -> impl Sized {
  |             ^^^^^^^^^^
  = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
error[E0277]: the trait bound `SomeType: LocalTrait` is not satisfied
 --> src/lib.rs:5:19
  |
5 |     impls_trait::<SomeType>();
  |                   ^^^^^^^^ the trait `LocalTrait` is not implemented for `SomeType`
  |
help: this trait has no implementations, consider adding one
 --> src/lib.rs:1:1
  |
1 | trait LocalTrait {}
  | ^^^^^^^^^^^^^^^^
note: required by a bound in `impls_trait`
 --> src/lib.rs:3:19
  |
3 | fn impls_trait<T: LocalTrait>() {}
  |                   ^^^^^^^^^^ required by this bound in `impls_trait`
note the query cycle before the actually desired error