You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
impl trait error message around 'static is probably confusing #48467
The error message for this example seems potentially confusing:
#![feature(dyn_trait)]#![feature(conservative_impl_trait)]#![feature(underscore_lifetimes)]fna<T>(items:&[T]) -> Box<implIterator>{Box::new(items.iter())//~ ERROR cannot infer an appropriate lifetime}fnmain(){}
I get:
error[E0621]: explicit lifetime required in the type of `items`
--> src/main.rs:5:29
|
5 | fn a<T>(items: &[T]) -> Box<impl Iterator> {
| ----- ^^^^^^^^^^^^^ lifetime `'static` required
| |
| consider changing the type of `items` to `&'static [T]`
What's going wrong here is that impl Iterator can only capture lifetimes that it names, and this one doesn't name any lifetimes. It can be fixed by changing to impl Iterator + '_ -- this is probably what we want to suggest? I'm not sure how best to phrase this, suggestions welcome.
Suggesting both might be good. However, this label is inaccurate:
this trait bound implies lifetime 'static
That trait bound forbids us from capturing the anonymous lifetime, because it is not mentioned anywhere, but it does not imply 'static. For example, we could capture values of type T, which is not known to be static. Obviously we won't be able to express all that subtlety, but maybe we can say something like "impl Trait types can only capture lifetimes that they reference"?
This comes up in Futures-heavy code a lot. In such cases the real solution is to use Arc, but users are sent chasing impossible lifetime annotations.
My suggestion is: No matter what, don't say 'static.
Users don't write Box< + 'static> explicitly by themselves. They see it only in the context of &'static str or static FOO: &'static.
When Rust points to a &-argument, and says it has to be 'static, users understand it literally that arg: &'static Foo is the only allowed solution, which leads to despair.
"Temporary references are not allowed here" may be guiding them towards better solutions.
where the suggestion would need to be to add + 'a constraint, but currently outputs the general lifetime error:
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
--> file.rs:6:20
|
6 | Box::new(items.iter())
| ^^^^
|
note: first, the lifetime cannot outlive the lifetime 'a as defined on the function body at 5:6...
--> file.rs:5:6
|
5 | fn a<'a, T>(items: &'a [T]) -> Box<impl Iterator> {
| ^^
note: ...so that reference does not outlive borrowed content
--> file.rs:6:14
|
6 | Box::new(items.iter())
| ^^^^^
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that return value is valid for the call
--> file.rs:5:36
|
5 | fn a<'a, T>(items: &'a [T]) -> Box<impl Iterator> {
| ^^^^^^^^^^^^^
Activity
estebank commentedon Feb 23, 2018
I've seen other cases of this too, it'd be great to improve it.
nikomatsakis commentedon Feb 23, 2018
@estebank
Suggesting both might be good. However, this label is inaccurate:
That trait bound forbids us from capturing the anonymous lifetime, because it is not mentioned anywhere, but it does not imply
'static. For example, we could capture values of typeT, which is not known to be static. Obviously we won't be able to express all that subtlety, but maybe we can say something like "impl Trait types can only capture lifetimes that they reference"?estebank commentedon Feb 23, 2018
Your suggested text seems great to me for the label, while this is one of the error messages that would benefit greatly from
--teach.kornelski commentedon Jan 5, 2019
This comes up in
Futures-heavy code a lot. In such cases the real solution is to useArc, but users are sent chasing impossible lifetime annotations.My suggestion is: No matter what, don't say
'static.Users don't write
Box< + 'static>explicitly by themselves. They see it only in the context of&'static strorstatic FOO: &'static.When Rust points to a
&-argument, and says it has to be'static, users understand it literally thatarg: &'static Foois the only allowed solution, which leads to despair."Temporary references are not allowed here" may be guiding them towards better solutions.
kornelski commentedon Jan 5, 2019
Another case from user forum:
The error points that the argument should be
'staticrather than the real (quite unobvious) solution oftype EvalFn<'a> = Fn() -> (u8) + 'a.nox commentedon Mar 4, 2019
I hit this when implementing
inert_derive.https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=dcbcd6a9d8e7b989ac3f574044557c64
In my case I can't seem to find a way to tell
rustcthat myimpl ToTokensvalue is'static.estebank commentedon Mar 4, 2019
A separate situation that would need to be handled is the following:
where the suggestion would need to be to add
+ 'aconstraint, but currently outputs the general lifetime error:Rollup merge of rust-lang#58919 - estebank:impl-trait-return-lifetime…
Rollup merge of rust-lang#58919 - estebank:impl-trait-return-lifetime…