Skip to content

Trait bound order-dependent type errors using non_lifetime_binders #126506

Open
@CraftSpider

Description

@CraftSpider

I tried this code:

#![feature(non_lifetime_binders)]

pub trait Trait1 {
    type Target;
    type This<T>: for<U> Trait1<This<U> = Self::This<U>> + Trait1<This<Self::Target> = Self>;
    
    fn cast<T>(self) -> Self::This<T>;
}

pub trait Trait2 {
    type Target;
    type This<T>: Trait2<This<Self::Target> = Self> + for<U> Trait2<This<U> = Self::This<U>>;
    
    fn cast<T>(self) -> Self::This<T>;
}

fn use1<T: Trait1>(val: T) -> T {
    val.cast::<()>().cast::<T::Target>()
}

fn use2<T: Trait2>(val: T) -> T {
    val.cast::<()>().cast::<T::Target>()
}

playground

I expected to see this happen: Both versions of the trait to behave the same

Instead, this happened: One version works as expected, succeeding, but the other has a Mismatched Type error

Other details: When using -Znext-solver, both code blocks error out with the same message:

error[E0284]: type annotations needed: cannot satisfy `<<T as Trait2>::This<()> as Trait2>::This<<T as Trait2>::Target> == T`
  --> src\lib.rs:22:5
   |
22 |     val.cast::<()>().cast::<T::Target>()
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot satisfy `<<T as Trait2>::This<()> as Trait2>::This<<T as Trait2>::Target> == T`

Meta

rustc --version --verbose:

rustc 1.80.0-nightly (98489f248 2024-06-06)
binary: rustc
commit-hash: 98489f2487465f3765e5dd28d7305ebfd40f0865
commit-date: 2024-06-06
host: x86_64-pc-windows-msvc
release: 1.80.0-nightly
LLVM version: 18.1.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-trait-systemArea: Trait systemC-bugCategory: This is a bug.F-non_lifetime_binders`#![feature(non_lifetime_binders)]`S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-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