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

Type inference falls over on lifetime-generic (HRTB) closures #39706

Closed
cramertj opened this issue Feb 9, 2017 · 2 comments
Closed

Type inference falls over on lifetime-generic (HRTB) closures #39706

cramertj opened this issue Feb 9, 2017 · 2 comments

Comments

@cramertj
Copy link
Member

cramertj commented Feb 9, 2017

This compiles:

trait MyTrait {}
impl<F> MyTrait for F where F: Fn(&i32) -> () {}

fn take_trait<F: MyTrait>(_f: F) {}

fn main() {
    let my_func = |_:&_| {};
    take_trait(my_func);
}

When we take out the &_ type annotation, it breaks:

trait MyTrait {}
impl<F> MyTrait for F where F: Fn(&i32) -> () {}

fn take_trait<F: MyTrait>(_f: F) {}

fn main() {
    let my_func = |_| {};
    take_trait(my_func);
}

It fails to see that the closure should be generic on the lifetime of the input, resulting in this error:

error[E0271]: type mismatch resolving `for<'r> <[closure@<anon>:7:19: 7:25] as std::ops::FnOnce<(&'r i32,)>>::Output == ()`
 --> <anon>:8:5
  |
8 |     take_trait(my_func);
  |     ^^^^^^^^^^ expected bound lifetime parameter , found concrete lifetime
  |
  = note: concrete lifetime that was found is lifetime '_#0r
  = note: required because of the requirements on the impl of `MyTrait` for `[closure@<anon>:7:19: 7:25]`
  = note: required by `take_trait`

error[E0281]: type mismatch: the type `[closure@<anon>:7:19: 7:25]` implements the trait `std::ops::Fn<(_,)>`, but the trait `for<'r> std::ops::Fn<(&'r i32,)>` is required (expected concrete lifetime, found bound lifetime parameter )
 --> <anon>:8:5
  |
8 |     take_trait(my_func);
  |     ^^^^^^^^^^
  |
  = note: required because of the requirements on the impl of `MyTrait` for `[closure@<anon>:7:19: 7:25]`
  = note: required by `take_trait`

error: aborting due to 2 previous errors

The same thing happens when taking lifetime-generic structs as input: the type needs to be manually annotated, or inference will fail. Is there any way to make this work, or to at least get a better error message? I'm interested in helping fix this if someone could point me in the right direction.

@Deewiant
Copy link
Contributor

I just ran into this so I poked around the issue tracker a bit. The inference issue looks like #32600 and #26937 (these three seem like duplicates). #24680 concerns the error message.

@cramertj
Copy link
Member Author

Closing as dup of #32600.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants