Skip to content

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

Open

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

Activity

  1. added
    C-bugCategory: This is a bug.
    regression-untriagedUntriaged performance or correctness regression.
    on Aug 22, 2026
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Aug 22, 2026
  3. dimpolo commented on Aug 23, 2026

    @dimpolo
    ContributorAuthor

    Today's nightly 2026-08-22 now produces the same error with -Znext-solver=coherence as well.

    So this just became a normal nightly-to-nightly regression irrespective of trait solver used.

    My LLM suggests this could be because of #154992

  4. added
    requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.
    and removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Aug 23, 2026
  5. lcnr commented on Aug 23, 2026

    @lcnr
    Contributor

    Thanks for opening this issue. Would be worth to get a better sense of why this errors. It doesn't feel too surprising to me that having a receiver reference the current trait is causing issues 🤔

  6. added
    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)
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Aug 23, 2026
  7. saberoueslati commented on Aug 29, 2026

    @saberoueslati
    Contributor

    @rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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