Skip to content

Commit ece6f68

Browse files
committed
rustc_expand: Simplify span quoting in proc macro server
- The `Rustc::expn_id` field kept redundant information - `SyntaxContext` is no longer thrown away before `save_proc_macro_span` because it's thrown away during metadata encoding anyway
1 parent de897f5 commit ece6f68

File tree

1 file changed

+9
-26
lines changed

1 file changed

+9
-26
lines changed

compiler/rustc_expand/src/proc_macro_server.rs

+9-26
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use rustc_parse::lexer::nfc_normalize;
1414
use rustc_parse::{nt_to_tokenstream, parse_stream_from_source_str};
1515
use rustc_session::parse::ParseSess;
1616
use rustc_span::def_id::CrateNum;
17-
use rustc_span::hygiene::ExpnId;
1817
use rustc_span::hygiene::ExpnKind;
1918
use rustc_span::symbol::{self, kw, sym, Symbol};
2019
use rustc_span::{BytePos, FileName, MultiSpan, Pos, RealFileName, SourceFile, Span};
@@ -363,26 +362,20 @@ pub(crate) struct Rustc<'a> {
363362
mixed_site: Span,
364363
span_debug: bool,
365364
krate: CrateNum,
366-
expn_id: ExpnId,
367365
rebased_spans: FxHashMap<usize, Span>,
368366
}
369367

370368
impl<'a> Rustc<'a> {
371369
pub fn new(cx: &'a ExtCtxt<'_>) -> Self {
372370
let expn_data = cx.current_expansion.id.expn_data();
373-
let def_site = cx.with_def_site_ctxt(expn_data.def_site);
374-
let call_site = cx.with_call_site_ctxt(expn_data.call_site);
375-
let mixed_site = cx.with_mixed_site_ctxt(expn_data.call_site);
376-
let sess = cx.parse_sess();
377371
Rustc {
378372
resolver: cx.resolver,
379-
sess,
380-
def_site,
381-
call_site,
382-
mixed_site,
373+
sess: cx.parse_sess(),
374+
def_site: cx.with_def_site_ctxt(expn_data.def_site),
375+
call_site: cx.with_call_site_ctxt(expn_data.call_site),
376+
mixed_site: cx.with_mixed_site_ctxt(expn_data.call_site),
383377
span_debug: cx.ecfg.span_debug,
384378
krate: expn_data.macro_def_id.unwrap().krate,
385-
expn_id: cx.current_expansion.id,
386379
rebased_spans: FxHashMap::default(),
387380
}
388381
}
@@ -782,25 +775,15 @@ impl server::Span for Rustc<'_> {
782775
/// span from the metadata of `my_proc_macro` (which we have access to,
783776
/// since we've loaded `my_proc_macro` from disk in order to execute it).
784777
/// In this way, we have obtained a span pointing into `my_proc_macro`
785-
fn save_span(&mut self, mut span: Self::Span) -> usize {
786-
// Throw away the `SyntaxContext`, since we currently
787-
// skip serializing `SyntaxContext`s for proc-macro crates
788-
span = span.with_ctxt(rustc_span::SyntaxContext::root());
778+
fn save_span(&mut self, span: Self::Span) -> usize {
789779
self.sess.save_proc_macro_span(span)
790780
}
791781
fn recover_proc_macro_span(&mut self, id: usize) -> Self::Span {
792-
let resolver = self.resolver;
793-
let krate = self.krate;
794-
let expn_id = self.expn_id;
782+
let (resolver, krate, def_site) = (self.resolver, self.krate, self.def_site);
795783
*self.rebased_spans.entry(id).or_insert_with(|| {
796-
let raw_span = resolver.get_proc_macro_quoted_span(krate, id);
797-
// Ignore the deserialized `SyntaxContext` entirely.
798-
// FIXME: Preserve the macro backtrace from the serialized span
799-
// For example, if a proc-macro crate has code like
800-
// `macro_one!() -> macro_two!() -> quote!()`, we might
801-
// want to 'concatenate' this backtrace with the backtrace from
802-
// our current call site.
803-
raw_span.with_def_site_ctxt(expn_id)
784+
// FIXME: `SyntaxContext` for spans from proc macro crates is lost during encoding,
785+
// replace it with a def-site context until we are encoding it properly.
786+
resolver.get_proc_macro_quoted_span(krate, id).with_ctxt(def_site.ctxt())
804787
})
805788
}
806789
}

0 commit comments

Comments
 (0)