Skip to content

Commit 5a90b66

Browse files
authored
Rollup merge of rust-lang#58581 - varkor:const-generics-encoder-refactor, r=eddyb
Refactor generic parameter encoder functions Addresses rust-lang#58503 (comment). r? @eddyb
2 parents c313647 + 6217ee1 commit 5a90b66

File tree

3 files changed

+39
-42
lines changed

3 files changed

+39
-42
lines changed

src/librustc_metadata/decoder.rs

+2
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,8 @@ impl<'tcx> EntryKind<'tcx> {
413413
EntryKind::ForeignFn(_) => Def::Fn(did),
414414
EntryKind::Method(_) => Def::Method(did),
415415
EntryKind::Type => Def::TyAlias(did),
416+
EntryKind::TypeParam => Def::TyParam(did),
417+
EntryKind::ConstParam => Def::ConstParam(did),
416418
EntryKind::Existential => Def::Existential(did),
417419
EntryKind::AssociatedType(_) => Def::AssociatedTy(did),
418420
EntryKind::AssociatedExistential(_) => Def::AssociatedExistential(did),

src/librustc_metadata/encoder.rs

+32-41
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc::middle::cstore::{LinkagePreference, NativeLibrary,
77
EncodedMetadata, ForeignModule};
88
use rustc::hir::def::CtorKind;
99
use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefIndex, DefId, LocalDefId, LOCAL_CRATE};
10+
use rustc::hir::GenericParamKind;
1011
use rustc::hir::map::definitions::DefPathTable;
1112
use rustc_data_structures::fingerprint::Fingerprint;
1213
use rustc::middle::dependency_format::Linkage;
@@ -1352,25 +1353,22 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
13521353
}
13531354
}
13541355

1355-
fn encode_info_for_ty_param(&mut self,
1356-
(def_id, Untracked(has_default)): (DefId, Untracked<bool>))
1357-
-> Entry<'tcx> {
1358-
debug!("IsolatedEncoder::encode_info_for_ty_param({:?})", def_id);
1356+
fn encode_info_for_generic_param(
1357+
&mut self,
1358+
def_id: DefId,
1359+
entry_kind: EntryKind<'tcx>,
1360+
encode_type: bool,
1361+
) -> Entry<'tcx> {
13591362
let tcx = self.tcx;
13601363
Entry {
1361-
kind: EntryKind::Type,
1364+
kind: entry_kind,
13621365
visibility: self.lazy(&ty::Visibility::Public),
13631366
span: self.lazy(&tcx.def_span(def_id)),
13641367
attributes: LazySeq::empty(),
13651368
children: LazySeq::empty(),
13661369
stability: None,
13671370
deprecation: None,
1368-
1369-
ty: if has_default {
1370-
Some(self.encode_item_type(def_id))
1371-
} else {
1372-
None
1373-
},
1371+
ty: if encode_type { Some(self.encode_item_type(def_id)) } else { None },
13741372
inherent_impls: LazySeq::empty(),
13751373
variances: LazySeq::empty(),
13761374
generics: None,
@@ -1381,27 +1379,20 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
13811379
}
13821380
}
13831381

1384-
fn encode_info_for_const_param(&mut self, def_id: DefId) -> Entry<'tcx> {
1385-
debug!("IsolatedEncoder::encode_info_for_const_param({:?})", def_id);
1386-
let tcx = self.tcx;
1387-
Entry {
1388-
kind: EntryKind::Type,
1389-
visibility: self.lazy(&ty::Visibility::Public),
1390-
span: self.lazy(&tcx.def_span(def_id)),
1391-
attributes: LazySeq::empty(),
1392-
children: LazySeq::empty(),
1393-
stability: None,
1394-
deprecation: None,
1395-
1396-
ty: Some(self.encode_item_type(def_id)),
1397-
inherent_impls: LazySeq::empty(),
1398-
variances: LazySeq::empty(),
1399-
generics: None,
1400-
predicates: None,
1401-
predicates_defined_on: None,
1382+
fn encode_info_for_ty_param(
1383+
&mut self,
1384+
(def_id, Untracked(encode_type)): (DefId, Untracked<bool>),
1385+
) -> Entry<'tcx> {
1386+
debug!("IsolatedEncoder::encode_info_for_ty_param({:?})", def_id);
1387+
self.encode_info_for_generic_param(def_id, EntryKind::TypeParam, encode_type)
1388+
}
14021389

1403-
mir: None,
1404-
}
1390+
fn encode_info_for_const_param(
1391+
&mut self,
1392+
def_id: DefId,
1393+
) -> Entry<'tcx> {
1394+
debug!("IsolatedEncoder::encode_info_for_const_param({:?})", def_id);
1395+
self.encode_info_for_generic_param(def_id, EntryKind::ConstParam, true)
14051396
}
14061397

14071398
fn encode_info_for_closure(&mut self, def_id: DefId) -> Entry<'tcx> {
@@ -1748,18 +1739,18 @@ impl<'a, 'b, 'tcx> IndexBuilder<'a, 'b, 'tcx> {
17481739

17491740
fn encode_info_for_generics(&mut self, generics: &hir::Generics) {
17501741
for param in &generics.params {
1742+
let def_id = self.tcx.hir().local_def_id_from_hir_id(param.hir_id);
17511743
match param.kind {
1752-
hir::GenericParamKind::Lifetime { .. } => {}
1753-
hir::GenericParamKind::Type { ref default, .. } => {
1754-
let def_id = self.tcx.hir().local_def_id_from_hir_id(param.hir_id);
1755-
let has_default = Untracked(default.is_some());
1756-
let encode_info = IsolatedEncoder::encode_info_for_ty_param;
1757-
self.record(def_id, encode_info, (def_id, has_default));
1744+
GenericParamKind::Lifetime { .. } => continue,
1745+
GenericParamKind::Type { ref default, .. } => {
1746+
self.record(
1747+
def_id,
1748+
IsolatedEncoder::encode_info_for_ty_param,
1749+
(def_id, Untracked(default.is_some())),
1750+
);
17581751
}
1759-
hir::GenericParamKind::Const { .. } => {
1760-
let def_id = self.tcx.hir().local_def_id_from_hir_id(param.hir_id);
1761-
let encode_info = IsolatedEncoder::encode_info_for_const_param;
1762-
self.record(def_id, encode_info, def_id);
1752+
GenericParamKind::Const { .. } => {
1753+
self.record(def_id, IsolatedEncoder::encode_info_for_const_param, def_id);
17631754
}
17641755
}
17651756
}

src/librustc_metadata/schema.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ pub enum EntryKind<'tcx> {
299299
ForeignType,
300300
GlobalAsm,
301301
Type,
302+
TypeParam,
303+
ConstParam,
302304
Existential,
303305
Enum(ReprOptions),
304306
Field,
@@ -335,7 +337,9 @@ impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for EntryKind<'gcx> {
335337
EntryKind::ForeignType |
336338
EntryKind::Field |
337339
EntryKind::Existential |
338-
EntryKind::Type => {
340+
EntryKind::Type |
341+
EntryKind::TypeParam |
342+
EntryKind::ConstParam => {
339343
// Nothing else to hash here.
340344
}
341345
EntryKind::Const(qualif, ref const_data) => {

0 commit comments

Comments
 (0)