Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
deb3fdd
Use new check-cfg syntax in rustc_llvm build script
Urgau Nov 16, 2023
347e382
Update unexpected_cfgs lint definition with new syntax and diagnostics
Urgau Nov 16, 2023
623e99e
Remove deprecated --check-cfg names() and values() syntax
Urgau Nov 16, 2023
b80d074
Fix typo in README.md
filenine Nov 30, 2023
605381a
Capitalize ToC in README.md
filenine Nov 30, 2023
8870768
Change prefetch to avoid deadlock
andjo403 Nov 30, 2023
4e99db9
Tweak unclosed generics errors
estebank Nov 15, 2023
e6a14c0
Use default params until effects in desugaring
fee1-dead Dec 4, 2023
8fb7117
Update books
rustbot Dec 4, 2023
65212a0
Remove `#[rustc_host]`, use internal desugaring
fee1-dead Dec 4, 2023
0e3e16c
rustc_driver_impl: Address all `rustc::potential_query_instability` l…
Enselic Dec 5, 2023
ae2427d
rustc_interface: Address all `rustc::potential_query_instability` lints
Enselic Dec 5, 2023
d7d867d
rustc_symbol_mangling: Address all `rustc::potential_query_instabilit…
Enselic Dec 5, 2023
2e97bce
Rollup merge of #117922 - estebank:unclosed-generics, r=b-naber
fee1-dead Dec 5, 2023
dfa6ff7
Rollup merge of #117981 - Urgau:check-cfg-remove-deprecated-syntax, r…
fee1-dead Dec 5, 2023
790dd75
Rollup merge of #118471 - filenine:fix-typos, r=workingjubilee
fee1-dead Dec 5, 2023
519669a
Rollup merge of #118488 - andjo403:remove_deadlock, r=SparrowLii
fee1-dead Dec 5, 2023
c7fbebd
Rollup merge of #118605 - fee1-dead-contrib:rm-rustc_host, r=compiler…
fee1-dead Dec 5, 2023
c778187
Rollup merge of #118608 - fee1-dead-contrib:backdoor-in-askconv, r=co…
fee1-dead Dec 5, 2023
834a8e8
Rollup merge of #118614 - rustbot:docs-update, r=ehuss
fee1-dead Dec 5, 2023
0a896b4
Rollup merge of #118637 - Enselic:query-instability-2, r=cjgillot
fee1-dead Dec 5, 2023
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
Prev Previous commit
Next Next commit
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.
  • Loading branch information
estebank committed Dec 1, 2023
commit 4e99db9e54a2f684ed22deee48317bd147f1c360
43 changes: 34 additions & 9 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,15 +674,6 @@ impl<'a> Parser<'a> {
);
}

// Add suggestion for a missing closing angle bracket if '>' is included in expected_tokens
// there are unclosed angle brackets
if self.unmatched_angle_bracket_count > 0
&& self.token.kind == TokenKind::Eq
&& expected.iter().any(|tok| matches!(tok, TokenType::Token(TokenKind::Gt)))
{
err.span_label(self.prev_token.span, "maybe try to close unmatched angle bracket");
}

let sp = if self.token == token::Eof {
// This is EOF; don't want to point at the following char, but rather the last token.
self.prev_token.span
Expand Down Expand Up @@ -819,6 +810,7 @@ impl<'a> Parser<'a> {
}
err.emit();
}

fn check_too_many_raw_str_terminators(&mut self, err: &mut Diagnostic) -> bool {
let sm = self.sess.source_map();
match (&self.prev_token.kind, &self.token.kind) {
Expand Down Expand Up @@ -1994,6 +1986,39 @@ impl<'a> Parser<'a> {
}
}

/// When trying to close a generics list and encountering code like
/// ```text
/// impl<S: Into<std::borrow::Cow<'static, str>> From<S> for Canonical {}
/// // ^ missing > here
/// ```
/// we provide a structured suggestion on the error from `expect_gt`.
pub(super) fn expect_gt_or_maybe_suggest_closing_generics(
&mut self,
params: &[ast::GenericParam],
) -> PResult<'a, ()> {
let Err(mut err) = self.expect_gt() else {
return Ok(());
};
// Attempt to find places where a missing `>` might belong.
if let [.., ast::GenericParam { bounds, .. }] = params
&& let Some(poly) = bounds
.iter()
.filter_map(|bound| match bound {
ast::GenericBound::Trait(poly, _) => Some(poly),
_ => None,
})
.last()
{
err.span_suggestion_verbose(
poly.span.shrink_to_hi(),
"you might have meant to end the type parameters here",
">",
Applicability::MaybeIncorrect,
);
}
Err(err)
}

pub(super) fn recover_seq_parse_error(
&mut self,
delim: Delimiter,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ impl<'a> Parser<'a> {
let span_lo = self.token.span;
let (params, span) = if self.eat_lt() {
let params = self.parse_generic_params()?;
self.expect_gt()?;
self.expect_gt_or_maybe_suggest_closing_generics(&params)?;
(params, span_lo.to(self.prev_token.span))
} else {
(ThinVec::new(), self.prev_token.span.shrink_to_hi())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ error: expected one of `!`, `(`, `+`, `,`, `::`, `<`, or `>`, found `=`
--> $DIR/trait-path-expected-token.rs:5:33
|
LL | fn f1<'a>(arg : Box<dyn X<Y = B = &'a ()>>) {}
| - ^ expected one of 7 possible tokens
| |
| maybe try to close unmatched angle bracket
| ^ expected one of 7 possible tokens

error: aborting due to 1 previous error

Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ error: expected one of `,`, `:`, or `>`, found `=`
--> $DIR/trait-path-expressions.rs:16:36
|
LL | fn f2<'a>(arg : Box<dyn X< { 1 } = 32 >>) {}
| - ^ expected one of `,`, `:`, or `>`
| |
| maybe try to close unmatched angle bracket
| ^ expected one of `,`, `:`, or `>`
|
help: you might have meant to end the type parameters here
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ error: expected one of `>`, a const expression, lifetime, or type, found `=`
--> $DIR/trait-path-missing-gen_arg.rs:11:30
|
LL | fn f1<'a>(arg : Box<dyn X< = 32 >>) {}
| - ^ expected one of `>`, a const expression, lifetime, or type
| |
| maybe try to close unmatched angle bracket
| ^ expected one of `>`, a const expression, lifetime, or type

error: aborting due to 2 previous errors

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ error: expected one of `!`, `(`, `+`, `,`, `::`, `:`, `<`, or `>`, found `=`
--> $DIR/trait-path-segments.rs:6:36
|
LL | fn f1<'a>(arg : Box<dyn X<X::Y = u32>>) {}
| - ^ expected one of 8 possible tokens
| |
| maybe try to close unmatched angle bracket
| ^ expected one of 8 possible tokens
|
help: you might have meant to end the type parameters here
|
Expand All @@ -15,9 +13,7 @@ error: expected one of `,`, `::`, `:`, or `>`, found `=`
--> $DIR/trait-path-segments.rs:17:35
|
LL | impl<T : X<<Self as X>::Y<'a> = &'a u32>> Z for T {}
| - ^ expected one of `,`, `::`, `:`, or `>`
| |
| maybe try to close unmatched angle bracket
| ^ expected one of `,`, `::`, `:`, or `>`
|
help: you might have meant to end the type parameters here
|
Expand All @@ -28,9 +24,7 @@ error: expected one of `!`, `+`, `,`, `::`, `:`, or `>`, found `=`
--> $DIR/trait-path-segments.rs:28:25
|
LL | impl<T : X<X::Y<'a> = &'a u32>> Z for T {}
| - ^ expected one of `!`, `+`, `,`, `::`, `:`, or `>`
| |
| maybe try to close unmatched angle bracket
| ^ expected one of `!`, `+`, `,`, `::`, `:`, or `>`
|
help: you might have meant to end the type parameters here
|
Expand Down
12 changes: 3 additions & 9 deletions tests/ui/generic-associated-types/parse/trait-path-types.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ error: expected one of `,`, `:`, or `>`, found `=`
--> $DIR/trait-path-types.rs:6:37
|
LL | fn f<'a>(arg : Box<dyn X< [u8; 1] = u32>>) {}
| - ^ expected one of `,`, `:`, or `>`
| |
| maybe try to close unmatched angle bracket
| ^ expected one of `,`, `:`, or `>`
|
help: you might have meant to end the type parameters here
|
Expand All @@ -15,9 +13,7 @@ error: expected one of `,`, `:`, or `>`, found `=`
--> $DIR/trait-path-types.rs:11:37
|
LL | fn f1<'a>(arg : Box<dyn X<(Y<'a>) = &'a ()>>) {}
| - ^ expected one of `,`, `:`, or `>`
| |
| maybe try to close unmatched angle bracket
| ^ expected one of `,`, `:`, or `>`
|
help: you might have meant to end the type parameters here
|
Expand All @@ -28,9 +24,7 @@ error: expected one of `,`, `:`, or `>`, found `=`
--> $DIR/trait-path-types.rs:16:33
|
LL | fn f1<'a>(arg : Box<dyn X< 'a = u32 >>) {}
| -- ^ expected one of `,`, `:`, or `>`
| |
| maybe try to close unmatched angle bracket
| ^ expected one of `,`, `:`, or `>`
|
help: you might have meant to end the type parameters here
|
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/generics/unclosed-generics-in-impl-def.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
impl<S: Into<std::borrow::Cow<'static, str>> From<S> for Canonical {} //~ ERROR expected
fn main() {}
13 changes: 13 additions & 0 deletions tests/ui/generics/unclosed-generics-in-impl-def.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error: expected one of `+`, `,`, `::`, `=`, or `>`, found `From`
--> $DIR/unclosed-generics-in-impl-def.rs:1:46
|
LL | impl<S: Into<std::borrow::Cow<'static, str>> From<S> for Canonical {}
| ^^^^ expected one of `+`, `,`, `::`, `=`, or `>`
|
help: you might have meant to end the type parameters here
|
LL | impl<S: Into<std::borrow::Cow<'static, str>>> From<S> for Canonical {}
| +

error: aborting due to 1 previous error

5 changes: 2 additions & 3 deletions tests/ui/issues/issue-34334.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ error: expected one of `,`, `:`, or `>`, found `=`
--> $DIR/issue-34334.rs:2:29
|
LL | let sr: Vec<(u32, _, _) = vec![];
| -- - ^ expected one of `,`, `:`, or `>`
| | |
| | maybe try to close unmatched angle bracket
| -- ^ expected one of `,`, `:`, or `>`
| |
| while parsing the type for `sr`
|
help: you might have meant to end the type parameters here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ error: expected one of `,`, `:`, or `>`, found `=`
--> $DIR/missing-closing-angle-bracket-eq-constraint.rs:7:23
|
LL | let v : Vec<(u32,_) = vec![];
| - - ^ expected one of `,`, `:`, or `>`
| | |
| | maybe try to close unmatched angle bracket
| - ^ expected one of `,`, `:`, or `>`
| |
| while parsing the type for `v`
|
help: you might have meant to end the type parameters here
Expand All @@ -29,9 +28,8 @@ error: expected one of `,`, `:`, or `>`, found `=`
--> $DIR/missing-closing-angle-bracket-eq-constraint.rs:18:18
|
LL | let v : Vec<'a = vec![];
| - -- ^ expected one of `,`, `:`, or `>`
| | |
| | maybe try to close unmatched angle bracket
| - ^ expected one of `,`, `:`, or `>`
| |
| while parsing the type for `v`
|
help: you might have meant to end the type parameters here
Expand Down