@@ -8,6 +8,7 @@ use rustc_lexer::unescape::{
88use rustc_span:: { Span , Symbol , kw, sym} ;
99use tracing:: debug;
1010
11+ use crate :: Sign ;
1112use crate :: ast:: { self , LitKind , MetaItemLit , StrStyle } ;
1213use crate :: token:: { self , Token } ;
1314
@@ -44,24 +45,21 @@ pub enum LitError {
4445impl LitKind {
4546 /// Converts literal token into a semantic literal.
4647 pub fn from_token_lit ( lit : token:: Lit ) -> Result < LitKind , LitError > {
47- Self :: from_token_lit_maybe_negated ( lit, false )
48+ Self :: from_token_lit_maybe_negated ( lit, Sign :: None )
4849 }
4950
5051 /// Converts literal token into a semantic literal.
5152 /// May optionally include a sign, and will error if a literal
5253 /// other than integer or float literals is negated.
53- pub fn from_token_lit_maybe_negated (
54- lit : token:: Lit ,
55- negated : bool ,
56- ) -> Result < LitKind , LitError > {
54+ pub fn from_token_lit_maybe_negated ( lit : token:: Lit , sign : Sign ) -> Result < LitKind , LitError > {
5755 let token:: Lit { kind, symbol, suffix } = lit;
5856 if let Some ( suffix) = suffix
5957 && !kind. may_have_suffix ( )
6058 {
6159 return Err ( LitError :: InvalidSuffix ( suffix) ) ;
6260 }
6361
64- if negated && !matches ! ( kind, token:: Integer | token:: Float ) {
62+ if sign . is_neg ( ) && !matches ! ( kind, token:: Integer | token:: Float ) {
6563 return Err ( LitError :: InvalidNegation ( kind) ) ;
6664 }
6765
@@ -86,8 +84,8 @@ impl LitKind {
8684
8785 // There are some valid suffixes for integer and float literals,
8886 // so all the handling is done internally.
89- token:: Integer => return integer_lit ( symbol, suffix, negated ) ,
90- token:: Float => return float_lit ( symbol, suffix, negated ) ,
87+ token:: Integer => return integer_lit ( symbol, suffix, sign ) ,
88+ token:: Float => return float_lit ( symbol, suffix, sign ) ,
9189
9290 token:: Str => {
9391 // If there are no characters requiring special treatment we can
@@ -206,14 +204,14 @@ impl fmt::Display for LitKind {
206204 }
207205 LitKind :: Int ( n, ty) => match ty {
208206 ast:: LitIntType :: Unsigned ( ty) => write ! ( f, "{n}{}" , ty. name( ) ) ?,
209- ast:: LitIntType :: Signed ( ty, negated ) => {
210- if negated {
207+ ast:: LitIntType :: Signed ( ty, sign ) => {
208+ if sign . is_neg ( ) {
211209 write ! ( f, "-" ) ?;
212210 }
213211 write ! ( f, "{n}{}" , ty. name( ) ) ?
214212 }
215- ast:: LitIntType :: Unsuffixed ( negated ) => {
216- if negated {
213+ ast:: LitIntType :: Unsuffixed ( sign ) => {
214+ if sign . is_neg ( ) {
217215 write ! ( f, "-" ) ?;
218216 }
219217 write ! ( f, "{n}" ) ?;
@@ -291,13 +289,14 @@ fn filtered_float_lit(
291289 symbol : Symbol ,
292290 suffix : Option < Symbol > ,
293291 base : u32 ,
294- negated : bool ,
292+ sign : Sign ,
295293) -> Result < LitKind , LitError > {
296- debug ! ( ?symbol, ?suffix, ?base, ?negated ) ;
294+ debug ! ( ?symbol, ?suffix, ?base, ?sign ) ;
297295 if base != 10 {
298296 return Err ( LitError :: NonDecimalFloat ( base) ) ;
299297 }
300- let symbol = if negated { Symbol :: intern ( & format ! ( "-{}" , symbol. as_str( ) ) ) } else { symbol } ;
298+ let symbol =
299+ if sign. is_neg ( ) { Symbol :: intern ( & format ! ( "-{}" , symbol. as_str( ) ) ) } else { symbol } ;
301300 Ok ( match suffix {
302301 Some ( suffix) => LitKind :: Float (
303302 symbol,
@@ -313,12 +312,12 @@ fn filtered_float_lit(
313312 } )
314313}
315314
316- fn float_lit ( symbol : Symbol , suffix : Option < Symbol > , negated : bool ) -> Result < LitKind , LitError > {
315+ fn float_lit ( symbol : Symbol , suffix : Option < Symbol > , sign : Sign ) -> Result < LitKind , LitError > {
317316 debug ! ( "float_lit: {:?}, {:?}" , symbol, suffix) ;
318- filtered_float_lit ( strip_underscores ( symbol) , suffix, 10 , negated )
317+ filtered_float_lit ( strip_underscores ( symbol) , suffix, 10 , sign )
319318}
320319
321- fn integer_lit ( symbol : Symbol , suffix : Option < Symbol > , negated : bool ) -> Result < LitKind , LitError > {
320+ fn integer_lit ( symbol : Symbol , suffix : Option < Symbol > , sign : Sign ) -> Result < LitKind , LitError > {
322321 debug ! ( "integer_lit: {:?}, {:?}" , symbol, suffix) ;
323322 let symbol = strip_underscores ( symbol) ;
324323 let s = symbol. as_str ( ) ;
@@ -332,12 +331,12 @@ fn integer_lit(symbol: Symbol, suffix: Option<Symbol>, negated: bool) -> Result<
332331
333332 let ty = match suffix {
334333 Some ( suf) => match suf {
335- sym:: isize => ast:: LitIntType :: Signed ( ast:: IntTy :: Isize , negated ) ,
336- sym:: i8 => ast:: LitIntType :: Signed ( ast:: IntTy :: I8 , negated ) ,
337- sym:: i16 => ast:: LitIntType :: Signed ( ast:: IntTy :: I16 , negated ) ,
338- sym:: i32 => ast:: LitIntType :: Signed ( ast:: IntTy :: I32 , negated ) ,
339- sym:: i64 => ast:: LitIntType :: Signed ( ast:: IntTy :: I64 , negated ) ,
340- sym:: i128 => ast:: LitIntType :: Signed ( ast:: IntTy :: I128 , negated ) ,
334+ sym:: isize => ast:: LitIntType :: Signed ( ast:: IntTy :: Isize , sign ) ,
335+ sym:: i8 => ast:: LitIntType :: Signed ( ast:: IntTy :: I8 , sign ) ,
336+ sym:: i16 => ast:: LitIntType :: Signed ( ast:: IntTy :: I16 , sign ) ,
337+ sym:: i32 => ast:: LitIntType :: Signed ( ast:: IntTy :: I32 , sign ) ,
338+ sym:: i64 => ast:: LitIntType :: Signed ( ast:: IntTy :: I64 , sign ) ,
339+ sym:: i128 => ast:: LitIntType :: Signed ( ast:: IntTy :: I128 , sign ) ,
341340 sym:: usize => ast:: LitIntType :: Unsigned ( ast:: UintTy :: Usize ) ,
342341 sym:: u8 => ast:: LitIntType :: Unsigned ( ast:: UintTy :: U8 ) ,
343342 sym:: u16 => ast:: LitIntType :: Unsigned ( ast:: UintTy :: U16 ) ,
@@ -347,14 +346,14 @@ fn integer_lit(symbol: Symbol, suffix: Option<Symbol>, negated: bool) -> Result<
347346 // `1f64` and `2f32` etc. are valid float literals, and
348347 // `fxxx` looks more like an invalid float literal than invalid integer literal.
349348 _ if suf. as_str ( ) . starts_with ( 'f' ) => {
350- return filtered_float_lit ( symbol, suffix, base, negated ) ;
349+ return filtered_float_lit ( symbol, suffix, base, sign ) ;
351350 }
352351 _ => return Err ( LitError :: InvalidIntSuffix ( suf) ) ,
353352 } ,
354- _ => ast:: LitIntType :: Unsuffixed ( negated ) ,
353+ _ => ast:: LitIntType :: Unsuffixed ( sign ) ,
355354 } ;
356355 if let ast:: LitIntType :: Unsigned ( _) = ty
357- && negated
356+ && sign . is_neg ( )
358357 {
359358 return Err ( LitError :: InvalidNegation ( token:: Integer ) ) ;
360359 }
0 commit comments