@@ -97,20 +97,39 @@ pub mod rt {
97
97
fn to_source ( & self ) -> String ;
98
98
}
99
99
100
+ // FIXME (Issue #16472): This should go away after ToToken impls
101
+ // are revised to go directly to token-trees.
102
+ trait ToSourceWithHygiene : ToSource {
103
+ // Takes a thing and generates a string containing rust code
104
+ // for it, encoding Idents as special byte sequences to
105
+ // maintain hygiene across serialization and deserialization.
106
+ fn to_source_with_hygiene ( & self ) -> String ;
107
+ }
108
+
100
109
macro_rules! impl_to_source(
101
110
( Gc <$t: ty>, $pp: ident) => (
102
111
impl ToSource for Gc <$t> {
103
112
fn to_source( & self ) -> String {
104
113
pprust:: $pp( & * * self )
105
114
}
106
115
}
116
+ impl ToSourceWithHygiene for Gc <$t> {
117
+ fn to_source_with_hygiene( & self ) -> String {
118
+ pprust:: with_hygiene:: $pp( & * * self )
119
+ }
120
+ }
107
121
) ;
108
122
( $t: ty, $pp: ident) => (
109
123
impl ToSource for $t {
110
124
fn to_source( & self ) -> String {
111
125
pprust:: $pp( self )
112
126
}
113
127
}
128
+ impl ToSourceWithHygiene for $t {
129
+ fn to_source_with_hygiene( & self ) -> String {
130
+ pprust:: with_hygiene:: $pp( self )
131
+ }
132
+ }
114
133
) ;
115
134
)
116
135
@@ -122,13 +141,28 @@ pub mod rt {
122
141
. to_string ( )
123
142
}
124
143
144
+ fn slice_to_source_with_hygiene < ' a , T : ToSourceWithHygiene > (
145
+ sep : & ' static str , xs : & ' a [ T ] ) -> String {
146
+ xs. iter ( )
147
+ . map ( |i| i. to_source_with_hygiene ( ) )
148
+ . collect :: < Vec < String > > ( )
149
+ . connect ( sep)
150
+ . to_string ( )
151
+ }
152
+
125
153
macro_rules! impl_to_source_slice(
126
154
( $t: ty, $sep: expr) => (
127
155
impl <' a> ToSource for & ' a [ $t] {
128
156
fn to_source( & self ) -> String {
129
157
slice_to_source( $sep, * self )
130
158
}
131
159
}
160
+
161
+ impl <' a> ToSourceWithHygiene for & ' a [ $t] {
162
+ fn to_source_with_hygiene( & self ) -> String {
163
+ slice_to_source_with_hygiene( $sep, * self )
164
+ }
165
+ }
132
166
)
133
167
)
134
168
@@ -138,6 +172,12 @@ pub mod rt {
138
172
}
139
173
}
140
174
175
+ impl ToSourceWithHygiene for ast:: Ident {
176
+ fn to_source_with_hygiene ( & self ) -> String {
177
+ self . encode_with_hygiene ( )
178
+ }
179
+ }
180
+
141
181
impl_to_source ! ( ast:: Ty , ty_to_string)
142
182
impl_to_source ! ( ast:: Block , block_to_string)
143
183
impl_to_source ! ( ast:: Arg , arg_to_string)
@@ -156,6 +196,11 @@ pub mod rt {
156
196
pprust:: attribute_to_string ( & dummy_spanned ( * self ) )
157
197
}
158
198
}
199
+ impl ToSourceWithHygiene for ast:: Attribute_ {
200
+ fn to_source_with_hygiene ( & self ) -> String {
201
+ self . to_source ( )
202
+ }
203
+ }
159
204
160
205
impl < ' a > ToSource for & ' a str {
161
206
fn to_source ( & self ) -> String {
@@ -164,26 +209,46 @@ pub mod rt {
164
209
pprust:: lit_to_string ( & lit)
165
210
}
166
211
}
212
+ impl < ' a > ToSourceWithHygiene for & ' a str {
213
+ fn to_source_with_hygiene ( & self ) -> String {
214
+ self . to_source ( )
215
+ }
216
+ }
167
217
168
218
impl ToSource for ( ) {
169
219
fn to_source ( & self ) -> String {
170
220
"()" . to_string ( )
171
221
}
172
222
}
223
+ impl ToSourceWithHygiene for ( ) {
224
+ fn to_source_with_hygiene ( & self ) -> String {
225
+ self . to_source ( )
226
+ }
227
+ }
173
228
174
229
impl ToSource for bool {
175
230
fn to_source ( & self ) -> String {
176
231
let lit = dummy_spanned ( ast:: LitBool ( * self ) ) ;
177
232
pprust:: lit_to_string ( & lit)
178
233
}
179
234
}
235
+ impl ToSourceWithHygiene for bool {
236
+ fn to_source_with_hygiene ( & self ) -> String {
237
+ self . to_source ( )
238
+ }
239
+ }
180
240
181
241
impl ToSource for char {
182
242
fn to_source ( & self ) -> String {
183
243
let lit = dummy_spanned ( ast:: LitChar ( * self ) ) ;
184
244
pprust:: lit_to_string ( & lit)
185
245
}
186
246
}
247
+ impl ToSourceWithHygiene for char {
248
+ fn to_source_with_hygiene ( & self ) -> String {
249
+ self . to_source ( )
250
+ }
251
+ }
187
252
188
253
macro_rules! impl_to_source_int(
189
254
( signed, $t: ty, $tag: ident) => (
@@ -194,6 +259,11 @@ pub mod rt {
194
259
pprust:: lit_to_string( & dummy_spanned( lit) )
195
260
}
196
261
}
262
+ impl ToSourceWithHygiene for $t {
263
+ fn to_source_with_hygiene( & self ) -> String {
264
+ self . to_source( )
265
+ }
266
+ }
197
267
) ;
198
268
( unsigned, $t: ty, $tag: ident) => (
199
269
impl ToSource for $t {
@@ -202,6 +272,11 @@ pub mod rt {
202
272
pprust:: lit_to_string( & dummy_spanned( lit) )
203
273
}
204
274
}
275
+ impl ToSourceWithHygiene for $t {
276
+ fn to_source_with_hygiene( & self ) -> String {
277
+ self . to_source( )
278
+ }
279
+ }
205
280
) ;
206
281
)
207
282
@@ -223,7 +298,7 @@ pub mod rt {
223
298
( $t: ty) => (
224
299
impl ToTokens for $t {
225
300
fn to_tokens( & self , cx: & ExtCtxt ) -> Vec <TokenTree > {
226
- cx. parse_tts ( self . to_source ( ) )
301
+ cx. parse_tts_with_hygiene ( self . to_source_with_hygiene ( ) )
227
302
}
228
303
}
229
304
)
@@ -233,7 +308,7 @@ pub mod rt {
233
308
( $t: ty) => (
234
309
impl <' a> ToTokens for $t {
235
310
fn to_tokens( & self , cx: & ExtCtxt ) -> Vec <TokenTree > {
236
- cx. parse_tts ( self . to_source ( ) )
311
+ cx. parse_tts_with_hygiene ( self . to_source_with_hygiene ( ) )
237
312
}
238
313
}
239
314
)
@@ -272,7 +347,13 @@ pub mod rt {
272
347
fn parse_item ( & self , s : String ) -> Gc < ast:: Item > ;
273
348
fn parse_expr ( & self , s : String ) -> Gc < ast:: Expr > ;
274
349
fn parse_stmt ( & self , s : String ) -> Gc < ast:: Stmt > ;
275
- fn parse_tts ( & self , s : String ) -> Vec < ast:: TokenTree > ;
350
+ fn parse_tts ( & self , s : String ) -> Vec < ast:: TokenTree > ;
351
+ }
352
+
353
+ trait ExtParseUtilsWithHygiene {
354
+ // FIXME (Issue #16472): This should go away after ToToken impls
355
+ // are revised to go directly to token-trees.
356
+ fn parse_tts_with_hygiene ( & self , s : String ) -> Vec < ast:: TokenTree > ;
276
357
}
277
358
278
359
impl < ' a > ExtParseUtils for ExtCtxt < ' a > {
@@ -315,6 +396,18 @@ pub mod rt {
315
396
}
316
397
}
317
398
399
+ impl < ' a > ExtParseUtilsWithHygiene for ExtCtxt < ' a > {
400
+
401
+ fn parse_tts_with_hygiene ( & self , s : String ) -> Vec < ast:: TokenTree > {
402
+ use parse:: with_hygiene:: parse_tts_from_source_str;
403
+ parse_tts_from_source_str ( "<quote expansion>" . to_string ( ) ,
404
+ s,
405
+ self . cfg ( ) ,
406
+ self . parse_sess ( ) )
407
+ }
408
+
409
+ }
410
+
318
411
}
319
412
320
413
pub fn expand_quote_tokens ( cx : & mut ExtCtxt ,
0 commit comments