summary refs log tree commit diff
path: root/tests/ui/lifetimes/lifetime-errors
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2024-05-05 03:42:47 +0000
committerEsteban Küber <esteban@kuber.com.ar>2024-05-17 20:31:13 +0000
commit120049fab4e293c19fa1c5a09d68f89bcd940b36 (patch)
treec5151a7e7ebc1f502f2254328558f4692cf314d7 /tests/ui/lifetimes/lifetime-errors
parent9f730e92f24ebd21b94112e46a0e44bdf7730e27 (diff)
downloadrust-120049fab4e293c19fa1c5a09d68f89bcd940b36.tar.gz
rust-120049fab4e293c19fa1c5a09d68f89bcd940b36.zip
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.
Diffstat (limited to 'tests/ui/lifetimes/lifetime-errors')
-rw-r--r--tests/ui/lifetimes/lifetime-errors/ex1-return-one-existing-name-return-type-is-anon.stderr4
-rw-r--r--tests/ui/lifetimes/lifetime-errors/ex3-both-anon-regions-return-type-is-anon.stderr4
-rw-r--r--tests/ui/lifetimes/lifetime-errors/ex3-both-anon-regions-self-is-anon.stderr4
3 files changed, 6 insertions, 6 deletions
diff --git a/tests/ui/lifetimes/lifetime-errors/ex1-return-one-existing-name-return-type-is-anon.stderr b/tests/ui/lifetimes/lifetime-errors/ex1-return-one-existing-name-return-type-is-anon.stderr
index cdcdc0c07e3..5cfa5717224 100644
--- a/tests/ui/lifetimes/lifetime-errors/ex1-return-one-existing-name-return-type-is-anon.stderr
+++ b/tests/ui/lifetimes/lifetime-errors/ex1-return-one-existing-name-return-type-is-anon.stderr
@@ -11,8 +11,8 @@ LL |     x
    |
 help: consider introducing a named lifetime parameter and update trait if needed
    |
-LL |   fn foo<'a>(&'a self, x: &'a i32) -> &i32 {
-   |               ++
+LL |   fn foo<'a>(&'a self, x: &'a i32) -> &'a i32 {
+   |               ++                       ++
 
 error: aborting due to 1 previous error
 
diff --git a/tests/ui/lifetimes/lifetime-errors/ex3-both-anon-regions-return-type-is-anon.stderr b/tests/ui/lifetimes/lifetime-errors/ex3-both-anon-regions-return-type-is-anon.stderr
index e934ce7c040..fd15d516999 100644
--- a/tests/ui/lifetimes/lifetime-errors/ex3-both-anon-regions-return-type-is-anon.stderr
+++ b/tests/ui/lifetimes/lifetime-errors/ex3-both-anon-regions-return-type-is-anon.stderr
@@ -10,8 +10,8 @@ LL |     x
    |
 help: consider introducing a named lifetime parameter and update trait if needed
    |
-LL |   fn foo<'a>(&'a self, x: &'a i32) -> &i32 {
-   |               ++           ++
+LL |   fn foo<'a>(&'a self, x: &'a i32) -> &'a i32 {
+   |               ++           ++          ++
 
 error: aborting due to 1 previous error
 
diff --git a/tests/ui/lifetimes/lifetime-errors/ex3-both-anon-regions-self-is-anon.stderr b/tests/ui/lifetimes/lifetime-errors/ex3-both-anon-regions-self-is-anon.stderr
index dde271d100c..0a312cf93c8 100644
--- a/tests/ui/lifetimes/lifetime-errors/ex3-both-anon-regions-self-is-anon.stderr
+++ b/tests/ui/lifetimes/lifetime-errors/ex3-both-anon-regions-self-is-anon.stderr
@@ -10,8 +10,8 @@ LL |         if true { x } else { self }
    |
 help: consider introducing a named lifetime parameter and update trait if needed
    |
-LL |     fn foo<'a>(&'a self, x: &'a Foo) -> &Foo {
-   |                 ++           ++
+LL |     fn foo<'a>(&'a self, x: &'a Foo) -> &'a Foo {
+   |                 ++           ++          ++
 
 error: aborting due to 1 previous error