Skip to content

Commit 0fb01d2

Browse files
committed
Audit uses of apply_mark in built-in macros
Replace them with equivalents of `Span::{def_site,call_site}` from proc macro API. The new API is much less error prone and doesn't rely on macros having default transparency.
1 parent 7602267 commit 0fb01d2

File tree

19 files changed

+56
-41
lines changed

19 files changed

+56
-41
lines changed

src/libsyntax/ext/base.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::attr::{HasAttrs, Stability, Deprecation};
33
use crate::source_map::SourceMap;
44
use crate::edition::Edition;
55
use crate::ext::expand::{self, AstFragment, Invocation};
6-
use crate::ext::hygiene::{ExpnId, SyntaxContext, Transparency};
6+
use crate::ext::hygiene::{ExpnId, Transparency};
77
use crate::mut_visit::{self, MutVisitor};
88
use crate::parse::{self, parser, DirectoryOwnership};
99
use crate::parse::token;
@@ -760,23 +760,39 @@ impl<'a> ExtCtxt<'a> {
760760
pub fn call_site(&self) -> Span {
761761
self.current_expansion.id.expn_data().call_site
762762
}
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)
765781
}
766782

767783
/// Returns span for the macro which originally caused the current expansion to happen.
768784
///
769785
/// Stops backtracing at include! boundary.
770786
pub fn expansion_cause(&self) -> Option<Span> {
771-
let mut ctxt = self.backtrace();
787+
let mut expn_id = self.current_expansion.id;
772788
let mut last_macro = None;
773789
loop {
774-
let expn_data = ctxt.outer_expn_data();
790+
let expn_data = expn_id.expn_data();
775791
// Stop going up the backtrace once include! is encountered
776792
if expn_data.is_root() || expn_data.kind.descr() == sym::include {
777793
break;
778794
}
779-
ctxt = expn_data.call_site.ctxt();
795+
expn_id = expn_data.call_site.ctxt().outer_expn();
780796
last_macro = Some(expn_data.call_site);
781797
}
782798
last_macro
@@ -865,7 +881,7 @@ impl<'a> ExtCtxt<'a> {
865881
ast::Ident::from_str(st)
866882
}
867883
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);
869885
iter::once(Ident::new(kw::DollarCrate, def_site))
870886
.chain(components.iter().map(|&s| Ident::with_dummy_span(s)))
871887
.collect()

src/libsyntax/ext/expand.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
565565
return fragment_kind.dummy(span);
566566
}
567567
let meta = ast::MetaItem { node: ast::MetaItemKind::Word, span, path };
568-
let span = span.with_ctxt(self.cx.backtrace());
569568
let items = expander.expand(self.cx, span, &meta, item);
570569
fragment_kind.expect_from_annotatables(items)
571570
}

src/libsyntax/ext/proc_macro_server.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::tokenstream::{self, DelimSpan, IsJoint::*, TokenStream, TreeAndJoint}
77
use errors::{Diagnostic, DiagnosticBuilder};
88
use rustc_data_structures::sync::Lrc;
99
use syntax_pos::{BytePos, FileName, MultiSpan, Pos, SourceFile, Span};
10-
use syntax_pos::hygiene::{SyntaxContext, Transparency};
1110
use syntax_pos::symbol::{kw, sym, Symbol};
1211

1312
use proc_macro::{Delimiter, Level, LineColumn, Spacing};
@@ -363,16 +362,10 @@ impl<'a> Rustc<'a> {
363362
pub fn new(cx: &'a ExtCtxt<'_>) -> Self {
364363
// No way to determine def location for a proc macro right now, so use call location.
365364
let location = cx.current_expansion.id.expn_data().call_site;
366-
let to_span = |transparency| {
367-
location.with_ctxt(
368-
SyntaxContext::root()
369-
.apply_mark_with_transparency(cx.current_expansion.id, transparency),
370-
)
371-
};
372365
Rustc {
373366
sess: cx.parse_sess,
374-
def_site: to_span(Transparency::Opaque),
375-
call_site: to_span(Transparency::Transparent),
367+
def_site: cx.with_def_site_ctxt(location),
368+
call_site: cx.with_call_site_ctxt(location),
376369
}
377370
}
378371

src/libsyntax_ext/asm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt<'_>,
6363
MacEager::expr(P(ast::Expr {
6464
id: ast::DUMMY_NODE_ID,
6565
node: ast::ExprKind::InlineAsm(P(inline_asm)),
66-
span: sp.with_ctxt(cx.backtrace()),
66+
span: cx.with_legacy_ctxt(sp),
6767
attrs: ThinVec::new(),
6868
}))
6969
}

src/libsyntax_ext/assert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn expand_assert<'cx>(
2323
}
2424
};
2525

26-
let sp = sp.apply_mark(cx.current_expansion.id);
26+
let sp = cx.with_legacy_ctxt(sp);
2727
let panic_call = Mac {
2828
path: Path::from_ident(Ident::new(sym::panic, sp)),
2929
tts: custom_message.unwrap_or_else(|| {

src/libsyntax_ext/cfg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub fn expand_cfg(
1616
sp: Span,
1717
tts: &[tokenstream::TokenTree],
1818
) -> Box<dyn base::MacResult + 'static> {
19-
let sp = sp.apply_mark(cx.current_expansion.id);
19+
let sp = cx.with_legacy_ctxt(sp);
2020

2121
match parse_cfg(cx, sp, tts) {
2222
Ok(cfg) => {

src/libsyntax_ext/concat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ pub fn expand_syntax_ext(
5959
} else if has_errors {
6060
return DummyResult::any(sp);
6161
}
62-
let sp = sp.apply_mark(cx.current_expansion.id);
62+
let sp = cx.with_legacy_ctxt(sp);
6363
base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&accumulator)))
6464
}

src/libsyntax_ext/concat_idents.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn expand_syntax_ext<'cx>(cx: &'cx mut ExtCtxt<'_>,
3939
}
4040
}
4141

42-
let ident = ast::Ident::new(Symbol::intern(&res_str), sp.apply_mark(cx.current_expansion.id));
42+
let ident = ast::Ident::new(Symbol::intern(&res_str), cx.with_legacy_ctxt(sp));
4343

4444
struct ConcatIdentsResult { ident: ast::Ident }
4545

src/libsyntax_ext/deriving/clone.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn cs_clone_shallow(name: &str,
112112
ty: P<ast::Ty>, span: Span, helper_name: &str) {
113113
// Generate statement `let _: helper_name<ty>;`,
114114
// set the expn ID so we can use the unstable struct.
115-
let span = span.with_ctxt(cx.backtrace());
115+
let span = cx.with_def_site_ctxt(span);
116116
let assert_path = cx.path_all(span, true,
117117
cx.std_path(&[sym::clone, Symbol::intern(helper_name)]),
118118
vec![GenericArg::Type(ty)], vec![]);

src/libsyntax_ext/deriving/cmp/eq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn cs_total_eq_assert(cx: &mut ExtCtxt<'_>,
5353
ty: P<ast::Ty>, span: Span, helper_name: &str) {
5454
// Generate statement `let _: helper_name<ty>;`,
5555
// set the expn ID so we can use the unstable struct.
56-
let span = span.with_ctxt(cx.backtrace());
56+
let span = cx.with_def_site_ctxt(span);
5757
let assert_path = cx.path_all(span, true,
5858
cx.std_path(&[sym::cmp, Symbol::intern(helper_name)]),
5959
vec![GenericArg::Type(ty)], vec![]);

0 commit comments

Comments
 (0)