@@ -23,7 +23,7 @@ pub use GenericArgs::*;
23
23
pub use UnsafeSource :: * ;
24
24
25
25
use crate :: ptr:: P ;
26
- use crate :: token:: { self , CommentKind , Delimiter } ;
26
+ use crate :: token:: { CommentKind , Delimiter } ;
27
27
use crate :: tokenstream:: { DelimSpan , LazyTokenStream , TokenStream } ;
28
28
use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
29
29
use rustc_data_structures:: stack:: ensure_sufficient_stack;
@@ -1719,15 +1719,49 @@ pub enum StrStyle {
1719
1719
/// An AST literal.
1720
1720
#[ derive( Clone , Encodable , Decodable , Debug , HashStable_Generic ) ]
1721
1721
pub 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 > ,
1724
1726
/// The "semantic" representation of the literal lowered from the original tokens.
1725
1727
/// Strings are unescaped, hexadecimal forms are eliminated, etc.
1726
- /// FIXME: Remove this and only create the semantic representation during lowering to HIR.
1727
1728
pub kind : LitKind ,
1728
1729
pub span : Span ,
1729
1730
}
1730
1731
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
+
1731
1765
/// Same as `Lit`, but restricted to string literals.
1732
1766
#[ derive( Clone , Copy , Encodable , Decodable , Debug ) ]
1733
1767
pub struct StrLit {
@@ -1737,18 +1771,14 @@ pub struct StrLit {
1737
1771
pub suffix : Option < Symbol > ,
1738
1772
pub span : Span ,
1739
1773
/// 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.
1741
1774
pub symbol_unescaped : Symbol ,
1742
1775
}
1743
1776
1744
1777
impl StrLit {
1745
1778
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
- } ;
1750
1779
Lit {
1751
- token_lit : token:: Lit :: new ( token_kind, self . symbol , self . suffix ) ,
1780
+ symbol : self . symbol ,
1781
+ suffix : self . suffix ,
1752
1782
span : self . span ,
1753
1783
kind : LitKind :: Str ( self . symbol_unescaped , self . style ) ,
1754
1784
}
@@ -1785,8 +1815,9 @@ pub enum LitKind {
1785
1815
/// A string literal (`"foo"`). The symbol is unescaped, and so may differ
1786
1816
/// from the original token's symbol.
1787
1817
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 ) ,
1790
1821
/// A byte char (`b'f'`).
1791
1822
Byte ( u8 ) ,
1792
1823
/// A character literal (`'a'`).
@@ -1810,7 +1841,7 @@ impl LitKind {
1810
1841
1811
1842
/// Returns `true` if this literal is byte literal string.
1812
1843
pub fn is_bytestr ( & self ) -> bool {
1813
- matches ! ( self , LitKind :: ByteStr ( _ ) )
1844
+ matches ! ( self , LitKind :: ByteStr ( .. ) )
1814
1845
}
1815
1846
1816
1847
/// Returns `true` if this is a numeric literal.
@@ -3084,7 +3115,7 @@ mod size_asserts {
3084
3115
static_assert_size ! ( Impl , 200 ) ;
3085
3116
static_assert_size ! ( Item , 184 ) ;
3086
3117
static_assert_size ! ( ItemKind , 112 ) ;
3087
- static_assert_size ! ( Lit , 48 ) ;
3118
+ static_assert_size ! ( Lit , 40 ) ;
3088
3119
static_assert_size ! ( LitKind , 24 ) ;
3089
3120
static_assert_size ! ( Local , 72 ) ;
3090
3121
static_assert_size ! ( Param , 40 ) ;
0 commit comments