From b524ade782d20b63b84febbcfe58e2b54bfe7ab8 Mon Sep 17 00:00:00 2001 From: varkor Date: Tue, 19 Feb 2019 21:20:45 +0000 Subject: [PATCH 1/3] Refactor generic parameter encoder functions --- src/librustc_metadata/encoder.rs | 60 ++++++++++---------------------- 1 file changed, 18 insertions(+), 42 deletions(-) diff --git a/src/librustc_metadata/encoder.rs b/src/librustc_metadata/encoder.rs index ce0618d4599e7..ed1113677d044 100644 --- a/src/librustc_metadata/encoder.rs +++ b/src/librustc_metadata/encoder.rs @@ -7,6 +7,7 @@ use rustc::middle::cstore::{LinkagePreference, NativeLibrary, EncodedMetadata, ForeignModule}; use rustc::hir::def::CtorKind; use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefIndex, DefId, LocalDefId, LOCAL_CRATE}; +use rustc::hir::GenericParamKind; use rustc::hir::map::definitions::DefPathTable; use rustc_data_structures::fingerprint::Fingerprint; use rustc::middle::dependency_format::Linkage; @@ -1307,10 +1308,11 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> { } } - fn encode_info_for_ty_param(&mut self, - (def_id, Untracked(has_default)): (DefId, Untracked)) - -> Entry<'tcx> { - debug!("IsolatedEncoder::encode_info_for_ty_param({:?})", def_id); + fn encode_info_for_generic_param( + &mut self, + (def_id, Untracked(encode_type)): (DefId, Untracked), + ) -> Entry<'tcx> { + debug!("IsolatedEncoder::encode_info_for_generic_param({:?})", def_id); let tcx = self.tcx; Entry { kind: EntryKind::Type, @@ -1321,7 +1323,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> { stability: None, deprecation: None, - ty: if has_default { + ty: if encode_type { Some(self.encode_item_type(def_id)) } else { None @@ -1336,29 +1338,6 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> { } } - fn encode_info_for_const_param(&mut self, def_id: DefId) -> Entry<'tcx> { - debug!("IsolatedEncoder::encode_info_for_const_param({:?})", def_id); - let tcx = self.tcx; - Entry { - kind: EntryKind::Type, - visibility: self.lazy(&ty::Visibility::Public), - span: self.lazy(&tcx.def_span(def_id)), - attributes: LazySeq::empty(), - children: LazySeq::empty(), - stability: None, - deprecation: None, - - ty: Some(self.encode_item_type(def_id)), - inherent_impls: LazySeq::empty(), - variances: LazySeq::empty(), - generics: None, - predicates: None, - predicates_defined_on: None, - - mir: None, - } - } - fn encode_info_for_closure(&mut self, def_id: DefId) -> Entry<'tcx> { debug!("IsolatedEncoder::encode_info_for_closure({:?})", def_id); let tcx = self.tcx; @@ -1703,20 +1682,17 @@ impl<'a, 'b, 'tcx> IndexBuilder<'a, 'b, 'tcx> { fn encode_info_for_generics(&mut self, generics: &hir::Generics) { for param in &generics.params { - match param.kind { - hir::GenericParamKind::Lifetime { .. } => {} - hir::GenericParamKind::Type { ref default, .. } => { - let def_id = self.tcx.hir().local_def_id_from_hir_id(param.hir_id); - let has_default = Untracked(default.is_some()); - let encode_info = IsolatedEncoder::encode_info_for_ty_param; - self.record(def_id, encode_info, (def_id, has_default)); - } - hir::GenericParamKind::Const { .. } => { - let def_id = self.tcx.hir().local_def_id_from_hir_id(param.hir_id); - let encode_info = IsolatedEncoder::encode_info_for_const_param; - self.record(def_id, encode_info, def_id); - } - } + let encode_type = match param.kind { + GenericParamKind::Lifetime { .. } => continue, + GenericParamKind::Type { ref default, .. } => default.is_some(), + GenericParamKind::Const { .. } => true, + }; + let def_id = self.tcx.hir().local_def_id_from_hir_id(param.hir_id); + self.record( + def_id, + IsolatedEncoder::encode_info_for_generic_param, + (def_id, Untracked(encode_type)), + ); } } From 7a23725e48617b797779971ca077742a1b984dc6 Mon Sep 17 00:00:00 2001 From: varkor Date: Tue, 26 Feb 2019 21:11:02 +0000 Subject: [PATCH 2/3] Rename encode_info_for_generic_param to encode_info_for_ty_in_generic_param --- src/librustc_metadata/encoder.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/librustc_metadata/encoder.rs b/src/librustc_metadata/encoder.rs index ed1113677d044..712dc869a7d09 100644 --- a/src/librustc_metadata/encoder.rs +++ b/src/librustc_metadata/encoder.rs @@ -1308,11 +1308,11 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> { } } - fn encode_info_for_generic_param( + fn encode_info_for_ty_in_generic_param( &mut self, (def_id, Untracked(encode_type)): (DefId, Untracked), ) -> Entry<'tcx> { - debug!("IsolatedEncoder::encode_info_for_generic_param({:?})", def_id); + debug!("IsolatedEncoder::encode_info_for_ty_in_generic_param({:?})", def_id); let tcx = self.tcx; Entry { kind: EntryKind::Type, @@ -1690,7 +1690,7 @@ impl<'a, 'b, 'tcx> IndexBuilder<'a, 'b, 'tcx> { let def_id = self.tcx.hir().local_def_id_from_hir_id(param.hir_id); self.record( def_id, - IsolatedEncoder::encode_info_for_generic_param, + IsolatedEncoder::encode_info_for_ty_in_generic_param, (def_id, Untracked(encode_type)), ); } From 6217ee12824574256ca22e848db9421210e0ceed Mon Sep 17 00:00:00 2001 From: varkor Date: Thu, 14 Mar 2019 00:14:33 +0000 Subject: [PATCH 3/3] Add `EntryKind::TypeParam` and `EntryKind::ConstParam` --- src/librustc_metadata/decoder.rs | 2 ++ src/librustc_metadata/encoder.rs | 55 ++++++++++++++++++++------------ src/librustc_metadata/schema.rs | 6 +++- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/src/librustc_metadata/decoder.rs b/src/librustc_metadata/decoder.rs index 6fe00a4ad2ff2..ca76de613914d 100644 --- a/src/librustc_metadata/decoder.rs +++ b/src/librustc_metadata/decoder.rs @@ -413,6 +413,8 @@ impl<'tcx> EntryKind<'tcx> { EntryKind::ForeignFn(_) => Def::Fn(did), EntryKind::Method(_) => Def::Method(did), EntryKind::Type => Def::TyAlias(did), + EntryKind::TypeParam => Def::TyParam(did), + EntryKind::ConstParam => Def::ConstParam(did), EntryKind::Existential => Def::Existential(did), EntryKind::AssociatedType(_) => Def::AssociatedTy(did), EntryKind::AssociatedExistential(_) => Def::AssociatedExistential(did), diff --git a/src/librustc_metadata/encoder.rs b/src/librustc_metadata/encoder.rs index 712dc869a7d09..1edcef8f14c62 100644 --- a/src/librustc_metadata/encoder.rs +++ b/src/librustc_metadata/encoder.rs @@ -1308,26 +1308,22 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> { } } - fn encode_info_for_ty_in_generic_param( + fn encode_info_for_generic_param( &mut self, - (def_id, Untracked(encode_type)): (DefId, Untracked), + def_id: DefId, + entry_kind: EntryKind<'tcx>, + encode_type: bool, ) -> Entry<'tcx> { - debug!("IsolatedEncoder::encode_info_for_ty_in_generic_param({:?})", def_id); let tcx = self.tcx; Entry { - kind: EntryKind::Type, + kind: entry_kind, visibility: self.lazy(&ty::Visibility::Public), span: self.lazy(&tcx.def_span(def_id)), attributes: LazySeq::empty(), children: LazySeq::empty(), stability: None, deprecation: None, - - ty: if encode_type { - Some(self.encode_item_type(def_id)) - } else { - None - }, + ty: if encode_type { Some(self.encode_item_type(def_id)) } else { None }, inherent_impls: LazySeq::empty(), variances: LazySeq::empty(), generics: None, @@ -1338,6 +1334,22 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> { } } + fn encode_info_for_ty_param( + &mut self, + (def_id, Untracked(encode_type)): (DefId, Untracked), + ) -> Entry<'tcx> { + debug!("IsolatedEncoder::encode_info_for_ty_param({:?})", def_id); + self.encode_info_for_generic_param(def_id, EntryKind::TypeParam, encode_type) + } + + fn encode_info_for_const_param( + &mut self, + def_id: DefId, + ) -> Entry<'tcx> { + debug!("IsolatedEncoder::encode_info_for_const_param({:?})", def_id); + self.encode_info_for_generic_param(def_id, EntryKind::ConstParam, true) + } + fn encode_info_for_closure(&mut self, def_id: DefId) -> Entry<'tcx> { debug!("IsolatedEncoder::encode_info_for_closure({:?})", def_id); let tcx = self.tcx; @@ -1682,17 +1694,20 @@ impl<'a, 'b, 'tcx> IndexBuilder<'a, 'b, 'tcx> { fn encode_info_for_generics(&mut self, generics: &hir::Generics) { for param in &generics.params { - let encode_type = match param.kind { - GenericParamKind::Lifetime { .. } => continue, - GenericParamKind::Type { ref default, .. } => default.is_some(), - GenericParamKind::Const { .. } => true, - }; let def_id = self.tcx.hir().local_def_id_from_hir_id(param.hir_id); - self.record( - def_id, - IsolatedEncoder::encode_info_for_ty_in_generic_param, - (def_id, Untracked(encode_type)), - ); + match param.kind { + GenericParamKind::Lifetime { .. } => continue, + GenericParamKind::Type { ref default, .. } => { + self.record( + def_id, + IsolatedEncoder::encode_info_for_ty_param, + (def_id, Untracked(default.is_some())), + ); + } + GenericParamKind::Const { .. } => { + self.record(def_id, IsolatedEncoder::encode_info_for_const_param, def_id); + } + } } } diff --git a/src/librustc_metadata/schema.rs b/src/librustc_metadata/schema.rs index afeea9947b5e3..ff97134df35fc 100644 --- a/src/librustc_metadata/schema.rs +++ b/src/librustc_metadata/schema.rs @@ -299,6 +299,8 @@ pub enum EntryKind<'tcx> { ForeignType, GlobalAsm, Type, + TypeParam, + ConstParam, Existential, Enum(ReprOptions), Field, @@ -335,7 +337,9 @@ impl<'a, 'gcx> HashStable> for EntryKind<'gcx> { EntryKind::ForeignType | EntryKind::Field | EntryKind::Existential | - EntryKind::Type => { + EntryKind::Type | + EntryKind::TypeParam | + EntryKind::ConstParam => { // Nothing else to hash here. } EntryKind::Const(qualif, ref const_data) => {