@@ -23,7 +23,7 @@ pub use GenericArgs::*;
2323pub use UnsafeSource :: * ;
2424
2525use crate :: ptr:: P ;
26- use crate :: token:: { self , CommentKind , Delimiter } ;
26+ use crate :: token:: { CommentKind , Delimiter } ;
2727use crate :: tokenstream:: { DelimSpan , LazyTokenStream , TokenStream } ;
2828use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
2929use rustc_data_structures:: stack:: ensure_sufficient_stack;
@@ -1719,15 +1719,49 @@ pub enum StrStyle {
17191719/// An AST literal.
17201720#[ derive( Clone , Encodable , Decodable , Debug , HashStable_Generic ) ]
17211721pub struct Lit {
1722- /// The original literal token as written in source code.
1723- pub token_lit : token:: Lit ,
1722+ /// The original literal as written in the source code.
1723+ pub symbol : Symbol ,
1724+ /// The original suffix as written in the source code.
1725+ pub suffix : Option < Symbol > ,
17241726 /// The "semantic" representation of the literal lowered from the original tokens.
17251727 /// Strings are unescaped, hexadecimal forms are eliminated, etc.
1726- /// FIXME: Remove this and only create the semantic representation during lowering to HIR.
17271728 pub kind : LitKind ,
17281729 pub span : Span ,
17291730}
17301731
1732+ impl fmt:: Display for Lit {
1733+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1734+ let Lit { kind, symbol, suffix, .. } = self ;
1735+ match kind {
1736+ LitKind :: Byte ( _) => write ! ( f, "b'{}'" , symbol) ?,
1737+ LitKind :: Char ( _) => write ! ( f, "'{}'" , symbol) ?,
1738+ LitKind :: Str ( _, StrStyle :: Cooked ) => write ! ( f, "\" {}\" " , symbol) ?,
1739+ LitKind :: Str ( _, StrStyle :: Raw ( n) ) => write ! (
1740+ f,
1741+ "r{delim}\" {string}\" {delim}" ,
1742+ delim = "#" . repeat( * n as usize ) ,
1743+ string = symbol
1744+ ) ?,
1745+ LitKind :: ByteStr ( _, StrStyle :: Cooked ) => write ! ( f, "b\" {}\" " , symbol) ?,
1746+ LitKind :: ByteStr ( _, StrStyle :: Raw ( n) ) => write ! (
1747+ f,
1748+ "br{delim}\" {string}\" {delim}" ,
1749+ delim = "#" . repeat( * n as usize ) ,
1750+ string = symbol
1751+ ) ?,
1752+ LitKind :: Int ( ..) | LitKind :: Float ( ..) | LitKind :: Bool ( ..) | LitKind :: Err => {
1753+ write ! ( f, "{}" , symbol) ?
1754+ }
1755+ }
1756+
1757+ if let Some ( suffix) = suffix {
1758+ write ! ( f, "{}" , suffix) ?;
1759+ }
1760+
1761+ Ok ( ( ) )
1762+ }
1763+ }
1764+
17311765/// Same as `Lit`, but restricted to string literals.
17321766#[ derive( Clone , Copy , Encodable , Decodable , Debug ) ]
17331767pub struct StrLit {
@@ -1737,18 +1771,14 @@ pub struct StrLit {
17371771 pub suffix : Option < Symbol > ,
17381772 pub span : Span ,
17391773 /// The unescaped "semantic" representation of the literal lowered from the original token.
1740- /// FIXME: Remove this and only create the semantic representation during lowering to HIR.
17411774 pub symbol_unescaped : Symbol ,
17421775}
17431776
17441777impl StrLit {
17451778 pub fn as_lit ( & self ) -> Lit {
1746- let token_kind = match self . style {
1747- StrStyle :: Cooked => token:: Str ,
1748- StrStyle :: Raw ( n) => token:: StrRaw ( n) ,
1749- } ;
17501779 Lit {
1751- token_lit : token:: Lit :: new ( token_kind, self . symbol , self . suffix ) ,
1780+ symbol : self . symbol ,
1781+ suffix : self . suffix ,
17521782 span : self . span ,
17531783 kind : LitKind :: Str ( self . symbol_unescaped , self . style ) ,
17541784 }
@@ -1785,8 +1815,9 @@ pub enum LitKind {
17851815 /// A string literal (`"foo"`). The symbol is unescaped, and so may differ
17861816 /// from the original token's symbol.
17871817 Str ( Symbol , StrStyle ) ,
1788- /// A byte string (`b"foo"`).
1789- ByteStr ( Lrc < [ u8 ] > ) ,
1818+ /// A byte string (`b"foo"`). Not stored as a symbol because it might be
1819+ /// non-utf8, and symbols only allow utf8 strings.
1820+ ByteStr ( Lrc < [ u8 ] > , StrStyle ) ,
17901821 /// A byte char (`b'f'`).
17911822 Byte ( u8 ) ,
17921823 /// A character literal (`'a'`).
@@ -1810,7 +1841,7 @@ impl LitKind {
18101841
18111842 /// Returns `true` if this literal is byte literal string.
18121843 pub fn is_bytestr ( & self ) -> bool {
1813- matches ! ( self , LitKind :: ByteStr ( _ ) )
1844+ matches ! ( self , LitKind :: ByteStr ( .. ) )
18141845 }
18151846
18161847 /// Returns `true` if this is a numeric literal.
@@ -3084,7 +3115,7 @@ mod size_asserts {
30843115 static_assert_size ! ( Impl , 200 ) ;
30853116 static_assert_size ! ( Item , 184 ) ;
30863117 static_assert_size ! ( ItemKind , 112 ) ;
3087- static_assert_size ! ( Lit , 48 ) ;
3118+ static_assert_size ! ( Lit , 40 ) ;
30883119 static_assert_size ! ( LitKind , 24 ) ;
30893120 static_assert_size ! ( Local , 72 ) ;
30903121 static_assert_size ! ( Param , 40 ) ;
0 commit comments