

Github Fix suggestions for missing return type lifetime specifiers by FabianWolf...
source link: https://github.com/rust-lang/rust/pull/85050
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

This pull request aims to fix #84592. The issue is that the current code seems to assume that there is only a single relevant span pointing to the missing lifetime, and only looks at the first one:
This is incorrect, though, and leads to incorrect error messages and invalid suggestions. For instance, the example from #84592:
struct TwoLifetimes<'x, 'y> { x: &'x (), y: &'y (), } fn two_lifetimes_needed(a: &(), b: &()) -> TwoLifetimes<'_, '_> { TwoLifetimes { x: &(), y: &() } }
currently leads to:
error[E0106]: missing lifetime specifiers
--> src/main.rs:6:57
|
6 | fn two_lifetimes_needed(a: &(), b: &()) -> TwoLifetimes<'_, '_> {
| --- --- ^^ expected 2 lifetime parameters
|
= help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `a` or `b`
help: consider introducing a named lifetime parameter
|
6 | fn two_lifetimes_needed<'a>(a: &'a (), b: &'a ()) -> TwoLifetimes<'_<'a, 'a>, '_> {
| ^^^^ ^^^^^^ ^^^^^^ ^^^^^^^^^^
There are two problems:
- The error message is wrong. There is only one lifetime parameter expected at the location pointed to by the error message (and another one at a separate location).
- The suggestion is incorrect and will not lead to correct code.
With the changes in this PR, I get the following output:
error[E0106]: missing lifetime specifiers
--> p.rs:6:57
|
6 | fn two_lifetimes_needed(a: &(), b: &()) -> TwoLifetimes<'_, '_> {
| --- --- ^^ ^^ expected named lifetime parameter
| |
| expected named lifetime parameter
|
= help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `a` or `b`
help: consider introducing a named lifetime parameter
|
6 | fn two_lifetimes_needed<'a>(a: &'a (), b: &'a ()) -> TwoLifetimes<'a, 'a> {
| ^^^^ ^^^^^^ ^^^^^^ ^^ ^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0106`.
Mainly, I changed add_missing_lifetime_specifiers_label()
to receive a vector of spans (and counts) instead of just one, and adjusted its body accordingly.
Recommend
-
32
Raymond August 30th, 2019 Windows adopted Unicode before most other operating systems. [citation ne...
-
13
Type Specifiers in C, Part 3mikeash.com: just this guy, you know? Friday Q&A 2009-07-10: Type Specifiers in C, Part 3 by Mike Ash Here at la...
-
6
Type Specifiers in C, Part 2mikeash.com: just this guy, you know? Friday Q&A 2009-07-03: Type Specifiers in C, Part 2 by Mike Ash Welcome to...
-
7
Home > Cpp > C++ Program to maintain a Cricket Team by using CLASS with PUBLIC & PRIVATE Access Specifiers – Q9 C...
-
9
C++ For C# Developers: Part 34 – Fold Expressions and Elaborated Type Specifiers January 4, 2021 Tags: generic,
-
7
Home > Cpp > C++ Program to implement Toll Tax Problem by using CLASS Access Specifiers and SWITCH CASE – Q12 C++ Pro...
-
11
Home > Cpp > C++ Program to maintain Student Result by using CLASS with PUBLIC & PRIVATE Access Specifiers – Q7
-
9
Copy link Contributor FabianWolff com...
-
16
Home > Cpp > C++ Program to work with Purchase Order Problem by using Pointers and CLASS with PUBLIC & PRIVATE Access Spec...
-
3
Conversation Contributor...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK