@@ -5,8 +5,7 @@ use crate::token::{self, Token};
55use  rustc_lexer:: unescape:: { byte_from_char,  unescape_byte,  unescape_char,  unescape_literal,  Mode } ; 
66use  rustc_span:: symbol:: { kw,  sym,  Symbol } ; 
77use  rustc_span:: Span ; 
8- use  std:: ascii; 
9- use  std:: str; 
8+ use  std:: { ascii,  fmt,  str} ; 
109
1110// Escapes a string, represented as a symbol. Reuses the original symbol, 
1211// avoiding interning, if no changes are required. 
@@ -162,54 +161,60 @@ impl LitKind {
162161            token:: Err  => LitKind :: Err , 
163162        } ) 
164163    } 
164+ } 
165165
166-     /// Synthesizes a token from a semantic literal. 
167- /// This function is used when the original token doesn't exist (e.g. the literal is created 
168- /// by an AST-based macro) or unavailable (e.g. from HIR pretty-printing). 
169- pub  fn  synthesize_token_lit ( & self )  -> token:: Lit  { 
170-         let  ( kind,  symbol,  suffix)  = match  * self  { 
171-             LitKind :: Str ( symbol,  ast:: StrStyle :: Cooked )  => { 
172-                 ( token:: Str ,  escape_string_symbol ( symbol) ,  None ) 
166+ impl  fmt:: Display  for  LitKind  { 
167+     fn  fmt ( & self ,  f :  & mut  fmt:: Formatter < ' _ > )  -> fmt:: Result  { 
168+         match  * self  { 
169+             LitKind :: Byte ( b)  => { 
170+                 let  b:  String  = ascii:: escape_default ( b) . map ( Into :: < char > :: into) . collect ( ) ; 
171+                 write ! ( f,  "b'{}'" ,  b) ?; 
173172            } 
174-             LitKind :: Str ( symbol,  ast:: StrStyle :: Raw ( n) )  => ( token:: StrRaw ( n) ,  symbol,  None ) , 
175-             LitKind :: ByteStr ( ref  bytes,  ast:: StrStyle :: Cooked )  => { 
176-                 ( token:: ByteStr ,  escape_byte_str_symbol ( bytes) ,  None ) 
173+             LitKind :: Char ( ch)  => write ! ( f,  "'{}'" ,  escape_char_symbol( ch) ) ?, 
174+             LitKind :: Str ( sym,  StrStyle :: Cooked )  => write ! ( f,  "\" {}\" " ,  escape_string_symbol( sym) ) ?, 
175+             LitKind :: Str ( sym,  StrStyle :: Raw ( n) )  => write ! ( 
176+                 f, 
177+                 "r{delim}\" {string}\" {delim}" , 
178+                 delim = "#" . repeat( n as  usize ) , 
179+                 string = sym
180+             ) ?, 
181+             LitKind :: ByteStr ( ref  bytes,  StrStyle :: Cooked )  => { 
182+                 write ! ( f,  "b\" {}\" " ,  escape_byte_str_symbol( bytes) ) ?
177183            } 
178-             LitKind :: ByteStr ( ref  bytes,  ast :: StrStyle :: Raw ( n) )  => { 
184+             LitKind :: ByteStr ( ref  bytes,  StrStyle :: Raw ( n) )  => { 
179185                // Unwrap because raw byte string literals can only contain ASCII. 
180-                 let  string = str:: from_utf8 ( bytes) . unwrap ( ) ; 
181-                 ( token:: ByteStrRaw ( n) ,  Symbol :: intern ( & string) ,  None ) 
182-             } 
183-             LitKind :: Byte ( byte)  => { 
184-                 let  string:  String  = ascii:: escape_default ( byte) . map ( Into :: < char > :: into) . collect ( ) ; 
185-                 ( token:: Byte ,  Symbol :: intern ( & string) ,  None ) 
186+                 let  symbol = str:: from_utf8 ( bytes) . unwrap ( ) ; 
187+                 write ! ( 
188+                     f, 
189+                     "br{delim}\" {string}\" {delim}" , 
190+                     delim = "#" . repeat( n as  usize ) , 
191+                     string = symbol
192+                 ) ?; 
186193            } 
187-             LitKind :: Char ( ch)  => ( token:: Char ,  escape_char_symbol ( ch) ,  None ) , 
188194            LitKind :: Int ( n,  ty)  => { 
189-                 let  suffix =  match  ty  { 
190-                     ast :: LitIntType :: Unsigned ( ty )  =>  Some ( ty . name ( ) ) , 
191-                     ast:: LitIntType :: Signed ( ty)  => Some ( ty. name ( ) ) , 
192-                     ast:: LitIntType :: Unsuffixed  => None , 
193-                 } ; 
194-                 ( token :: Integer ,  sym :: integer ( n ) ,  suffix ) 
195+                 write ! ( f ,   "{}" ,  n ) ? ; 
196+                 match  ty  { 
197+                     ast:: LitIntType :: Unsigned ( ty)  => write ! ( f ,   "{}" ,   ty. name( ) ) ? , 
198+                     ast:: LitIntType :: Signed ( ty )  => write ! ( f ,   "{}" ,  ty . name ( ) ) ? , 
199+                     ast :: LitIntType :: Unsuffixed  =>  { } 
200+                 } 
195201            } 
196202            LitKind :: Float ( symbol,  ty)  => { 
197-                 let  suffix =  match  ty  { 
198-                     ast :: LitFloatType :: Suffixed ( ty )  =>  Some ( ty . name ( ) ) , 
199-                     ast:: LitFloatType :: Unsuffixed  => None , 
200-                 } ; 
201-                 ( token :: Float ,  symbol ,  suffix ) 
203+                 write ! ( f ,   "{}" ,  symbol ) ? ; 
204+                 match  ty  { 
205+                     ast:: LitFloatType :: Suffixed ( ty )  => write ! ( f ,   "{}" ,  ty . name ( ) ) ? , 
206+                     ast :: LitFloatType :: Unsuffixed  =>  { } 
207+                 } 
202208            } 
203-             LitKind :: Bool ( value)  => { 
204-                 let  symbol = if  value {  kw:: True  }  else  {  kw:: False  } ; 
205-                 ( token:: Bool ,  symbol,  None ) 
209+             LitKind :: Bool ( b)  => write ! ( f,  "{}" ,  if  b {  "true"  }  else {  "false"  } ) ?, 
210+             LitKind :: Err  => { 
211+                 // This only shows up in places like `-Zunpretty=hir` output, so we 
212+                 // don't bother to produce something useful. 
213+                 write ! ( f,  "<bad-literal>" ) ?; 
206214            } 
207-             // This only shows up in places like `-Zunpretty=hir` output, so we 
208-             // don't bother to produce something useful. 
209-             LitKind :: Err  => ( token:: Err ,  Symbol :: intern ( "<bad-literal>" ) ,  None ) , 
210-         } ; 
215+         } 
211216
212-         token :: Lit :: new ( kind ,  symbol ,  suffix ) 
217+         Ok ( ( ) ) 
213218    } 
214219} 
215220
0 commit comments