Skip to content

Commit 34657cc

Browse files
authored
Rollup merge of #94449 - GuillaumeGomez:explanation-e0726, r=Urgau
Add long explanation for E0726 This is the cleaned up version of #87655 with the missing fixes. Part of #61137. r? `@Urgau`
2 parents 5be38d2 + 8f36d4a commit 34657cc

File tree

7 files changed

+53
-2
lines changed

7 files changed

+53
-2
lines changed

compiler/rustc_error_codes/src/error_codes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ E0720: include_str!("./error_codes/E0720.md"),
429429
E0722: include_str!("./error_codes/E0722.md"),
430430
E0724: include_str!("./error_codes/E0724.md"),
431431
E0725: include_str!("./error_codes/E0725.md"),
432+
E0726: include_str!("./error_codes/E0726.md"),
432433
E0727: include_str!("./error_codes/E0727.md"),
433434
E0728: include_str!("./error_codes/E0728.md"),
434435
E0729: include_str!("./error_codes/E0729.md"),
@@ -641,6 +642,5 @@ E0787: include_str!("./error_codes/E0787.md"),
641642
E0717, // rustc_promotable without stability attribute
642643
// E0721, // `await` keyword
643644
// E0723, // unstable feature in `const` context
644-
E0726, // non-explicit (not `'_`) elided lifetime in unsupported position
645645
// E0738, // Removed; errored on `#[track_caller] fn`s in `extern "Rust" { ... }`.
646646
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
An argument lifetime was elided in an async function.
2+
3+
Erroneous code example:
4+
5+
When a struct or a type is bound/declared with a lifetime it is important for
6+
the Rust compiler to know, on usage, the lifespan of the type. When the
7+
lifetime is not explicitly mentioned and the Rust Compiler cannot determine
8+
the lifetime of your type, the following error occurs.
9+
10+
```compile_fail,E0726
11+
use futures::executor::block_on;
12+
struct Content<'a> {
13+
title: &'a str,
14+
body: &'a str,
15+
}
16+
async fn create(content: Content) { // error: implicit elided
17+
// lifetime not allowed here
18+
println!("title: {}", content.title);
19+
println!("body: {}", content.body);
20+
}
21+
let content = Content { title: "Rust", body: "is great!" };
22+
let future = create(content);
23+
block_on(future);
24+
```
25+
26+
Specify desired lifetime of parameter `content` or indicate the anonymous
27+
lifetime like `content: Content<'_>`. The anonymous lifetime tells the Rust
28+
compiler that `content` is only needed until create function is done with
29+
it's execution.
30+
31+
The `implicit elision` meaning the omission of suggested lifetime that is
32+
`pub async fn create<'a>(content: Content<'a>) {}` is not allowed here as
33+
lifetime of the `content` can differ from current context:
34+
35+
```ignore (needs futures dependency)
36+
async fn create(content: Content<'_>) { // ok!
37+
println!("title: {}", content.title);
38+
println!("body: {}", content.body);
39+
}
40+
```
41+
42+
Know more about lifetime elision in this [chapter][lifetime-elision] and a
43+
chapter on lifetimes can be found [here][lifetimes].
44+
45+
[lifetime-elision]: https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html#lifetime-elision
46+
[lifetimes]: https://doc.rust-lang.org/rust-by-example/scope/lifetime.html

src/test/ui/async-await/async-fn-path-elision.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ LL | async fn error(lt: HasLifetime) {
88

99
error: aborting due to previous error
1010

11+
For more information about this error, try `rustc --explain E0726`.

src/test/ui/impl-header-lifetime-elision/path-elided.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ LL | impl MyTrait for Foo {
88

99
error: aborting due to previous error
1010

11+
For more information about this error, try `rustc --explain E0726`.

src/test/ui/impl-header-lifetime-elision/trait-elided.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ LL | impl MyTrait for u32 {
88

99
error: aborting due to previous error
1010

11+
For more information about this error, try `rustc --explain E0726`.

src/test/ui/issues/issue-10412.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,5 @@ LL | trait Serializable<'self, T: ?Sized> {
6767

6868
error: aborting due to 9 previous errors
6969

70-
For more information about this error, try `rustc --explain E0277`.
70+
Some errors have detailed explanations: E0277, E0726.
71+
For more information about an error, try `rustc --explain E0277`.

src/test/ui/wf/wf-in-foreign-fn-decls-issue-80468.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ LL | impl Trait for Ref {}
3030

3131
error: aborting due to 2 previous errors
3232

33+
For more information about this error, try `rustc --explain E0726`.

0 commit comments

Comments
 (0)