Skip to content

Commit 7a80a11

Browse files
committed
rustc_metadata: use a table for fn_sig.
1 parent 74db3e8 commit 7a80a11

File tree

3 files changed

+38
-60
lines changed

3 files changed

+38
-60
lines changed

src/librustc_metadata/decoder.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ impl<'tcx> EntryKind<'tcx> {
458458
EntryKind::Impl(_) |
459459
EntryKind::Field |
460460
EntryKind::Generator(_) |
461-
EntryKind::Closure(_) => return None,
461+
EntryKind::Closure => return None,
462462
})
463463
}
464464
}
@@ -1239,16 +1239,7 @@ impl<'a, 'tcx> CrateMetadata {
12391239
}
12401240

12411241
crate fn fn_sig(&self, id: DefIndex, tcx: TyCtxt<'tcx>) -> ty::PolyFnSig<'tcx> {
1242-
let sig = match self.kind(id) {
1243-
EntryKind::Fn(data) |
1244-
EntryKind::ForeignFn(data) => data.decode(self).sig,
1245-
EntryKind::Method(data) => data.decode(self).fn_data.sig,
1246-
EntryKind::Variant(data) |
1247-
EntryKind::Struct(data, _) => data.decode(self).ctor_sig.unwrap(),
1248-
EntryKind::Closure(data) => data.decode(self).sig,
1249-
_ => bug!(),
1250-
};
1251-
sig.decode((self, tcx))
1242+
self.root.per_def.fn_sig.get(self, id).unwrap().decode((self, tcx))
12521243
}
12531244

12541245
#[inline]

src/librustc_metadata/encoder.rs

+24-29
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ struct PerDefTables<'tcx> {
7171
deprecation: PerDefTable<Lazy<attr::Deprecation>>,
7272

7373
ty: PerDefTable<Lazy<Ty<'tcx>>>,
74+
fn_sig: PerDefTable<Lazy<ty::PolyFnSig<'tcx>>>,
7475
inherent_impls: PerDefTable<Lazy<[DefIndex]>>,
7576
variances: PerDefTable<Lazy<[ty::Variance]>>,
7677
generics: PerDefTable<Lazy<ty::Generics>>,
@@ -509,6 +510,7 @@ impl<'tcx> EncodeContext<'tcx> {
509510
deprecation: self.per_def.deprecation.encode(&mut self.opaque),
510511

511512
ty: self.per_def.ty.encode(&mut self.opaque),
513+
fn_sig: self.per_def.fn_sig.encode(&mut self.opaque),
512514
inherent_impls: self.per_def.inherent_impls.encode(&mut self.opaque),
513515
variances: self.per_def.variances.encode(&mut self.opaque),
514516
generics: self.per_def.generics.encode(&mut self.opaque),
@@ -637,13 +639,7 @@ impl EncodeContext<'tcx> {
637639
let data = VariantData {
638640
ctor_kind: variant.ctor_kind,
639641
discr: variant.discr,
640-
// FIXME(eddyb) deduplicate these with `encode_enum_variant_ctor`.
641642
ctor: variant.ctor_def_id.map(|did| did.index),
642-
ctor_sig: if variant.ctor_kind == CtorKind::Fn {
643-
variant.ctor_def_id.map(|ctor_def_id| self.lazy(&tcx.fn_sig(ctor_def_id)))
644-
} else {
645-
None
646-
},
647643
};
648644

649645
let enum_id = tcx.hir().as_local_hir_id(enum_did).unwrap();
@@ -662,6 +658,11 @@ impl EncodeContext<'tcx> {
662658
self.encode_deprecation(def_id);
663659
self.encode_item_type(def_id);
664660
if variant.ctor_kind == CtorKind::Fn {
661+
// FIXME(eddyb) encode signature only in `encode_enum_variant_ctor`.
662+
if let Some(ctor_def_id) = variant.ctor_def_id {
663+
record!(self.per_def.fn_sig[def_id] <- tcx.fn_sig(ctor_def_id));
664+
}
665+
// FIXME(eddyb) is this ever used?
665666
self.encode_variances_of(def_id);
666667
}
667668
self.encode_generics(def_id);
@@ -681,15 +682,11 @@ impl EncodeContext<'tcx> {
681682
let def_id = variant.ctor_def_id.unwrap();
682683
debug!("EncodeContext::encode_enum_variant_ctor({:?})", def_id);
683684

685+
// FIXME(eddyb) encode only the `CtorKind` for constructors.
684686
let data = VariantData {
685687
ctor_kind: variant.ctor_kind,
686688
discr: variant.discr,
687689
ctor: Some(def_id.index),
688-
ctor_sig: if variant.ctor_kind == CtorKind::Fn {
689-
Some(self.lazy(tcx.fn_sig(def_id)))
690-
} else {
691-
None
692-
}
693690
};
694691

695692
// Variant constructors have the same visibility as the parent enums, unless marked as
@@ -708,6 +705,7 @@ impl EncodeContext<'tcx> {
708705
self.encode_deprecation(def_id);
709706
self.encode_item_type(def_id);
710707
if variant.ctor_kind == CtorKind::Fn {
708+
record!(self.per_def.fn_sig[def_id] <- tcx.fn_sig(def_id));
711709
self.encode_variances_of(def_id);
712710
}
713711
self.encode_generics(def_id);
@@ -782,11 +780,6 @@ impl EncodeContext<'tcx> {
782780
ctor_kind: variant.ctor_kind,
783781
discr: variant.discr,
784782
ctor: Some(def_id.index),
785-
ctor_sig: if variant.ctor_kind == CtorKind::Fn {
786-
Some(self.lazy(tcx.fn_sig(def_id)))
787-
} else {
788-
None
789-
}
790783
};
791784

792785
let struct_id = tcx.hir().as_local_hir_id(adt_def_id).unwrap();
@@ -813,6 +806,7 @@ impl EncodeContext<'tcx> {
813806
self.encode_deprecation(def_id);
814807
self.encode_item_type(def_id);
815808
if variant.ctor_kind == CtorKind::Fn {
809+
record!(self.per_def.fn_sig[def_id] <- tcx.fn_sig(def_id));
816810
self.encode_variances_of(def_id);
817811
}
818812
self.encode_generics(def_id);
@@ -881,7 +875,6 @@ impl EncodeContext<'tcx> {
881875
asyncness: m_sig.header.asyncness,
882876
constness: hir::Constness::NotConst,
883877
param_names,
884-
sig: self.lazy(tcx.fn_sig(def_id)),
885878
}
886879
} else {
887880
bug!()
@@ -913,6 +906,7 @@ impl EncodeContext<'tcx> {
913906
ty::AssocKind::OpaqueTy => unreachable!(),
914907
}
915908
if trait_item.kind == ty::AssocKind::Method {
909+
record!(self.per_def.fn_sig[def_id] <- tcx.fn_sig(def_id));
916910
self.encode_variances_of(def_id);
917911
}
918912
self.encode_generics(def_id);
@@ -959,7 +953,6 @@ impl EncodeContext<'tcx> {
959953
asyncness: sig.header.asyncness,
960954
constness: sig.header.constness,
961955
param_names: self.encode_fn_param_names_for_body(body),
962-
sig: self.lazy(tcx.fn_sig(def_id)),
963956
}
964957
} else {
965958
bug!()
@@ -980,6 +973,7 @@ impl EncodeContext<'tcx> {
980973
self.encode_deprecation(def_id);
981974
self.encode_item_type(def_id);
982975
if impl_item.kind == ty::AssocKind::Method {
976+
record!(self.per_def.fn_sig[def_id] <- tcx.fn_sig(def_id));
983977
self.encode_variances_of(def_id);
984978
}
985979
self.encode_generics(def_id);
@@ -1088,7 +1082,6 @@ impl EncodeContext<'tcx> {
10881082
asyncness: header.asyncness,
10891083
constness: header.constness,
10901084
param_names: self.encode_fn_param_names_for_body(body),
1091-
sig: self.lazy(tcx.fn_sig(def_id)),
10921085
};
10931086

10941087
EntryKind::Fn(self.lazy(data))
@@ -1116,7 +1109,6 @@ impl EncodeContext<'tcx> {
11161109
ctor_kind: variant.ctor_kind,
11171110
discr: variant.discr,
11181111
ctor,
1119-
ctor_sig: None,
11201112
}), adt_def.repr)
11211113
}
11221114
hir::ItemKind::Union(..) => {
@@ -1127,7 +1119,6 @@ impl EncodeContext<'tcx> {
11271119
ctor_kind: variant.ctor_kind,
11281120
discr: variant.discr,
11291121
ctor: None,
1130-
ctor_sig: None,
11311122
}), adt_def.repr)
11321123
}
11331124
hir::ItemKind::Impl(_, _, defaultness, ..) => {
@@ -1232,6 +1223,9 @@ impl EncodeContext<'tcx> {
12321223
hir::ItemKind::Impl(..) => self.encode_item_type(def_id),
12331224
_ => {}
12341225
}
1226+
if let hir::ItemKind::Fn(..) = item.kind {
1227+
record!(self.per_def.fn_sig[def_id] <- tcx.fn_sig(def_id));
1228+
}
12351229
self.encode_inherent_implementations(def_id);
12361230
match item.kind {
12371231
hir::ItemKind::Enum(..) |
@@ -1328,10 +1322,12 @@ impl EncodeContext<'tcx> {
13281322
fn encode_info_for_closure(&mut self, def_id: DefId) {
13291323
debug!("EncodeContext::encode_info_for_closure({:?})", def_id);
13301324

1331-
let tables = self.tcx.typeck_tables_of(def_id);
1325+
// NOTE(eddyb) `tcx.type_of(def_id)` isn't used because it's fully generic,
1326+
// including on the signature, which is inferred in `typeck_tables_of.
13321327
let hir_id = self.tcx.hir().as_local_hir_id(def_id).unwrap();
1328+
let ty = self.tcx.typeck_tables_of(def_id).node_type(hir_id);
13331329

1334-
record!(self.per_def.kind[def_id] <- match tables.node_type(hir_id).kind {
1330+
record!(self.per_def.kind[def_id] <- match ty.kind {
13351331
ty::Generator(def_id, ..) => {
13361332
let layout = self.tcx.generator_layout(def_id);
13371333
let data = GeneratorData {
@@ -1340,18 +1336,17 @@ impl EncodeContext<'tcx> {
13401336
EntryKind::Generator(self.lazy(data))
13411337
}
13421338

1343-
ty::Closure(def_id, substs) => {
1344-
let sig = substs.as_closure().sig(def_id, self.tcx);
1345-
let data = ClosureData { sig: self.lazy(sig) };
1346-
EntryKind::Closure(self.lazy(data))
1347-
}
1339+
ty::Closure(..) => EntryKind::Closure,
13481340

13491341
_ => bug!("closure that is neither generator nor closure"),
13501342
});
13511343
record!(self.per_def.visibility[def_id] <- ty::Visibility::Public);
13521344
record!(self.per_def.span[def_id] <- self.tcx.def_span(def_id));
13531345
record!(self.per_def.attributes[def_id] <- &self.tcx.get_attrs(def_id)[..]);
13541346
self.encode_item_type(def_id);
1347+
if let ty::Closure(def_id, substs) = ty.kind {
1348+
record!(self.per_def.fn_sig[def_id] <- substs.as_closure().sig(def_id, self.tcx));
1349+
}
13551350
self.encode_generics(def_id);
13561351
self.encode_optimized_mir(def_id);
13571352
self.encode_promoted_mir(def_id);
@@ -1560,7 +1555,6 @@ impl EncodeContext<'tcx> {
15601555
asyncness: hir::IsAsync::NotAsync,
15611556
constness: hir::Constness::NotConst,
15621557
param_names: self.encode_fn_param_names(names),
1563-
sig: self.lazy(tcx.fn_sig(def_id)),
15641558
};
15651559
EntryKind::ForeignFn(self.lazy(data))
15661560
}
@@ -1576,6 +1570,7 @@ impl EncodeContext<'tcx> {
15761570
self.encode_deprecation(def_id);
15771571
self.encode_item_type(def_id);
15781572
if let hir::ForeignItemKind::Fn(..) = nitem.kind {
1573+
record!(self.per_def.fn_sig[def_id] <- tcx.fn_sig(def_id));
15791574
self.encode_variances_of(def_id);
15801575
}
15811576
self.encode_generics(def_id);

src/librustc_metadata/schema.rs

+12-20
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ crate struct LazyPerDefTables<'tcx> {
238238
pub deprecation: Lazy!(PerDefTable<Lazy<attr::Deprecation>>),
239239

240240
pub ty: Lazy!(PerDefTable<Lazy!(Ty<'tcx>)>),
241+
pub fn_sig: Lazy!(PerDefTable<Lazy!(ty::PolyFnSig<'tcx>)>),
241242
pub inherent_impls: Lazy!(PerDefTable<Lazy<[DefIndex]>>),
242243
pub variances: Lazy!(PerDefTable<Lazy<[ty::Variance]>>),
243244
pub generics: Lazy!(PerDefTable<Lazy<ty::Generics>>),
@@ -265,18 +266,18 @@ crate enum EntryKind<'tcx> {
265266
OpaqueTy,
266267
Enum(ReprOptions),
267268
Field,
268-
Variant(Lazy!(VariantData<'tcx>)),
269-
Struct(Lazy!(VariantData<'tcx>), ReprOptions),
270-
Union(Lazy!(VariantData<'tcx>), ReprOptions),
271-
Fn(Lazy!(FnData<'tcx>)),
272-
ForeignFn(Lazy!(FnData<'tcx>)),
269+
Variant(Lazy<VariantData>),
270+
Struct(Lazy<VariantData>, ReprOptions),
271+
Union(Lazy<VariantData>, ReprOptions),
272+
Fn(Lazy<FnData>),
273+
ForeignFn(Lazy<FnData>),
273274
Mod(Lazy<ModData>),
274275
MacroDef(Lazy<MacroDef>),
275-
Closure(Lazy!(ClosureData<'tcx>)),
276+
Closure,
276277
Generator(Lazy!(GeneratorData<'tcx>)),
277278
Trait(Lazy<TraitData>),
278279
Impl(Lazy!(ImplData<'tcx>)),
279-
Method(Lazy!(MethodData<'tcx>)),
280+
Method(Lazy<MethodData>),
280281
AssocType(AssocContainer),
281282
AssocOpaqueTy(AssocContainer),
282283
AssocConst(AssocContainer, ConstQualif, Lazy<RenderedConst>),
@@ -306,22 +307,18 @@ crate struct MacroDef {
306307
}
307308

308309
#[derive(RustcEncodable, RustcDecodable)]
309-
crate struct FnData<'tcx> {
310+
crate struct FnData {
310311
pub asyncness: hir::IsAsync,
311312
pub constness: hir::Constness,
312313
pub param_names: Lazy<[ast::Name]>,
313-
pub sig: Lazy!(ty::PolyFnSig<'tcx>),
314314
}
315315

316316
#[derive(RustcEncodable, RustcDecodable)]
317-
crate struct VariantData<'tcx> {
317+
crate struct VariantData {
318318
pub ctor_kind: CtorKind,
319319
pub discr: ty::VariantDiscr,
320320
/// If this is unit or tuple-variant/struct, then this is the index of the ctor id.
321321
pub ctor: Option<DefIndex>,
322-
/// If this is a tuple struct or variant
323-
/// ctor, this is its "function" signature.
324-
pub ctor_sig: Option<Lazy!(ty::PolyFnSig<'tcx>)>,
325322
}
326323

327324
#[derive(RustcEncodable, RustcDecodable)]
@@ -383,17 +380,12 @@ impl AssocContainer {
383380
}
384381

385382
#[derive(RustcEncodable, RustcDecodable)]
386-
crate struct MethodData<'tcx> {
387-
pub fn_data: FnData<'tcx>,
383+
crate struct MethodData {
384+
pub fn_data: FnData,
388385
pub container: AssocContainer,
389386
pub has_self: bool,
390387
}
391388

392-
#[derive(RustcEncodable, RustcDecodable)]
393-
crate struct ClosureData<'tcx> {
394-
pub sig: Lazy!(ty::PolyFnSig<'tcx>),
395-
}
396-
397389
#[derive(RustcEncodable, RustcDecodable)]
398390
crate struct GeneratorData<'tcx> {
399391
pub layout: mir::GeneratorLayout<'tcx>,

0 commit comments

Comments
 (0)