Skip to content

Fix two incorrect turbofish suggestions #139264

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

Merged
merged 1 commit into from
Apr 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
@@ -2960,13 +2960,30 @@ impl<'a> Parser<'a> {
let parser_snapshot_before_ty = this.create_snapshot_for_diagnostic();
this.eat_incorrect_doc_comment_for_param_type();
let mut ty = this.parse_ty_for_param();
if ty.is_ok()
&& this.token != token::Comma
&& this.token != token::CloseDelim(Delimiter::Parenthesis)
{
// This wasn't actually a type, but a pattern looking like a type,
// so we are going to rollback and re-parse for recovery.
ty = this.unexpected_any();

if let Ok(t) = &ty {
// Check for trailing angle brackets
if let TyKind::Path(_, Path { segments, .. }) = &t.kind {
if let Some(segment) = segments.last() {
if let Some(guar) =
this.check_trailing_angle_brackets(segment, &[exp!(CloseParen)])
{
return Ok((
dummy_arg(segment.ident, guar),
Trailing::No,
UsePreAttrPos::No,
));
}
}
}

if this.token != token::Comma
&& this.token != token::CloseDelim(Delimiter::Parenthesis)
{
// This wasn't actually a type, but a pattern looking like a type,
// so we are going to rollback and re-parse for recovery.
ty = this.unexpected_any();
}
}
match ty {
Ok(ty) => {
@@ -2977,6 +2994,7 @@ impl<'a> Parser<'a> {
}
// If this is a C-variadic argument and we hit an error, return the error.
Err(err) if this.token == token::DotDotDot => return Err(err),
Err(err) if this.unmatched_angle_bracket_count > 0 => return Err(err),
// Recover from attempting to parse the argument as a type without pattern.
Err(err) => {
err.cancel();
8 changes: 8 additions & 0 deletions tests/ui/fn/bad-turbofish-hints-issue-121901.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Regression test for the parser wrongfully suggesting turbofish syntax in below syntax errors

type One = for<'a> fn(Box<dyn Send + 'a);
//~^ ERROR: expected one of `+`, `,`, or `>`, found `)`
type Two = for<'a> fn(Box<dyn Send + 'a>>);
//~^ ERROR: unmatched angle bracket

fn main() {}
25 changes: 25 additions & 0 deletions tests/ui/fn/bad-turbofish-hints-issue-121901.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
error: expected one of `+`, `,`, or `>`, found `)`
--> $DIR/bad-turbofish-hints-issue-121901.rs:3:40
|
LL | type One = for<'a> fn(Box<dyn Send + 'a);
| ^ expected one of `+`, `,`, or `>`
|
help: you might have meant to end the type parameters here
|
LL | type One = for<'a> fn(Box<dyn Send + 'a>);
| +

error: unmatched angle bracket
--> $DIR/bad-turbofish-hints-issue-121901.rs:5:41
|
LL | type Two = for<'a> fn(Box<dyn Send + 'a>>);
| ^
|
help: remove extra angle bracket
|
LL - type Two = for<'a> fn(Box<dyn Send + 'a>>);
LL + type Two = for<'a> fn(Box<dyn Send + 'a>);
|

error: aborting due to 2 previous errors