Skip to content

Commit 278bc67

Browse files
authored
Rollup merge of #139264 - freyacodes:fix/bad-turbofish-hints, r=petrochenkov
Fix two incorrect turbofish suggestions This fixes #121901 This is my contribution to Rust, and my first contribution to a language parser that I didn't write myself. I am a bit outside my depth here, so any constructive criticism is appreciated.
2 parents 43f1728 + d8d27ca commit 278bc67

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

compiler/rustc_parse/src/parser/item.rs

+25-7
Original file line numberDiff line numberDiff line change
@@ -2960,13 +2960,30 @@ impl<'a> Parser<'a> {
29602960
let parser_snapshot_before_ty = this.create_snapshot_for_diagnostic();
29612961
this.eat_incorrect_doc_comment_for_param_type();
29622962
let mut ty = this.parse_ty_for_param();
2963-
if ty.is_ok()
2964-
&& this.token != token::Comma
2965-
&& this.token != token::CloseDelim(Delimiter::Parenthesis)
2966-
{
2967-
// This wasn't actually a type, but a pattern looking like a type,
2968-
// so we are going to rollback and re-parse for recovery.
2969-
ty = this.unexpected_any();
2963+
2964+
if let Ok(t) = &ty {
2965+
// Check for trailing angle brackets
2966+
if let TyKind::Path(_, Path { segments, .. }) = &t.kind {
2967+
if let Some(segment) = segments.last() {
2968+
if let Some(guar) =
2969+
this.check_trailing_angle_brackets(segment, &[exp!(CloseParen)])
2970+
{
2971+
return Ok((
2972+
dummy_arg(segment.ident, guar),
2973+
Trailing::No,
2974+
UsePreAttrPos::No,
2975+
));
2976+
}
2977+
}
2978+
}
2979+
2980+
if this.token != token::Comma
2981+
&& this.token != token::CloseDelim(Delimiter::Parenthesis)
2982+
{
2983+
// This wasn't actually a type, but a pattern looking like a type,
2984+
// so we are going to rollback and re-parse for recovery.
2985+
ty = this.unexpected_any();
2986+
}
29702987
}
29712988
match ty {
29722989
Ok(ty) => {
@@ -2977,6 +2994,7 @@ impl<'a> Parser<'a> {
29772994
}
29782995
// If this is a C-variadic argument and we hit an error, return the error.
29792996
Err(err) if this.token == token::DotDotDot => return Err(err),
2997+
Err(err) if this.unmatched_angle_bracket_count > 0 => return Err(err),
29802998
// Recover from attempting to parse the argument as a type without pattern.
29812999
Err(err) => {
29823000
err.cancel();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Regression test for the parser wrongfully suggesting turbofish syntax in below syntax errors
2+
3+
type One = for<'a> fn(Box<dyn Send + 'a);
4+
//~^ ERROR: expected one of `+`, `,`, or `>`, found `)`
5+
type Two = for<'a> fn(Box<dyn Send + 'a>>);
6+
//~^ ERROR: unmatched angle bracket
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error: expected one of `+`, `,`, or `>`, found `)`
2+
--> $DIR/bad-turbofish-hints-issue-121901.rs:3:40
3+
|
4+
LL | type One = for<'a> fn(Box<dyn Send + 'a);
5+
| ^ expected one of `+`, `,`, or `>`
6+
|
7+
help: you might have meant to end the type parameters here
8+
|
9+
LL | type One = for<'a> fn(Box<dyn Send + 'a>);
10+
| +
11+
12+
error: unmatched angle bracket
13+
--> $DIR/bad-turbofish-hints-issue-121901.rs:5:41
14+
|
15+
LL | type Two = for<'a> fn(Box<dyn Send + 'a>>);
16+
| ^
17+
|
18+
help: remove extra angle bracket
19+
|
20+
LL - type Two = for<'a> fn(Box<dyn Send + 'a>>);
21+
LL + type Two = for<'a> fn(Box<dyn Send + 'a>);
22+
|
23+
24+
error: aborting due to 2 previous errors
25+

0 commit comments

Comments
 (0)