Skip to content

Compiler error doesn't show full query chain when evaluating const cycle #66332

Open
@davidhewitt

Description

@davidhewitt

Consider the following code (both on stable 1.39.0 and nightly)

trait Const {
    const A: i32 = Self::B;
    const B: i32 = Self::A;
    const C: i32 = Self::A;
}

impl <T> Const for T {}

pub fn main() -> () {
    dbg!(i32::C);
}

There's a cycle evaluating the constants C -> A -> B -> A -> ...

The dbg!(i32::C) line is the offending code which causes this cycle to be evaluated.

However, the emitted error message doesn't actually reference the usage dbg!(i32::C) which caused the evaluation, as below:

   Compiling playground v0.0.1 (/playground)
error[E0391]: cycle detected when const-evaluating `Const::A`
 --> src/main.rs:4:24
  |
4 |         const A: i32 = Self::B;
  |                        ^^^^^^^
  |
note: ...which requires const-evaluating `Const::B`...
 --> src/main.rs:5:24
  |
5 |         const B: i32 = Self::A;
  |                        ^^^^^^^
  = note: ...which again requires const-evaluating `Const::A`, completing the cycle
note: cycle used when const-evaluating `Const::C`
 --> src/main.rs:6:24
  |
6 |         const C: i32 = Self::A;
  |                        ^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0391`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

This error could probably be improved by having an additional note: showing that the cycle was used when evaluating dbg!(i32::C).

cc @oli-obk

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions