Skip to content

Commit 6f0b237

Browse files
authored
Rollup merge of #128376 - compiler-errors:finish-ur-vegetables, r=jieyouxu
Mark `Parser::eat`/`check` methods as `#[must_use]` These methods return a `bool`, but we probably should either use these values or explicitly throw them away (e.g. when we just want to unconditionally eat a token if it exists). I changed a few places from `eat` to `expect`, but otherwise I tried to leave a comment explaining why the `eat` was okay. This also adds a test for the `pattern_type!` macro, which used to silently accept a missing `is` token.
2 parents 894db1f + 4776ac0 commit 6f0b237

File tree

10 files changed

+51
-18
lines changed

10 files changed

+51
-18
lines changed

compiler/rustc_builtin_macros/src/pattern_type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn parse_pat_ty<'a>(cx: &mut ExtCtxt<'a>, stream: TokenStream) -> PResult<'a, (P
2424
let mut parser = cx.new_parser_from_tts(stream);
2525

2626
let ty = parser.parse_ty()?;
27-
parser.eat_keyword(sym::is);
27+
parser.expect_keyword(sym::is)?;
2828
let pat = parser.parse_pat_no_top_alt(None, None)?;
2929

3030
Ok((ty, pat))

compiler/rustc_parse/src/parser/expr.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -3153,7 +3153,8 @@ impl<'a> Parser<'a> {
31533153

31543154
if !require_comma {
31553155
arm_body = Some(expr);
3156-
this.eat(&token::Comma);
3156+
// Eat a comma if it exists, though.
3157+
let _ = this.eat(&token::Comma);
31573158
Ok(Recovered::No)
31583159
} else if let Some((span, guar)) =
31593160
this.parse_arm_body_missing_braces(&expr, arrow_span)
@@ -3654,7 +3655,7 @@ impl<'a> Parser<'a> {
36543655
fields.push(f);
36553656
}
36563657
self.recover_stmt_(SemiColonMode::Comma, BlockMode::Ignore);
3657-
self.eat(&token::Comma);
3658+
let _ = self.eat(&token::Comma);
36583659
}
36593660
}
36603661
}

compiler/rustc_parse/src/parser/generics.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ impl<'a> Parser<'a> {
178178
span: this.prev_token.span,
179179
});
180180

181-
this.eat(&token::Comma);
181+
// Eat a trailing comma, if it exists.
182+
let _ = this.eat(&token::Comma);
182183
}
183184

184185
let param = if this.check_lifetime() {

compiler/rustc_parse/src/parser/item.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1192,13 +1192,14 @@ impl<'a> Parser<'a> {
11921192
mut safety: Safety,
11931193
) -> PResult<'a, ItemInfo> {
11941194
let abi = self.parse_abi(); // ABI?
1195+
// FIXME: This recovery should be tested better.
11951196
if safety == Safety::Default
11961197
&& self.token.is_keyword(kw::Unsafe)
11971198
&& self.look_ahead(1, |t| t.kind == token::OpenDelim(Delimiter::Brace))
11981199
{
11991200
self.expect(&token::OpenDelim(Delimiter::Brace)).unwrap_err().emit();
12001201
safety = Safety::Unsafe(self.token.span);
1201-
self.eat_keyword(kw::Unsafe);
1202+
let _ = self.eat_keyword(kw::Unsafe);
12021203
}
12031204
let module = ast::ForeignMod {
12041205
safety,
@@ -1759,7 +1760,7 @@ impl<'a> Parser<'a> {
17591760
}
17601761
}
17611762
}
1762-
self.eat(&token::CloseDelim(Delimiter::Brace));
1763+
self.expect(&token::CloseDelim(Delimiter::Brace))?;
17631764
} else {
17641765
let token_str = super::token_descr(&self.token);
17651766
let where_str = if parsed_where { "" } else { "`where`, or " };
@@ -1902,7 +1903,7 @@ impl<'a> Parser<'a> {
19021903
if let Some(_guar) = guar {
19031904
// Handle a case like `Vec<u8>>,` where we can continue parsing fields
19041905
// after the comma
1905-
self.eat(&token::Comma);
1906+
let _ = self.eat(&token::Comma);
19061907

19071908
// `check_trailing_angle_brackets` already emitted a nicer error, as
19081909
// proven by the presence of `_guar`. We can continue parsing.

compiler/rustc_parse/src/parser/mod.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ impl<'a> Parser<'a> {
547547
}
548548

549549
#[inline]
550+
#[must_use]
550551
fn check_noexpect(&self, tok: &TokenKind) -> bool {
551552
self.token == *tok
552553
}
@@ -556,6 +557,7 @@ impl<'a> Parser<'a> {
556557
/// the main purpose of this function is to reduce the cluttering of the suggestions list
557558
/// which using the normal eat method could introduce in some cases.
558559
#[inline]
560+
#[must_use]
559561
fn eat_noexpect(&mut self, tok: &TokenKind) -> bool {
560562
let is_present = self.check_noexpect(tok);
561563
if is_present {
@@ -566,6 +568,7 @@ impl<'a> Parser<'a> {
566568

567569
/// Consumes a token 'tok' if it exists. Returns whether the given token was present.
568570
#[inline]
571+
#[must_use]
569572
pub fn eat(&mut self, tok: &TokenKind) -> bool {
570573
let is_present = self.check(tok);
571574
if is_present {
@@ -577,12 +580,14 @@ impl<'a> Parser<'a> {
577580
/// If the next token is the given keyword, returns `true` without eating it.
578581
/// An expectation is also added for diagnostics purposes.
579582
#[inline]
583+
#[must_use]
580584
fn check_keyword(&mut self, kw: Symbol) -> bool {
581585
self.expected_tokens.push(TokenType::Keyword(kw));
582586
self.token.is_keyword(kw)
583587
}
584588

585589
#[inline]
590+
#[must_use]
586591
fn check_keyword_case(&mut self, kw: Symbol, case: Case) -> bool {
587592
if self.check_keyword(kw) {
588593
return true;
@@ -602,6 +607,7 @@ impl<'a> Parser<'a> {
602607
/// Otherwise, returns `false`. An expectation is also added for diagnostics purposes.
603608
// Public for rustc_builtin_macros and rustfmt usage.
604609
#[inline]
610+
#[must_use]
605611
pub fn eat_keyword(&mut self, kw: Symbol) -> bool {
606612
if self.check_keyword(kw) {
607613
self.bump();
@@ -615,6 +621,7 @@ impl<'a> Parser<'a> {
615621
/// If the case differs (and is ignored) an error is issued.
616622
/// This is useful for recovery.
617623
#[inline]
624+
#[must_use]
618625
fn eat_keyword_case(&mut self, kw: Symbol, case: Case) -> bool {
619626
if self.eat_keyword(kw) {
620627
return true;
@@ -636,6 +643,7 @@ impl<'a> Parser<'a> {
636643
/// Otherwise, returns `false`. No expectation is added.
637644
// Public for rustc_builtin_macros usage.
638645
#[inline]
646+
#[must_use]
639647
pub fn eat_keyword_noexpect(&mut self, kw: Symbol) -> bool {
640648
if self.token.is_keyword(kw) {
641649
self.bump();
@@ -648,7 +656,7 @@ impl<'a> Parser<'a> {
648656
/// If the given word is not a keyword, signals an error.
649657
/// If the next token is not the given word, signals an error.
650658
/// Otherwise, eats it.
651-
fn expect_keyword(&mut self, kw: Symbol) -> PResult<'a, ()> {
659+
pub fn expect_keyword(&mut self, kw: Symbol) -> PResult<'a, ()> {
652660
if !self.eat_keyword(kw) { self.unexpected() } else { Ok(()) }
653661
}
654662

@@ -1025,8 +1033,11 @@ impl<'a> Parser<'a> {
10251033
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
10261034
) -> PResult<'a, (ThinVec<T>, Trailing)> {
10271035
let (val, trailing, recovered) = self.parse_seq_to_before_end(ket, sep, f)?;
1028-
if matches!(recovered, Recovered::No) {
1029-
self.eat(ket);
1036+
if matches!(recovered, Recovered::No) && !self.eat(ket) {
1037+
self.dcx().span_delayed_bug(
1038+
self.token.span,
1039+
"recovered but `parse_seq_to_before_end` did not give us the ket token",
1040+
);
10301041
}
10311042
Ok((val, trailing))
10321043
}
@@ -1250,7 +1261,7 @@ impl<'a> Parser<'a> {
12501261
if pat {
12511262
self.psess.gated_spans.gate(sym::inline_const_pat, span);
12521263
}
1253-
self.eat_keyword(kw::Const);
1264+
self.expect_keyword(kw::Const)?;
12541265
let (attrs, blk) = self.parse_inner_attrs_and_block()?;
12551266
let anon_const = AnonConst {
12561267
id: DUMMY_NODE_ID,

compiler/rustc_parse/src/parser/path.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ impl<'a> Parser<'a> {
313313
}
314314

315315
// Generic arguments are found - `<`, `(`, `::<` or `::(`.
316-
self.eat(&token::PathSep);
316+
// First, eat `::` if it exists.
317+
let _ = self.eat(&token::PathSep);
317318
let lo = self.token.span;
318319
let args = if self.eat_lt() {
319320
// `<'a, T, A = U>`

src/tools/rustfmt/src/parse/macros/lazy_static.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,17 @@ pub(crate) fn parse_lazy_static(
3333
}
3434
while parser.token.kind != TokenKind::Eof {
3535
// Parse a `lazy_static!` item.
36+
// FIXME: These `eat_*` calls should be converted to `parse_or` to avoid
37+
// silently formatting malformed lazy-statics.
3638
let vis = parse_or!(parse_visibility, rustc_parse::parser::FollowedByType::No);
37-
parser.eat_keyword(kw::Static);
38-
parser.eat_keyword(kw::Ref);
39+
let _ = parser.eat_keyword(kw::Static);
40+
let _ = parser.eat_keyword(kw::Ref);
3941
let id = parse_or!(parse_ident);
40-
parser.eat(&TokenKind::Colon);
42+
let _ = parser.eat(&TokenKind::Colon);
4143
let ty = parse_or!(parse_ty);
42-
parser.eat(&TokenKind::Eq);
44+
let _ = parser.eat(&TokenKind::Eq);
4345
let expr = parse_or!(parse_expr);
44-
parser.eat(&TokenKind::Semi);
46+
let _ = parser.eat(&TokenKind::Semi);
4547
result.push((vis, id, ty, expr));
4648
}
4749

tests/crashes/123809.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
//@ known-bug: #123809
2-
type Positive = std::pat::pattern_type!(std::pat:: is 0..);
2+
type Positive = std::pat::pattern_type!(std::pat is 0..);
33

44
pub fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![feature(core_pattern_type, core_pattern_types)]
2+
3+
use std::pat::pattern_type;
4+
5+
fn main() {
6+
let x: pattern_type!(i32 0..1);
7+
//~^ ERROR expected one of `!`, `(`, `+`, `::`, `<`, or `is`, found `0`
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: expected one of `!`, `(`, `+`, `::`, `<`, or `is`, found `0`
2+
--> $DIR/missing-is.rs:6:30
3+
|
4+
LL | let x: pattern_type!(i32 0..1);
5+
| ^ expected one of `!`, `(`, `+`, `::`, `<`, or `is`
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)