Skip to content

Commit bd94288

Browse files
committed
Stop emitting spans from proc macro compile time in quote expansion
Before this commit if the proc_macro::quote!{} macro was used, the span of each token as written in the source of the proc macro itself would be saved in the crate metadata of the proc macro and then recovered at proc macro runtime to forward this to the macro expansion of the proc macro. This commit stops doing this and instead generates def-site spans for each token. This removes the only case where spans from the proc macro source have a semantic effect on the compilation of crates that use the proc macro. This makes it easier to stop requiring all dependencies of proc macros to be present when using the proc macro. And will make it easier to stop requiring a proc macro to be present when using a crate that used this proc macro internally but doesn't expose it as part of it's public api. The latter is necessary to be able to cross-compile tools that link against rustc internals without requiring to be built as part of rustc with the -Zdual-proc-macro hack. It may also enable using proc macros inside the standard library or it's dependencies without breaking cross-compilation.
1 parent b56aaec commit bd94288

File tree

11 files changed

+5
-118
lines changed

11 files changed

+5
-118
lines changed

compiler/rustc_expand/src/base.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_parse::parser::{ForceCollect, Parser};
2424
use rustc_session::config::CollapseMacroDebuginfo;
2525
use rustc_session::parse::ParseSess;
2626
use rustc_session::{Limit, Session};
27-
use rustc_span::def_id::{CrateNum, DefId, LocalDefId};
27+
use rustc_span::def_id::{DefId, LocalDefId};
2828
use rustc_span::edition::Edition;
2929
use rustc_span::hygiene::{AstPass, ExpnData, ExpnKind, LocalExpnId, MacroKind};
3030
use rustc_span::source_map::SourceMap;
@@ -1113,10 +1113,6 @@ pub trait ResolverExpand {
11131113
path: &ast::Path,
11141114
) -> Result<bool, Indeterminate>;
11151115

1116-
/// Decodes the proc-macro quoted span in the specified crate, with the specified id.
1117-
/// No caching is performed.
1118-
fn get_proc_macro_quoted_span(&self, krate: CrateNum, id: usize) -> Span;
1119-
11201116
/// The order of items in the HIR is unrelated to the order of
11211117
/// items in the AST. However, we generate proc macro harnesses
11221118
/// based on the AST order, and later refer to these harnesses

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc_ast::token;
66
use rustc_ast::tokenstream::{self, DelimSpacing, Spacing, TokenStream};
77
use rustc_ast::util::literal::escape_byte_str_symbol;
88
use rustc_ast_pretty::pprust;
9-
use rustc_data_structures::fx::FxHashMap;
109
use rustc_errors::{Diag, ErrorGuaranteed, MultiSpan, PResult};
1110
use rustc_parse::lexer::nfc_normalize;
1211
use rustc_parse::parser::Parser;
@@ -16,7 +15,6 @@ use rustc_proc_macro::bridge::{
1615
};
1716
use rustc_proc_macro::{Delimiter, Level};
1817
use rustc_session::parse::ParseSess;
19-
use rustc_span::def_id::CrateNum;
2018
use rustc_span::{BytePos, FileName, Pos, Span, Symbol, sym};
2119
use smallvec::{SmallVec, smallvec};
2220

@@ -432,8 +430,6 @@ pub(crate) struct Rustc<'a, 'b> {
432430
def_site: Span,
433431
call_site: Span,
434432
mixed_site: Span,
435-
krate: CrateNum,
436-
rebased_spans: FxHashMap<usize, Span>,
437433
}
438434

439435
impl<'a, 'b> Rustc<'a, 'b> {
@@ -443,8 +439,6 @@ impl<'a, 'b> Rustc<'a, 'b> {
443439
def_site: ecx.with_def_site_ctxt(expn_data.def_site),
444440
call_site: ecx.with_call_site_ctxt(expn_data.call_site),
445441
mixed_site: ecx.with_mixed_site_ctxt(expn_data.call_site),
446-
krate: expn_data.macro_def_id.unwrap().krate,
447-
rebased_spans: FxHashMap::default(),
448442
ecx,
449443
}
450444
}
@@ -788,43 +782,6 @@ impl server::Span for Rustc<'_, '_> {
788782
fn source_text(&mut self, span: Self::Span) -> Option<String> {
789783
self.psess().source_map().span_to_snippet(span).ok()
790784
}
791-
792-
/// Saves the provided span into the metadata of
793-
/// *the crate we are currently compiling*, which must
794-
/// be a proc-macro crate. This id can be passed to
795-
/// `recover_proc_macro_span` when our current crate
796-
/// is *run* as a proc-macro.
797-
///
798-
/// Let's suppose that we have two crates - `my_client`
799-
/// and `my_proc_macro`. The `my_proc_macro` crate
800-
/// contains a procedural macro `my_macro`, which
801-
/// is implemented as: `quote! { "hello" }`
802-
///
803-
/// When we *compile* `my_proc_macro`, we will execute
804-
/// the `quote` proc-macro. This will save the span of
805-
/// "hello" into the metadata of `my_proc_macro`. As a result,
806-
/// the body of `my_proc_macro` (after expansion) will end
807-
/// up containing a call that looks like this:
808-
/// `proc_macro::Ident::new("hello", proc_macro::Span::recover_proc_macro_span(0))`
809-
///
810-
/// where `0` is the id returned by this function.
811-
/// When `my_proc_macro` *executes* (during the compilation of `my_client`),
812-
/// the call to `recover_proc_macro_span` will load the corresponding
813-
/// span from the metadata of `my_proc_macro` (which we have access to,
814-
/// since we've loaded `my_proc_macro` from disk in order to execute it).
815-
/// In this way, we have obtained a span pointing into `my_proc_macro`
816-
fn save_span(&mut self, span: Self::Span) -> usize {
817-
self.psess().save_proc_macro_span(span)
818-
}
819-
820-
fn recover_proc_macro_span(&mut self, id: usize) -> Self::Span {
821-
let (resolver, krate, def_site) = (&*self.ecx.resolver, self.krate, self.def_site);
822-
*self.rebased_spans.entry(id).or_insert_with(|| {
823-
// FIXME: `SyntaxContext` for spans from proc macro crates is lost during encoding,
824-
// replace it with a def-site context until we are encoding it properly.
825-
resolver.get_proc_macro_quoted_span(krate, id).with_ctxt(def_site.ctxt())
826-
})
827-
}
828785
}
829786

830787
impl server::Symbol for Rustc<'_, '_> {

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,15 +1470,6 @@ impl<'a> CrateMetadataRef<'a> {
14701470
self.root.native_libraries.decode((self, sess))
14711471
}
14721472

1473-
fn get_proc_macro_quoted_span(self, index: usize, sess: &Session) -> Span {
1474-
self.root
1475-
.tables
1476-
.proc_macro_quoted_spans
1477-
.get(self, index)
1478-
.unwrap_or_else(|| panic!("Missing proc macro quoted span: {index:?}"))
1479-
.decode((self, sess))
1480-
}
1481-
14821473
fn get_foreign_modules(self, sess: &'a Session) -> impl Iterator<Item = ForeignModule> {
14831474
self.root.foreign_modules.decode((self, sess))
14841475
}

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -606,15 +606,6 @@ impl CStore {
606606
self.get_crate_data(cnum).num_def_ids()
607607
}
608608

609-
pub fn get_proc_macro_quoted_span_untracked(
610-
&self,
611-
cnum: CrateNum,
612-
id: usize,
613-
sess: &Session,
614-
) -> Span {
615-
self.get_crate_data(cnum).get_proc_macro_quoted_span(id, sess)
616-
}
617-
618609
pub fn set_used_recursively(&mut self, cnum: CrateNum) {
619610
let cmeta = self.get_crate_data_mut(cnum);
620611
if !cmeta.used {

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,10 +1933,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
19331933
let stability = tcx.lookup_stability(CRATE_DEF_ID);
19341934
let macros =
19351935
self.lazy_array(tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index));
1936-
for (i, span) in self.tcx.sess.psess.proc_macro_quoted_spans() {
1937-
let span = self.lazy(span);
1938-
self.tables.proc_macro_quoted_spans.set_some(i, span);
1939-
}
19401936

19411937
self.tables.def_kind.set_some(LOCAL_CRATE.as_def_id().index, DefKind::Mod);
19421938
record!(self.tables.def_span[LOCAL_CRATE.as_def_id()] <- tcx.def_span(LOCAL_CRATE.as_def_id()));

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,6 @@ define_tables! {
469469
// `DefPathTable` up front, since we may only ever use a few
470470
// definitions from any given crate.
471471
def_keys: Table<DefIndex, LazyValue<DefKey>>,
472-
proc_macro_quoted_spans: Table<usize, LazyValue<Span>>,
473472
variant_data: Table<DefIndex, LazyValue<VariantData>>,
474473
assoc_container: Table<DefIndex, ty::AssocItemContainer>,
475474
macro_definition: Table<DefIndex, LazyValue<ast::DelimArgs>>,

compiler/rustc_resolve/src/macros.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_expand::expand::{
1919
};
2020
use rustc_expand::{MacroRulesMacroExpander, compile_declarative_macro};
2121
use rustc_hir::def::{self, DefKind, Namespace, NonMacroAttrKind};
22-
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
22+
use rustc_hir::def_id::{DefId, LocalDefId};
2323
use rustc_middle::middle::stability;
2424
use rustc_middle::ty::{RegisteredTools, TyCtxt};
2525
use rustc_session::lint::BuiltinLintDiag;
@@ -480,10 +480,6 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
480480
self.path_accessible(expn_id, path, &[MacroNS])
481481
}
482482

483-
fn get_proc_macro_quoted_span(&self, krate: CrateNum, id: usize) -> Span {
484-
self.cstore().get_proc_macro_quoted_span_untracked(krate, id, self.tcx.sess)
485-
}
486-
487483
fn declare_proc_macro(&mut self, id: NodeId) {
488484
self.proc_macros.push(self.local_def_id(id))
489485
}

compiler/rustc_session/src/parse.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,6 @@ pub struct ParseSess {
273273
pub file_depinfo: Lock<FxIndexSet<Symbol>>,
274274
/// Whether cfg(version) should treat the current release as incomplete
275275
pub assume_incomplete_release: bool,
276-
/// Spans passed to `proc_macro::quote_span`. Each span has a numerical
277-
/// identifier represented by its position in the vector.
278-
proc_macro_quoted_spans: AppendOnlyVec<Span>,
279276
/// Used to generate new `AttrId`s. Every `AttrId` is unique.
280277
pub attr_id_generator: AttrIdGenerator,
281278
}
@@ -310,7 +307,6 @@ impl ParseSess {
310307
env_depinfo: Default::default(),
311308
file_depinfo: Default::default(),
312309
assume_incomplete_release: false,
313-
proc_macro_quoted_spans: Default::default(),
314310
attr_id_generator: AttrIdGenerator::new(),
315311
}
316312
}
@@ -364,16 +360,6 @@ impl ParseSess {
364360
});
365361
}
366362

367-
pub fn save_proc_macro_span(&self, span: Span) -> usize {
368-
self.proc_macro_quoted_spans.push(span)
369-
}
370-
371-
pub fn proc_macro_quoted_spans(&self) -> impl Iterator<Item = (usize, Span)> {
372-
// This is equivalent to `.iter().copied().enumerate()`, but that isn't possible for
373-
// AppendOnlyVec, so we resort to this scheme.
374-
self.proc_macro_quoted_spans.iter_enumerated()
375-
}
376-
377363
pub fn dcx(&self) -> DiagCtxtHandle<'_> {
378364
self.dcx.handle()
379365
}

library/proc_macro/src/bridge/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ macro_rules! with_api {
9393
fn subspan($self: $S::Span, start: Bound<usize>, end: Bound<usize>) -> Option<$S::Span>;
9494
fn resolved_at($self: $S::Span, at: $S::Span) -> $S::Span;
9595
fn source_text($self: $S::Span) -> Option<String>;
96-
fn save_span($self: $S::Span) -> usize;
97-
fn recover_proc_macro_span(id: usize) -> $S::Span;
9896
},
9997
Symbol {
10098
fn normalize_and_validate_ident(string: &str) -> Result<$S::Symbol, ()>;

library/proc_macro/src/lib.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ impl Default for TokenStream {
238238
}
239239

240240
#[unstable(feature = "proc_macro_quote", issue = "54722")]
241-
pub use quote::{HasIterator, RepInterp, ThereIsNoIteratorInRepetition, ext, quote, quote_span};
241+
pub use quote::{HasIterator, RepInterp, ThereIsNoIteratorInRepetition, ext, quote};
242242

243243
fn tree_to_bridge_tree(
244244
tree: TokenTree,
@@ -601,20 +601,6 @@ impl Span {
601601
self.0.source_text()
602602
}
603603

604-
// Used by the implementation of `Span::quote`
605-
#[doc(hidden)]
606-
#[unstable(feature = "proc_macro_internals", issue = "27812")]
607-
pub fn save_span(&self) -> usize {
608-
self.0.save_span()
609-
}
610-
611-
// Used by the implementation of `Span::quote`
612-
#[doc(hidden)]
613-
#[unstable(feature = "proc_macro_internals", issue = "27812")]
614-
pub fn recover_proc_macro_span(id: usize) -> Span {
615-
Span(bridge::client::Span::recover_proc_macro_span(id))
616-
}
617-
618604
diagnostic_method!(error, Level::Error);
619605
diagnostic_method!(warning, Level::Warning);
620606
diagnostic_method!(note, Level::Note);

0 commit comments

Comments
 (0)