Skip to content

Commit 693fba1

Browse files
Make parse_seq_to_before_tokens take expected/nonexpected tokens, use in parse_precise_capturing_syntax
1 parent 7ebd2bd commit 693fba1

File tree

8 files changed

+38
-51
lines changed

8 files changed

+38
-51
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use super::pat::Expected;
22
use super::{
3-
BlockMode, CommaRecoveryMode, Parser, PathStyle, Restrictions, SemiColonMode, SeqSep,
4-
TokenExpectType, TokenType,
3+
BlockMode, CommaRecoveryMode, Parser, PathStyle, Restrictions, SemiColonMode, SeqSep, TokenType,
54
};
65
use crate::errors::{
76
AmbiguousPlus, AsyncMoveBlockIn2015, AttributeOnParamType, BadQPathStage2, BadTypePlus,
@@ -1045,9 +1044,7 @@ impl<'a> Parser<'a> {
10451044
/// passes through any errors encountered. Used for error recovery.
10461045
pub(super) fn eat_to_tokens(&mut self, kets: &[&TokenKind]) {
10471046
if let Err(err) =
1048-
self.parse_seq_to_before_tokens(kets, SeqSep::none(), TokenExpectType::Expect, |p| {
1049-
Ok(p.parse_token_tree())
1050-
})
1047+
self.parse_seq_to_before_tokens(kets, &[], SeqSep::none(), |p| Ok(p.parse_token_tree()))
10511048
{
10521049
err.cancel();
10531050
}

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::pat::{CommaRecoveryMode, Expected, RecoverColon, RecoverComma};
44
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
55
use super::{
66
AttrWrapper, BlockMode, ClosureSpans, ForceCollect, Parser, PathStyle, Restrictions,
7-
SemiColonMode, SeqSep, TokenExpectType, TokenType, Trailing, TrailingToken,
7+
SemiColonMode, SeqSep, TokenType, Trailing, TrailingToken,
88
};
99

1010
use crate::errors;
@@ -2468,9 +2468,9 @@ impl<'a> Parser<'a> {
24682468
self.expect(&token::BinOp(token::Or))?;
24692469
let args = self
24702470
.parse_seq_to_before_tokens(
2471-
&[&token::BinOp(token::Or), &token::OrOr],
2471+
&[&token::BinOp(token::Or)],
2472+
&[&token::OrOr],
24722473
SeqSep::trailing_allowed(token::Comma),
2473-
TokenExpectType::NoExpect,
24742474
|p| p.parse_fn_block_param(),
24752475
)?
24762476
.0;

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -336,18 +336,6 @@ impl TokenType {
336336
}
337337
}
338338

339-
/// Used by [`Parser::expect_any_with_type`].
340-
#[derive(Copy, Clone, Debug)]
341-
enum TokenExpectType {
342-
/// Unencountered tokens are inserted into [`Parser::expected_tokens`].
343-
/// See [`Parser::check`].
344-
Expect,
345-
346-
/// Unencountered tokens are not inserted into [`Parser::expected_tokens`].
347-
/// See [`Parser::check_noexpect`].
348-
NoExpect,
349-
}
350-
351339
/// A sequence separator.
352340
#[derive(Debug)]
353341
struct SeqSep {
@@ -808,29 +796,31 @@ impl<'a> Parser<'a> {
808796
}
809797

810798
/// Checks if the next token is contained within `kets`, and returns `true` if so.
811-
fn expect_any_with_type(&mut self, kets: &[&TokenKind], expect: TokenExpectType) -> bool {
812-
kets.iter().any(|k| match expect {
813-
TokenExpectType::Expect => self.check(k),
814-
TokenExpectType::NoExpect => self.check_noexpect(k),
815-
})
799+
fn expect_any_with_type(
800+
&mut self,
801+
kets_expected: &[&TokenKind],
802+
kets_not_expected: &[&TokenKind],
803+
) -> bool {
804+
kets_expected.iter().any(|k| self.check(k))
805+
|| kets_not_expected.iter().any(|k| self.check_noexpect(k))
816806
}
817807

818808
/// Parses a sequence until the specified delimiters. The function
819809
/// `f` must consume tokens until reaching the next separator or
820810
/// closing bracket.
821811
fn parse_seq_to_before_tokens<T>(
822812
&mut self,
823-
kets: &[&TokenKind],
813+
kets_expected: &[&TokenKind],
814+
kets_not_expected: &[&TokenKind],
824815
sep: SeqSep,
825-
expect: TokenExpectType,
826816
mut f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
827817
) -> PResult<'a, (ThinVec<T>, Trailing, Recovered)> {
828818
let mut first = true;
829819
let mut recovered = Recovered::No;
830820
let mut trailing = Trailing::No;
831821
let mut v = ThinVec::new();
832822

833-
while !self.expect_any_with_type(kets, expect) {
823+
while !self.expect_any_with_type(kets_expected, kets_not_expected) {
834824
if let token::CloseDelim(..) | token::Eof = self.token.kind {
835825
break;
836826
}
@@ -928,7 +918,8 @@ impl<'a> Parser<'a> {
928918
if self.token == token::Colon {
929919
// we will try to recover in `maybe_recover_struct_lit_bad_delims`
930920
return Err(expect_err);
931-
} else if let [token::CloseDelim(Delimiter::Parenthesis)] = kets
921+
} else if let [token::CloseDelim(Delimiter::Parenthesis)] =
922+
kets_expected
932923
{
933924
return Err(expect_err);
934925
} else {
@@ -941,7 +932,9 @@ impl<'a> Parser<'a> {
941932
}
942933
}
943934
}
944-
if sep.trailing_sep_allowed && self.expect_any_with_type(kets, expect) {
935+
if sep.trailing_sep_allowed
936+
&& self.expect_any_with_type(kets_expected, kets_not_expected)
937+
{
945938
trailing = Trailing::Yes;
946939
break;
947940
}
@@ -1021,7 +1014,7 @@ impl<'a> Parser<'a> {
10211014
sep: SeqSep,
10221015
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
10231016
) -> PResult<'a, (ThinVec<T>, Trailing, Recovered)> {
1024-
self.parse_seq_to_before_tokens(&[ket], sep, TokenExpectType::Expect, f)
1017+
self.parse_seq_to_before_tokens(&[ket], &[], sep, f)
10251018
}
10261019

10271020
/// Parses a sequence, including only the closing delimiter. The function

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::errors::{
99
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
1010

1111
use rustc_ast::ptr::P;
12-
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
12+
use rustc_ast::token::{self, BinOpToken, Delimiter, Token, TokenKind};
1313
use rustc_ast::util::case::Case;
1414
use rustc_ast::{
1515
self as ast, BareFnTy, BoundAsyncness, BoundConstness, BoundPolarity, FnRetTy, GenericBound,
@@ -694,9 +694,14 @@ impl<'a> Parser<'a> {
694694
&mut self,
695695
) -> PResult<'a, (ThinVec<PreciseCapturingArg>, Span)> {
696696
let lo = self.token.span;
697-
let (args, _) = self.parse_unspanned_seq(
698-
&TokenKind::Lt,
699-
&TokenKind::Gt,
697+
self.expect_lt()?;
698+
let (args, _, _) = self.parse_seq_to_before_tokens(
699+
&[&TokenKind::Gt],
700+
&[
701+
&TokenKind::Ge,
702+
&TokenKind::BinOp(BinOpToken::Shr),
703+
&TokenKind::BinOpEq(BinOpToken::Shr),
704+
],
700705
SeqSep::trailing_allowed(token::Comma),
701706
|self_| {
702707
if self_.check_keyword(kw::SelfUpper) {
@@ -717,6 +722,7 @@ impl<'a> Parser<'a> {
717722
}
718723
},
719724
)?;
725+
self.expect_gt()?;
720726
Ok((args, lo.to(self.prev_token.span)))
721727
}
722728

tests/ui/impl-trait/precise-capturing/unexpected-token.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44

55
fn hello() -> impl use<'a {}> Sized {}
66
//~^ ERROR expected one of `,` or `>`, found `{`
7-
//~| ERROR expected item, found `>`
87

98
fn main() {}

tests/ui/impl-trait/precise-capturing/unexpected-token.stderr

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,5 @@ error: expected one of `,` or `>`, found `{`
44
LL | fn hello() -> impl use<'a {}> Sized {}
55
| ^ expected one of `,` or `>`
66

7-
error: expected item, found `>`
8-
--> $DIR/unexpected-token.rs:5:29
9-
|
10-
LL | fn hello() -> impl use<'a {}> Sized {}
11-
| ^ expected item
12-
|
13-
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
14-
15-
error: aborting due to 2 previous errors
7+
error: aborting due to 1 previous error
168

tests/ui/issues/issue-66706.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fn a() {
22
[0; [|_: _ &_| ()].len()]
3-
//~^ ERROR expected `,`, found `&`
3+
//~^ ERROR expected one of `,` or `|`, found `&`
44
//~| ERROR type annotations needed
55
}
66

@@ -11,7 +11,7 @@ fn b() {
1111

1212
fn c() {
1313
[0; [|&_: _ &_| {}; 0 ].len()]
14-
//~^ ERROR expected `,`, found `&`
14+
//~^ ERROR expected one of `,` or `|`, found `&`
1515
//~| ERROR type annotations needed
1616
}
1717

tests/ui/issues/issue-66706.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: expected `,`, found `&`
1+
error: expected one of `,` or `|`, found `&`
22
--> $DIR/issue-66706.rs:2:16
33
|
44
LL | [0; [|_: _ &_| ()].len()]
5-
| -^ expected `,`
5+
| -^ expected one of `,` or `|`
66
| |
77
| help: missing `,`
88

@@ -12,11 +12,11 @@ error: expected identifier, found reserved identifier `_`
1212
LL | [0; [|f @ &ref _| {} ; 0 ].len() ];
1313
| ^ expected identifier, found reserved identifier
1414

15-
error: expected `,`, found `&`
15+
error: expected one of `,` or `|`, found `&`
1616
--> $DIR/issue-66706.rs:13:17
1717
|
1818
LL | [0; [|&_: _ &_| {}; 0 ].len()]
19-
| -^ expected `,`
19+
| -^ expected one of `,` or `|`
2020
| |
2121
| help: missing `,`
2222

0 commit comments

Comments
 (0)