Skip to content

Cycle detected when computing whether impls specialize one another, across crate boundaries #90817

Open
@patrick-gu

Description

@patrick-gu

I tried this code (note: the crate name is bug, and this only occurs when the files are separate):

In src/lib.rs:

pub trait Reducible {
    type Reduced;
}

pub struct Base;

pub struct Higher;

impl Reducible for Higher {
    type Reduced = Base;
}

pub trait Reduce<R> {}

impl<T> Reduce<Base> for T {}

impl<T, R> Reduce<R> for T
where
    R: Reducible,
    T: Reduce<R::Reduced>,
{
}

pub fn reduce<R>(_: R)
where
    (): Reduce<R>,
{
}

In src/main.rs:

fn main() {
    bug::reduce(bug::Higher);
}

I expected to see this happen: Code compiles successfully, as it would if everything was in main.rs.

Instead, this happened: the compiler emits an error:

error[E0391]: cycle detected when computing whether impls specialize one another
  --> C:\path\to\bug\src\lib.rs:17:1
   |
17 | / impl<T, R> Reduce<R> for T
18 | | where
19 | |     R: Reducible,
20 | |     T: Reduce<R::Reduced>,
21 | | {
22 | | }
   | |_^
   |
   = note: ...which immediately requires computing whether impls specialize one another again
note: cycle used when type-checking `main`
  --> src\main.rs:1:1
   |
1  | fn main() {
   | ^^^^^^^^^

For more information about this error, try `rustc --explain E0391`.
error: could not compile `bug` due to previous error

Meta

rustc --version --verbose:

rustc 1.56.0 (09c42c458 2021-10-18)
binary: rustc
commit-hash: 09c42c45858d5f3aedfa670698275303a3d19afa
commit-date: 2021-10-18
host: x86_64-pc-windows-msvc
release: 1.56.0
LLVM version: 13.0.0

This also appears to be present on 1.58.0-nightly 2021-11-10, commit hash 82af160.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-specializationArea: Trait impl specializationA-trait-systemArea: Trait systemC-bugCategory: This is a bug.I-cycleIssue: A query cycle occurred while none was expectedP-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions