Skip to content

Commit a5fb2d5

Browse files
authored
Unrolled build for rust-lang#126708
Rollup merge of rust-lang#126708 - nnethercote:minimize-can_begin_literal_maybe_minus, r=compiler-errors Minimize `can_begin_literal_maybe_minus` usage `can_begin_literal_maybe_minus` is used in a few confusing ways. This PR makes them clearer. r? ``@spastorino``
2 parents 54fcd5b + c6f7827 commit a5fb2d5

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

compiler/rustc_ast/src/token.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -558,9 +558,10 @@ impl Token {
558558
/// Returns `true` if the token can appear at the start of a const param.
559559
pub fn can_begin_const_arg(&self) -> bool {
560560
match self.kind {
561-
OpenDelim(Delimiter::Brace) => true,
561+
OpenDelim(Delimiter::Brace) | Literal(..) | BinOp(Minus) => true,
562+
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
562563
Interpolated(ref nt) => matches!(&**nt, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
563-
_ => self.can_begin_literal_maybe_minus(),
564+
_ => false,
564565
}
565566
}
566567

@@ -620,6 +621,21 @@ impl Token {
620621
}
621622
}
622623

624+
pub fn can_begin_string_literal(&self) -> bool {
625+
match self.uninterpolate().kind {
626+
Literal(..) => true,
627+
Interpolated(ref nt) => match &**nt {
628+
NtLiteral(_) => true,
629+
NtExpr(e) => match &e.kind {
630+
ast::ExprKind::Lit(_) => true,
631+
_ => false,
632+
},
633+
_ => false,
634+
},
635+
_ => false,
636+
}
637+
}
638+
623639
/// A convenience function for matching on identifiers during parsing.
624640
/// Turns interpolated identifier (`$i: ident`) or lifetime (`$l: lifetime`) token
625641
/// into the regular identifier or lifetime token it refers to,

compiler/rustc_parse/src/parser/item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,7 @@ impl<'a> Parser<'a> {
12591259
self.token.is_keyword(kw::Unsafe)
12601260
&& self.is_keyword_ahead(1, &[kw::Extern])
12611261
&& self.look_ahead(
1262-
2 + self.look_ahead(2, |t| t.can_begin_literal_maybe_minus() as usize),
1262+
2 + self.look_ahead(2, |t| t.can_begin_string_literal() as usize),
12631263
|t| t.kind == token::OpenDelim(Delimiter::Brace),
12641264
)
12651265
}
@@ -2448,7 +2448,7 @@ impl<'a> Parser<'a> {
24482448
})
24492449
// `extern ABI fn`
24502450
|| self.check_keyword_case(kw::Extern, case)
2451-
&& self.look_ahead(1, |t| t.can_begin_literal_maybe_minus())
2451+
&& self.look_ahead(1, |t| t.can_begin_string_literal())
24522452
&& (self.look_ahead(2, |t| t.is_keyword_case(kw::Fn, case)) ||
24532453
// this branch is only for better diagnostic in later, `pub` is not allowed here
24542454
(self.may_recover()

compiler/rustc_parse/src/parser/pat.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,8 @@ impl<'a> Parser<'a> {
939939
|| self.look_ahead(dist, |t| {
940940
t.is_path_start() // e.g. `MY_CONST`;
941941
|| t.kind == token::Dot // e.g. `.5` for recovery;
942-
|| t.can_begin_literal_maybe_minus() // e.g. `42`.
942+
|| matches!(t.kind, token::Literal(..) | token::BinOp(token::Minus))
943+
|| t.is_bool_lit()
943944
|| t.is_whole_expr()
944945
|| t.is_lifetime() // recover `'a` instead of `'a'`
945946
|| (self.may_recover() // recover leading `(`

0 commit comments

Comments
 (0)