1818use GLOBALS ;
1919use Span ;
2020use edition:: { Edition , DEFAULT_EDITION } ;
21- use symbol:: Symbol ;
21+ use symbol:: { keywords , Symbol } ;
2222
2323use serialize:: { Encodable , Decodable , Encoder , Decoder } ;
2424use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
25- use std:: fmt;
25+ use std:: { fmt, mem } ;
2626
2727/// A SyntaxContext represents a chain of macro expansions (represented by marks).
2828#[ derive( Clone , Copy , PartialEq , Eq , Default , PartialOrd , Ord , Hash ) ]
@@ -37,6 +37,8 @@ struct SyntaxContextData {
3737 opaque : SyntaxContext ,
3838 // This context, but with all transparent marks filtered away.
3939 opaque_and_semitransparent : SyntaxContext ,
40+ // Name of the crate to which `$crate` with this context would resolve.
41+ dollar_crate_name : Symbol ,
4042}
4143
4244/// A mark is a unique id associated with a macro expansion.
@@ -200,6 +202,7 @@ impl HygieneData {
200202 prev_ctxt: SyntaxContext ( 0 ) ,
201203 opaque: SyntaxContext ( 0 ) ,
202204 opaque_and_semitransparent: SyntaxContext ( 0 ) ,
205+ dollar_crate_name: keywords:: DollarCrate . name( ) ,
203206 } ] ,
204207 markings : FxHashMap :: default ( ) ,
205208 default_edition : DEFAULT_EDITION ,
@@ -258,6 +261,7 @@ impl SyntaxContext {
258261 prev_ctxt : SyntaxContext :: empty ( ) ,
259262 opaque : SyntaxContext :: empty ( ) ,
260263 opaque_and_semitransparent : SyntaxContext :: empty ( ) ,
264+ dollar_crate_name : keywords:: DollarCrate . name ( ) ,
261265 } ) ;
262266 SyntaxContext ( data. syntax_contexts . len ( ) as u32 - 1 )
263267 } )
@@ -324,6 +328,7 @@ impl SyntaxContext {
324328 prev_ctxt,
325329 opaque : new_opaque,
326330 opaque_and_semitransparent : new_opaque,
331+ dollar_crate_name : keywords:: DollarCrate . name ( ) ,
327332 } ) ;
328333 new_opaque
329334 } ) ;
@@ -341,6 +346,7 @@ impl SyntaxContext {
341346 prev_ctxt,
342347 opaque,
343348 opaque_and_semitransparent : new_opaque_and_semitransparent,
349+ dollar_crate_name : keywords:: DollarCrate . name ( ) ,
344350 } ) ;
345351 new_opaque_and_semitransparent
346352 } ) ;
@@ -356,6 +362,7 @@ impl SyntaxContext {
356362 prev_ctxt,
357363 opaque,
358364 opaque_and_semitransparent,
365+ dollar_crate_name : keywords:: DollarCrate . name ( ) ,
359366 } ) ;
360367 new_opaque_and_semitransparent_and_transparent
361368 } )
@@ -510,6 +517,21 @@ impl SyntaxContext {
510517 pub fn outer ( self ) -> Mark {
511518 HygieneData :: with ( |data| data. syntax_contexts [ self . 0 as usize ] . outer_mark )
512519 }
520+
521+ pub fn dollar_crate_name ( self ) -> Symbol {
522+ HygieneData :: with ( |data| data. syntax_contexts [ self . 0 as usize ] . dollar_crate_name )
523+ }
524+
525+ pub fn set_dollar_crate_name ( self , dollar_crate_name : Symbol ) {
526+ HygieneData :: with ( |data| {
527+ let prev_dollar_crate_name = mem:: replace (
528+ & mut data. syntax_contexts [ self . 0 as usize ] . dollar_crate_name , dollar_crate_name
529+ ) ;
530+ assert ! ( dollar_crate_name == prev_dollar_crate_name ||
531+ prev_dollar_crate_name == keywords:: DollarCrate . name( ) ,
532+ "$crate name is reset for a syntax context" ) ;
533+ } )
534+ }
513535}
514536
515537impl fmt:: Debug for SyntaxContext {
0 commit comments