Skip to content

Trait implementation implies trait bounds for associated types but not for GAT associated types #111158

Open
@inanna-malick

Description

@inanna-malick

As always, I want to thank you for implementing GATs and to preemptively apologize for using them to implement Haskell Brain nonsense. With that out of the way, I tried this code:

trait Requirement {
}

trait Super 
where Self::Assoc: Requirement
{
    type Assoc;
}

trait Sub: Super 
  where <Self as Super>::Assoc: Requirement {
    
}

// 'Super' implies Assoc: Requirement, compiles
fn compiles1<X: Super>(x: X) {}
// 'Sub' implies Assoc: Requirement, compiles
fn compiles2<X: Sub>(x: X) {}


enum Token{}


trait SuperGAT
where Self::Assoc<Token>: Requirement
{
    type Assoc<X>;
}

trait SubGAT: SuperGAT
  where <Self as SuperGAT>::Assoc<Token>: Requirement {
    
}

// 'SuperGAT' should imply Assoc<Token>: Requirement, but it doesn't compile:
// error[E0277]: the trait bound `<X as SuperGAT>::Assoc<Token>: Requirement` is not satisfied
fn doesnt_compile_gat_1<X: SuperGAT>(x: X) {}
// 'SubGAT' should imply Assoc<Token>: Requirement, but it doesn't compile:
// error[E0277]: the trait bound `<X as SuperGAT>::Assoc<Token>: Requirement` is not satisfied
fn doesnt_compile_gat_2<X: SubGAT>(x: X) {}

I expected to see SuperGAT and SubGAT imply that the associated type implements Requirement as with Super and Sub.

Instead, I get E027 for both cases noting that that requirement is not satisfied. This occurs on both stable and nightly.

Meta

stable: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=721bd4fe341ce41dc74f72700fe2bacf
nightly: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=c1b755aa1af7f2fc6b0f26e465a91a9f

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-GATsArea: Generic associated types (GATs)A-implied-boundsArea: Implied bounds / inferred outlives-boundsC-bugCategory: This is a bug.T-typesRelevant to the types 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