Skip to content

Commit

Permalink
Auto merge of rust-lang#87042 - petrochenkov:cleanquotspan, r=Aaron1011
Browse files Browse the repository at this point in the history
Cleanup span quoting

I finally got to reviewing rust-lang#84278.
See the individual commit messages.
r? `@Aaron1011`
  • Loading branch information
bors committed Jul 11, 2021
2 parents 0d76b73 + 4ba91a0 commit 4581c4e
Show file tree
Hide file tree
Showing 23 changed files with 89 additions and 203 deletions.
6 changes: 1 addition & 5 deletions compiler/rustc_builtin_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use crate::deriving::*;

use rustc_expand::base::{MacroExpanderFn, ResolverExpand, SyntaxExtensionKind};
use rustc_expand::proc_macro::BangProcMacro;
use rustc_span::def_id::LOCAL_CRATE;
use rustc_span::symbol::sym;

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

let client = proc_macro::bridge::client::Client::expand1(proc_macro::quote);
register(
sym::quote,
SyntaxExtensionKind::Bang(Box::new(BangProcMacro { client, krate: LOCAL_CRATE })),
);
register(sym::quote, SyntaxExtensionKind::Bang(Box::new(BangProcMacro { client })));
}
21 changes: 3 additions & 18 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,7 @@ pub trait Emitter {
// are some which do actually involve macros.
ExpnKind::Inlined | ExpnKind::Desugaring(..) | ExpnKind::AstPass(..) => None,

ExpnKind::Macro { kind: macro_kind, name, proc_macro: _ } => {
Some((macro_kind, name))
}
ExpnKind::Macro(macro_kind, name) => Some((macro_kind, name)),
}
});

Expand Down Expand Up @@ -372,19 +370,10 @@ pub trait Emitter {
new_labels
.push((trace.call_site, "in the inlined copy of this code".to_string()));
} else if always_backtrace {
let proc_macro = if let ExpnKind::Macro { kind: _, name: _, proc_macro: true } =
trace.kind
{
"procedural macro "
} else {
""
};

new_labels.push((
trace.def_site,
format!(
"in this expansion of {}`{}`{}",
proc_macro,
"in this expansion of `{}`{}",
trace.kind.descr(),
if macro_backtrace.len() > 1 {
// if macro_backtrace.len() == 1 it'll be
Expand All @@ -410,11 +399,7 @@ pub trait Emitter {
// and it needs an "in this macro invocation" label to match that.
let redundant_span = trace.call_site.contains(sp);

if !redundant_span
&& matches!(
trace.kind,
ExpnKind::Macro { kind: MacroKind::Bang, name: _, proc_macro: _ }
)
if !redundant_span && matches!(trace.kind, ExpnKind::Macro(MacroKind::Bang, _))
|| always_backtrace
{
new_labels.push((
Expand Down
10 changes: 1 addition & 9 deletions compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,16 +811,8 @@ impl SyntaxExtension {
macro_def_id: Option<DefId>,
parent_module: Option<DefId>,
) -> ExpnData {
use SyntaxExtensionKind::*;
let proc_macro = match self.kind {
// User-defined proc macro
Bang(..) | Attr(..) | Derive(..) => true,
// Consider everthing else to be not a proc
// macro for diagnostic purposes
LegacyBang(..) | LegacyAttr(..) | NonMacroAttr { .. } | LegacyDerive(..) => false,
};
ExpnData::new(
ExpnKind::Macro { kind: self.macro_kind(), name: descr, proc_macro },
ExpnKind::Macro(self.macro_kind(), descr),
parent,
call_site,
self.span,
Expand Down
10 changes: 3 additions & 7 deletions compiler/rustc_expand/src/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ use rustc_data_structures::sync::Lrc;
use rustc_errors::ErrorReported;
use rustc_parse::nt_to_tokenstream;
use rustc_parse::parser::ForceCollect;
use rustc_span::def_id::CrateNum;
use rustc_span::{Span, DUMMY_SP};

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

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

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

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

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

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

impl MultiItemModifier for ProcMacroDerive {
Expand Down Expand Up @@ -101,7 +97,7 @@ impl MultiItemModifier for ProcMacroDerive {
nt_to_tokenstream(&item, &ecx.sess.parse_sess, CanSynthesizeMissingTokens::No)
};

let server = proc_macro_server::Rustc::new(ecx, self.krate);
let server = proc_macro_server::Rustc::new(ecx);
let stream =
match self.client.run(&EXEC_STRATEGY, server, input, ecx.ecfg.proc_macro_backtrace) {
Ok(stream) => stream,
Expand Down
41 changes: 12 additions & 29 deletions compiler/rustc_expand/src/proc_macro_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use rustc_parse::lexer::nfc_normalize;
use rustc_parse::{nt_to_tokenstream, parse_stream_from_source_str};
use rustc_session::parse::ParseSess;
use rustc_span::def_id::CrateNum;
use rustc_span::hygiene::ExpnId;
use rustc_span::hygiene::ExpnKind;
use rustc_span::symbol::{self, kw, sym, Symbol};
use rustc_span::{BytePos, FileName, MultiSpan, Pos, RealFileName, SourceFile, Span};
Expand Down Expand Up @@ -363,26 +362,20 @@ pub(crate) struct Rustc<'a> {
mixed_site: Span,
span_debug: bool,
krate: CrateNum,
expn_id: ExpnId,
rebased_spans: FxHashMap<usize, Span>,
}

impl<'a> Rustc<'a> {
pub fn new(cx: &'a ExtCtxt<'_>, krate: CrateNum) -> Self {
pub fn new(cx: &'a ExtCtxt<'_>) -> Self {
let expn_data = cx.current_expansion.id.expn_data();
let def_site = cx.with_def_site_ctxt(expn_data.def_site);
let call_site = cx.with_call_site_ctxt(expn_data.call_site);
let mixed_site = cx.with_mixed_site_ctxt(expn_data.call_site);
let sess = cx.parse_sess();
Rustc {
resolver: cx.resolver,
sess,
def_site,
call_site,
mixed_site,
sess: cx.parse_sess(),
def_site: cx.with_def_site_ctxt(expn_data.def_site),
call_site: cx.with_call_site_ctxt(expn_data.call_site),
mixed_site: cx.with_mixed_site_ctxt(expn_data.call_site),
span_debug: cx.ecfg.span_debug,
krate,
expn_id: cx.current_expansion.id,
krate: expn_data.macro_def_id.unwrap().krate,
rebased_spans: FxHashMap::default(),
}
}
Expand Down Expand Up @@ -782,25 +775,15 @@ impl server::Span for Rustc<'_> {
/// span from the metadata of `my_proc_macro` (which we have access to,
/// since we've loaded `my_proc_macro` from disk in order to execute it).
/// In this way, we have obtained a span pointing into `my_proc_macro`
fn save_span(&mut self, mut span: Self::Span) -> usize {
// Throw away the `SyntaxContext`, since we currently
// skip serializing `SyntaxContext`s for proc-macro crates
span = span.with_ctxt(rustc_span::SyntaxContext::root());
fn save_span(&mut self, span: Self::Span) -> usize {
self.sess.save_proc_macro_span(span)
}
fn recover_proc_macro_span(&mut self, id: usize) -> Self::Span {
let resolver = self.resolver;
let krate = self.krate;
let expn_id = self.expn_id;
let (resolver, krate, def_site) = (self.resolver, self.krate, self.def_site);
*self.rebased_spans.entry(id).or_insert_with(|| {
let raw_span = resolver.get_proc_macro_quoted_span(krate, id);
// Ignore the deserialized `SyntaxContext` entirely.
// FIXME: Preserve the macro backtrace from the serialized span
// For example, if a proc-macro crate has code like
// `macro_one!() -> macro_two!() -> quote!()`, we might
// want to 'concatenate' this backtrace with the backtrace from
// our current call site.
raw_span.with_def_site_ctxt(expn_id)
// FIXME: `SyntaxContext` for spans from proc macro crates is lost during encoding,
// replace it with a def-site context until we are encoding it properly.
resolver.get_proc_macro_quoted_span(krate, id).with_ctxt(def_site.ctxt())
})
}
}
Expand All @@ -812,7 +795,7 @@ fn ident_name_compatibility_hack(
rustc: &mut Rustc<'_>,
) -> Option<(rustc_span::symbol::Ident, bool)> {
if let NtIdent(ident, is_raw) = nt {
if let ExpnKind::Macro { name: macro_name, .. } = orig_span.ctxt().outer_expn_data().kind {
if let ExpnKind::Macro(_, macro_name) = orig_span.ctxt().outer_expn_data().kind {
let source_map = rustc.sess.source_map();
let filename = source_map.span_to_filename(orig_span);
if let FileName::Real(RealFileName::LocalPath(path)) = filename {
Expand Down
19 changes: 4 additions & 15 deletions compiler/rustc_lint/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,21 +248,10 @@ impl EarlyLintPass for LintPassImpl {
if last.ident.name == sym::LintPass {
let expn_data = lint_pass.path.span.ctxt().outer_expn_data();
let call_site = expn_data.call_site;
if !matches!(
expn_data.kind,
ExpnKind::Macro {
kind: MacroKind::Bang,
name: sym::impl_lint_pass,
proc_macro: _
}
) && !matches!(
call_site.ctxt().outer_expn_data().kind,
ExpnKind::Macro {
kind: MacroKind::Bang,
name: sym::declare_lint_pass,
proc_macro: _
}
) {
if expn_data.kind != ExpnKind::Macro(MacroKind::Bang, sym::impl_lint_pass)
&& call_site.ctxt().outer_expn_data().kind
!= ExpnKind::Macro(MacroKind::Bang, sym::declare_lint_pass)
{
cx.struct_span_lint(
LINT_PASS_IMPL_WITHOUT_MACRO,
lint_pass.path.span,
Expand Down
6 changes: 1 addition & 5 deletions compiler/rustc_lint/src/non_fmt_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,6 @@ fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span,
}

let macro_symbol =
if let hygiene::ExpnKind::Macro { kind: _, name: symbol, proc_macro: _ } = expn.kind {
symbol
} else {
Symbol::intern("panic")
};
if let hygiene::ExpnKind::Macro(_, symbol) = expn.kind { symbol } else { sym::panic };
(expn.call_site, panic_macro, macro_symbol.as_str())
}
29 changes: 11 additions & 18 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,37 +725,30 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.decode((self, sess))
}

fn load_proc_macro(&self, def_id: DefId, sess: &Session) -> SyntaxExtension {
let (name, kind, helper_attrs) = match *self.raw_proc_macro(def_id.index) {
fn load_proc_macro(&self, id: DefIndex, sess: &Session) -> SyntaxExtension {
let (name, kind, helper_attrs) = match *self.raw_proc_macro(id) {
ProcMacro::CustomDerive { trait_name, attributes, client } => {
let helper_attrs =
attributes.iter().cloned().map(Symbol::intern).collect::<Vec<_>>();
(
trait_name,
SyntaxExtensionKind::Derive(Box::new(ProcMacroDerive {
client,
krate: def_id.krate,
})),
SyntaxExtensionKind::Derive(Box::new(ProcMacroDerive { client })),
helper_attrs,
)
}
ProcMacro::Attr { name, client } => (
name,
SyntaxExtensionKind::Attr(Box::new(AttrProcMacro { client, krate: def_id.krate })),
Vec::new(),
),
ProcMacro::Bang { name, client } => (
name,
SyntaxExtensionKind::Bang(Box::new(BangProcMacro { client, krate: def_id.krate })),
Vec::new(),
),
ProcMacro::Attr { name, client } => {
(name, SyntaxExtensionKind::Attr(Box::new(AttrProcMacro { client })), Vec::new())
}
ProcMacro::Bang { name, client } => {
(name, SyntaxExtensionKind::Bang(Box::new(BangProcMacro { client })), Vec::new())
}
};

let attrs: Vec<_> = self.get_item_attrs(def_id.index, sess).collect();
let attrs: Vec<_> = self.get_item_attrs(id, sess).collect();
SyntaxExtension::new(
sess,
kind,
self.get_span(def_id.index, sess),
self.get_span(id, sess),
helper_attrs,
self.root.edition,
Symbol::intern(name),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ impl CStore {

let data = self.get_crate_data(id.krate);
if data.root.is_proc_macro_crate() {
return LoadedMacro::ProcMacro(data.load_proc_macro(id, sess));
return LoadedMacro::ProcMacro(data.load_proc_macro(id.index, sess));
}

let span = data.get_span(id.index, sess);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ pub fn in_external_macro(sess: &Session, span: Span) -> bool {
false
}
ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external"
ExpnKind::Macro { kind: MacroKind::Bang, name: _, proc_macro: _ } => {
ExpnKind::Macro(MacroKind::Bang, _) => {
// Dummy span for the `def_site` means it's an external macro.
expn_data.def_site.is_dummy() || sess.source_map().is_imported(expn_data.def_site)
}
Expand Down
7 changes: 2 additions & 5 deletions compiler/rustc_mir/src/transform/coverage/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,8 @@ impl CoverageSpan {
self.current_macro_or_none
.borrow_mut()
.get_or_insert_with(|| {
if let ExpnKind::Macro {
kind: MacroKind::Bang,
name: current_macro,
proc_macro: _,
} = self.expn_span.ctxt().outer_expn_data().kind
if let ExpnKind::Macro(MacroKind::Bang, current_macro) =
self.expn_span.ctxt().outer_expn_data().kind
{
return Some(current_macro);
}
Expand Down
8 changes: 3 additions & 5 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1775,11 +1775,9 @@ impl<'a> Resolver<'a> {
let expn_data = expn_id.expn_data();
match expn_data.kind {
ExpnKind::Root
| ExpnKind::Macro {
kind: MacroKind::Bang | MacroKind::Derive,
name: _,
proc_macro: _,
} => Scope::DeriveHelpersCompat,
| ExpnKind::Macro(MacroKind::Bang | MacroKind::Derive, _) => {
Scope::DeriveHelpersCompat
}
_ => Scope::DeriveHelpers(expn_data.parent),
}
}
Expand Down
6 changes: 1 addition & 5 deletions compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,7 @@ impl<'a> ResolverExpand for Resolver<'a> {
let expn_data = expn_id.expn_data();
match expn_data.kind {
ExpnKind::Root
| ExpnKind::Macro {
name: _,
kind: MacroKind::Bang | MacroKind::Derive,
proc_macro: _,
} => {
| ExpnKind::Macro(MacroKind::Bang | MacroKind::Derive, _) => {
break;
}
_ => expn_id = expn_data.parent,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_save_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ impl<'tcx> SaveContext<'tcx> {
let callee = span.source_callee()?;

let mac_name = match callee.kind {
ExpnKind::Macro { kind, name, proc_macro: _ } => match kind {
ExpnKind::Macro(kind, name) => match kind {
MacroKind::Bang => name,

// Ignore attribute macros, their spans are usually mangled
Expand Down
Loading

0 comments on commit 4581c4e

Please sign in to comment.