Skip to content

Commit 68bd001

Browse files
Make parse_seq_to_before_tokens take expected/nonexpected tokens, use in parse_precise_capturing_syntax
1 parent 59e2c01 commit 68bd001

File tree

8 files changed

+38
-51
lines changed

8 files changed

+38
-51
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+2-5
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

+3-3
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;
@@ -2456,9 +2456,9 @@ impl<'a> Parser<'a> {
24562456
self.expect(&token::BinOp(token::Or))?;
24572457
let args = self
24582458
.parse_seq_to_before_tokens(
2459-
&[&token::BinOp(token::Or), &token::OrOr],
2459+
&[&token::BinOp(token::Or)],
2460+
&[&token::OrOr],
24602461
SeqSep::trailing_allowed(token::Comma),
2461-
TokenExpectType::NoExpect,
24622462
|p| p.parse_fn_block_param(),
24632463
)?
24642464
.0;

compiler/rustc_parse/src/parser/mod.rs

+16-23
Original file line numberDiff line numberDiff line change
@@ -335,18 +335,6 @@ impl TokenType {
335335
}
336336
}
337337

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

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

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

832-
while !self.expect_any_with_type(kets, expect) {
822+
while !self.expect_any_with_type(kets_expected, kets_not_expected) {
833823
if let token::CloseDelim(..) | token::Eof = self.token.kind {
834824
break;
835825
}
@@ -927,7 +917,8 @@ impl<'a> Parser<'a> {
927917
if self.token == token::Colon {
928918
// we will try to recover in `maybe_recover_struct_lit_bad_delims`
929919
return Err(expect_err);
930-
} else if let [token::CloseDelim(Delimiter::Parenthesis)] = kets
920+
} else if let [token::CloseDelim(Delimiter::Parenthesis)] =
921+
kets_expected
931922
{
932923
return Err(expect_err);
933924
} else {
@@ -940,7 +931,9 @@ impl<'a> Parser<'a> {
940931
}
941932
}
942933
}
943-
if sep.trailing_sep_allowed && self.expect_any_with_type(kets, expect) {
934+
if sep.trailing_sep_allowed
935+
&& self.expect_any_with_type(kets_expected, kets_not_expected)
936+
{
944937
trailing = Trailing::Yes;
945938
break;
946939
}
@@ -1020,7 +1013,7 @@ impl<'a> Parser<'a> {
10201013
sep: SeqSep,
10211014
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
10221015
) -> PResult<'a, (ThinVec<T>, Trailing, Recovered)> {
1023-
self.parse_seq_to_before_tokens(&[ket], sep, TokenExpectType::Expect, f)
1016+
self.parse_seq_to_before_tokens(&[ket], &[], sep, f)
10241017
}
10251018

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

compiler/rustc_parse/src/parser/ty.rs

+10-4
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

-1
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

+1-9
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

+2-2
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

+4-4
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)