Skip to content

Commit 327d807

Browse files
authored
Rollup merge of #89922 - JohnTitor:update-e0637, r=jackh726
Update E0637 description to mention `&` w/o an explicit lifetime name Deal with #89824 (comment). Another solution would be splitting the error code into two as (I think) it's a bit unclear to users why they have the same error code.
2 parents 91fb223 + 4333091 commit 327d807

File tree

3 files changed

+58
-27
lines changed

3 files changed

+58
-27
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,51 @@
1-
An underscore `_` character has been used as the identifier for a lifetime.
1+
`'_` lifetime name or `&T` without an explicit lifetime name has been used
2+
on illegal place.
23

34
Erroneous code example:
45

56
```compile_fail,E0106,E0637
6-
fn longest<'_>(str1: &'_ str, str2: &'_ str) -> &'_ str {
7-
//^^ `'_` is a reserved lifetime name
7+
fn underscore_lifetime<'_>(str1: &'_ str, str2: &'_ str) -> &'_ str {
8+
//^^ `'_` is a reserved lifetime name
89
if str1.len() > str2.len() {
910
str1
1011
} else {
1112
str2
1213
}
1314
}
15+
16+
fn and_without_explicit_lifetime<T>()
17+
where
18+
T: Into<&u32>,
19+
//^ `&` without an explicit lifetime name
20+
{
21+
}
1422
```
1523

16-
`'_`, cannot be used as a lifetime identifier because it is a reserved for the
17-
anonymous lifetime. To fix this, use a lowercase letter such as 'a, or a series
18-
of lowercase letters such as `'foo`. For more information, see [the
19-
book][bk-no]. For more information on using the anonymous lifetime in rust
20-
nightly, see [the nightly book][bk-al].
24+
First, `'_` cannot be used as a lifetime identifier in some places
25+
because it is a reserved for the anonymous lifetime. Second, `&T`
26+
without an explicit lifetime name cannot also be used in some places.
27+
To fix them, use a lowercase letter such as `'a`, or a series
28+
of lowercase letters such as `'foo`. For more information about lifetime
29+
identifier, see [the book][bk-no]. For more information on using
30+
the anonymous lifetime in Rust 2018, see [the Rust 2018 blog post][blog-al].
2131

2232
Corrected example:
2333

2434
```
25-
fn longest<'a>(str1: &'a str, str2: &'a str) -> &'a str {
35+
fn underscore_lifetime<'a>(str1: &'a str, str2: &'a str) -> &'a str {
2636
if str1.len() > str2.len() {
2737
str1
2838
} else {
2939
str2
3040
}
3141
}
42+
43+
fn and_without_explicit_lifetime<'foo, T>()
44+
where
45+
T: Into<&'foo u32>,
46+
{
47+
}
3248
```
3349

3450
[bk-no]: https://doc.rust-lang.org/book/appendix-02-operators.html#non-operator-symbols
35-
[bk-al]: https://doc.rust-lang.org/nightly/edition-guide/rust-2018/ownership-and-lifetimes/the-anonymous-lifetime.html
51+
[blog-al]: https://blog.rust-lang.org/2018/12/06/Rust-1.31-and-rust-2018.html#more-lifetime-elision-rules

src/test/ui/error-codes/E0637.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
struct Foo<'a: '_>(&'a u8); //~ ERROR cannot be used here
2-
fn foo<'a: '_>(_: &'a u8) {} //~ ERROR cannot be used here
1+
fn underscore_lifetime<'_>(str1: &'_ str, str2: &'_ str) -> &'_ str {
2+
//~^ ERROR: `'_` cannot be used here [E0637]
3+
//~| ERROR: missing lifetime specifier
4+
if str1.len() > str2.len() {
5+
str1
6+
} else {
7+
str2
8+
}
9+
}
310

4-
struct Bar<'a>(&'a u8);
5-
impl<'a: '_> Bar<'a> { //~ ERROR cannot be used here
6-
fn bar() {}
11+
fn and_without_explicit_lifetime<T>()
12+
where
13+
T: Into<&u32>, //~ ERROR: `&` without an explicit lifetime name cannot be used here [E0637]
14+
{
715
}
816

917
fn main() {}

src/test/ui/error-codes/E0637.stderr

+19-12
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
error[E0637]: `'_` cannot be used here
2-
--> $DIR/E0637.rs:1:16
2+
--> $DIR/E0637.rs:1:24
33
|
4-
LL | struct Foo<'a: '_>(&'a u8);
5-
| ^^ `'_` is a reserved lifetime name
4+
LL | fn underscore_lifetime<'_>(str1: &'_ str, str2: &'_ str) -> &'_ str {
5+
| ^^ `'_` is a reserved lifetime name
66

7-
error[E0637]: `'_` cannot be used here
8-
--> $DIR/E0637.rs:2:12
7+
error[E0637]: `&` without an explicit lifetime name cannot be used here
8+
--> $DIR/E0637.rs:13:13
99
|
10-
LL | fn foo<'a: '_>(_: &'a u8) {}
11-
| ^^ `'_` is a reserved lifetime name
10+
LL | T: Into<&u32>,
11+
| ^ explicit lifetime name needed here
1212

13-
error[E0637]: `'_` cannot be used here
14-
--> $DIR/E0637.rs:5:10
13+
error[E0106]: missing lifetime specifier
14+
--> $DIR/E0637.rs:1:62
15+
|
16+
LL | fn underscore_lifetime<'_>(str1: &'_ str, str2: &'_ str) -> &'_ str {
17+
| ------- ------- ^^ expected named lifetime parameter
18+
|
19+
= help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `str1` or `str2`
20+
help: consider introducing a named lifetime parameter
1521
|
16-
LL | impl<'a: '_> Bar<'a> {
17-
| ^^ `'_` is a reserved lifetime name
22+
LL | fn underscore_lifetime<'a, '_>(str1: &'a str, str2: &'a str) -> &'a str {
23+
| +++ ~~ ~~ ~~
1824

1925
error: aborting due to 3 previous errors
2026

21-
For more information about this error, try `rustc --explain E0637`.
27+
Some errors have detailed explanations: E0106, E0637.
28+
For more information about an error, try `rustc --explain E0106`.

0 commit comments

Comments
 (0)