Skip to content

Commit 4581c4e

Browse files
committed
Auto merge of #87042 - petrochenkov:cleanquotspan, r=Aaron1011
Cleanup span quoting I finally got to reviewing #84278. See the individual commit messages. r? `@Aaron1011`
2 parents 0d76b73 + 4ba91a0 commit 4581c4e

File tree

23 files changed

+89
-203
lines changed

23 files changed

+89
-203
lines changed

compiler/rustc_builtin_macros/src/lib.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use crate::deriving::*;
1919

2020
use rustc_expand::base::{MacroExpanderFn, ResolverExpand, SyntaxExtensionKind};
2121
use rustc_expand::proc_macro::BangProcMacro;
22-
use rustc_span::def_id::LOCAL_CRATE;
2322
use rustc_span::symbol::sym;
2423

2524
mod asm;
@@ -113,8 +112,5 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
113112
}
114113

115114
let client = proc_macro::bridge::client::Client::expand1(proc_macro::quote);
116-
register(
117-
sym::quote,
118-
SyntaxExtensionKind::Bang(Box::new(BangProcMacro { client, krate: LOCAL_CRATE })),
119-
);
115+
register(sym::quote, SyntaxExtensionKind::Bang(Box::new(BangProcMacro { client })));
120116
}

compiler/rustc_errors/src/emitter.rs

+3-18
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,7 @@ pub trait Emitter {
309309
// are some which do actually involve macros.
310310
ExpnKind::Inlined | ExpnKind::Desugaring(..) | ExpnKind::AstPass(..) => None,
311311

312-
ExpnKind::Macro { kind: macro_kind, name, proc_macro: _ } => {
313-
Some((macro_kind, name))
314-
}
312+
ExpnKind::Macro(macro_kind, name) => Some((macro_kind, name)),
315313
}
316314
});
317315

@@ -372,19 +370,10 @@ pub trait Emitter {
372370
new_labels
373371
.push((trace.call_site, "in the inlined copy of this code".to_string()));
374372
} else if always_backtrace {
375-
let proc_macro = if let ExpnKind::Macro { kind: _, name: _, proc_macro: true } =
376-
trace.kind
377-
{
378-
"procedural macro "
379-
} else {
380-
""
381-
};
382-
383373
new_labels.push((
384374
trace.def_site,
385375
format!(
386-
"in this expansion of {}`{}`{}",
387-
proc_macro,
376+
"in this expansion of `{}`{}",
388377
trace.kind.descr(),
389378
if macro_backtrace.len() > 1 {
390379
// if macro_backtrace.len() == 1 it'll be
@@ -410,11 +399,7 @@ pub trait Emitter {
410399
// and it needs an "in this macro invocation" label to match that.
411400
let redundant_span = trace.call_site.contains(sp);
412401

413-
if !redundant_span
414-
&& matches!(
415-
trace.kind,
416-
ExpnKind::Macro { kind: MacroKind::Bang, name: _, proc_macro: _ }
417-
)
402+
if !redundant_span && matches!(trace.kind, ExpnKind::Macro(MacroKind::Bang, _))
418403
|| always_backtrace
419404
{
420405
new_labels.push((

compiler/rustc_expand/src/base.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -811,16 +811,8 @@ impl SyntaxExtension {
811811
macro_def_id: Option<DefId>,
812812
parent_module: Option<DefId>,
813813
) -> ExpnData {
814-
use SyntaxExtensionKind::*;
815-
let proc_macro = match self.kind {
816-
// User-defined proc macro
817-
Bang(..) | Attr(..) | Derive(..) => true,
818-
// Consider everthing else to be not a proc
819-
// macro for diagnostic purposes
820-
LegacyBang(..) | LegacyAttr(..) | NonMacroAttr { .. } | LegacyDerive(..) => false,
821-
};
822814
ExpnData::new(
823-
ExpnKind::Macro { kind: self.macro_kind(), name: descr, proc_macro },
815+
ExpnKind::Macro(self.macro_kind(), descr),
824816
parent,
825817
call_site,
826818
self.span,

compiler/rustc_expand/src/proc_macro.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ use rustc_data_structures::sync::Lrc;
99
use rustc_errors::ErrorReported;
1010
use rustc_parse::nt_to_tokenstream;
1111
use rustc_parse::parser::ForceCollect;
12-
use rustc_span::def_id::CrateNum;
1312
use rustc_span::{Span, DUMMY_SP};
1413

1514
const EXEC_STRATEGY: pm::bridge::server::SameThread = pm::bridge::server::SameThread;
1615

1716
pub struct BangProcMacro {
1817
pub client: pm::bridge::client::Client<fn(pm::TokenStream) -> pm::TokenStream>,
19-
pub krate: CrateNum,
2018
}
2119

2220
impl base::ProcMacro for BangProcMacro {
@@ -26,7 +24,7 @@ impl base::ProcMacro for BangProcMacro {
2624
span: Span,
2725
input: TokenStream,
2826
) -> Result<TokenStream, ErrorReported> {
29-
let server = proc_macro_server::Rustc::new(ecx, self.krate);
27+
let server = proc_macro_server::Rustc::new(ecx);
3028
self.client.run(&EXEC_STRATEGY, server, input, ecx.ecfg.proc_macro_backtrace).map_err(|e| {
3129
let mut err = ecx.struct_span_err(span, "proc macro panicked");
3230
if let Some(s) = e.as_str() {
@@ -40,7 +38,6 @@ impl base::ProcMacro for BangProcMacro {
4038

4139
pub struct AttrProcMacro {
4240
pub client: pm::bridge::client::Client<fn(pm::TokenStream, pm::TokenStream) -> pm::TokenStream>,
43-
pub krate: CrateNum,
4441
}
4542

4643
impl base::AttrProcMacro for AttrProcMacro {
@@ -51,7 +48,7 @@ impl base::AttrProcMacro for AttrProcMacro {
5148
annotation: TokenStream,
5249
annotated: TokenStream,
5350
) -> Result<TokenStream, ErrorReported> {
54-
let server = proc_macro_server::Rustc::new(ecx, self.krate);
51+
let server = proc_macro_server::Rustc::new(ecx);
5552
self.client
5653
.run(&EXEC_STRATEGY, server, annotation, annotated, ecx.ecfg.proc_macro_backtrace)
5754
.map_err(|e| {
@@ -67,7 +64,6 @@ impl base::AttrProcMacro for AttrProcMacro {
6764

6865
pub struct ProcMacroDerive {
6966
pub client: pm::bridge::client::Client<fn(pm::TokenStream) -> pm::TokenStream>,
70-
pub krate: CrateNum,
7167
}
7268

7369
impl MultiItemModifier for ProcMacroDerive {
@@ -101,7 +97,7 @@ impl MultiItemModifier for ProcMacroDerive {
10197
nt_to_tokenstream(&item, &ecx.sess.parse_sess, CanSynthesizeMissingTokens::No)
10298
};
10399

104-
let server = proc_macro_server::Rustc::new(ecx, self.krate);
100+
let server = proc_macro_server::Rustc::new(ecx);
105101
let stream =
106102
match self.client.run(&EXEC_STRATEGY, server, input, ecx.ecfg.proc_macro_backtrace) {
107103
Ok(stream) => stream,

compiler/rustc_expand/src/proc_macro_server.rs

+12-29
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> {
371-
pub fn new(cx: &'a ExtCtxt<'_>, krate: CrateNum) -> Self {
369+
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,
384-
krate,
385-
expn_id: cx.current_expansion.id,
378+
krate: expn_data.macro_def_id.unwrap().krate,
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
}
@@ -812,7 +795,7 @@ fn ident_name_compatibility_hack(
812795
rustc: &mut Rustc<'_>,
813796
) -> Option<(rustc_span::symbol::Ident, bool)> {
814797
if let NtIdent(ident, is_raw) = nt {
815-
if let ExpnKind::Macro { name: macro_name, .. } = orig_span.ctxt().outer_expn_data().kind {
798+
if let ExpnKind::Macro(_, macro_name) = orig_span.ctxt().outer_expn_data().kind {
816799
let source_map = rustc.sess.source_map();
817800
let filename = source_map.span_to_filename(orig_span);
818801
if let FileName::Real(RealFileName::LocalPath(path)) = filename {

compiler/rustc_lint/src/internal.rs

+4-15
Original file line numberDiff line numberDiff line change
@@ -248,21 +248,10 @@ impl EarlyLintPass for LintPassImpl {
248248
if last.ident.name == sym::LintPass {
249249
let expn_data = lint_pass.path.span.ctxt().outer_expn_data();
250250
let call_site = expn_data.call_site;
251-
if !matches!(
252-
expn_data.kind,
253-
ExpnKind::Macro {
254-
kind: MacroKind::Bang,
255-
name: sym::impl_lint_pass,
256-
proc_macro: _
257-
}
258-
) && !matches!(
259-
call_site.ctxt().outer_expn_data().kind,
260-
ExpnKind::Macro {
261-
kind: MacroKind::Bang,
262-
name: sym::declare_lint_pass,
263-
proc_macro: _
264-
}
265-
) {
251+
if expn_data.kind != ExpnKind::Macro(MacroKind::Bang, sym::impl_lint_pass)
252+
&& call_site.ctxt().outer_expn_data().kind
253+
!= ExpnKind::Macro(MacroKind::Bang, sym::declare_lint_pass)
254+
{
266255
cx.struct_span_lint(
267256
LINT_PASS_IMPL_WITHOUT_MACRO,
268257
lint_pass.path.span,

compiler/rustc_lint/src/non_fmt_panic.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,6 @@ fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span,
256256
}
257257

258258
let macro_symbol =
259-
if let hygiene::ExpnKind::Macro { kind: _, name: symbol, proc_macro: _ } = expn.kind {
260-
symbol
261-
} else {
262-
Symbol::intern("panic")
263-
};
259+
if let hygiene::ExpnKind::Macro(_, symbol) = expn.kind { symbol } else { sym::panic };
264260
(expn.call_site, panic_macro, macro_symbol.as_str())
265261
}

compiler/rustc_metadata/src/rmeta/decoder.rs

+11-18
Original file line numberDiff line numberDiff line change
@@ -725,37 +725,30 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
725725
.decode((self, sess))
726726
}
727727

728-
fn load_proc_macro(&self, def_id: DefId, sess: &Session) -> SyntaxExtension {
729-
let (name, kind, helper_attrs) = match *self.raw_proc_macro(def_id.index) {
728+
fn load_proc_macro(&self, id: DefIndex, sess: &Session) -> SyntaxExtension {
729+
let (name, kind, helper_attrs) = match *self.raw_proc_macro(id) {
730730
ProcMacro::CustomDerive { trait_name, attributes, client } => {
731731
let helper_attrs =
732732
attributes.iter().cloned().map(Symbol::intern).collect::<Vec<_>>();
733733
(
734734
trait_name,
735-
SyntaxExtensionKind::Derive(Box::new(ProcMacroDerive {
736-
client,
737-
krate: def_id.krate,
738-
})),
735+
SyntaxExtensionKind::Derive(Box::new(ProcMacroDerive { client })),
739736
helper_attrs,
740737
)
741738
}
742-
ProcMacro::Attr { name, client } => (
743-
name,
744-
SyntaxExtensionKind::Attr(Box::new(AttrProcMacro { client, krate: def_id.krate })),
745-
Vec::new(),
746-
),
747-
ProcMacro::Bang { name, client } => (
748-
name,
749-
SyntaxExtensionKind::Bang(Box::new(BangProcMacro { client, krate: def_id.krate })),
750-
Vec::new(),
751-
),
739+
ProcMacro::Attr { name, client } => {
740+
(name, SyntaxExtensionKind::Attr(Box::new(AttrProcMacro { client })), Vec::new())
741+
}
742+
ProcMacro::Bang { name, client } => {
743+
(name, SyntaxExtensionKind::Bang(Box::new(BangProcMacro { client })), Vec::new())
744+
}
752745
};
753746

754-
let attrs: Vec<_> = self.get_item_attrs(def_id.index, sess).collect();
747+
let attrs: Vec<_> = self.get_item_attrs(id, sess).collect();
755748
SyntaxExtension::new(
756749
sess,
757750
kind,
758-
self.get_span(def_id.index, sess),
751+
self.get_span(id, sess),
759752
helper_attrs,
760753
self.root.edition,
761754
Symbol::intern(name),

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ impl CStore {
411411

412412
let data = self.get_crate_data(id.krate);
413413
if data.root.is_proc_macro_crate() {
414-
return LoadedMacro::ProcMacro(data.load_proc_macro(id, sess));
414+
return LoadedMacro::ProcMacro(data.load_proc_macro(id.index, sess));
415415
}
416416

417417
let span = data.get_span(id.index, sess);

compiler/rustc_middle/src/lint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ pub fn in_external_macro(sess: &Session, span: Span) -> bool {
387387
false
388388
}
389389
ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external"
390-
ExpnKind::Macro { kind: MacroKind::Bang, name: _, proc_macro: _ } => {
390+
ExpnKind::Macro(MacroKind::Bang, _) => {
391391
// Dummy span for the `def_site` means it's an external macro.
392392
expn_data.def_site.is_dummy() || sess.source_map().is_imported(expn_data.def_site)
393393
}

compiler/rustc_mir/src/transform/coverage/spans.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,8 @@ impl CoverageSpan {
184184
self.current_macro_or_none
185185
.borrow_mut()
186186
.get_or_insert_with(|| {
187-
if let ExpnKind::Macro {
188-
kind: MacroKind::Bang,
189-
name: current_macro,
190-
proc_macro: _,
191-
} = self.expn_span.ctxt().outer_expn_data().kind
187+
if let ExpnKind::Macro(MacroKind::Bang, current_macro) =
188+
self.expn_span.ctxt().outer_expn_data().kind
192189
{
193190
return Some(current_macro);
194191
}

compiler/rustc_resolve/src/lib.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1775,11 +1775,9 @@ impl<'a> Resolver<'a> {
17751775
let expn_data = expn_id.expn_data();
17761776
match expn_data.kind {
17771777
ExpnKind::Root
1778-
| ExpnKind::Macro {
1779-
kind: MacroKind::Bang | MacroKind::Derive,
1780-
name: _,
1781-
proc_macro: _,
1782-
} => Scope::DeriveHelpersCompat,
1778+
| ExpnKind::Macro(MacroKind::Bang | MacroKind::Derive, _) => {
1779+
Scope::DeriveHelpersCompat
1780+
}
17831781
_ => Scope::DeriveHelpers(expn_data.parent),
17841782
}
17851783
}

compiler/rustc_resolve/src/macros.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,7 @@ impl<'a> ResolverExpand for Resolver<'a> {
319319
let expn_data = expn_id.expn_data();
320320
match expn_data.kind {
321321
ExpnKind::Root
322-
| ExpnKind::Macro {
323-
name: _,
324-
kind: MacroKind::Bang | MacroKind::Derive,
325-
proc_macro: _,
326-
} => {
322+
| ExpnKind::Macro(MacroKind::Bang | MacroKind::Derive, _) => {
327323
break;
328324
}
329325
_ => expn_id = expn_data.parent,

compiler/rustc_save_analysis/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ impl<'tcx> SaveContext<'tcx> {
788788
let callee = span.source_callee()?;
789789

790790
let mac_name = match callee.kind {
791-
ExpnKind::Macro { kind, name, proc_macro: _ } => match kind {
791+
ExpnKind::Macro(kind, name) => match kind {
792792
MacroKind::Bang => name,
793793

794794
// Ignore attribute macros, their spans are usually mangled

0 commit comments

Comments
 (0)