Skip to content

Treat cycle errors as bug by default #145611

@lcnr

Description

@lcnr

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.

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.C-cleanupCategory: PRs that clean code up or issues documenting cleanup.I-cycleIssue: A query cycle occurred while none was expectedT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions