Closed
Description
A Fn
trait bound with implicit lifetimes is not equivalent to the trait bound with explicit lifetimes.
fn foo<F>(f: F) where F: for<'a> Fn(&'a str) -> &'a str {}
fn bar<F>(f: F) where F: Fn(&str) -> &str {}
fn main() {
foo(|a: &str| a); // Fails
bar(|a: &str| a); // Works
}
(playpen: https://is.gd/nKZx1O)
I expected that foo
and bar
would be equivalent and that both calls would compile (or fail to compile the same way).
instead, calling bar
compiles fine, but calling foo
results in the following error:
rustc 1.14.0 (e8a012324 2016-12-16)
error[E0312]: lifetime of reference outlives lifetime of borrowed content...
--> <anon>:6:19
|
6 | foo(|a: &str| a); // Fails
| ^
|
note: ...the reference is valid for the lifetime 'a as defined on the block at 6:18...
--> <anon>:6:19
|
6 | foo(|a: &str| a); // Fails
| ^
note: ...but the borrowed content is only valid for the anonymous lifetime #1 defined on the block at 6:18
--> <anon>:6:19
|
6 | foo(|a: &str| a); // Fails
| ^
help: consider using an explicit lifetime parameter as shown: fn main()
--> <anon>:5:1
|
5 | fn main() {
| ^
error: aborting due to previous error
Somewhat related (but different) problem about a closure with annotated argument was reported in #22557
Metadata
Metadata
Assignees
Labels
Type
Projects
Relationships
Development
No branches or pull requests
Activity
JDemler commentedon Feb 20, 2017
I think I ran into the same problem. Alas, for me, it is not possible to use implicit lifetimes:
playpen
results in:
vickenty commentedon Feb 20, 2017
@JDemler This problem only shows up if the closure parameter is typed explicitly. Your example compiles if the type annotation is removed: https://is.gd/e5ILB5
JDemler commentedon Feb 20, 2017
Aha. Thank you. Didn't know that was possible!
F001 commentedon Sep 2, 2017
I ran into this issue too. The compiler panicked when I was testing @vickenty 's test case.
For the good case, if I declare the closure as a local variable, it also leads to compile error.
arielb1 commentedon Sep 3, 2017
This causes
ICE: no representative region
on beta.The code not working with a local would be expected, because of the way higher-ranked inference works.
nikomatsakis commentedon Sep 7, 2017
In what sense is this a regression? That is, which code specifically works and where...?
arielb1 commentedon Sep 7, 2017
Causes an ICE in beta, none in stable.
nikomatsakis commentedon Sep 7, 2017
triage: P-high
Assigning to myself to investigate.
16 remaining items