-
-
Notifications
You must be signed in to change notification settings - Fork 15.8k
Recover missing turbofish for lifetime arguments in expression position #162749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
raushan728
wants to merge
3
commits into
rust-lang:main
Choose a base branch
from
raushan728:issues/162656
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
9786ee6
test(parser): add baseline for missing turbofish with lifetime in str…
raushan728 67007be
refactor(parser): add IS_RHS_OF_LT_AFTER_PATH restriction and delay K…
raushan728 2e5a236
fix(parser): recover missing turbofish for reserved keyword lifetimes
raushan728 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
tests/ui/parser/recover/missing-turbofish-lifetime-run-pass.fixed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| //@ run-rustfix | ||
| struct Struct<'a>(&'a str); | ||
|
|
||
| fn main() { | ||
| let s = Struct::<'_>("hi"); | ||
| //~^ ERROR labels cannot use keyword names | ||
| //~| HELP use `::<...>` instead of `<...>` to specify lifetime arguments for `Struct` | ||
| println!("{}", s.0); | ||
| } |
9 changes: 9 additions & 0 deletions
9
tests/ui/parser/recover/missing-turbofish-lifetime-run-pass.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| //@ run-rustfix | ||
| struct Struct<'a>(&'a str); | ||
|
|
||
| fn main() { | ||
| let s = Struct<'_>("hi"); | ||
| //~^ ERROR labels cannot use keyword names | ||
| //~| HELP use `::<...>` instead of `<...>` to specify lifetime arguments for `Struct` | ||
| println!("{}", s.0); | ||
| } |
13 changes: 13 additions & 0 deletions
13
tests/ui/parser/recover/missing-turbofish-lifetime-run-pass.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| error: labels cannot use keyword names | ||
| --> $DIR/missing-turbofish-lifetime-run-pass.rs:5:20 | ||
| | | ||
| LL | let s = Struct<'_>("hi"); | ||
| | ^^ | ||
| | | ||
| help: use `::<...>` instead of `<...>` to specify lifetime arguments for `Struct` | ||
| | | ||
| LL | let s = Struct::<'_>("hi"); | ||
| | ++ | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| struct Struct<'a> { | ||
| string: &'a str, | ||
| } | ||
|
|
||
| fn struct_with_reserved_lifetime() { | ||
| let _ = Struct<'_> { | ||
| //~^ ERROR labels cannot use keyword names | ||
| string: "", | ||
| }; | ||
| } | ||
|
|
||
| fn struct_with_named_lifetime<'a>() { | ||
| let _ = Struct<'a> { | ||
| //~^ ERROR expected `while`, `for`, `loop` or `{` after a label | ||
| string: "", | ||
| }; | ||
| } | ||
|
|
||
| fn struct_with_multichar_lifetime<'abc>() { | ||
| let _ = Struct<'abc> { | ||
| //~^ ERROR expected `while`, `for`, `loop` or `{` after a label | ||
| string: "", | ||
| }; | ||
| } | ||
|
|
||
| fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| error: labels cannot use keyword names | ||
| --> $DIR/missing-turbofish-lifetime.rs:6:20 | ||
| | | ||
| LL | let _ = Struct<'_> { | ||
| | ^^ | ||
| | | ||
| help: use `::<...>` instead of `<...>` to specify lifetime arguments for `Struct` | ||
| | | ||
| LL | let _ = Struct::<'_> { | ||
| | ++ | ||
|
|
||
| error: expected `while`, `for`, `loop` or `{` after a label | ||
| --> $DIR/missing-turbofish-lifetime.rs:13:22 | ||
| | | ||
| LL | let _ = Struct<'a> { | ||
| | ^ expected `while`, `for`, `loop` or `{` after a label | ||
| | | ||
| help: use `::<...>` instead of `<...>` to specify lifetime arguments for `Struct` | ||
| | | ||
| LL | let _ = Struct::<'a> { | ||
| | ++ | ||
|
|
||
| error: expected `while`, `for`, `loop` or `{` after a label | ||
| --> $DIR/missing-turbofish-lifetime.rs:20:24 | ||
| | | ||
| LL | let _ = Struct<'abc> { | ||
| | ^ expected `while`, `for`, `loop` or `{` after a label | ||
| | | ||
| help: use `::<...>` instead of `<...>` to specify lifetime arguments for `Struct` | ||
| | | ||
| LL | let _ = Struct::<'abc> { | ||
| | ++ | ||
|
|
||
| error: aborting due to 3 previous errors | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code handled three cases when building the turbofish suggestion a path aware span_suggestion, a fallback span_suggestion using just a bare lifetime span, and a final bare
.help()with no span at all. In practice only the first one is ever reachable the restriction flag that gates this whole recovery branch (IS_RHS_OF_LT_AFTER_PATH) and the path context used to build the message are always set together, in the same code path inparse_expr_assoc_rest, and aPathis guaranteed to have at least one segment. So the other two branches could never actually fire with the current call graph.I'd originally kept them as a defensive fallback in case some future change set the restriction flag from a different call site without also populating the context - but on reflection that's the wrong kind of defensiveness for a compiler internal it would silently degrade to a less informative message instead of surfacing the broken invariant. I replaced it with a single
.expect()carrying an explicit message about the invariant, so if this assumption is ever violated by future changes, it fails loudly and immediately at the exact point of violation.This also drops the now-unused
lt_spanparameter fromparse_expr_labeledentirely, which was only ever feeding the dead fallback branch.View changes since the review