Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upIncorrect lifetime error with a generic FnMut after annotating the type in a closure. #22557
Comments
This comment has been minimized.
This comment has been minimized.
|
Note that declaring fn bar<'a, 'b, F: FnMut(&'a mut Nothing) -> &'b mut Nothing>(_: F) {} |
huonw
added
A-lifetimes
I-nominated
labels
Feb 20, 2015
This comment has been minimized.
This comment has been minimized.
|
The code in the comment seems... bad. This is possibly related to #22077. |
This comment has been minimized.
This comment has been minimized.
|
A variant that uses struct Nothing;
fn foo<'a, 'b, F: Fn(&'a Nothing) -> T, T: 'b>(_: F) { }
fn bar<'a, 'b, F: Fn(&'a Nothing) -> &'b Nothing>(_: F) { }
fn main() {
bar(|x| x); // OK
bar(|x: &Nothing| x); // OK
foo(|x| x); // OK
foo(|x: &Nothing| x); // Error
} |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
punting to next week's triage. |
pnkfelix
added
the
C-enhancement
label
Mar 5, 2015
This comment has been minimized.
This comment has been minimized.
|
an interesting corner case. we could try to fix this (and such a fix should be backwards compatible), but its not a terribly high priority. P-low, I-enhancement |
pnkfelix
added
P-low
and removed
I-nominated
labels
Mar 5, 2015
This comment has been minimized.
This comment has been minimized.
|
This is actually the expected behavior, I believe, though writing this comment is making me want to dig a bit deeper to make sure. What is happening is that when an explicit annotation is provided, That said, if my hypothesis is correct, then there are two routes to fix this:
Similarly the example from #22557 (comment) is I think harmless -- basically the caller is providing a fn where 'a and 'b will be bound the same lifetime, and that's ok, but the callee doesn't get to assume that it does. |
This comment has been minimized.
This comment has been minimized.
|
OK, so, I looked a bit at this. The reason the results are a bit surprising is that the code is a bit hokey right now but not (I believe) unsound (though it wouldn't surprise me if there was a gotcha in some particular case). One thing it does do is separate out the expected argument and return types. So in these cases part of what results for the inconsistent errors is that we infer the return type based on the expectation. For An example of code that probably ought to compile but doesn't because of this phenomena is: fn main() {
let f = |x: &i32| x;
let i = &3;
let j = f(i);
}In this case, there is no expected return type, so you wind up with a fresh variable, just as in |
This comment has been minimized.
This comment has been minimized.
|
This (or something similar) appears to happen with higher-ranked bounds too: fn foo<F>(_: F) where F: for<'a> Fn(&'a str) -> &'a str
{}
fn main() {
foo(|s| s); // ok
foo(|s: &str| s); // not ok
}
|
vickenty
referenced this issue
Dec 30, 2016
Closed
Implicit and explicit HRTB are not equivalent #38714
ExpHP
referenced this issue
Feb 19, 2017
Open
Lifetime elision is too greedy without explicit type declaration #39943
brson
added
T-lang
I-needs-decision
labels
Mar 23, 2017
This comment has been minimized.
This comment has been minimized.
|
Expected behavior or not? |
Mark-Simulacrum
referenced this issue
Apr 15, 2017
Open
Confusing type error due to strange inferred type for a closure argument #41078
This comment has been minimized.
This comment has been minimized.
|
All cases except for @nikomatsakis' no longer cause any error. Should we keep this issue open or reopen a more targeted one for that case? |
pnkfelix
referenced this issue
Dec 5, 2018
Closed
Shouldn't lifetime elision apply to closure return type annotation? #56537
memoryruins
referenced this issue
Jan 29, 2019
Open
issue triage: closures, lifetimes, and nll #57964
This comment has been minimized.
This comment has been minimized.
|
Its even questionable whether @nikomatsakis 's example should compile. (To be fair, they did qualify that example with "probably should compile.") Lets close this and open up a separate issue dedicated to that example, especially since I think that example is related to #22340 |
pnkfelix
referenced this issue
Feb 1, 2019
Open
Annotating higher-ranked lifetimes on closures is arduous #58052
This comment has been minimized.
This comment has been minimized.
|
Closing and leaving the remaining example in its own issuein #58052. |
theemathas commentedFeb 20, 2015
The code:
Playpen
Compiling this on playpen results in this compile error:
I think that this code is supposed to compile, and the error message isn't very helpful either. What exactly is
(expected&mut Nothing, found&mut Nothing)?