Skip to content

Trait aliases considers same type as not equal to itself if used in impl Trait return type #57612

Closed

Description

@matprec

Example:

#![feature(trait_alias)]
#![feature(existential_type)]

trait Foo {
    type Bar: Baz<Self, Self>;

    fn bar(&self) -> Self::Bar;
}

struct X;

impl Foo for X {
    existential type Bar: Baz<Self, Self>;

    fn bar(&self) -> Self::Bar {
        |x: &X| x
    }
}

trait Baz<A, B> = Fn(&A) -> &B;

Workaround: Replace existential type Bar: Baz<Self, Self>; with existential type Bar: Fn(&X) -> &X;
Possibly related to #57611
Playground

Activity

  1. self-assigned this
    on Jan 25, 2019
  2. oli-obk commented on Mar 18, 2019

    @oli-obk
    Contributor

    Reduced example

    #![feature(trait_alias)]
    
    struct X;
    
    fn bar() -> impl Baz<X, X> {
        |x: &X| x
    }
    
    trait Baz<A, B> = Fn(&A) -> &B;
  3. removed their assignment
    on Mar 18, 2019
  4. changed the title [-]Trait aliases in existential types considers same type as more general[/-] [+]Trait aliases considers same type as not equal to itself if used in `impl Trait` return type[/+] on Feb 18, 2021
  5. added
    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.
    on Feb 18, 2021
  6. plazmoid commented on Aug 31, 2022

    @plazmoid

    Not a bug anymore, Oli's example compiles now (with fixing some lifetime issues):

    #![feature(trait_alias)]
    #![feature(closure_lifetime_binder)]
    
    struct X;
    
    fn bar() -> impl Baz<X, X> {
        for<'a> |x: &'a X| -> &'a X { x }
    }
    
    trait Baz<A, B> = Fn(&A) -> &B;

    Interestingly that moving HRTB into trait alias doesn't solve the lifetimes issue

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-trait-systemArea: Trait systemA-type-systemArea: Type systemF-trait_alias`#![feature(trait_alias)]`

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions