rust-lang / rust-lang/rust

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

Open
#161,536 3 comments 0 reactions 1 assignee View on GitHub

@saberoueslati is already working on this.

Since Aug 29, 2026.

C-bug F-arbitrary_self_types regression-untriaged requires-nightly T-compiler T-types WG-trait-system-refactor
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

I have some weird code I was using as a workaround for https://github.com/rust-lang/rust/issues/17893#issuecomment-1022582713 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

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.