Skip to content

Mismatched types issue with GATs #87429

Closed
@cynecx

Description

@cynecx

I kind of expected this to work:

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=95574c86f67ea4cd5b7c3509a9549f6d

#![feature(generic_associated_types)]

use std::fmt::Debug;

trait Family {
    type Member<'a>:
        'a +
        Debug +
        for<'b> PartialEq<Self::Member<'b>>;
    
}

struct I32;

impl Family for I32 {
    type Member<'a> = i32;
}

struct StrRef;

impl Family for StrRef {
    type Member<'a> = &'a str;
}

fn check<'a, 'b, F: Family>(a: F::Member<'a>, b: F::Member<'b>) {
    assert_eq!(a, b);
}

fn main() {
    check::<I32>(0, 0);
}

However it fails with:

error[E0308]: mismatched types
  --> src/main.rs:17:5
   |
17 |     type Member<'a> = i32;
   |     ^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
   |
   = note: expected associated type `<I32 as Family>::Member<'a>`
              found associated type `<I32 as Family>::Member<'b>`

error[E0308]: mismatched types
  --> src/main.rs:23:5
   |
23 |     type Member<'a> = &'a str;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
   |
   = note: expected associated type `<StrRef as Family>::Member<'a>`
              found associated type `<StrRef as Family>::Member<'b>`

For more information about this error, try `rustc --explain E0308`.

cc @jackh726

EDIT: I am not 100% sure but this may has to do with the variance of the gat.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-GATsArea: Generic associated types (GATs)F-generic_associated_types`#![feature(generic_associated_types)]` a.k.a. GATsT-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