Skip to content

Trait generic over lifetime causes uses of associated types to fail to infer properly inside default methods #24338

Closed
@huonw

Description

@huonw
trait DictLike<'a> {
    type ItemsIterator: Iterator<Item=u8>;

    fn items(&self) -> Self::ItemsIterator;

    fn get(&self)  {
        for k in self.items() {} 
        // get(self)
    }
}

// fn get<'a, X: ?Sized + DictLike<'a>>(x: &X) { for k in x.items() {} }

fn main() {}
<anon>:7:13: 7:14 error: unable to infer enough type information about `_`; type annotations required [E0282]
<anon>:7         for k in self.items() {} 
                     ^

Strangely, switching to the commented out form works fine, i.e. just moving the body of the code out of the default method.

Nominating since I am unsure how important we regard inference issues like this to be.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceA-lifetimesArea: Lifetimes / regionsA-trait-systemArea: Trait systemC-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.P-lowLow priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions