File tree Expand file tree Collapse file tree 4 files changed +15
-7
lines changed Expand file tree Collapse file tree 4 files changed +15
-7
lines changed Original file line number Diff line number Diff line change @@ -104,7 +104,7 @@ impl LitKind {
104
104
105
105
Ok ( match kind {
106
106
token:: Bool => {
107
- assert ! ( symbol == kw :: True || symbol == kw :: False ) ;
107
+ assert ! ( symbol. is_bool_lit ( ) ) ;
108
108
LitKind :: Bool ( symbol == kw:: True )
109
109
}
110
110
token:: Byte => return unescape_byte ( & symbol. as_str ( ) )
@@ -261,7 +261,7 @@ impl Lit {
261
261
/// Converts arbitrary token into an AST literal.
262
262
crate fn from_token ( token : & Token ) -> Result < Lit , LitError > {
263
263
let lit = match token. kind {
264
- token:: Ident ( name, false ) if name == kw :: True || name == kw :: False =>
264
+ token:: Ident ( name, false ) if name. is_bool_lit ( ) =>
265
265
token:: Lit :: new ( token:: Bool , name, None ) ,
266
266
token:: Literal ( lit) =>
267
267
lit,
Original file line number Diff line number Diff line change @@ -423,7 +423,7 @@ impl<'a> Parser<'a> {
423
423
// FIXME(const_generics): to distinguish between idents for types and consts,
424
424
// we should introduce a GenericArg::Ident in the AST and distinguish when
425
425
// lowering to the HIR. For now, idents for const args are not permitted.
426
- if self . token . is_keyword ( kw :: True ) || self . token . is_keyword ( kw :: False ) {
426
+ if self . token . is_bool_lit ( ) {
427
427
self . parse_literal_maybe_minus ( ) ?
428
428
} else {
429
429
return Err (
Original file line number Diff line number Diff line change @@ -417,10 +417,8 @@ impl Token {
417
417
/// for example a '-42', or one of the boolean idents).
418
418
crate fn can_begin_literal_or_bool ( & self ) -> bool {
419
419
match self . kind {
420
- Literal ( ..) => true ,
421
- BinOp ( Minus ) => true ,
422
- Ident ( name, false ) if name == kw:: True => true ,
423
- Ident ( name, false ) if name == kw:: False => true ,
420
+ Literal ( ..) | BinOp ( Minus ) => true ,
421
+ Ident ( name, false ) if name. is_bool_lit ( ) => true ,
424
422
Interpolated ( ref nt) => match * * nt {
425
423
NtLiteral ( ..) => true ,
426
424
_ => false ,
@@ -537,6 +535,11 @@ impl Token {
537
535
self . is_non_raw_ident_where ( ast:: Ident :: is_reserved)
538
536
}
539
537
538
+ /// Returns `true` if the token is the identifier `true` or `false`.
539
+ crate fn is_bool_lit ( & self ) -> bool {
540
+ self . is_non_raw_ident_where ( |id| id. name . is_bool_lit ( ) )
541
+ }
542
+
540
543
/// Returns `true` if the token is a non-raw identifier for which `pred` holds.
541
544
fn is_non_raw_ident_where ( & self , pred : impl FnOnce ( ast:: Ident ) -> bool ) -> bool {
542
545
match self . ident ( ) {
Original file line number Diff line number Diff line change @@ -1063,6 +1063,11 @@ impl Symbol {
1063
1063
self == kw:: DollarCrate
1064
1064
}
1065
1065
1066
+ /// Returns `true` if the symbol is `true` or `false`.
1067
+ pub fn is_bool_lit ( self ) -> bool {
1068
+ self == kw:: True || self == kw:: False
1069
+ }
1070
+
1066
1071
/// This symbol can be a raw identifier.
1067
1072
pub fn can_be_raw ( self ) -> bool {
1068
1073
self != kw:: Invalid && self != kw:: Underscore && !self . is_path_segment_keyword ( )
You can’t perform that action at this time.
0 commit comments