Skip to content

Error in trait resolution on traits with associated types with generic lifetimes #113818

Closed
@zommiommy

Description

@zommiommy

Hi! I think I've found an error in the type system.

I think the following code should compile (Link to rust playground):

pub trait Base {
    type Item<'a> where Self: 'a;
}

pub trait ClonableBase: Base
where
    for<'a> Self::Item<'a>: Clone
{}
impl<T: Base> ClonableBase for T
where
    for<'a> Self::Item<'a>: Clone
{}

fn test<G: ClonableBase>(g: G) 
where
    for<'a> G::Item<'a>: Clone
{
    todo!()
}

pub fn main() {
    struct Data;
    
    impl Base for Data {
        type Item<'a> = usize where Self: 'a;
    }
    
    test(Data)
}

I expected this to compile as usize does implement Clone so ClonableBase is satisfied

Instead, rust refuse to compile as it can't infer that Item is usize which implements Clone.

Compiling playground v0.0.1 (/playground)
error[[E0277]](https://doc.rust-lang.org/stable/error_codes/E0277.html): the trait bound `for<'a> <_ as Base>::Item<'a>: Clone` is not satisfied
  --> src/main.rs:29:10
   |
29 |     test(Data)
   |     ---- ^^^^ the trait `for<'a> Clone` is not implemented for `<_ as Base>::Item<'a>`
   |     |
   |     required by a bound introduced by this call
   |
note: required by a bound in `test`
  --> src/main.rs:17:26
   |
15 | fn test<G: ClonableBase>(g: G) 
   |    ---- required by a bound in this function
16 | where
17 |     for<'a> G::Item<'a>: Clone
   |                          ^^^^^ required by this bound in `test`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (bin "playground") due to previous error

Also, weirdly, test requires:

where
    for<'a> G::Item<'a>: Clone

As it is a constrait already specified by ClonableBase

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-trait-systemArea: Trait systemA-type-systemArea: Type systemC-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