Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
huonw opened this issue Apr 12, 2015 · 5 comments
Labels
A-inference Area: Type inference A-lifetimes Area: lifetime related A-traits Area: Trait system C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. P-low Low priority

Comments

@huonw
Copy link
Member

huonw commented Apr 12, 2015

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.

@huonw huonw added A-lifetimes Area: lifetime related A-traits Area: Trait system I-nominated labels Apr 12, 2015
@pnkfelix
Copy link
Member

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

@pnkfelix pnkfelix added P-low Low priority and removed I-nominated labels Apr 16, 2015
@eefriedman
Copy link
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() {}

@mitchmindtree
Copy link
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.

@malbarbo
Copy link
Contributor

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

@malbarbo
Copy link
Contributor

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

@japaric japaric added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Sep 30, 2016
@Mark-Simulacrum Mark-Simulacrum added A-inference Area: Type inference C-bug Category: This is a bug. labels Jul 22, 2017
euclio added a commit to euclio/rust that referenced this issue Oct 4, 2018
bors added a commit that referenced this issue Oct 7, 2018
Add tests for some E-needstest issues

Fixes #28134.
Fixes #24338.
Fixes #29743.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inference Area: Type inference A-lifetimes Area: lifetime related A-traits Area: Trait system C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. P-low Low priority
Projects
None yet
Development

No branches or pull requests

7 participants