Skip to content

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

Closed

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.

Activity

  1. added
    A-lifetimes
    A-trait-system
    on Apr 12, 2015
  2. pnkfelix commented on Apr 16, 2015

    @pnkfelix
    Contributor

    Probably "just-a-bug"; P-low, not 1.0.

  3. added
    P-low
    and removed on Apr 16, 2015
  4. eefriedman commented on Aug 9, 2015

    @eefriedman
    Contributor

    Testcase without for loop desugaring:

    trait DictLike<'a> {
        type ItemsIterator: Iterator<Item=u8>;
        fn get(mut c: Self::ItemsIterator) {
            c = c.into_iter();
        }
    }
    fn main() {}
  5. mitchmindtree commented on Sep 14, 2015

    @mitchmindtree
    Contributor

    Just thought I'd mention I ran into this bug just now while writing a trait with some associated iterator type - frustrating errors, but managed to side-step it with @huonw 's workaround here.

  6. malbarbo commented on Jun 23, 2016

    @malbarbo
    Contributor

    The test cases in this issue compile fine since rustc 1.4 (Playground for rustc stable).

  7. malbarbo commented on Sep 30, 2016

    @malbarbo
    Contributor

    Please add E-needstest tag so one can write a regression test an the issue be closed.

  8. added
    E-needs-test
    on Sep 30, 2016
  9. added
    A-inference
    C-bug
    on Jul 22, 2017
  10. added a commit that references this issue on Oct 4, 2018
  11. added a commit that references this issue on Oct 7, 2018
    987a50b
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-inferenceA-lifetimesA-trait-systemC-bugE-needs-testP-low

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions