Skip to content

Commit c6f7827

Browse files
committed
Introduce can_begin_string_literal.
We currently use `can_begin_literal_maybe_minus` in a couple of places where only string literals are allowed. This commit introduces a more specific function, which makes things clearer. It doesn't change behaviour because the two functions affected (`is_unsafe_foreign_mod` and `check_keyword_case`) are always followed by a call to `parse_abi`, which checks again for a string literal.
1 parent 7d9a92b commit c6f7827

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

compiler/rustc_ast/src/token.rs

+15
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,21 @@ impl Token {
621621
}
622622
}
623623

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+
624639
/// A convenience function for matching on identifiers during parsing.
625640
/// Turns interpolated identifier (`$i: ident`) or lifetime (`$l: lifetime`) token
626641
/// 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()

0 commit comments

Comments
 (0)