@@ -97,20 +97,39 @@ pub mod rt {
9797 fn to_source ( & self ) -> String ;
9898 }
9999
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+
100109 macro_rules! impl_to_source(
101110 ( Gc <$t: ty>, $pp: ident) => (
102111 impl ToSource for Gc <$t> {
103112 fn to_source( & self ) -> String {
104113 pprust:: $pp( & * * self )
105114 }
106115 }
116+ impl ToSourceWithHygiene for Gc <$t> {
117+ fn to_source_with_hygiene( & self ) -> String {
118+ pprust:: with_hygiene:: $pp( & * * self )
119+ }
120+ }
107121 ) ;
108122 ( $t: ty, $pp: ident) => (
109123 impl ToSource for $t {
110124 fn to_source( & self ) -> String {
111125 pprust:: $pp( self )
112126 }
113127 }
128+ impl ToSourceWithHygiene for $t {
129+ fn to_source_with_hygiene( & self ) -> String {
130+ pprust:: with_hygiene:: $pp( self )
131+ }
132+ }
114133 ) ;
115134 )
116135
@@ -122,13 +141,28 @@ pub mod rt {
122141 . to_string ( )
123142 }
124143
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+
125153 macro_rules! impl_to_source_slice(
126154 ( $t: ty, $sep: expr) => (
127155 impl <' a> ToSource for & ' a [ $t] {
128156 fn to_source( & self ) -> String {
129157 slice_to_source( $sep, * self )
130158 }
131159 }
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+ }
132166 )
133167 )
134168
@@ -138,6 +172,12 @@ pub mod rt {
138172 }
139173 }
140174
175+ impl ToSourceWithHygiene for ast:: Ident {
176+ fn to_source_with_hygiene ( & self ) -> String {
177+ self . encode_with_hygiene ( )
178+ }
179+ }
180+
141181 impl_to_source ! ( ast:: Ty , ty_to_string)
142182 impl_to_source ! ( ast:: Block , block_to_string)
143183 impl_to_source ! ( ast:: Arg , arg_to_string)
@@ -156,6 +196,11 @@ pub mod rt {
156196 pprust:: attribute_to_string ( & dummy_spanned ( * self ) )
157197 }
158198 }
199+ impl ToSourceWithHygiene for ast:: Attribute_ {
200+ fn to_source_with_hygiene ( & self ) -> String {
201+ self . to_source ( )
202+ }
203+ }
159204
160205 impl < ' a > ToSource for & ' a str {
161206 fn to_source ( & self ) -> String {
@@ -164,26 +209,46 @@ pub mod rt {
164209 pprust:: lit_to_string ( & lit)
165210 }
166211 }
212+ impl < ' a > ToSourceWithHygiene for & ' a str {
213+ fn to_source_with_hygiene ( & self ) -> String {
214+ self . to_source ( )
215+ }
216+ }
167217
168218 impl ToSource for ( ) {
169219 fn to_source ( & self ) -> String {
170220 "()" . to_string ( )
171221 }
172222 }
223+ impl ToSourceWithHygiene for ( ) {
224+ fn to_source_with_hygiene ( & self ) -> String {
225+ self . to_source ( )
226+ }
227+ }
173228
174229 impl ToSource for bool {
175230 fn to_source ( & self ) -> String {
176231 let lit = dummy_spanned ( ast:: LitBool ( * self ) ) ;
177232 pprust:: lit_to_string ( & lit)
178233 }
179234 }
235+ impl ToSourceWithHygiene for bool {
236+ fn to_source_with_hygiene ( & self ) -> String {
237+ self . to_source ( )
238+ }
239+ }
180240
181241 impl ToSource for char {
182242 fn to_source ( & self ) -> String {
183243 let lit = dummy_spanned ( ast:: LitChar ( * self ) ) ;
184244 pprust:: lit_to_string ( & lit)
185245 }
186246 }
247+ impl ToSourceWithHygiene for char {
248+ fn to_source_with_hygiene ( & self ) -> String {
249+ self . to_source ( )
250+ }
251+ }
187252
188253 macro_rules! impl_to_source_int(
189254 ( signed, $t: ty, $tag: ident) => (
@@ -194,6 +259,11 @@ pub mod rt {
194259 pprust:: lit_to_string( & dummy_spanned( lit) )
195260 }
196261 }
262+ impl ToSourceWithHygiene for $t {
263+ fn to_source_with_hygiene( & self ) -> String {
264+ self . to_source( )
265+ }
266+ }
197267 ) ;
198268 ( unsigned, $t: ty, $tag: ident) => (
199269 impl ToSource for $t {
@@ -202,6 +272,11 @@ pub mod rt {
202272 pprust:: lit_to_string( & dummy_spanned( lit) )
203273 }
204274 }
275+ impl ToSourceWithHygiene for $t {
276+ fn to_source_with_hygiene( & self ) -> String {
277+ self . to_source( )
278+ }
279+ }
205280 ) ;
206281 )
207282
@@ -223,7 +298,7 @@ pub mod rt {
223298 ( $t: ty) => (
224299 impl ToTokens for $t {
225300 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 ( ) )
227302 }
228303 }
229304 )
@@ -233,7 +308,7 @@ pub mod rt {
233308 ( $t: ty) => (
234309 impl <' a> ToTokens for $t {
235310 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 ( ) )
237312 }
238313 }
239314 )
@@ -272,7 +347,13 @@ pub mod rt {
272347 fn parse_item ( & self , s : String ) -> Gc < ast:: Item > ;
273348 fn parse_expr ( & self , s : String ) -> Gc < ast:: Expr > ;
274349 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 > ;
276357 }
277358
278359 impl < ' a > ExtParseUtils for ExtCtxt < ' a > {
@@ -315,6 +396,18 @@ pub mod rt {
315396 }
316397 }
317398
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+
318411}
319412
320413pub fn expand_quote_tokens ( cx : & mut ExtCtxt ,
0 commit comments