Skip to content

Commit

Permalink
Auto merge of #83166 - cjgillot:defkey-vge, r=oli-obk
Browse files Browse the repository at this point in the history
Iterate on DefId for variances and generics.

Split from #80347
  • Loading branch information
bors committed Mar 18, 2021
2 parents 0464f63 + fcd6f20 commit 9d0446f
Showing 1 changed file with 86 additions and 92 deletions.
178 changes: 86 additions & 92 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,76 @@ fn should_encode_mir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> (bool, bool) {
}
}

fn should_encode_variances(def_kind: DefKind) -> bool {
match def_kind {
DefKind::Struct
| DefKind::Union
| DefKind::Enum
| DefKind::Variant
| DefKind::Fn
| DefKind::Ctor(..)
| DefKind::AssocFn => true,
DefKind::Mod
| DefKind::Field
| DefKind::AssocTy
| DefKind::AssocConst
| DefKind::TyParam
| DefKind::ConstParam
| DefKind::Static
| DefKind::Const
| DefKind::ForeignMod
| DefKind::TyAlias
| DefKind::OpaqueTy
| DefKind::Impl
| DefKind::Trait
| DefKind::TraitAlias
| DefKind::Macro(..)
| DefKind::ForeignTy
| DefKind::Use
| DefKind::LifetimeParam
| DefKind::AnonConst
| DefKind::GlobalAsm
| DefKind::Closure
| DefKind::Generator
| DefKind::ExternCrate => false,
}
}

fn should_encode_generics(def_kind: DefKind) -> bool {
match def_kind {
DefKind::Struct
| DefKind::Union
| DefKind::Enum
| DefKind::Variant
| DefKind::Trait
| DefKind::TyAlias
| DefKind::ForeignTy
| DefKind::TraitAlias
| DefKind::AssocTy
| DefKind::Fn
| DefKind::Const
| DefKind::Static
| DefKind::Ctor(..)
| DefKind::AssocFn
| DefKind::AssocConst
| DefKind::AnonConst
| DefKind::OpaqueTy
| DefKind::Impl
| DefKind::Closure
| DefKind::Generator => true,
DefKind::Mod
| DefKind::Field
| DefKind::ForeignMod
| DefKind::TyParam
| DefKind::ConstParam
| DefKind::Macro(..)
| DefKind::Use
| DefKind::LifetimeParam
| DefKind::GlobalAsm
| DefKind::ExternCrate => false,
}
}

impl EncodeContext<'a, 'tcx> {
fn encode_def_ids(&mut self) {
if self.is_proc_macro {
Expand Down Expand Up @@ -864,6 +934,22 @@ impl EncodeContext<'a, 'tcx> {
self.encode_const_stability(def_id);
self.encode_deprecation(def_id);
}
if should_encode_variances(def_kind) {
let v = self.tcx.variances_of(def_id);
record!(self.tables.variances[def_id] <- v);
}
if should_encode_generics(def_kind) {
let g = tcx.generics_of(def_id);
record!(self.tables.generics[def_id] <- g);
record!(self.tables.explicit_predicates[def_id] <- self.tcx.explicit_predicates_of(def_id));
let inferred_outlives = self.tcx.inferred_outlives_of(def_id);
if !inferred_outlives.is_empty() {
record!(self.tables.inferred_outlives[def_id] <- inferred_outlives);
}
}
if let DefKind::Trait | DefKind::TraitAlias = def_kind {
record!(self.tables.super_predicates[def_id] <- self.tcx.super_predicates_of(def_id));
}
}
let inherent_impls = tcx.crate_inherent_impls(LOCAL_CRATE);
for (def_id, implementations) in inherent_impls.inherent_impls.iter() {
Expand All @@ -878,11 +964,6 @@ impl EncodeContext<'a, 'tcx> {
}
}

fn encode_variances_of(&mut self, def_id: DefId) {
debug!("EncodeContext::encode_variances_of({:?})", def_id);
record!(self.tables.variances[def_id] <- self.tcx.variances_of(def_id));
}

fn encode_item_type(&mut self, def_id: DefId) {
debug!("EncodeContext::encode_item_type({:?})", def_id);
record!(self.tables.ty[def_id] <- self.tcx.type_of(def_id));
Expand Down Expand Up @@ -913,12 +994,7 @@ impl EncodeContext<'a, 'tcx> {
if let Some(ctor_def_id) = variant.ctor_def_id {
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(ctor_def_id));
}
// FIXME(eddyb) is this ever used?
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
}

fn encode_enum_variant_ctor(&mut self, def: &ty::AdtDef, index: VariantIdx) {
Expand All @@ -939,11 +1015,7 @@ impl EncodeContext<'a, 'tcx> {
self.encode_item_type(def_id);
if variant.ctor_kind == CtorKind::Fn {
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
}

fn encode_info_for_mod(&mut self, local_def_id: LocalDefId, md: &hir::Mod<'_>) {
Expand Down Expand Up @@ -1002,9 +1074,6 @@ impl EncodeContext<'a, 'tcx> {
record!(self.tables.kind[def_id] <- EntryKind::Field);
self.encode_ident_span(def_id, field.ident);
self.encode_item_type(def_id);
self.encode_generics(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
}

fn encode_struct_ctor(&mut self, adt_def: &ty::AdtDef, def_id: DefId) {
Expand All @@ -1023,37 +1092,9 @@ impl EncodeContext<'a, 'tcx> {
self.encode_item_type(def_id);
if variant.ctor_kind == CtorKind::Fn {
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
}

fn encode_generics(&mut self, def_id: DefId) {
debug!("EncodeContext::encode_generics({:?})", def_id);
record!(self.tables.generics[def_id] <- self.tcx.generics_of(def_id));
}

fn encode_explicit_predicates(&mut self, def_id: DefId) {
debug!("EncodeContext::encode_explicit_predicates({:?})", def_id);
record!(self.tables.explicit_predicates[def_id] <-
self.tcx.explicit_predicates_of(def_id));
}

fn encode_inferred_outlives(&mut self, def_id: DefId) {
debug!("EncodeContext::encode_inferred_outlives({:?})", def_id);
let inferred_outlives = self.tcx.inferred_outlives_of(def_id);
if !inferred_outlives.is_empty() {
record!(self.tables.inferred_outlives[def_id] <- inferred_outlives);
}
}

fn encode_super_predicates(&mut self, def_id: DefId) {
debug!("EncodeContext::encode_super_predicates({:?})", def_id);
record!(self.tables.super_predicates[def_id] <- self.tcx.super_predicates_of(def_id));
}

fn encode_explicit_item_bounds(&mut self, def_id: DefId) {
debug!("EncodeContext::encode_explicit_item_bounds({:?})", def_id);
let bounds = self.tcx.explicit_item_bounds(def_id);
Expand Down Expand Up @@ -1128,11 +1169,7 @@ impl EncodeContext<'a, 'tcx> {
}
if trait_item.kind == ty::AssocKind::Fn {
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
}

fn encode_info_for_impl_item(&mut self, def_id: DefId) {
Expand Down Expand Up @@ -1189,11 +1226,7 @@ impl EncodeContext<'a, 'tcx> {
self.encode_item_type(def_id);
if impl_item.kind == ty::AssocKind::Fn {
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
}

fn encode_fn_param_names_for_body(&mut self, body_id: hir::BodyId) -> Lazy<[Ident]> {
Expand Down Expand Up @@ -1458,37 +1491,6 @@ impl EncodeContext<'a, 'tcx> {
record!(self.tables.impl_trait_ref[def_id] <- trait_ref);
}
}
match item.kind {
hir::ItemKind::Enum(..)
| hir::ItemKind::Struct(..)
| hir::ItemKind::Union(..)
| hir::ItemKind::Fn(..) => self.encode_variances_of(def_id),
_ => {}
}
match item.kind {
hir::ItemKind::Static(..)
| hir::ItemKind::Const(..)
| hir::ItemKind::Fn(..)
| hir::ItemKind::TyAlias(..)
| hir::ItemKind::Enum(..)
| hir::ItemKind::Struct(..)
| hir::ItemKind::Union(..)
| hir::ItemKind::Impl { .. }
| hir::ItemKind::OpaqueTy(..)
| hir::ItemKind::Trait(..)
| hir::ItemKind::TraitAlias(..) => {
self.encode_generics(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
}
_ => {}
}
match item.kind {
hir::ItemKind::Trait(..) | hir::ItemKind::TraitAlias(..) => {
self.encode_super_predicates(def_id);
}
_ => {}
}
}

/// Serialize the text of exported macros
Expand Down Expand Up @@ -1529,7 +1531,6 @@ impl EncodeContext<'a, 'tcx> {
if let ty::Closure(def_id, substs) = *ty.kind() {
record!(self.tables.fn_sig[def_id] <- substs.as_closure().sig());
}
self.encode_generics(def_id.to_def_id());
}

fn encode_info_for_anon_const(&mut self, def_id: LocalDefId) {
Expand All @@ -1541,9 +1542,6 @@ impl EncodeContext<'a, 'tcx> {

record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst(qualifs, const_data));
self.encode_item_type(def_id.to_def_id());
self.encode_generics(def_id.to_def_id());
self.encode_explicit_predicates(def_id.to_def_id());
self.encode_inferred_outlives(def_id.to_def_id());
}

fn encode_native_libraries(&mut self) -> Lazy<[NativeLib]> {
Expand Down Expand Up @@ -1822,11 +1820,7 @@ impl EncodeContext<'a, 'tcx> {
self.encode_item_type(def_id);
if let hir::ForeignItemKind::Fn(..) = nitem.kind {
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
}
}

Expand Down

0 comments on commit 9d0446f

Please sign in to comment.