Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 72be5b8

Browse files
committed
Directly encode DefKind in metadata.
1 parent 2129866 commit 72be5b8

File tree

4 files changed

+64
-14
lines changed

4 files changed

+64
-14
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ trait LazyQueryDecodable<'a, 'tcx, T> {
292292
) -> T;
293293
}
294294

295+
impl<'a, 'tcx, T> LazyQueryDecodable<'a, 'tcx, T> for T {
296+
fn decode_query(self, _: CrateMetadataRef<'a>, _: TyCtxt<'tcx>, _: impl FnOnce() -> !) -> T {
297+
self
298+
}
299+
}
300+
295301
impl<'a, 'tcx, T> LazyQueryDecodable<'a, 'tcx, T> for Option<T> {
296302
fn decode_query(self, _: CrateMetadataRef<'a>, _: TyCtxt<'tcx>, err: impl FnOnce() -> !) -> T {
297303
if let Some(l) = self { l } else { err() }
@@ -862,16 +868,14 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
862868
}
863869

864870
fn def_kind(self, item_id: DefIndex) -> DefKind {
865-
self.root.tables.opt_def_kind.get(self, item_id).map(|k| k.decode(self)).unwrap_or_else(
866-
|| {
867-
bug!(
868-
"CrateMetadata::def_kind({:?}): id not found, in crate {:?} with number {}",
869-
item_id,
870-
self.root.name,
871-
self.cnum,
872-
)
873-
},
874-
)
871+
self.root.tables.opt_def_kind.get(self, item_id).unwrap_or_else(|| {
872+
bug!(
873+
"CrateMetadata::def_kind({:?}): id not found, in crate {:?} with number {}",
874+
item_id,
875+
self.root.name,
876+
self.cnum,
877+
)
878+
})
875879
}
876880

877881
fn get_span(self, index: DefIndex, sess: &Session) -> Span {

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
988988
let def_id = local_id.to_def_id();
989989
let def_kind = tcx.opt_def_kind(local_id);
990990
let Some(def_kind) = def_kind else { continue };
991-
record!(self.tables.opt_def_kind[def_id] <- def_kind);
991+
self.tables.opt_def_kind.set(def_id.index, def_kind);
992992
record!(self.tables.def_span[def_id] <- tcx.def_span(def_id));
993993
record!(self.tables.attributes[def_id] <- tcx.get_attrs(def_id));
994994
record!(self.tables.expn_that_defined[def_id] <- self.tcx.expn_that_defined(def_id));
@@ -1644,7 +1644,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16441644
self.tables.proc_macro_quoted_spans.set(i, span);
16451645
}
16461646

1647-
record!(self.tables.opt_def_kind[LOCAL_CRATE.as_def_id()] <- DefKind::Mod);
1647+
self.tables.opt_def_kind.set(LOCAL_CRATE.as_def_id().index, DefKind::Mod);
16481648
record!(self.tables.def_span[LOCAL_CRATE.as_def_id()] <- tcx.def_span(LOCAL_CRATE.as_def_id()));
16491649
record!(self.tables.attributes[LOCAL_CRATE.as_def_id()] <- tcx.get_attrs(LOCAL_CRATE.as_def_id()));
16501650
record!(self.tables.visibility[LOCAL_CRATE.as_def_id()] <- tcx.visibility(LOCAL_CRATE.as_def_id()));
@@ -1685,7 +1685,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16851685
def_key.disambiguated_data.data = DefPathData::MacroNs(name);
16861686

16871687
let def_id = id.to_def_id();
1688-
record!(self.tables.opt_def_kind[def_id] <- DefKind::Macro(macro_kind));
1688+
self.tables.opt_def_kind.set(def_id.index, DefKind::Macro(macro_kind));
16891689
record!(self.tables.kind[def_id] <- EntryKind::ProcMacro(macro_kind));
16901690
record!(self.tables.attributes[def_id] <- attrs);
16911691
record!(self.tables.def_keys[def_id] <- def_key);

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ define_tables! {
286286
attributes: Table<DefIndex, Lazy<[ast::Attribute]>>,
287287
children: Table<DefIndex, Lazy<[DefIndex]>>,
288288

289-
opt_def_kind: Table<DefIndex, Lazy<DefKind>>,
289+
opt_def_kind: Table<DefIndex, DefKind>,
290290
visibility: Table<DefIndex, Lazy<ty::Visibility>>,
291291
def_span: Table<DefIndex, Lazy<Span>>,
292292
def_ident_span: Table<DefIndex, Lazy<Span>>,

compiler/rustc_metadata/src/rmeta/table.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use crate::rmeta::*;
22

3+
use rustc_hir::def::{CtorKind, CtorOf};
34
use rustc_index::vec::Idx;
45
use rustc_serialize::opaque::Encoder;
56
use rustc_serialize::Encoder as _;
7+
use rustc_span::hygiene::MacroKind;
68
use std::convert::TryInto;
79
use std::marker::PhantomData;
810
use std::num::NonZeroUsize;
@@ -105,6 +107,50 @@ macro_rules! fixed_size_enum {
105107
}
106108
}
107109

110+
fixed_size_enum! {
111+
DefKind {
112+
( Mod )
113+
( Struct )
114+
( Union )
115+
( Enum )
116+
( Variant )
117+
( Trait )
118+
( TyAlias )
119+
( ForeignTy )
120+
( TraitAlias )
121+
( AssocTy )
122+
( TyParam )
123+
( Fn )
124+
( Const )
125+
( ConstParam )
126+
( AssocFn )
127+
( AssocConst )
128+
( ExternCrate )
129+
( Use )
130+
( ForeignMod )
131+
( AnonConst )
132+
( InlineConst )
133+
( OpaqueTy )
134+
( Field )
135+
( LifetimeParam )
136+
( GlobalAsm )
137+
( Impl )
138+
( Closure )
139+
( Generator )
140+
( Static(ast::Mutability::Not) )
141+
( Static(ast::Mutability::Mut) )
142+
( Ctor(CtorOf::Struct, CtorKind::Fn) )
143+
( Ctor(CtorOf::Struct, CtorKind::Const) )
144+
( Ctor(CtorOf::Struct, CtorKind::Fictive) )
145+
( Ctor(CtorOf::Variant, CtorKind::Fn) )
146+
( Ctor(CtorOf::Variant, CtorKind::Const) )
147+
( Ctor(CtorOf::Variant, CtorKind::Fictive) )
148+
( Macro(MacroKind::Bang) )
149+
( Macro(MacroKind::Attr) )
150+
( Macro(MacroKind::Derive) )
151+
}
152+
}
153+
108154
fixed_size_enum! {
109155
ty::ImplPolarity {
110156
( Positive )

0 commit comments

Comments
 (0)