Skip to content

Commit d976836

Browse files
committed
Tweak unclosed generics errors
Remove unnecessary span label for parse errors that already have a suggestion. Provide structured suggestion to close generics in more cases.
1 parent d97bb19 commit d976836

11 files changed

+48
-46
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

-9
Original file line numberDiff line numberDiff line change
@@ -670,15 +670,6 @@ impl<'a> Parser<'a> {
670670
);
671671
}
672672

673-
// Add suggestion for a missing closing angle bracket if '>' is included in expected_tokens
674-
// there are unclosed angle brackets
675-
if self.unmatched_angle_bracket_count > 0
676-
&& self.token.kind == TokenKind::Eq
677-
&& expected.iter().any(|tok| matches!(tok, TokenType::Token(TokenKind::Gt)))
678-
{
679-
err.span_label(self.prev_token.span, "maybe try to close unmatched angle bracket");
680-
}
681-
682673
let sp = if self.token == token::Eof {
683674
// This is EOF; don't want to point at the following char, but rather the last token.
684675
self.prev_token.span

compiler/rustc_parse/src/parser/generics.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,24 @@ impl<'a> Parser<'a> {
279279
let span_lo = self.token.span;
280280
let (params, span) = if self.eat_lt() {
281281
let params = self.parse_generic_params()?;
282-
self.expect_gt()?;
282+
if let Err(mut err) = self.expect_gt() {
283+
// Try to recover a `:` into a `::`
284+
// Attempt to find places where a missing `>` might belong.
285+
if let [.., GenericParam { bounds, .. }] = &params[..]
286+
&& let Some(poly) = bounds.iter().filter_map(|bound| match bound {
287+
ast::GenericBound::Trait(poly, _) => Some(poly),
288+
_ => None,
289+
}).last()
290+
{
291+
err.span_suggestion_verbose(
292+
poly.span.shrink_to_hi(),
293+
"you might have meant to end the type parameters here",
294+
">",
295+
Applicability::MaybeIncorrect,
296+
);
297+
}
298+
return Err(err);
299+
}
283300
(params, span_lo.to(self.prev_token.span))
284301
} else {
285302
(ThinVec::new(), self.prev_token.span.shrink_to_hi())

tests/ui/generic-associated-types/parse/trait-path-expected-token.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ error: expected one of `!`, `(`, `+`, `,`, `::`, `<`, or `>`, found `=`
22
--> $DIR/trait-path-expected-token.rs:5:33
33
|
44
LL | fn f1<'a>(arg : Box<dyn X<Y = B = &'a ()>>) {}
5-
| - ^ expected one of 7 possible tokens
6-
| |
7-
| maybe try to close unmatched angle bracket
5+
| ^ expected one of 7 possible tokens
86

97
error: aborting due to previous error
108

tests/ui/generic-associated-types/parse/trait-path-expressions.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ error: expected one of `,`, `:`, or `>`, found `=`
1010
--> $DIR/trait-path-expressions.rs:16:36
1111
|
1212
LL | fn f2<'a>(arg : Box<dyn X< { 1 } = 32 >>) {}
13-
| - ^ expected one of `,`, `:`, or `>`
14-
| |
15-
| maybe try to close unmatched angle bracket
13+
| ^ expected one of `,`, `:`, or `>`
1614
|
1715
help: you might have meant to end the type parameters here
1816
|

tests/ui/generic-associated-types/parse/trait-path-missing-gen_arg.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ error: expected one of `>`, a const expression, lifetime, or type, found `=`
88
--> $DIR/trait-path-missing-gen_arg.rs:11:30
99
|
1010
LL | fn f1<'a>(arg : Box<dyn X< = 32 >>) {}
11-
| - ^ expected one of `>`, a const expression, lifetime, or type
12-
| |
13-
| maybe try to close unmatched angle bracket
11+
| ^ expected one of `>`, a const expression, lifetime, or type
1412

1513
error: aborting due to 2 previous errors
1614

tests/ui/generic-associated-types/parse/trait-path-segments.stderr

+3-9
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ error: expected one of `!`, `(`, `+`, `,`, `::`, `:`, `<`, or `>`, found `=`
22
--> $DIR/trait-path-segments.rs:6:36
33
|
44
LL | fn f1<'a>(arg : Box<dyn X<X::Y = u32>>) {}
5-
| - ^ expected one of 8 possible tokens
6-
| |
7-
| maybe try to close unmatched angle bracket
5+
| ^ expected one of 8 possible tokens
86
|
97
help: you might have meant to end the type parameters here
108
|
@@ -15,9 +13,7 @@ error: expected one of `,`, `::`, `:`, or `>`, found `=`
1513
--> $DIR/trait-path-segments.rs:17:35
1614
|
1715
LL | impl<T : X<<Self as X>::Y<'a> = &'a u32>> Z for T {}
18-
| - ^ expected one of `,`, `::`, `:`, or `>`
19-
| |
20-
| maybe try to close unmatched angle bracket
16+
| ^ expected one of `,`, `::`, `:`, or `>`
2117
|
2218
help: you might have meant to end the type parameters here
2319
|
@@ -28,9 +24,7 @@ error: expected one of `!`, `+`, `,`, `::`, `:`, or `>`, found `=`
2824
--> $DIR/trait-path-segments.rs:28:25
2925
|
3026
LL | impl<T : X<X::Y<'a> = &'a u32>> Z for T {}
31-
| - ^ expected one of `!`, `+`, `,`, `::`, `:`, or `>`
32-
| |
33-
| maybe try to close unmatched angle bracket
27+
| ^ expected one of `!`, `+`, `,`, `::`, `:`, or `>`
3428
|
3529
help: you might have meant to end the type parameters here
3630
|

tests/ui/generic-associated-types/parse/trait-path-types.stderr

+3-9
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ error: expected one of `,`, `:`, or `>`, found `=`
22
--> $DIR/trait-path-types.rs:6:37
33
|
44
LL | fn f<'a>(arg : Box<dyn X< [u8; 1] = u32>>) {}
5-
| - ^ expected one of `,`, `:`, or `>`
6-
| |
7-
| maybe try to close unmatched angle bracket
5+
| ^ expected one of `,`, `:`, or `>`
86
|
97
help: you might have meant to end the type parameters here
108
|
@@ -15,9 +13,7 @@ error: expected one of `,`, `:`, or `>`, found `=`
1513
--> $DIR/trait-path-types.rs:11:37
1614
|
1715
LL | fn f1<'a>(arg : Box<dyn X<(Y<'a>) = &'a ()>>) {}
18-
| - ^ expected one of `,`, `:`, or `>`
19-
| |
20-
| maybe try to close unmatched angle bracket
16+
| ^ expected one of `,`, `:`, or `>`
2117
|
2218
help: you might have meant to end the type parameters here
2319
|
@@ -28,9 +24,7 @@ error: expected one of `,`, `:`, or `>`, found `=`
2824
--> $DIR/trait-path-types.rs:16:33
2925
|
3026
LL | fn f1<'a>(arg : Box<dyn X< 'a = u32 >>) {}
31-
| -- ^ expected one of `,`, `:`, or `>`
32-
| |
33-
| maybe try to close unmatched angle bracket
27+
| ^ expected one of `,`, `:`, or `>`
3428
|
3529
help: you might have meant to end the type parameters here
3630
|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
impl<S: Into<std::borrow::Cow<'static, str>> From<S> for Canonical {} //~ ERROR expected
2+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: expected one of `+`, `,`, `::`, `=`, or `>`, found `From`
2+
--> $DIR/unclosed-generics-in-impl-def.rs:1:46
3+
|
4+
LL | impl<S: Into<std::borrow::Cow<'static, str>> From<S> for Canonical {}
5+
| ^^^^ expected one of `+`, `,`, `::`, `=`, or `>`
6+
|
7+
help: you might have meant to end the type parameters here
8+
|
9+
LL | impl<S: Into<std::borrow::Cow<'static, str>>> From<S> for Canonical {}
10+
| +
11+
12+
error: aborting due to previous error
13+

tests/ui/issues/issue-34334.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ error: expected one of `,`, `:`, or `>`, found `=`
22
--> $DIR/issue-34334.rs:2:29
33
|
44
LL | let sr: Vec<(u32, _, _) = vec![];
5-
| -- - ^ expected one of `,`, `:`, or `>`
6-
| | |
7-
| | maybe try to close unmatched angle bracket
5+
| -- ^ expected one of `,`, `:`, or `>`
6+
| |
87
| while parsing the type for `sr`
98
|
109
help: you might have meant to end the type parameters here

tests/ui/parser/missing-closing-angle-bracket-eq-constraint.stderr

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ error: expected one of `,`, `:`, or `>`, found `=`
22
--> $DIR/missing-closing-angle-bracket-eq-constraint.rs:7:23
33
|
44
LL | let v : Vec<(u32,_) = vec![];
5-
| - - ^ expected one of `,`, `:`, or `>`
6-
| | |
7-
| | maybe try to close unmatched angle bracket
5+
| - ^ expected one of `,`, `:`, or `>`
6+
| |
87
| while parsing the type for `v`
98
|
109
help: you might have meant to end the type parameters here
@@ -29,9 +28,8 @@ error: expected one of `,`, `:`, or `>`, found `=`
2928
--> $DIR/missing-closing-angle-bracket-eq-constraint.rs:18:18
3029
|
3130
LL | let v : Vec<'a = vec![];
32-
| - -- ^ expected one of `,`, `:`, or `>`
33-
| | |
34-
| | maybe try to close unmatched angle bracket
31+
| - ^ expected one of `,`, `:`, or `>`
32+
| |
3533
| while parsing the type for `v`
3634
|
3735
help: you might have meant to end the type parameters here

0 commit comments

Comments
 (0)