@@ -18,7 +18,7 @@ use ext::build::AstBuilder;
18
18
use attr;
19
19
use attr:: { AttrMetaMethods , WithAttrs , ThinAttributesExt } ;
20
20
use codemap;
21
- use codemap:: { Span , Spanned , ExpnInfo , NameAndSpan , MacroBang , MacroAttribute } ;
21
+ use codemap:: { Span , Spanned , ExpnInfo , ExpnId , NameAndSpan , MacroBang , MacroAttribute } ;
22
22
use ext:: base:: * ;
23
23
use feature_gate:: { self , Features } ;
24
24
use fold;
@@ -33,7 +33,6 @@ use visit::Visitor;
33
33
use std_inject;
34
34
35
35
use std:: collections:: HashSet ;
36
- use std:: env;
37
36
38
37
// A trait for AST nodes and AST node lists into which macro invocations may expand.
39
38
trait MacroGenerable : Sized {
@@ -160,10 +159,10 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
160
159
let new_node = ast:: ExprKind :: Closure ( capture_clause,
161
160
rewritten_fn_decl,
162
161
rewritten_block,
163
- fld . new_span ( fn_decl_span) ) ;
162
+ fn_decl_span) ;
164
163
P ( ast:: Expr { id : id,
165
164
node : new_node,
166
- span : fld . new_span ( span) ,
165
+ span : span,
167
166
attrs : fold_thin_attrs ( attrs, fld) } )
168
167
}
169
168
@@ -322,7 +321,7 @@ fn expand_mac_invoc<T>(mac: ast::Mac, ident: Option<Ident>, attrs: Vec<ast::Attr
322
321
return T :: dummy ( span) ;
323
322
} ;
324
323
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 ( ) ) } ) ;
326
325
let fully_expanded = marked. fold_with ( fld) ;
327
326
fld. cx . bt_pop ( ) ;
328
327
fully_expanded
@@ -699,12 +698,12 @@ impl<'a> Folder for PatIdentRenamer<'a> {
699
698
mtwt:: apply_renames ( self . renames , ident. ctxt ) ) ;
700
699
let new_node =
701
700
PatKind :: Ident ( binding_mode,
702
- Spanned { span : self . new_span ( sp ) , node : new_ident} ,
701
+ Spanned { span : sp , node : new_ident} ,
703
702
sub. map ( |p| self . fold_pat ( p) ) ) ;
704
703
ast:: Pat {
705
704
id : id,
706
705
node : new_node,
707
- span : self . new_span ( span)
706
+ span : span,
708
707
}
709
708
} ,
710
709
_ => unreachable ! ( )
@@ -774,7 +773,7 @@ fn expand_annotatable(a: Annotatable,
774
773
}
775
774
_ => unreachable ! ( )
776
775
} ,
777
- span : fld . new_span ( ti. span )
776
+ span : ti. span ,
778
777
} )
779
778
}
780
779
_ => fold:: noop_fold_trait_item ( it. unwrap ( ) , fld)
@@ -914,7 +913,7 @@ fn expand_impl_item(ii: ast::ImplItem, fld: &mut MacroExpander)
914
913
}
915
914
_ => unreachable ! ( )
916
915
} ,
917
- span : fld . new_span ( ii. span )
916
+ span : ii. span ,
918
917
} ) ,
919
918
ast:: ImplItemKind :: Macro ( mac) => {
920
919
expand_mac_invoc ( mac, None , ii. attrs , ii. span , fld)
@@ -1060,10 +1059,6 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
1060
1059
fn fold_ty ( & mut self , ty : P < ast:: Ty > ) -> P < ast:: Ty > {
1061
1060
expand_type ( ty, self )
1062
1061
}
1063
-
1064
- fn new_span ( & mut self , span : Span ) -> Span {
1065
- new_span ( self . cx , span)
1066
- }
1067
1062
}
1068
1063
1069
1064
impl < ' a , ' b > MacroExpander < ' a , ' b > {
@@ -1081,45 +1076,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
1081
1076
}
1082
1077
}
1083
1078
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
-
1123
1079
pub struct ExpansionConfig < ' feat > {
1124
1080
pub crate_name : String ,
1125
1081
pub features : Option < & ' feat Features > ,
@@ -1206,8 +1162,9 @@ pub fn expand_crate(mut cx: ExtCtxt,
1206
1162
// the ones defined here include:
1207
1163
// Marker - add a mark to a context
1208
1164
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 > }
1211
1168
1212
1169
impl Folder for Marker {
1213
1170
fn fold_ident ( & mut self , id : Ident ) -> Ident {
@@ -1220,14 +1177,21 @@ impl Folder for Marker {
1220
1177
tts : self . fold_tts ( & node. tts ) ,
1221
1178
ctxt : mtwt:: apply_mark ( self . mark , node. ctxt ) ,
1222
1179
} ,
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;
1224
1187
}
1188
+ span
1225
1189
}
1226
1190
}
1227
1191
1228
1192
// apply a given mark to the given token trees. Used prior to expansion of a macro.
1229
1193
fn 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 } )
1231
1195
}
1232
1196
1233
1197
/// Check that there are no macro invocations left in the AST:
0 commit comments