@@ -18,7 +18,7 @@ use ext::build::AstBuilder;
1818use attr;
1919use attr:: { AttrMetaMethods , WithAttrs , ThinAttributesExt } ;
2020use codemap;
21- use codemap:: { Span , Spanned , ExpnInfo , NameAndSpan , MacroBang , MacroAttribute } ;
21+ use codemap:: { Span , Spanned , ExpnInfo , ExpnId , NameAndSpan , MacroBang , MacroAttribute } ;
2222use ext:: base:: * ;
2323use feature_gate:: { self , Features } ;
2424use fold;
@@ -33,7 +33,6 @@ use visit::Visitor;
3333use std_inject;
3434
3535use std:: collections:: HashSet ;
36- use std:: env;
3736
3837// A trait for AST nodes and AST node lists into which macro invocations may expand.
3938trait MacroGenerable : Sized {
@@ -160,10 +159,10 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
160159 let new_node = ast:: ExprKind :: Closure ( capture_clause,
161160 rewritten_fn_decl,
162161 rewritten_block,
163- fld . new_span ( fn_decl_span) ) ;
162+ fn_decl_span) ;
164163 P ( ast:: Expr { id : id,
165164 node : new_node,
166- span : fld . new_span ( span) ,
165+ span : span,
167166 attrs : fold_thin_attrs ( attrs, fld) } )
168167 }
169168
@@ -322,7 +321,7 @@ fn expand_mac_invoc<T>(mac: ast::Mac, ident: Option<Ident>, attrs: Vec<ast::Attr
322321 return T :: dummy ( span) ;
323322 } ;
324323
325- let marked = expanded. fold_with ( & mut Marker { mark : mark } ) ;
324+ let marked = expanded. fold_with ( & mut Marker { mark : mark, expn_id : Some ( fld . cx . backtrace ( ) ) } ) ;
326325 let fully_expanded = marked. fold_with ( fld) ;
327326 fld. cx . bt_pop ( ) ;
328327 fully_expanded
@@ -699,12 +698,12 @@ impl<'a> Folder for PatIdentRenamer<'a> {
699698 mtwt:: apply_renames ( self . renames , ident. ctxt ) ) ;
700699 let new_node =
701700 PatKind :: Ident ( binding_mode,
702- Spanned { span : self . new_span ( sp ) , node : new_ident} ,
701+ Spanned { span : sp , node : new_ident} ,
703702 sub. map ( |p| self . fold_pat ( p) ) ) ;
704703 ast:: Pat {
705704 id : id,
706705 node : new_node,
707- span : self . new_span ( span)
706+ span : span,
708707 }
709708 } ,
710709 _ => unreachable ! ( )
@@ -774,7 +773,7 @@ fn expand_annotatable(a: Annotatable,
774773 }
775774 _ => unreachable ! ( )
776775 } ,
777- span : fld . new_span ( ti. span )
776+ span : ti. span ,
778777 } )
779778 }
780779 _ => fold:: noop_fold_trait_item ( it. unwrap ( ) , fld)
@@ -914,7 +913,7 @@ fn expand_impl_item(ii: ast::ImplItem, fld: &mut MacroExpander)
914913 }
915914 _ => unreachable ! ( )
916915 } ,
917- span : fld . new_span ( ii. span )
916+ span : ii. span ,
918917 } ) ,
919918 ast:: ImplItemKind :: Macro ( mac) => {
920919 expand_mac_invoc ( mac, None , ii. attrs , ii. span , fld)
@@ -1060,10 +1059,6 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
10601059 fn fold_ty ( & mut self , ty : P < ast:: Ty > ) -> P < ast:: Ty > {
10611060 expand_type ( ty, self )
10621061 }
1063-
1064- fn new_span ( & mut self , span : Span ) -> Span {
1065- new_span ( self . cx , span)
1066- }
10671062}
10681063
10691064impl < ' a , ' b > MacroExpander < ' a , ' b > {
@@ -1081,45 +1076,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
10811076 }
10821077}
10831078
1084- fn new_span ( cx : & ExtCtxt , sp : Span ) -> Span {
1085- debug ! ( "new_span(sp={:?})" , sp) ;
1086-
1087- if cx. codemap ( ) . more_specific_trace ( sp. expn_id , cx. backtrace ( ) ) {
1088- // If the span we are looking at has a backtrace that has more
1089- // detail than our current backtrace, then we keep that
1090- // backtrace. Honestly, I have no idea if this makes sense,
1091- // because I have no idea why we are stripping the backtrace
1092- // below. But the reason I made this change is because, in
1093- // deriving, we were generating attributes with a specific
1094- // backtrace, which was essential for `#[structural_match]` to
1095- // be properly supported, but these backtraces were being
1096- // stripped and replaced with a null backtrace. Sort of
1097- // unclear why this is the case. --nmatsakis
1098- debug ! ( "new_span: keeping trace from {:?} because it is more specific" ,
1099- sp. expn_id) ;
1100- sp
1101- } else {
1102- // This discards information in the case of macro-defining macros.
1103- //
1104- // The comment above was originally added in
1105- // b7ec2488ff2f29681fe28691d20fd2c260a9e454 in Feb 2012. I
1106- // *THINK* the reason we are doing this is because we want to
1107- // replace the backtrace of the macro contents with the
1108- // backtrace that contains the macro use. But it's pretty
1109- // unclear to me. --nmatsakis
1110- let sp1 = Span {
1111- lo : sp. lo ,
1112- hi : sp. hi ,
1113- expn_id : cx. backtrace ( ) ,
1114- } ;
1115- debug ! ( "new_span({:?}) = {:?}" , sp, sp1) ;
1116- if sp. expn_id . into_u32 ( ) == 0 && env:: var_os ( "NDM" ) . is_some ( ) {
1117- panic ! ( "NDM" ) ;
1118- }
1119- sp1
1120- }
1121- }
1122-
11231079pub struct ExpansionConfig < ' feat > {
11241080 pub crate_name : String ,
11251081 pub features : Option < & ' feat Features > ,
@@ -1206,8 +1162,9 @@ pub fn expand_crate(mut cx: ExtCtxt,
12061162// the ones defined here include:
12071163// Marker - add a mark to a context
12081164
1209- // A Marker adds the given mark to the syntax context
1210- struct Marker { mark : Mrk }
1165+ // A Marker adds the given mark to the syntax context and
1166+ // sets spans' `expn_id` to the given expn_id (unless it is `None`).
1167+ struct Marker { mark : Mrk , expn_id : Option < ExpnId > }
12111168
12121169impl Folder for Marker {
12131170 fn fold_ident ( & mut self , id : Ident ) -> Ident {
@@ -1220,14 +1177,21 @@ impl Folder for Marker {
12201177 tts : self . fold_tts ( & node. tts ) ,
12211178 ctxt : mtwt:: apply_mark ( self . mark , node. ctxt ) ,
12221179 } ,
1223- span : span,
1180+ span : self . new_span ( span) ,
1181+ }
1182+ }
1183+
1184+ fn new_span ( & mut self , mut span : Span ) -> Span {
1185+ if let Some ( expn_id) = self . expn_id {
1186+ span. expn_id = expn_id;
12241187 }
1188+ span
12251189 }
12261190}
12271191
12281192// apply a given mark to the given token trees. Used prior to expansion of a macro.
12291193fn mark_tts ( tts : & [ TokenTree ] , m : Mrk ) -> Vec < TokenTree > {
1230- noop_fold_tts ( tts, & mut Marker { mark : m} )
1194+ noop_fold_tts ( tts, & mut Marker { mark : m, expn_id : None } )
12311195}
12321196
12331197/// Check that there are no macro invocations left in the AST:
0 commit comments