1
1
//! Code related to parsing literals.
2
2
3
- use crate :: ast:: { self , LitKind , MetaItemLit } ;
3
+ use crate :: ast:: { self , LitKind , MetaItemLit , StrStyle } ;
4
4
use crate :: token:: { self , Token } ;
5
5
use rustc_lexer:: unescape:: { byte_from_char, unescape_byte, unescape_char, unescape_literal, Mode } ;
6
6
use rustc_span:: symbol:: { kw, sym, Symbol } ;
7
7
use rustc_span:: Span ;
8
8
use std:: ascii;
9
+ use std:: str;
9
10
10
11
#[ derive( Debug ) ]
11
12
pub enum LitError {
@@ -115,9 +116,9 @@ impl LitKind {
115
116
}
116
117
} ) ;
117
118
error?;
118
- LitKind :: ByteStr ( buf. into ( ) )
119
+ LitKind :: ByteStr ( buf. into ( ) , StrStyle :: Cooked )
119
120
}
120
- token:: ByteStrRaw ( _ ) => {
121
+ token:: ByteStrRaw ( n ) => {
121
122
let s = symbol. as_str ( ) ;
122
123
let bytes = if s. contains ( '\r' ) {
123
124
let mut buf = Vec :: with_capacity ( s. len ( ) ) ;
@@ -136,7 +137,7 @@ impl LitKind {
136
137
symbol. to_string ( ) . into_bytes ( )
137
138
} ;
138
139
139
- LitKind :: ByteStr ( bytes. into ( ) )
140
+ LitKind :: ByteStr ( bytes. into ( ) , StrStyle :: Raw ( n ) )
140
141
}
141
142
token:: Err => LitKind :: Err ,
142
143
} )
@@ -155,10 +156,15 @@ impl LitKind {
155
156
( token:: Str , symbol, None )
156
157
}
157
158
LitKind :: Str ( symbol, ast:: StrStyle :: Raw ( n) ) => ( token:: StrRaw ( n) , symbol, None ) ,
158
- LitKind :: ByteStr ( ref bytes) => {
159
+ LitKind :: ByteStr ( ref bytes, ast :: StrStyle :: Cooked ) => {
159
160
let string = bytes. escape_ascii ( ) . to_string ( ) ;
160
161
( token:: ByteStr , Symbol :: intern ( & string) , None )
161
162
}
163
+ LitKind :: ByteStr ( ref bytes, ast:: StrStyle :: Raw ( n) ) => {
164
+ // Unwrap because raw byte string literals can only contain ASCII.
165
+ let string = str:: from_utf8 ( bytes) . unwrap ( ) ;
166
+ ( token:: ByteStrRaw ( n) , Symbol :: intern ( & string) , None )
167
+ }
162
168
LitKind :: Byte ( byte) => {
163
169
let string: String = ascii:: escape_default ( byte) . map ( Into :: < char > :: into) . collect ( ) ;
164
170
( token:: Byte , Symbol :: intern ( & string) , None )
0 commit comments