Skip to content

cycle detected when checking if trait is dyn-compatible with next-gen trait solver and arbitrary_self_types #161536

Description

@dimpolo

I have some weird code I was using as a workaround for #17893 (comment) but it stopped compiling on the latest nightly. Using -Znext-solver=coherence makes it compile again so it's presumably because of #160895.
I tried to minimize it below:

Code

I tried this code:

#![feature(arbitrary_self_types, derive_coerce_pointee)]
#![allow(dead_code)]
use core::marker::{CoercePointee, PhantomData};
use std::ops::Deref;

#[derive(CoercePointee)]
#[repr(transparent)]
struct Ref<#[pointee] T: ?Sized, D: ?Sized + GenericTrait<D>> {
    ptr: Box<T>,
    phantom: PhantomData<D>,
}

impl<T: ?Sized, D: ?Sized + GenericTrait<D>> Deref for Ref<T, D> {
    type Target = T;
    fn deref(&self) -> &T {
        &self.ptr
    }
}

trait GenericTrait<D: ?Sized + GenericTrait<D>> {}

trait MyTrait {
    fn causes_cycle(self: Ref<Self, dyn MyTrait>);
}

impl GenericTrait<dyn MyTrait> for dyn MyTrait {}

fn main() {}

I expected to see this happen: compiles, as it does with -Znext-solver=coherence

Instead, this happened:

error[E0391]: cycle detected when checking if trait `MyTrait` is dyn-compatible                                                                    
  --> src\main.rs:22:1
   |
22 | trait MyTrait {
   | ^^^^^^^^^^^^^
   |
   = note: ...which requires determining dyn-compatibility of trait `MyTrait`...
   = note: ...which again requires checking if trait `MyTrait` is dyn-compatible, completing the cycle
note: cycle used when coherence checking all impls of trait `GenericTrait`
  --> src\main.rs:20:1
   |
20 | trait GenericTrait<D: ?Sized + GenericTrait<D>> {}
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: for more information, see <https://rustc-dev-guide.rust-lang.org/overview.html#queries> and <https://rustc-dev-guide.rust-lang.org/query.html>

Version it worked on

Same as below but with -Znext-solver=coherence

Version with regression

rustc 1.100.0-nightly (c656540d6 2026-08-21)
binary: rustc
commit-hash: c656540d6467dee1381f0cbd882412d6bd1cd5ae
commit-date: 2026-08-21
host: x86_64-pc-windows-msvc
release: 1.100.0-nightly
LLVM version: 23.1.0

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.F-arbitrary_self_types`#![feature(arbitrary_self_types)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.WG-trait-system-refactorThe Rustc Trait System Refactor Initiative (-Znext-solver)regression-untriagedUntriaged performance or correctness regression.requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions