Skip to content

Commit e792ee0

Browse files
committed
Add beginner friendly lifetime elision hint to E0623
Suggest adding a new lifetime parameter when two elided lifetimes should match up but don't Issue #90170
1 parent 45b600c commit e792ee0

9 files changed

+180
-2
lines changed

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/different_lifetimes.rs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use crate::infer::error_reporting::nice_region_error::NiceRegionError;
77
use crate::infer::lexical_region_resolve::RegionResolutionError;
88
use crate::infer::SubregionOrigin;
99

10-
use rustc_errors::{struct_span_err, ErrorReported};
10+
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorReported};
11+
use rustc_hir as hir;
12+
use rustc_hir::Ty;
13+
use rustc_middle::ty::Region;
1114

1215
impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
1316
/// Print the error message for lifetime errors when both the concerned regions are anonymous.
@@ -166,6 +169,8 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
166169
e.span_label(span_2, String::new());
167170
e.span_label(span, span_label);
168171

172+
self.suggest_adding_lifetime_params(sub, ty_sup, ty_sub, &mut e);
173+
169174
if let Some(t) = future_return_type {
170175
let snip = self
171176
.tcx()
@@ -178,7 +183,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
178183
(_, "") => None,
179184
_ => Some(s),
180185
})
181-
.unwrap_or("{unnamed_type}".to_string());
186+
.unwrap_or_else(|| "{unnamed_type}".to_string());
182187

183188
e.span_label(
184189
t.span,
@@ -188,4 +193,53 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
188193
e.emit();
189194
Some(ErrorReported)
190195
}
196+
197+
fn suggest_adding_lifetime_params(
198+
&self,
199+
sub: Region<'tcx>,
200+
ty_sup: &Ty<'_>,
201+
ty_sub: &Ty<'_>,
202+
e: &mut DiagnosticBuilder<'_>,
203+
) {
204+
if let (
205+
hir::Ty { kind: hir::TyKind::Rptr(lifetime_sub, _), .. },
206+
hir::Ty { kind: hir::TyKind::Rptr(lifetime_sup, _), .. },
207+
) = (ty_sub, ty_sup)
208+
{
209+
if lifetime_sub.name.is_elided() && lifetime_sup.name.is_elided() {
210+
if let Some(anon_reg) = self.tcx().is_suitable_region(sub) {
211+
let hir_id = self.tcx().hir().local_def_id_to_hir_id(anon_reg.def_id);
212+
if let hir::Node::Item(&hir::Item {
213+
kind: hir::ItemKind::Fn(_, ref generics, ..),
214+
..
215+
}) = self.tcx().hir().get(hir_id)
216+
{
217+
let add_lifetime_param = match &generics.params {
218+
[] => (generics.span, "<'a>".to_string()),
219+
[first, ..] => (first.span.shrink_to_lo(), "'a, ".to_string()),
220+
};
221+
222+
e.multipart_suggestion(
223+
"Explicitly declare a lifetime and assign it to both",
224+
vec![
225+
add_lifetime_param,
226+
if let hir::LifetimeName::Underscore = lifetime_sub.name {
227+
(lifetime_sub.span, "'a".to_string())
228+
} else {
229+
(lifetime_sub.span.shrink_to_hi(), "'a ".to_string())
230+
},
231+
if let hir::LifetimeName::Underscore = lifetime_sup.name {
232+
(lifetime_sup.span, "'a".to_string())
233+
} else {
234+
(lifetime_sup.span.shrink_to_hi(), "'a ".to_string())
235+
},
236+
],
237+
Applicability::MaybeIncorrect,
238+
);
239+
e.note("Each elided lifetime in input position becomes a distinct lifetime.");
240+
}
241+
}
242+
}
243+
}
244+
}
191245
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
fn foo(slice_a: &mut [u8], slice_b: &mut [u8]) {
2+
core::mem::swap(&mut slice_a, &mut slice_b); //~ ERROR lifetime mismatch
3+
//~^ ERROR lifetime mismatch
4+
}
5+
6+
fn foo2<U, W, O>(slice_a: &mut [u8], slice_b: &mut [u8], _: U, _: W, _: O) {
7+
core::mem::swap(&mut slice_a, &mut slice_b); //~ ERROR lifetime mismatch
8+
//~^ ERROR lifetime mismatch
9+
}
10+
11+
fn ok<'a>(slice_a: &'a mut [u8], slice_b: &'a mut [u8]) {
12+
core::mem::swap(&mut slice_a, &mut slice_b);
13+
}
14+
15+
fn main() {
16+
let a = [1u8, 2, 3];
17+
let b = [4u8, 5, 6];
18+
foo(&mut a, &mut b);
19+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
error[E0623]: lifetime mismatch
2+
--> $DIR/issue-90170-elision-mismatch.rs:2:35
3+
|
4+
LL | fn foo(slice_a: &mut [u8], slice_b: &mut [u8]) {
5+
| --------- --------- these two types are declared with different lifetimes...
6+
LL | core::mem::swap(&mut slice_a, &mut slice_b);
7+
| ^^^^^^^^^^^^ ...but data from `slice_b` flows into `slice_a` here
8+
|
9+
= note: Each elided lifetime in input position becomes a distinct lifetime.
10+
help: Explicitly declare a lifetime and assign it to both
11+
|
12+
LL | fn foo<'a>(slice_a: &'a mut [u8], slice_b: &'a mut [u8]) {
13+
| ++++ ++ ++
14+
15+
error[E0623]: lifetime mismatch
16+
--> $DIR/issue-90170-elision-mismatch.rs:2:35
17+
|
18+
LL | fn foo(slice_a: &mut [u8], slice_b: &mut [u8]) {
19+
| --------- ---------
20+
| |
21+
| these two types are declared with different lifetimes...
22+
LL | core::mem::swap(&mut slice_a, &mut slice_b);
23+
| ^^^^^^^^^^^^ ...but data from `slice_a` flows into `slice_b` here
24+
|
25+
= note: Each elided lifetime in input position becomes a distinct lifetime.
26+
help: Explicitly declare a lifetime and assign it to both
27+
|
28+
LL | fn foo<'a>(slice_a: &'a mut [u8], slice_b: &'a mut [u8]) {
29+
| ++++ ++ ++
30+
31+
error[E0623]: lifetime mismatch
32+
--> $DIR/issue-90170-elision-mismatch.rs:7:35
33+
|
34+
LL | fn foo2<U, W, O>(slice_a: &mut [u8], slice_b: &mut [u8], _: U, _: W, _: O) {
35+
| --------- --------- these two types are declared with different lifetimes...
36+
LL | core::mem::swap(&mut slice_a, &mut slice_b);
37+
| ^^^^^^^^^^^^ ...but data from `slice_b` flows into `slice_a` here
38+
|
39+
= note: Each elided lifetime in input position becomes a distinct lifetime.
40+
help: Explicitly declare a lifetime and assign it to both
41+
|
42+
LL | fn foo2<'a, U, W, O>(slice_a: &'a mut [u8], slice_b: &'a mut [u8], _: U, _: W, _: O) {
43+
| +++ ++ ++
44+
45+
error[E0623]: lifetime mismatch
46+
--> $DIR/issue-90170-elision-mismatch.rs:7:35
47+
|
48+
LL | fn foo2<U, W, O>(slice_a: &mut [u8], slice_b: &mut [u8], _: U, _: W, _: O) {
49+
| --------- ---------
50+
| |
51+
| these two types are declared with different lifetimes...
52+
LL | core::mem::swap(&mut slice_a, &mut slice_b);
53+
| ^^^^^^^^^^^^ ...but data from `slice_a` flows into `slice_b` here
54+
|
55+
= note: Each elided lifetime in input position becomes a distinct lifetime.
56+
help: Explicitly declare a lifetime and assign it to both
57+
|
58+
LL | fn foo2<'a, U, W, O>(slice_a: &'a mut [u8], slice_b: &'a mut [u8], _: U, _: W, _: O) {
59+
| +++ ++ ++
60+
61+
error: aborting due to 4 previous errors
62+
63+
For more information about this error, try `rustc --explain E0623`.

src/test/ui/lifetimes/lifetime-errors/ex3-both-anon-regions-2.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ LL | fn foo(&mut (ref mut v, w): &mut (&u8, &u8), x: &u8) {
55
| --- --- these two types are declared with different lifetimes...
66
LL | *v = x;
77
| ^ ...but data from `x` flows here
8+
|
9+
= note: Each elided lifetime in input position becomes a distinct lifetime.
10+
help: Explicitly declare a lifetime and assign it to both
11+
|
12+
LL | fn foo<'a>(&mut (ref mut v, w): &mut (&'a u8, &u8), x: &'a u8) {
13+
| ++++ ++ ++
814

915
error: aborting due to previous error
1016

src/test/ui/lifetimes/lifetime-errors/ex3-both-anon-regions-3.stderr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ LL | fn foo(z: &mut Vec<(&u8,&u8)>, (x, y): (&u8, &u8)) {
55
| --- --- these two types are declared with different lifetimes...
66
LL | z.push((x,y));
77
| ^ ...but data flows into `z` here
8+
|
9+
= note: Each elided lifetime in input position becomes a distinct lifetime.
10+
help: Explicitly declare a lifetime and assign it to both
11+
|
12+
LL | fn foo<'a>(z: &mut Vec<(&'a u8,&u8)>, (x, y): (&'a u8, &u8)) {
13+
| ++++ ++ ++
814

915
error[E0623]: lifetime mismatch
1016
--> $DIR/ex3-both-anon-regions-3.rs:2:15
@@ -13,6 +19,12 @@ LL | fn foo(z: &mut Vec<(&u8,&u8)>, (x, y): (&u8, &u8)) {
1319
| --- --- these two types are declared with different lifetimes...
1420
LL | z.push((x,y));
1521
| ^ ...but data flows into `z` here
22+
|
23+
= note: Each elided lifetime in input position becomes a distinct lifetime.
24+
help: Explicitly declare a lifetime and assign it to both
25+
|
26+
LL | fn foo<'a>(z: &mut Vec<(&u8,&'a u8)>, (x, y): (&u8, &'a u8)) {
27+
| ++++ ++ ++
1628

1729
error: aborting due to 2 previous errors
1830

src/test/ui/lifetimes/lifetime-errors/ex3-both-anon-regions-using-fn-items.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ LL | fn foo(x:fn(&u8, &u8), y: Vec<&u8>, z: &u8) {
55
| --- --- these two types are declared with different lifetimes...
66
LL | y.push(z);
77
| ^ ...but data from `z` flows into `y` here
8+
|
9+
= note: Each elided lifetime in input position becomes a distinct lifetime.
10+
help: Explicitly declare a lifetime and assign it to both
11+
|
12+
LL | fn foo<'a>(x:fn(&u8, &u8), y: Vec<&'a u8>, z: &'a u8) {
13+
| ++++ ++ ++
814

915
error: aborting due to previous error
1016

src/test/ui/lifetimes/lifetime-errors/ex3-both-anon-regions-using-trait-objects.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ LL | fn foo(x:Box<dyn Fn(&u8, &u8)> , y: Vec<&u8>, z: &u8) {
55
| --- --- these two types are declared with different lifetimes...
66
LL | y.push(z);
77
| ^ ...but data from `z` flows into `y` here
8+
|
9+
= note: Each elided lifetime in input position becomes a distinct lifetime.
10+
help: Explicitly declare a lifetime and assign it to both
11+
|
12+
LL | fn foo<'a>(x:Box<dyn Fn(&'a u8, &'a u8)> , y: Vec<&u8>, z: &u8) {
13+
| ++++ ++ ++
814

915
error: aborting due to previous error
1016

src/test/ui/lifetimes/lifetime-errors/ex3-both-anon-regions.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ LL | fn foo(x: &mut Vec<&u8>, y: &u8) {
55
| --- --- these two types are declared with different lifetimes...
66
LL | x.push(y);
77
| ^ ...but data from `y` flows into `x` here
8+
|
9+
= note: Each elided lifetime in input position becomes a distinct lifetime.
10+
help: Explicitly declare a lifetime and assign it to both
11+
|
12+
LL | fn foo<'a>(x: &mut Vec<&'a u8>, y: &'a u8) {
13+
| ++++ ++ ++
814

915
error: aborting due to previous error
1016

src/test/ui/underscore-lifetime/underscore-lifetime-elison-mismatch.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ LL | fn foo(x: &mut Vec<&'_ u8>, y: &'_ u8) { x.push(y); }
55
| ------ ------ ^ ...but data from `y` flows into `x` here
66
| |
77
| these two types are declared with different lifetimes...
8+
|
9+
= note: Each elided lifetime in input position becomes a distinct lifetime.
10+
help: Explicitly declare a lifetime and assign it to both
11+
|
12+
LL | fn foo<'a>(x: &mut Vec<&'a u8>, y: &'a u8) { x.push(y); }
13+
| ++++ ~~ ~~
814

915
error: aborting due to previous error
1016

0 commit comments

Comments
 (0)