rust-lang / rust-lang/rust

Treat cycle errors as bug by default

Open
#145,611 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics C-bug C-cleanup I-cycle T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

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.

https://github.com/rust-lang/rust/blob/8c32e313cccf7df531e2d49ffb8227bb92304aee/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs#L2871-L2889

This query fetches the hidden type of all opaque types https://github.com/rust-lang/rust/blob/8c32e313cccf7df531e2d49ffb8227bb92304aee/compiler/rustc_privacy/src/lib.rs#L1841-L1846

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the query-cycle handling for crate_variances and effective_visibilities, then inspect the cycle path in compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs and hidden-type lookup in compiler/rustc_privacy/src/lib.rs. Use the reproducer in the issue to verify that unexpected cycles become ICEs while expected cycles retain appropriate handling.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.