Skip to content

Commit a1ef856

Browse files
committed
Update spans' expn_id during the marking fold
1 parent e9243d3 commit a1ef856

File tree

2 files changed

+41
-101
lines changed

2 files changed

+41
-101
lines changed

src/libsyntax/codemap.rs

-25
Original file line numberDiff line numberDiff line change
@@ -1258,31 +1258,6 @@ impl CodeMap {
12581258
return a;
12591259
}
12601260

1261-
/// Check if the backtrace `subtrace` contains `suptrace` as a prefix.
1262-
pub fn more_specific_trace(&self,
1263-
mut subtrace: ExpnId,
1264-
suptrace: ExpnId)
1265-
-> bool {
1266-
loop {
1267-
if subtrace == suptrace {
1268-
return true;
1269-
}
1270-
1271-
let stop = self.with_expn_info(subtrace, |opt_expn_info| {
1272-
if let Some(expn_info) = opt_expn_info {
1273-
subtrace = expn_info.call_site.expn_id;
1274-
false
1275-
} else {
1276-
true
1277-
}
1278-
});
1279-
1280-
if stop {
1281-
return false;
1282-
}
1283-
}
1284-
}
1285-
12861261
pub fn record_expansion(&self, expn_info: ExpnInfo) -> ExpnId {
12871262
let mut expansions = self.expansions.borrow_mut();
12881263
expansions.push(expn_info);

src/libsyntax/ext/expand.rs

+41-76
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use ext::build::AstBuilder;
1818
use attr;
1919
use attr::{AttrMetaMethods, WithAttrs};
2020
use codemap;
21-
use codemap::{Span, Spanned, ExpnInfo, NameAndSpan, MacroBang, MacroAttribute};
21+
use codemap::{Span, Spanned, ExpnInfo, ExpnId, NameAndSpan, MacroBang, MacroAttribute};
2222
use ext::base::*;
2323
use feature_gate::{self, Features};
2424
use fold;
@@ -33,7 +33,6 @@ use visit::Visitor;
3333
use std_inject;
3434

3535
use std::collections::HashSet;
36-
use std::env;
3736

3837
// this function is called to detect use of feature-gated or invalid attributes
3938
// 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> {
164163
let new_node = ast::ExprKind::Closure(capture_clause,
165164
rewritten_fn_decl,
166165
rewritten_block,
167-
fld.new_span(fn_decl_span));
166+
fn_decl_span);
168167
P(ast::Expr{ id:id,
169168
node: new_node,
170-
span: fld.new_span(span),
169+
span: span,
171170
attrs: fold_thin_attrs(attrs, fld) })
172171
}
173172

@@ -193,7 +192,7 @@ fn expand_mac_invoc<T, F, G>(mac: ast::Mac,
193192
fld: &mut MacroExpander)
194193
-> Option<T> where
195194
F: for<'a> FnOnce(Box<MacResult+'a>) -> Option<T>,
196-
G: FnOnce(T, Mrk) -> T,
195+
G: FnOnce(T, Mrk, ExpnId) -> T,
197196
{
198197
// it would almost certainly be cleaner to pass the whole
199198
// macro invocation in, rather than pulling it apart and
@@ -258,7 +257,7 @@ fn expand_mac_invoc<T, F, G>(mac: ast::Mac,
258257
return None;
259258
}
260259
};
261-
Some(mark_thunk(parsed,fm))
260+
Some(mark_thunk(parsed, fm, fld.cx.backtrace()))
262261
}
263262
_ => {
264263
fld.cx.span_err(
@@ -485,8 +484,9 @@ pub fn expand_item_mac(it: P<ast::Item>,
485484

486485
let items = match items {
487486
Some(items) => {
487+
let expn_id = fld.cx.backtrace();
488488
items.into_iter()
489-
.map(|i| mark_item(i, fm))
489+
.map(|i| mark_item(i, fm, expn_id))
490490
.flat_map(|i| fld.fold_item(i).into_iter())
491491
.collect()
492492
}
@@ -526,7 +526,7 @@ fn expand_stmt(stmt: Stmt, fld: &mut MacroExpander) -> SmallVector<Stmt> {
526526
let maybe_new_items =
527527
expand_mac_invoc(mac.unwrap(), stmt.span,
528528
|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)),
530530
fld);
531531

532532
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> {
805805
};
806806

807807
// mark after:
808-
mark_pat(expanded,fm)
808+
mark_pat(expanded, fm, fld.cx.backtrace())
809809
}
810810
_ => {
811811
fld.cx.span_err(span,
@@ -865,12 +865,12 @@ impl<'a> Folder for PatIdentRenamer<'a> {
865865
mtwt::apply_renames(self.renames, ident.ctxt));
866866
let new_node =
867867
PatKind::Ident(binding_mode,
868-
Spanned{span: self.new_span(sp), node: new_ident},
868+
Spanned{span: sp, node: new_ident},
869869
sub.map(|p| self.fold_pat(p)));
870870
ast::Pat {
871871
id: id,
872872
node: new_node,
873-
span: self.new_span(span)
873+
span: span,
874874
}
875875
},
876876
_ => unreachable!()
@@ -934,7 +934,7 @@ fn expand_annotatable(a: Annotatable,
934934
}
935935
_ => unreachable!()
936936
},
937-
span: fld.new_span(ti.span)
937+
span: ti.span,
938938
})
939939
}
940940
_ => fold::noop_fold_trait_item(it.unwrap(), fld)
@@ -1074,16 +1074,16 @@ fn expand_impl_item(ii: ast::ImplItem, fld: &mut MacroExpander)
10741074
}
10751075
_ => unreachable!()
10761076
},
1077-
span: fld.new_span(ii.span)
1077+
span: ii.span,
10781078
}),
10791079
ast::ImplItemKind::Macro(mac) => {
10801080
check_attributes(&ii.attrs, fld);
10811081

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+
};
10821085
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);
10871087

10881088
match maybe_new_items {
10891089
Some(impl_items) => {
@@ -1256,10 +1256,6 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
12561256
fn fold_ty(&mut self, ty: P<ast::Ty>) -> P<ast::Ty> {
12571257
expand_type(ty, self)
12581258
}
1259-
1260-
fn new_span(&mut self, span: Span) -> Span {
1261-
new_span(self.cx, span)
1262-
}
12631259
}
12641260

12651261
impl<'a, 'b> MacroExpander<'a, 'b> {
@@ -1277,45 +1273,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
12771273
}
12781274
}
12791275

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-
13191276
pub struct ExpansionConfig<'feat> {
13201277
pub crate_name: String,
13211278
pub features: Option<&'feat Features>,
@@ -1402,8 +1359,9 @@ pub fn expand_crate(mut cx: ExtCtxt,
14021359
// the ones defined here include:
14031360
// Marker - add a mark to a context
14041361

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> }
14071365

14081366
impl Folder for Marker {
14091367
fn fold_ident(&mut self, id: Ident) -> Ident {
@@ -1416,46 +1374,53 @@ impl Folder for Marker {
14161374
tts: self.fold_tts(&node.tts),
14171375
ctxt: mtwt::apply_mark(self.mark, node.ctxt),
14181376
},
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;
14201384
}
1385+
span
14211386
}
14221387
}
14231388

14241389
// apply a given mark to the given token trees. Used prior to expansion of a macro.
14251390
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})
14271392
}
14281393

14291394
// 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)
14321397
}
14331398

14341399
// 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)
14371402
}
14381403

14391404
// 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)
14421407
.expect_one("marking a stmt didn't return exactly one stmt")
14431408
}
14441409

14451410
// 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)
14481413
.expect_one("marking an item didn't return exactly one item")
14491414
}
14501415

14511416
// 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)
14541419
.expect_one("marking an impl item didn't return exactly one impl item")
14551420
}
14561421

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)
14591424
}
14601425

14611426
/// Check that there are no macro invocations left in the AST:

0 commit comments

Comments
 (0)