Skip to content

Recover missing turbofish for lifetime arguments in expression position - #162749

Open
raushan728 wants to merge 4 commits into
rust-lang:mainfrom
raushan728:issues/162656
Open

raushan728 wants to merge 4 commits into
rust-lang:mainfrom
raushan728:issues/162656

Conversation

@raushan728

@raushan728 raushan728 commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Fixes #162656.

rustc already suggests ::<> when a type/const generic argument is missing its turbofish (foo<Bar>()), but not for lifetime arguments - Struct<'a> { .. } previously misfired through label/char-literal recovery, producing a confusing cascade of unrelated errors instead of one actionable turbofish suggestion.

cc @estebank

@rustbot

rustbot commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

The parser was modified, potentially altering the grammar of (stable) Rust
which would be a breaking change.

cc @fmease

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 14, 2026
@rustbot

rustbot commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

r? @chenyukang

rustbot has assigned @chenyukang.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, parser
  • compiler, parser expanded to 76 candidates
  • Random selection from 18 candidates

@estebank estebank left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to do a more in-depth review, as I have some mild concerns about increasing the size of the parser, but the results look reasonable so far. I'll look at this again later this week if the assigned reviewer doesn't manage to get the time before then.

View changes since this review

Comment on lines +1810 to +1830
if let Some((op_span, ident)) = self.expected_turbofish_context.take() {
err.help(format!(
"if you meant to specify lifetime parameters for `{ident}`, use the `::<>` turbofish"
));
err.span_suggestion(
op_span.shrink_to_lo(),
"use the `::<>` turbofish",
"::",
Applicability::MachineApplicable,
);
} else {
err.help("if you meant to specify lifetime parameters, use the `::<>` turbofish");
if let Some(lt_sp) = lt_span {
err.span_suggestion(
lt_sp.shrink_to_lo(),
"use the `::<>` turbofish",
"::",
Applicability::MachineApplicable,
);
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mild nitpicks (the help text can be the one used for the suggestion message, making the suggestion verbose and using the wording from the existing diagnostics)

Suggested change
if let Some((op_span, ident)) = self.expected_turbofish_context.take() {
err.help(format!(
"if you meant to specify lifetime parameters for `{ident}`, use the `::<>` turbofish"
));
err.span_suggestion(
op_span.shrink_to_lo(),
"use the `::<>` turbofish",
"::",
Applicability::MachineApplicable,
);
} else {
err.help("if you meant to specify lifetime parameters, use the `::<>` turbofish");
if let Some(lt_sp) = lt_span {
err.span_suggestion(
lt_sp.shrink_to_lo(),
"use the `::<>` turbofish",
"::",
Applicability::MachineApplicable,
);
}
}
if let Some((op_span, ident)) = self.expected_turbofish_context.take() {
err.span_suggestion(
op_span.shrink_to_lo(),
format!(
"use `::<...>` instead of `<...>` to specify lifetime arguments for `{ident}`",
),
"::",
Applicability::MachineApplicable,
);
} else if let Some(lt_sp) = lt_span {
err.span_suggestion(
lt_sp.shrink_to_lo(),
"use `::<...>` instead of `<...>` to specify lifetime arguments",
"::",
Applicability::MachineApplicable,
);
} else {
err.help("use `::<...>` instead of `<...>` to specify lifetime arguments");
}

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 14, 2026
@rustbot

rustbot commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Detect missing turbofish in paths in expression context when written with lifetimes Struct<'a> {}

4 participants