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 {
104104
105105 Ok ( match kind {
106106 token:: Bool => {
107- assert ! ( symbol == kw :: True || symbol == kw :: False ) ;
107+ assert ! ( symbol. is_bool_lit ( ) ) ;
108108 LitKind :: Bool ( symbol == kw:: True )
109109 }
110110 token:: Byte => return unescape_byte ( & symbol. as_str ( ) )
@@ -261,7 +261,7 @@ impl Lit {
261261 /// Converts arbitrary token into an AST literal.
262262 crate fn from_token ( token : & Token ) -> Result < Lit , LitError > {
263263 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 ( ) =>
265265 token:: Lit :: new ( token:: Bool , name, None ) ,
266266 token:: Literal ( lit) =>
267267 lit,
Original file line number Diff line number Diff line change @@ -423,7 +423,7 @@ impl<'a> Parser<'a> {
423423 // FIXME(const_generics): to distinguish between idents for types and consts,
424424 // we should introduce a GenericArg::Ident in the AST and distinguish when
425425 // 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 ( ) {
427427 self . parse_literal_maybe_minus ( ) ?
428428 } else {
429429 return Err (
Original file line number Diff line number Diff line change @@ -417,10 +417,8 @@ impl Token {
417417 /// for example a '-42', or one of the boolean idents).
418418 crate fn can_begin_literal_or_bool ( & self ) -> bool {
419419 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 ,
424422 Interpolated ( ref nt) => match * * nt {
425423 NtLiteral ( ..) => true ,
426424 _ => false ,
@@ -537,6 +535,11 @@ impl Token {
537535 self . is_non_raw_ident_where ( ast:: Ident :: is_reserved)
538536 }
539537
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+
540543 /// Returns `true` if the token is a non-raw identifier for which `pred` holds.
541544 fn is_non_raw_ident_where ( & self , pred : impl FnOnce ( ast:: Ident ) -> bool ) -> bool {
542545 match self . ident ( ) {
Original file line number Diff line number Diff line change @@ -1063,6 +1063,11 @@ impl Symbol {
10631063 self == kw:: DollarCrate
10641064 }
10651065
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+
10661071 /// This symbol can be a raw identifier.
10671072 pub fn can_be_raw ( self ) -> bool {
10681073 self != kw:: Invalid && self != kw:: Underscore && !self . is_path_segment_keyword ( )
You can’t perform that action at this time.
0 commit comments