Skip to content

Commit 1420bc5

Browse files
committed
Always constrain the return type in lifetime suggestion
``` error: lifetime may not live long enough --> f205.rs:8:16 | 7 | fn resolve_symbolic_reference(&self, reference: Option<Reference>) -> Option<Reference> { | - --------- has type `Option<Reference<'1>>` | | | let's call the lifetime of this reference `'2` 8 | return reference; | ^^^^^^^^^ method was supposed to return data with lifetime `'2` but it is returning data with lifetime `'1` | help: consider introducing a named lifetime parameter | 7 | fn resolve_symbolic_reference<'a>(&'a self, reference: Option<Reference<'a>>) -> Option<Reference<'a>> { | ++++ ++ ++++ ++++ ``` The correct suggestion would be ``` help: consider introducing a named lifetime parameter | 7 | fn resolve_symbolic_reference<'a>(&self, reference: Option<Reference<'a>>) -> Option<Reference<'a>> { | ++++ ++++ ++++ ``` but we are not doing the analysis to detect that yet. If we constrain `&'a self`, then the return type with a borrow will implicitly take its lifetime from `'a`, it is better to make it explicit in the suggestion, in case that `&self` *doesn't* need to be `'a`, but the return does.
1 parent 106a46a commit 1420bc5

11 files changed

+77
-72
lines changed

compiler/rustc_infer/src/errors/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,11 @@ impl Subdiagnostic for AddLifetimeParamsSuggestion<'_> {
450450
};
451451
visitor.visit_ty(self.ty_sub);
452452
visitor.visit_ty(self.ty_sup);
453+
if let Some(fn_decl) = node.fn_decl()
454+
&& let hir::FnRetTy::Return(ty) = fn_decl.output
455+
{
456+
visitor.visit_ty(ty);
457+
}
453458
if visitor.suggestions.is_empty() {
454459
return false;
455460
}

tests/ui/lifetimes/issue-17728.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ LL | Some(entry) => Ok(entry),
2929
|
3030
help: consider introducing a named lifetime parameter
3131
|
32-
LL | fn attemptTraverse<'a>(&'a self, room: &'a Room, directionStr: &str) -> Result<&Room, &str> {
33-
| ++++ ++ ++
32+
LL | fn attemptTraverse<'a>(&'a self, room: &'a Room, directionStr: &str) -> Result<&'a Room, &'a str> {
33+
| ++++ ++ ++ ++ ++
3434

3535
error: aborting due to 2 previous errors
3636

tests/ui/lifetimes/lifetime-errors/ex1-return-one-existing-name-return-type-is-anon.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ LL | x
1111
|
1212
help: consider introducing a named lifetime parameter and update trait if needed
1313
|
14-
LL | fn foo<'a>(&'a self, x: &'a i32) -> &i32 {
15-
| ++
14+
LL | fn foo<'a>(&'a self, x: &'a i32) -> &'a i32 {
15+
| ++ ++
1616

1717
error: aborting due to 1 previous error
1818

tests/ui/lifetimes/lifetime-errors/ex3-both-anon-regions-return-type-is-anon.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ LL | x
1010
|
1111
help: consider introducing a named lifetime parameter and update trait if needed
1212
|
13-
LL | fn foo<'a>(&'a self, x: &'a i32) -> &i32 {
14-
| ++ ++
13+
LL | fn foo<'a>(&'a self, x: &'a i32) -> &'a i32 {
14+
| ++ ++ ++
1515

1616
error: aborting due to 1 previous error
1717

tests/ui/lifetimes/lifetime-errors/ex3-both-anon-regions-self-is-anon.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ LL | if true { x } else { self }
1010
|
1111
help: consider introducing a named lifetime parameter and update trait if needed
1212
|
13-
LL | fn foo<'a>(&'a self, x: &'a Foo) -> &Foo {
14-
| ++ ++
13+
LL | fn foo<'a>(&'a self, x: &'a Foo) -> &'a Foo {
14+
| ++ ++ ++
1515

1616
error: aborting due to 1 previous error
1717

tests/ui/self/arbitrary_self_types_pin_lifetime_mismatch.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ LL | fn a(self: Pin<&Foo>, f: &Foo) -> &Foo { f }
99
|
1010
help: consider introducing a named lifetime parameter and update trait if needed
1111
|
12-
LL | fn a<'a>(self: Pin<&'a Foo>, f: &'a Foo) -> &Foo { f }
13-
| ++++ ++ ++
12+
LL | fn a<'a>(self: Pin<&'a Foo>, f: &'a Foo) -> &'a Foo { f }
13+
| ++++ ++ ++ ++
1414

1515
error: lifetime may not live long enough
1616
--> $DIR/arbitrary_self_types_pin_lifetime_mismatch.rs:9:69
@@ -23,8 +23,8 @@ LL | fn c(self: Pin<&Self>, f: &Foo, g: &Foo) -> (Pin<&Foo>, &Foo) { (self,
2323
|
2424
help: consider introducing a named lifetime parameter and update trait if needed
2525
|
26-
LL | fn c<'a>(self: Pin<&'a Self>, f: &'a Foo, g: &Foo) -> (Pin<&Foo>, &Foo) { (self, f) }
27-
| ++++ ++ ++
26+
LL | fn c<'a>(self: Pin<&'a Self>, f: &'a Foo, g: &Foo) -> (Pin<&'a Foo>, &'a Foo) { (self, f) }
27+
| ++++ ++ ++ ++ ++
2828

2929
error: lifetime may not live long enough
3030
--> $DIR/arbitrary_self_types_pin_lifetime_mismatch.rs:15:58
@@ -36,8 +36,8 @@ LL | fn bar<'a>(self: Alias<&Self>, arg: &'a ()) -> &() { arg }
3636
|
3737
help: consider introducing a named lifetime parameter and update trait if needed
3838
|
39-
LL | fn bar<'a>(self: Alias<&'a Self>, arg: &'a ()) -> &() { arg }
40-
| ++
39+
LL | fn bar<'a>(self: Alias<&'a Self>, arg: &'a ()) -> &'a () { arg }
40+
| ++ ++
4141

4242
error: aborting due to 3 previous errors
4343

tests/ui/self/elision/lt-ref-self.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ LL | f
1010
|
1111
help: consider introducing a named lifetime parameter and update trait if needed
1212
|
13-
LL | fn ref_self<'a>(&'a self, f: &'a u32) -> &u32 {
14-
| ++++ ++ ++
13+
LL | fn ref_self<'a>(&'a self, f: &'a u32) -> &'a u32 {
14+
| ++++ ++ ++ ++
1515

1616
error: lifetime may not live long enough
1717
--> $DIR/lt-ref-self.rs:18:9
@@ -25,8 +25,8 @@ LL | f
2525
|
2626
help: consider introducing a named lifetime parameter and update trait if needed
2727
|
28-
LL | fn ref_Self<'a>(self: &'a Self, f: &'a u32) -> &u32 {
29-
| ++++ ++ ++
28+
LL | fn ref_Self<'a>(self: &'a Self, f: &'a u32) -> &'a u32 {
29+
| ++++ ++ ++ ++
3030

3131
error: lifetime may not live long enough
3232
--> $DIR/lt-ref-self.rs:23:9
@@ -40,8 +40,8 @@ LL | f
4040
|
4141
help: consider introducing a named lifetime parameter and update trait if needed
4242
|
43-
LL | fn box_ref_Self<'a>(self: Box<&'a Self>, f: &'a u32) -> &u32 {
44-
| ++++ ++ ++
43+
LL | fn box_ref_Self<'a>(self: Box<&'a Self>, f: &'a u32) -> &'a u32 {
44+
| ++++ ++ ++ ++
4545

4646
error: lifetime may not live long enough
4747
--> $DIR/lt-ref-self.rs:28:9
@@ -55,8 +55,8 @@ LL | f
5555
|
5656
help: consider introducing a named lifetime parameter and update trait if needed
5757
|
58-
LL | fn pin_ref_Self<'a>(self: Pin<&'a Self>, f: &'a u32) -> &u32 {
59-
| ++++ ++ ++
58+
LL | fn pin_ref_Self<'a>(self: Pin<&'a Self>, f: &'a u32) -> &'a u32 {
59+
| ++++ ++ ++ ++
6060

6161
error: lifetime may not live long enough
6262
--> $DIR/lt-ref-self.rs:33:9
@@ -70,8 +70,8 @@ LL | f
7070
|
7171
help: consider introducing a named lifetime parameter and update trait if needed
7272
|
73-
LL | fn box_box_ref_Self<'a>(self: Box<Box<&'a Self>>, f: &'a u32) -> &u32 {
74-
| ++++ ++ ++
73+
LL | fn box_box_ref_Self<'a>(self: Box<Box<&'a Self>>, f: &'a u32) -> &'a u32 {
74+
| ++++ ++ ++ ++
7575

7676
error: lifetime may not live long enough
7777
--> $DIR/lt-ref-self.rs:38:9
@@ -85,8 +85,8 @@ LL | f
8585
|
8686
help: consider introducing a named lifetime parameter and update trait if needed
8787
|
88-
LL | fn box_pin_Self<'a>(self: Box<Pin<&'a Self>>, f: &'a u32) -> &u32 {
89-
| ++++ ++ ++
88+
LL | fn box_pin_Self<'a>(self: Box<Pin<&'a Self>>, f: &'a u32) -> &'a u32 {
89+
| ++++ ++ ++ ++
9090

9191
error: aborting due to 6 previous errors
9292

tests/ui/self/elision/ref-mut-self.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ LL | f
1010
|
1111
help: consider introducing a named lifetime parameter and update trait if needed
1212
|
13-
LL | fn ref_self<'a>(&'a mut self, f: &'a u32) -> &u32 {
14-
| ++++ ++ ++
13+
LL | fn ref_self<'a>(&'a mut self, f: &'a u32) -> &'a u32 {
14+
| ++++ ++ ++ ++
1515

1616
error: lifetime may not live long enough
1717
--> $DIR/ref-mut-self.rs:18:9
@@ -25,8 +25,8 @@ LL | f
2525
|
2626
help: consider introducing a named lifetime parameter and update trait if needed
2727
|
28-
LL | fn ref_Self<'a>(self: &'a mut Self, f: &'a u32) -> &u32 {
29-
| ++++ ++ ++
28+
LL | fn ref_Self<'a>(self: &'a mut Self, f: &'a u32) -> &'a u32 {
29+
| ++++ ++ ++ ++
3030

3131
error: lifetime may not live long enough
3232
--> $DIR/ref-mut-self.rs:23:9
@@ -40,8 +40,8 @@ LL | f
4040
|
4141
help: consider introducing a named lifetime parameter and update trait if needed
4242
|
43-
LL | fn box_ref_Self<'a>(self: Box<&'a mut Self>, f: &'a u32) -> &u32 {
44-
| ++++ ++ ++
43+
LL | fn box_ref_Self<'a>(self: Box<&'a mut Self>, f: &'a u32) -> &'a u32 {
44+
| ++++ ++ ++ ++
4545

4646
error: lifetime may not live long enough
4747
--> $DIR/ref-mut-self.rs:28:9
@@ -55,8 +55,8 @@ LL | f
5555
|
5656
help: consider introducing a named lifetime parameter and update trait if needed
5757
|
58-
LL | fn pin_ref_Self<'a>(self: Pin<&'a mut Self>, f: &'a u32) -> &u32 {
59-
| ++++ ++ ++
58+
LL | fn pin_ref_Self<'a>(self: Pin<&'a mut Self>, f: &'a u32) -> &'a u32 {
59+
| ++++ ++ ++ ++
6060

6161
error: lifetime may not live long enough
6262
--> $DIR/ref-mut-self.rs:33:9
@@ -70,8 +70,8 @@ LL | f
7070
|
7171
help: consider introducing a named lifetime parameter and update trait if needed
7272
|
73-
LL | fn box_box_ref_Self<'a>(self: Box<Box<&'a mut Self>>, f: &'a u32) -> &u32 {
74-
| ++++ ++ ++
73+
LL | fn box_box_ref_Self<'a>(self: Box<Box<&'a mut Self>>, f: &'a u32) -> &'a u32 {
74+
| ++++ ++ ++ ++
7575

7676
error: lifetime may not live long enough
7777
--> $DIR/ref-mut-self.rs:38:9
@@ -85,8 +85,8 @@ LL | f
8585
|
8686
help: consider introducing a named lifetime parameter and update trait if needed
8787
|
88-
LL | fn box_pin_ref_Self<'a>(self: Box<Pin<&'a mut Self>>, f: &'a u32) -> &u32 {
89-
| ++++ ++ ++
88+
LL | fn box_pin_ref_Self<'a>(self: Box<Pin<&'a mut Self>>, f: &'a u32) -> &'a u32 {
89+
| ++++ ++ ++ ++
9090

9191
error: aborting due to 6 previous errors
9292

tests/ui/self/elision/ref-mut-struct.stderr

+10-10
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ LL | f
1010
|
1111
help: consider introducing a named lifetime parameter and update trait if needed
1212
|
13-
LL | fn ref_Struct<'a>(self: &'a mut Struct, f: &'a u32) -> &u32 {
14-
| ++++ ++ ++
13+
LL | fn ref_Struct<'a>(self: &'a mut Struct, f: &'a u32) -> &'a u32 {
14+
| ++++ ++ ++ ++
1515

1616
error: lifetime may not live long enough
1717
--> $DIR/ref-mut-struct.rs:16:9
@@ -25,8 +25,8 @@ LL | f
2525
|
2626
help: consider introducing a named lifetime parameter and update trait if needed
2727
|
28-
LL | fn box_ref_Struct<'a>(self: Box<&'a mut Struct>, f: &'a u32) -> &u32 {
29-
| ++++ ++ ++
28+
LL | fn box_ref_Struct<'a>(self: Box<&'a mut Struct>, f: &'a u32) -> &'a u32 {
29+
| ++++ ++ ++ ++
3030

3131
error: lifetime may not live long enough
3232
--> $DIR/ref-mut-struct.rs:21:9
@@ -40,8 +40,8 @@ LL | f
4040
|
4141
help: consider introducing a named lifetime parameter and update trait if needed
4242
|
43-
LL | fn pin_ref_Struct<'a>(self: Pin<&'a mut Struct>, f: &'a u32) -> &u32 {
44-
| ++++ ++ ++
43+
LL | fn pin_ref_Struct<'a>(self: Pin<&'a mut Struct>, f: &'a u32) -> &'a u32 {
44+
| ++++ ++ ++ ++
4545

4646
error: lifetime may not live long enough
4747
--> $DIR/ref-mut-struct.rs:26:9
@@ -55,8 +55,8 @@ LL | f
5555
|
5656
help: consider introducing a named lifetime parameter and update trait if needed
5757
|
58-
LL | fn box_box_ref_Struct<'a>(self: Box<Box<&'a mut Struct>>, f: &'a u32) -> &u32 {
59-
| ++++ ++ ++
58+
LL | fn box_box_ref_Struct<'a>(self: Box<Box<&'a mut Struct>>, f: &'a u32) -> &'a u32 {
59+
| ++++ ++ ++ ++
6060

6161
error: lifetime may not live long enough
6262
--> $DIR/ref-mut-struct.rs:31:9
@@ -70,8 +70,8 @@ LL | f
7070
|
7171
help: consider introducing a named lifetime parameter and update trait if needed
7272
|
73-
LL | fn box_pin_ref_Struct<'a>(self: Box<Pin<&'a mut Struct>>, f: &'a u32) -> &u32 {
74-
| ++++ ++ ++
73+
LL | fn box_pin_ref_Struct<'a>(self: Box<Pin<&'a mut Struct>>, f: &'a u32) -> &'a u32 {
74+
| ++++ ++ ++ ++
7575

7676
error: aborting due to 5 previous errors
7777

tests/ui/self/elision/ref-self.stderr

+14-14
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ LL | f
1010
|
1111
help: consider introducing a named lifetime parameter and update trait if needed
1212
|
13-
LL | fn ref_self<'a>(&'a self, f: &'a u32) -> &u32 {
14-
| ++++ ++ ++
13+
LL | fn ref_self<'a>(&'a self, f: &'a u32) -> &'a u32 {
14+
| ++++ ++ ++ ++
1515

1616
error: lifetime may not live long enough
1717
--> $DIR/ref-self.rs:28:9
@@ -25,8 +25,8 @@ LL | f
2525
|
2626
help: consider introducing a named lifetime parameter and update trait if needed
2727
|
28-
LL | fn ref_Self<'a>(self: &'a Self, f: &'a u32) -> &u32 {
29-
| ++++ ++ ++
28+
LL | fn ref_Self<'a>(self: &'a Self, f: &'a u32) -> &'a u32 {
29+
| ++++ ++ ++ ++
3030

3131
error: lifetime may not live long enough
3232
--> $DIR/ref-self.rs:33:9
@@ -40,8 +40,8 @@ LL | f
4040
|
4141
help: consider introducing a named lifetime parameter and update trait if needed
4242
|
43-
LL | fn box_ref_Self<'a>(self: Box<&'a Self>, f: &'a u32) -> &u32 {
44-
| ++++ ++ ++
43+
LL | fn box_ref_Self<'a>(self: Box<&'a Self>, f: &'a u32) -> &'a u32 {
44+
| ++++ ++ ++ ++
4545

4646
error: lifetime may not live long enough
4747
--> $DIR/ref-self.rs:38:9
@@ -55,8 +55,8 @@ LL | f
5555
|
5656
help: consider introducing a named lifetime parameter and update trait if needed
5757
|
58-
LL | fn pin_ref_Self<'a>(self: Pin<&'a Self>, f: &'a u32) -> &u32 {
59-
| ++++ ++ ++
58+
LL | fn pin_ref_Self<'a>(self: Pin<&'a Self>, f: &'a u32) -> &'a u32 {
59+
| ++++ ++ ++ ++
6060

6161
error: lifetime may not live long enough
6262
--> $DIR/ref-self.rs:43:9
@@ -70,8 +70,8 @@ LL | f
7070
|
7171
help: consider introducing a named lifetime parameter and update trait if needed
7272
|
73-
LL | fn box_box_ref_Self<'a>(self: Box<Box<&'a Self>>, f: &'a u32) -> &u32 {
74-
| ++++ ++ ++
73+
LL | fn box_box_ref_Self<'a>(self: Box<Box<&'a Self>>, f: &'a u32) -> &'a u32 {
74+
| ++++ ++ ++ ++
7575

7676
error: lifetime may not live long enough
7777
--> $DIR/ref-self.rs:48:9
@@ -85,8 +85,8 @@ LL | f
8585
|
8686
help: consider introducing a named lifetime parameter and update trait if needed
8787
|
88-
LL | fn box_pin_ref_Self<'a>(self: Box<Pin<&'a Self>>, f: &'a u32) -> &u32 {
89-
| ++++ ++ ++
88+
LL | fn box_pin_ref_Self<'a>(self: Box<Pin<&'a Self>>, f: &'a u32) -> &'a u32 {
89+
| ++++ ++ ++ ++
9090

9191
error: lifetime may not live long enough
9292
--> $DIR/ref-self.rs:53:9
@@ -100,8 +100,8 @@ LL | f
100100
|
101101
help: consider introducing a named lifetime parameter and update trait if needed
102102
|
103-
LL | fn wrap_ref_Self_Self<'a>(self: Wrap<&'a Self, Self>, f: &'a u8) -> &u8 {
104-
| ++++ ++ ++
103+
LL | fn wrap_ref_Self_Self<'a>(self: Wrap<&'a Self, Self>, f: &'a u8) -> &'a u8 {
104+
| ++++ ++ ++ ++
105105

106106
error: aborting due to 7 previous errors
107107

0 commit comments

Comments
 (0)