Skip to content

Commit f8e5209

Browse files
committed
Auto merge of #79353 - cjgillot:procmacro, r=petrochenkov
Setup proc-macro metadata at encoding instead of decoding This should improve the common non-proc-macro case for metadata decoding.
2 parents 650d9d3 + 9dd32e1 commit f8e5209

File tree

3 files changed

+62
-67
lines changed

3 files changed

+62
-67
lines changed

Diff for: compiler/rustc_metadata/src/rmeta/decoder.rs

+25-62
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ impl EntryKind {
644644
EntryKind::TraitAlias => DefKind::TraitAlias,
645645
EntryKind::Enum(..) => DefKind::Enum,
646646
EntryKind::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
647+
EntryKind::ProcMacro(kind) => DefKind::Macro(kind),
647648
EntryKind::ForeignType => DefKind::ForeignTy,
648649
EntryKind::Impl(_) => DefKind::Impl,
649650
EntryKind::Closure => DefKind::Closure,
@@ -685,20 +686,11 @@ impl CrateRoot<'_> {
685686
}
686687

687688
impl<'a, 'tcx> CrateMetadataRef<'a> {
688-
fn is_proc_macro(&self, id: DefIndex) -> bool {
689-
self.root
690-
.proc_macro_data
691-
.as_ref()
692-
.and_then(|data| data.macros.decode(self).find(|x| *x == id))
693-
.is_some()
694-
}
695-
696689
fn maybe_kind(&self, item_id: DefIndex) -> Option<EntryKind> {
697690
self.root.tables.kind.get(self, item_id).map(|k| k.decode(self))
698691
}
699692

700693
fn kind(&self, item_id: DefIndex) -> EntryKind {
701-
assert!(!self.is_proc_macro(item_id));
702694
self.maybe_kind(item_id).unwrap_or_else(|| {
703695
bug!(
704696
"CrateMetadata::kind({:?}): id not found, in crate {:?} with number {}",
@@ -725,35 +717,24 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
725717
}
726718

727719
fn item_ident(&self, item_index: DefIndex, sess: &Session) -> Ident {
728-
if !self.is_proc_macro(item_index) {
729-
let name = self
730-
.def_key(item_index)
731-
.disambiguated_data
732-
.data
733-
.get_opt_name()
734-
.expect("no name in item_ident");
735-
let span = self
736-
.root
737-
.tables
738-
.ident_span
739-
.get(self, item_index)
740-
.map(|data| data.decode((self, sess)))
741-
.unwrap_or_else(|| panic!("Missing ident span for {:?} ({:?})", name, item_index));
742-
Ident::new(name, span)
743-
} else {
744-
Ident::new(
745-
Symbol::intern(self.raw_proc_macro(item_index).name()),
746-
self.get_span(item_index, sess),
747-
)
748-
}
720+
let name = self
721+
.def_key(item_index)
722+
.disambiguated_data
723+
.data
724+
.get_opt_name()
725+
.expect("no name in item_ident");
726+
let span = self
727+
.root
728+
.tables
729+
.ident_span
730+
.get(self, item_index)
731+
.map(|data| data.decode((self, sess)))
732+
.unwrap_or_else(|| panic!("Missing ident span for {:?} ({:?})", name, item_index));
733+
Ident::new(name, span)
749734
}
750735

751736
fn def_kind(&self, index: DefIndex) -> DefKind {
752-
if !self.is_proc_macro(index) {
753-
self.kind(index).def_kind()
754-
} else {
755-
DefKind::Macro(macro_kind(self.raw_proc_macro(index)))
756-
}
737+
self.kind(index).def_kind()
757738
}
758739

759740
fn get_span(&self, index: DefIndex, sess: &Session) -> Span {
@@ -956,30 +937,19 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
956937
}
957938

958939
fn get_stability(&self, id: DefIndex) -> Option<attr::Stability> {
959-
match self.is_proc_macro(id) {
960-
true => self.root.proc_macro_data.as_ref().unwrap().stability,
961-
false => self.root.tables.stability.get(self, id).map(|stab| stab.decode(self)),
962-
}
940+
self.root.tables.stability.get(self, id).map(|stab| stab.decode(self))
963941
}
964942

965943
fn get_const_stability(&self, id: DefIndex) -> Option<attr::ConstStability> {
966944
self.root.tables.const_stability.get(self, id).map(|stab| stab.decode(self))
967945
}
968946

969947
fn get_deprecation(&self, id: DefIndex) -> Option<attr::Deprecation> {
970-
self.root
971-
.tables
972-
.deprecation
973-
.get(self, id)
974-
.filter(|_| !self.is_proc_macro(id))
975-
.map(|depr| depr.decode(self))
948+
self.root.tables.deprecation.get(self, id).map(|depr| depr.decode(self))
976949
}
977950

978951
fn get_visibility(&self, id: DefIndex) -> ty::Visibility {
979-
match self.is_proc_macro(id) {
980-
true => ty::Visibility::Public,
981-
false => self.root.tables.visibility.get(self, id).unwrap().decode(self),
982-
}
952+
self.root.tables.visibility.get(self, id).unwrap().decode(self)
983953
}
984954

985955
fn get_impl_data(&self, id: DefIndex) -> ImplData {
@@ -1191,7 +1161,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11911161
}
11921162

11931163
fn is_item_mir_available(&self, id: DefIndex) -> bool {
1194-
!self.is_proc_macro(id) && self.root.tables.mir.get(self, id).is_some()
1164+
self.root.tables.mir.get(self, id).is_some()
11951165
}
11961166

11971167
fn module_expansion(&self, id: DefIndex, sess: &Session) -> ExpnId {
@@ -1207,7 +1177,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
12071177
.tables
12081178
.mir
12091179
.get(self, id)
1210-
.filter(|_| !self.is_proc_macro(id))
12111180
.unwrap_or_else(|| {
12121181
bug!("get_optimized_mir: missing MIR for `{:?}`", self.local_def_id(id))
12131182
})
@@ -1223,7 +1192,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
12231192
.tables
12241193
.mir_abstract_consts
12251194
.get(self, id)
1226-
.filter(|_| !self.is_proc_macro(id))
12271195
.map_or(Ok(None), |v| Ok(Some(v.decode((self, tcx)))))
12281196
}
12291197

@@ -1232,7 +1200,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
12321200
.tables
12331201
.unused_generic_params
12341202
.get(self, id)
1235-
.filter(|_| !self.is_proc_macro(id))
12361203
.map(|params| params.decode(self))
12371204
.unwrap_or_default()
12381205
}
@@ -1242,7 +1209,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
12421209
.tables
12431210
.promoted_mir
12441211
.get(self, id)
1245-
.filter(|_| !self.is_proc_macro(id))
12461212
.unwrap_or_else(|| {
12471213
bug!("get_promoted_mir: missing MIR for `{:?}`", self.local_def_id(id))
12481214
})
@@ -1546,14 +1512,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
15461512

15471513
#[inline]
15481514
fn def_key(&self, index: DefIndex) -> DefKey {
1549-
*self.def_key_cache.lock().entry(index).or_insert_with(|| {
1550-
let mut key = self.root.tables.def_keys.get(self, index).unwrap().decode(self);
1551-
if self.is_proc_macro(index) {
1552-
let name = self.raw_proc_macro(index).name();
1553-
key.disambiguated_data.data = DefPathData::MacroNs(Symbol::intern(name));
1554-
}
1555-
key
1556-
})
1515+
*self
1516+
.def_key_cache
1517+
.lock()
1518+
.entry(index)
1519+
.or_insert_with(|| self.root.tables.def_keys.get(self, index).unwrap().decode(self))
15571520
}
15581521

15591522
// Returns the path leading to the thing with this `id`.

Diff for: compiler/rustc_metadata/src/rmeta/encoder.rs

+35-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_data_structures::sync::{join, Lrc};
99
use rustc_hir as hir;
1010
use rustc_hir::def::CtorKind;
1111
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
12+
use rustc_hir::definitions::DefPathData;
1213
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
1314
use rustc_hir::itemlikevisit::{ItemLikeVisitor, ParItemLikeVisitor};
1415
use rustc_hir::lang_items;
@@ -27,7 +28,7 @@ use rustc_middle::ty::codec::TyEncoder;
2728
use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt};
2829
use rustc_serialize::{opaque, Encodable, Encoder};
2930
use rustc_session::config::CrateType;
30-
use rustc_span::hygiene::{ExpnDataEncodeMode, HygieneEncodeContext};
31+
use rustc_span::hygiene::{ExpnDataEncodeMode, HygieneEncodeContext, MacroKind};
3132
use rustc_span::symbol::{sym, Ident, Symbol};
3233
use rustc_span::{self, ExternalSource, FileName, SourceFile, Span, SyntaxContext};
3334
use rustc_target::abi::VariantIdx;
@@ -1539,12 +1540,41 @@ impl EncodeContext<'a, 'tcx> {
15391540
// so we manually encode just the information that we need
15401541
for proc_macro in &hir.krate().proc_macros {
15411542
let id = proc_macro.owner.local_def_index;
1542-
let span = self.lazy(hir.span(*proc_macro));
1543+
let mut name = hir.name(*proc_macro);
1544+
let span = hir.span(*proc_macro);
15431545
// Proc-macros may have attributes like `#[allow_internal_unstable]`,
15441546
// so downstream crates need access to them.
1545-
let attrs = self.lazy(hir.attrs(*proc_macro));
1546-
self.tables.span.set(id, span);
1547-
self.tables.attributes.set(id, attrs);
1547+
let attrs = hir.attrs(*proc_macro);
1548+
let macro_kind = if tcx.sess.contains_name(attrs, sym::proc_macro) {
1549+
MacroKind::Bang
1550+
} else if tcx.sess.contains_name(attrs, sym::proc_macro_attribute) {
1551+
MacroKind::Attr
1552+
} else if let Some(attr) = tcx.sess.find_by_name(attrs, sym::proc_macro_derive) {
1553+
// This unwrap chain should have been checked by the proc-macro harness.
1554+
name = attr.meta_item_list().unwrap()[0]
1555+
.meta_item()
1556+
.unwrap()
1557+
.ident()
1558+
.unwrap()
1559+
.name;
1560+
MacroKind::Derive
1561+
} else {
1562+
bug!("Unknown proc-macro type for item {:?}", id);
1563+
};
1564+
1565+
let mut def_key = self.tcx.hir().def_key(proc_macro.owner);
1566+
def_key.disambiguated_data.data = DefPathData::MacroNs(name);
1567+
1568+
let def_id = DefId::local(id);
1569+
record!(self.tables.kind[def_id] <- EntryKind::ProcMacro(macro_kind));
1570+
record!(self.tables.attributes[def_id] <- attrs);
1571+
record!(self.tables.def_keys[def_id] <- def_key);
1572+
record!(self.tables.ident_span[def_id] <- span);
1573+
record!(self.tables.span[def_id] <- span);
1574+
record!(self.tables.visibility[def_id] <- ty::Visibility::Public);
1575+
if let Some(stability) = stability {
1576+
record!(self.tables.stability[def_id] <- stability);
1577+
}
15481578
}
15491579

15501580
Some(ProcMacroData { proc_macro_decls_static, stability, macros })

Diff for: compiler/rustc_metadata/src/rmeta/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rustc_serialize::opaque::Encoder;
2020
use rustc_session::config::SymbolManglingVersion;
2121
use rustc_session::CrateDisambiguator;
2222
use rustc_span::edition::Edition;
23+
use rustc_span::hygiene::MacroKind;
2324
use rustc_span::symbol::{Ident, Symbol};
2425
use rustc_span::{self, ExpnData, ExpnId, Span};
2526
use rustc_target::spec::{PanicStrategy, TargetTriple};
@@ -336,6 +337,7 @@ enum EntryKind {
336337
ForeignFn(Lazy<FnData>),
337338
Mod(Lazy<ModData>),
338339
MacroDef(Lazy<MacroDef>),
340+
ProcMacro(MacroKind),
339341
Closure,
340342
Generator(hir::GeneratorKind),
341343
Trait(Lazy<TraitData>),

0 commit comments

Comments
 (0)