@@ -18,7 +18,7 @@ use ext::build::AstBuilder;
18
18
use attr;
19
19
use attr:: { AttrMetaMethods , WithAttrs } ;
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
// this function is called to detect use of feature-gated or invalid attributes
39
38
// on macro invoations since they will not be detected after macro expansion
@@ -164,10 +163,10 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
164
163
let new_node = ast:: ExprKind :: Closure ( capture_clause,
165
164
rewritten_fn_decl,
166
165
rewritten_block,
167
- fld . new_span ( fn_decl_span) ) ;
166
+ fn_decl_span) ;
168
167
P ( ast:: Expr { id : id,
169
168
node : new_node,
170
- span : fld . new_span ( span) ,
169
+ span : span,
171
170
attrs : fold_thin_attrs ( attrs, fld) } )
172
171
}
173
172
@@ -193,7 +192,7 @@ fn expand_mac_invoc<T, F, G>(mac: ast::Mac,
193
192
fld : & mut MacroExpander )
194
193
-> Option < T > where
195
194
F : for < ' a > FnOnce ( Box < MacResult +' a > ) -> Option < T > ,
196
- G : FnOnce ( T , Mrk ) -> T ,
195
+ G : FnOnce ( T , Mrk , ExpnId ) -> T ,
197
196
{
198
197
// it would almost certainly be cleaner to pass the whole
199
198
// macro invocation in, rather than pulling it apart and
@@ -258,7 +257,7 @@ fn expand_mac_invoc<T, F, G>(mac: ast::Mac,
258
257
return None ;
259
258
}
260
259
} ;
261
- Some ( mark_thunk ( parsed, fm ) )
260
+ Some ( mark_thunk ( parsed, fm , fld . cx . backtrace ( ) ) )
262
261
}
263
262
_ => {
264
263
fld. cx . span_err (
@@ -485,8 +484,9 @@ pub fn expand_item_mac(it: P<ast::Item>,
485
484
486
485
let items = match items {
487
486
Some ( items) => {
487
+ let expn_id = fld. cx . backtrace ( ) ;
488
488
items. into_iter ( )
489
- . map ( |i| mark_item ( i, fm) )
489
+ . map ( |i| mark_item ( i, fm, expn_id ) )
490
490
. flat_map ( |i| fld. fold_item ( i) . into_iter ( ) )
491
491
. collect ( )
492
492
}
@@ -526,7 +526,7 @@ fn expand_stmt(stmt: Stmt, fld: &mut MacroExpander) -> SmallVector<Stmt> {
526
526
let maybe_new_items =
527
527
expand_mac_invoc ( mac. unwrap ( ) , stmt. span ,
528
528
|r| r. make_stmts ( ) ,
529
- |stmts, mark| stmts. move_map ( |m| mark_stmt ( m, mark) ) ,
529
+ |stmts, mark, expn_id | stmts. move_map ( |m| mark_stmt ( m, mark, expn_id ) ) ,
530
530
fld) ;
531
531
532
532
let mut fully_expanded = match maybe_new_items {
@@ -805,7 +805,7 @@ fn expand_pat(p: P<ast::Pat>, fld: &mut MacroExpander) -> P<ast::Pat> {
805
805
} ;
806
806
807
807
// mark after:
808
- mark_pat ( expanded, fm )
808
+ mark_pat ( expanded, fm , fld . cx . backtrace ( ) )
809
809
}
810
810
_ => {
811
811
fld. cx . span_err ( span,
@@ -865,12 +865,12 @@ impl<'a> Folder for PatIdentRenamer<'a> {
865
865
mtwt:: apply_renames ( self . renames , ident. ctxt ) ) ;
866
866
let new_node =
867
867
PatKind :: Ident ( binding_mode,
868
- Spanned { span : self . new_span ( sp ) , node : new_ident} ,
868
+ Spanned { span : sp , node : new_ident} ,
869
869
sub. map ( |p| self . fold_pat ( p) ) ) ;
870
870
ast:: Pat {
871
871
id : id,
872
872
node : new_node,
873
- span : self . new_span ( span)
873
+ span : span,
874
874
}
875
875
} ,
876
876
_ => unreachable ! ( )
@@ -934,7 +934,7 @@ fn expand_annotatable(a: Annotatable,
934
934
}
935
935
_ => unreachable ! ( )
936
936
} ,
937
- span : fld . new_span ( ti. span )
937
+ span : ti. span ,
938
938
} )
939
939
}
940
940
_ => fold:: noop_fold_trait_item ( it. unwrap ( ) , fld)
@@ -1074,16 +1074,16 @@ fn expand_impl_item(ii: ast::ImplItem, fld: &mut MacroExpander)
1074
1074
}
1075
1075
_ => unreachable ! ( )
1076
1076
} ,
1077
- span : fld . new_span ( ii. span )
1077
+ span : ii. span ,
1078
1078
} ) ,
1079
1079
ast:: ImplItemKind :: Macro ( mac) => {
1080
1080
check_attributes ( & ii. attrs , fld) ;
1081
1081
1082
+ let mark_impl_items = |meths : SmallVector < ast:: ImplItem > , mark, expn_id| {
1083
+ meths. move_map ( |m| mark_impl_item ( m, mark, expn_id) )
1084
+ } ;
1082
1085
let maybe_new_items =
1083
- expand_mac_invoc ( mac, ii. span ,
1084
- |r| r. make_impl_items ( ) ,
1085
- |meths, mark| meths. move_map ( |m| mark_impl_item ( m, mark) ) ,
1086
- fld) ;
1086
+ expand_mac_invoc ( mac, ii. span , |r| r. make_impl_items ( ) , mark_impl_items, fld) ;
1087
1087
1088
1088
match maybe_new_items {
1089
1089
Some ( impl_items) => {
@@ -1256,10 +1256,6 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
1256
1256
fn fold_ty ( & mut self , ty : P < ast:: Ty > ) -> P < ast:: Ty > {
1257
1257
expand_type ( ty, self )
1258
1258
}
1259
-
1260
- fn new_span ( & mut self , span : Span ) -> Span {
1261
- new_span ( self . cx , span)
1262
- }
1263
1259
}
1264
1260
1265
1261
impl < ' a , ' b > MacroExpander < ' a , ' b > {
@@ -1277,45 +1273,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
1277
1273
}
1278
1274
}
1279
1275
1280
- fn new_span ( cx : & ExtCtxt , sp : Span ) -> Span {
1281
- debug ! ( "new_span(sp={:?})" , sp) ;
1282
-
1283
- if cx. codemap ( ) . more_specific_trace ( sp. expn_id , cx. backtrace ( ) ) {
1284
- // If the span we are looking at has a backtrace that has more
1285
- // detail than our current backtrace, then we keep that
1286
- // backtrace. Honestly, I have no idea if this makes sense,
1287
- // because I have no idea why we are stripping the backtrace
1288
- // below. But the reason I made this change is because, in
1289
- // deriving, we were generating attributes with a specific
1290
- // backtrace, which was essential for `#[structural_match]` to
1291
- // be properly supported, but these backtraces were being
1292
- // stripped and replaced with a null backtrace. Sort of
1293
- // unclear why this is the case. --nmatsakis
1294
- debug ! ( "new_span: keeping trace from {:?} because it is more specific" ,
1295
- sp. expn_id) ;
1296
- sp
1297
- } else {
1298
- // This discards information in the case of macro-defining macros.
1299
- //
1300
- // The comment above was originally added in
1301
- // b7ec2488ff2f29681fe28691d20fd2c260a9e454 in Feb 2012. I
1302
- // *THINK* the reason we are doing this is because we want to
1303
- // replace the backtrace of the macro contents with the
1304
- // backtrace that contains the macro use. But it's pretty
1305
- // unclear to me. --nmatsakis
1306
- let sp1 = Span {
1307
- lo : sp. lo ,
1308
- hi : sp. hi ,
1309
- expn_id : cx. backtrace ( ) ,
1310
- } ;
1311
- debug ! ( "new_span({:?}) = {:?}" , sp, sp1) ;
1312
- if sp. expn_id . into_u32 ( ) == 0 && env:: var_os ( "NDM" ) . is_some ( ) {
1313
- panic ! ( "NDM" ) ;
1314
- }
1315
- sp1
1316
- }
1317
- }
1318
-
1319
1276
pub struct ExpansionConfig < ' feat > {
1320
1277
pub crate_name : String ,
1321
1278
pub features : Option < & ' feat Features > ,
@@ -1402,8 +1359,9 @@ pub fn expand_crate(mut cx: ExtCtxt,
1402
1359
// the ones defined here include:
1403
1360
// Marker - add a mark to a context
1404
1361
1405
- // A Marker adds the given mark to the syntax context
1406
- struct Marker { mark : Mrk }
1362
+ // A Marker adds the given mark to the syntax context and
1363
+ // sets spans' `expn_id` to the given expn_id (unless it is `None`).
1364
+ struct Marker { mark : Mrk , expn_id : Option < ExpnId > }
1407
1365
1408
1366
impl Folder for Marker {
1409
1367
fn fold_ident ( & mut self , id : Ident ) -> Ident {
@@ -1416,46 +1374,53 @@ impl Folder for Marker {
1416
1374
tts : self . fold_tts ( & node. tts ) ,
1417
1375
ctxt : mtwt:: apply_mark ( self . mark , node. ctxt ) ,
1418
1376
} ,
1419
- span : span,
1377
+ span : self . new_span ( span) ,
1378
+ }
1379
+ }
1380
+
1381
+ fn new_span ( & mut self , mut span : Span ) -> Span {
1382
+ if let Some ( expn_id) = self . expn_id {
1383
+ span. expn_id = expn_id;
1420
1384
}
1385
+ span
1421
1386
}
1422
1387
}
1423
1388
1424
1389
// apply a given mark to the given token trees. Used prior to expansion of a macro.
1425
1390
fn mark_tts ( tts : & [ TokenTree ] , m : Mrk ) -> Vec < TokenTree > {
1426
- noop_fold_tts ( tts, & mut Marker { mark : m} )
1391
+ noop_fold_tts ( tts, & mut Marker { mark : m, expn_id : None } )
1427
1392
}
1428
1393
1429
1394
// apply a given mark to the given expr. Used following the expansion of a macro.
1430
- fn mark_expr ( expr : P < ast:: Expr > , m : Mrk ) -> P < ast:: Expr > {
1431
- Marker { mark : m} . fold_expr ( expr)
1395
+ fn mark_expr ( expr : P < ast:: Expr > , m : Mrk , expn_id : ExpnId ) -> P < ast:: Expr > {
1396
+ Marker { mark : m, expn_id : Some ( expn_id ) } . fold_expr ( expr)
1432
1397
}
1433
1398
1434
1399
// apply a given mark to the given pattern. Used following the expansion of a macro.
1435
- fn mark_pat ( pat : P < ast:: Pat > , m : Mrk ) -> P < ast:: Pat > {
1436
- Marker { mark : m} . fold_pat ( pat)
1400
+ fn mark_pat ( pat : P < ast:: Pat > , m : Mrk , expn_id : ExpnId ) -> P < ast:: Pat > {
1401
+ Marker { mark : m, expn_id : Some ( expn_id ) } . fold_pat ( pat)
1437
1402
}
1438
1403
1439
1404
// apply a given mark to the given stmt. Used following the expansion of a macro.
1440
- fn mark_stmt ( stmt : ast:: Stmt , m : Mrk ) -> ast:: Stmt {
1441
- Marker { mark : m} . fold_stmt ( stmt)
1405
+ fn mark_stmt ( stmt : ast:: Stmt , m : Mrk , expn_id : ExpnId ) -> ast:: Stmt {
1406
+ Marker { mark : m, expn_id : Some ( expn_id ) } . fold_stmt ( stmt)
1442
1407
. expect_one ( "marking a stmt didn't return exactly one stmt" )
1443
1408
}
1444
1409
1445
1410
// apply a given mark to the given item. Used following the expansion of a macro.
1446
- fn mark_item ( expr : P < ast:: Item > , m : Mrk ) -> P < ast:: Item > {
1447
- Marker { mark : m} . fold_item ( expr)
1411
+ fn mark_item ( expr : P < ast:: Item > , m : Mrk , expn_id : ExpnId ) -> P < ast:: Item > {
1412
+ Marker { mark : m, expn_id : Some ( expn_id ) } . fold_item ( expr)
1448
1413
. expect_one ( "marking an item didn't return exactly one item" )
1449
1414
}
1450
1415
1451
1416
// apply a given mark to the given item. Used following the expansion of a macro.
1452
- fn mark_impl_item ( ii : ast:: ImplItem , m : Mrk ) -> ast:: ImplItem {
1453
- Marker { mark : m} . fold_impl_item ( ii)
1417
+ fn mark_impl_item ( ii : ast:: ImplItem , m : Mrk , expn_id : ExpnId ) -> ast:: ImplItem {
1418
+ Marker { mark : m, expn_id : Some ( expn_id ) } . fold_impl_item ( ii)
1454
1419
. expect_one ( "marking an impl item didn't return exactly one impl item" )
1455
1420
}
1456
1421
1457
- fn mark_ty ( ty : P < ast:: Ty > , m : Mrk ) -> P < ast:: Ty > {
1458
- Marker { mark : m } . fold_ty ( ty)
1422
+ fn mark_ty ( ty : P < ast:: Ty > , m : Mrk , expn_id : ExpnId ) -> P < ast:: Ty > {
1423
+ Marker { mark : m, expn_id : Some ( expn_id ) } . fold_ty ( ty)
1459
1424
}
1460
1425
1461
1426
/// Check that there are no macro invocations left in the AST:
0 commit comments