@@ -3,7 +3,7 @@ use crate::attr::{HasAttrs, Stability, Deprecation};
3
3
use crate :: source_map:: SourceMap ;
4
4
use crate :: edition:: Edition ;
5
5
use crate :: ext:: expand:: { self , AstFragment , Invocation } ;
6
- use crate :: ext:: hygiene:: { ExpnId , SyntaxContext , Transparency } ;
6
+ use crate :: ext:: hygiene:: { ExpnId , Transparency } ;
7
7
use crate :: mut_visit:: { self , MutVisitor } ;
8
8
use crate :: parse:: { self , parser, DirectoryOwnership } ;
9
9
use crate :: parse:: token;
@@ -760,23 +760,39 @@ impl<'a> ExtCtxt<'a> {
760
760
pub fn call_site ( & self ) -> Span {
761
761
self . current_expansion . id . expn_data ( ) . call_site
762
762
}
763
- pub fn backtrace ( & self ) -> SyntaxContext {
764
- SyntaxContext :: root ( ) . apply_mark ( self . current_expansion . id )
763
+
764
+ /// Equivalent of `Span::def_site` from the proc macro API,
765
+ /// except that the location is taken from the span passed as an argument.
766
+ pub fn with_def_site_ctxt ( & self , span : Span ) -> Span {
767
+ span. with_ctxt_from_mark ( self . current_expansion . id , Transparency :: Opaque )
768
+ }
769
+
770
+ /// Equivalent of `Span::call_site` from the proc macro API,
771
+ /// except that the location is taken from the span passed as an argument.
772
+ pub fn with_call_site_ctxt ( & self , span : Span ) -> Span {
773
+ span. with_ctxt_from_mark ( self . current_expansion . id , Transparency :: Transparent )
774
+ }
775
+
776
+ /// Span with a context reproducing `macro_rules` hygiene (hygienic locals, unhygienic items).
777
+ /// FIXME: This should be eventually replaced either with `with_def_site_ctxt` (preferably),
778
+ /// or with `with_call_site_ctxt` (where necessary).
779
+ pub fn with_legacy_ctxt ( & self , span : Span ) -> Span {
780
+ span. with_ctxt_from_mark ( self . current_expansion . id , Transparency :: SemiTransparent )
765
781
}
766
782
767
783
/// Returns span for the macro which originally caused the current expansion to happen.
768
784
///
769
785
/// Stops backtracing at include! boundary.
770
786
pub fn expansion_cause ( & self ) -> Option < Span > {
771
- let mut ctxt = self . backtrace ( ) ;
787
+ let mut expn_id = self . current_expansion . id ;
772
788
let mut last_macro = None ;
773
789
loop {
774
- let expn_data = ctxt . outer_expn_data ( ) ;
790
+ let expn_data = expn_id . expn_data ( ) ;
775
791
// Stop going up the backtrace once include! is encountered
776
792
if expn_data. is_root ( ) || expn_data. kind . descr ( ) == sym:: include {
777
793
break ;
778
794
}
779
- ctxt = expn_data. call_site . ctxt ( ) ;
795
+ expn_id = expn_data. call_site . ctxt ( ) . outer_expn ( ) ;
780
796
last_macro = Some ( expn_data. call_site ) ;
781
797
}
782
798
last_macro
@@ -865,7 +881,7 @@ impl<'a> ExtCtxt<'a> {
865
881
ast:: Ident :: from_str ( st)
866
882
}
867
883
pub fn std_path ( & self , components : & [ Symbol ] ) -> Vec < ast:: Ident > {
868
- let def_site = DUMMY_SP . apply_mark ( self . current_expansion . id ) ;
884
+ let def_site = self . with_def_site_ctxt ( DUMMY_SP ) ;
869
885
iter:: once ( Ident :: new ( kw:: DollarCrate , def_site) )
870
886
. chain ( components. iter ( ) . map ( |& s| Ident :: with_dummy_span ( s) ) )
871
887
. collect ( )
0 commit comments