From c8072bec1606e72e94158791260b026cf51352d1 Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Sun, 18 Oct 2020 02:16:35 -0600 Subject: [PATCH 1/3] Dedupe encode_info_for(_foreign)_item --- compiler/rustc_metadata/src/rmeta/encoder.rs | 159 +++++++++++-------- 1 file changed, 91 insertions(+), 68 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 7e33a479228ba..d340588508915 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1196,12 +1196,90 @@ impl EncodeContext<'a, 'tcx> { self.lazy(rendered_const) } - fn encode_info_for_item(&mut self, def_id: DefId, item: &'tcx hir::Item<'tcx>) { - let tcx = self.tcx; + /// Common encoding operations between [`encode_info_for_item`] and [`encode_info_for_foreign_item`]. + /// + /// [`encode_info_for_item`]: EncodeContext::encode_info_for_item + /// [`encode_info_for_foreign_item`]: EncodeContext::encode_info_for_foreign_item + fn encode_info_for_item_foreign_item_common( + &mut self, + def_id: DefId, + attrs: &'_ [ast::Attribute], + ident: Ident, + should_encode_item_type: bool, + should_encode_fn_sig: bool, + should_encode_variances: bool, + should_encode_generics_explicit_predicates_inferred_outlives: bool, + ) { + record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); + record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); + record!(self.tables.attributes[def_id] <- attrs); + self.encode_ident_span(def_id, ident); + self.encode_stability(def_id); + self.encode_const_stability(def_id); + self.encode_deprecation(def_id); + if should_encode_item_type { + self.encode_item_type(def_id); + } + self.encode_inherent_implementations(def_id); + if should_encode_fn_sig { + record!(self.tables.fn_sig[def_id] <- self.tcx.fn_sig(def_id)); + } + if should_encode_variances { + self.encode_variances_of(def_id); + } + if should_encode_generics_explicit_predicates_inferred_outlives { + self.encode_generics(def_id); + self.encode_explicit_predicates(def_id); + self.encode_inferred_outlives(def_id); + } + } + fn encode_info_for_item(&mut self, def_id: DefId, item: &'tcx hir::Item<'tcx>) { debug!("EncodeContext::encode_info_for_item({:?})", def_id); - self.encode_ident_span(def_id, item.ident); + // Must be at top of function, so ident span encoding happens early + // Otherwise later operations panic with 'Missing ident span ...' + self.encode_info_for_item_foreign_item_common( + def_id, + item.attrs, + item.ident, + matches!( + item.kind, + hir::ItemKind::Static(..) + | hir::ItemKind::Const(..) + | hir::ItemKind::Fn(..) + | hir::ItemKind::TyAlias(..) + | hir::ItemKind::OpaqueTy(..) + | hir::ItemKind::Enum(..) + | hir::ItemKind::Struct(..) + | hir::ItemKind::Union(..) + | hir::ItemKind::Impl { .. } + ), + matches!(item.kind, hir::ItemKind::Fn(..)), + matches!( + item.kind, + hir::ItemKind::Enum(..) + | hir::ItemKind::Struct(..) + | hir::ItemKind::Union(..) + | hir::ItemKind::Fn(..) + ), + matches!( + 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(..) + ), + ); + + let tcx = self.tcx; record!(self.tables.kind[def_id] <- match item.kind { hir::ItemKind::Static(_, hir::Mutability::Mut, _) => EntryKind::MutStatic, @@ -1314,9 +1392,6 @@ impl EncodeContext<'a, 'tcx> { hir::ItemKind::ExternCrate(_) | hir::ItemKind::Use(..) => bug!("cannot encode info for item {:?}", item), }); - record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); - record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); - record!(self.tables.attributes[def_id] <- item.attrs); record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id)); // FIXME(eddyb) there should be a nicer way to do this. match item.kind { @@ -1351,55 +1426,11 @@ impl EncodeContext<'a, 'tcx> { } _ => {} } - self.encode_stability(def_id); - self.encode_const_stability(def_id); - self.encode_deprecation(def_id); - match item.kind { - hir::ItemKind::Static(..) - | hir::ItemKind::Const(..) - | hir::ItemKind::Fn(..) - | hir::ItemKind::TyAlias(..) - | hir::ItemKind::OpaqueTy(..) - | hir::ItemKind::Enum(..) - | hir::ItemKind::Struct(..) - | hir::ItemKind::Union(..) - | hir::ItemKind::Impl { .. } => self.encode_item_type(def_id), - _ => {} - } - if let hir::ItemKind::Fn(..) = item.kind { - record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id)); - } if let hir::ItemKind::Impl { .. } = item.kind { if let Some(trait_ref) = self.tcx.impl_trait_ref(def_id) { record!(self.tables.impl_trait_ref[def_id] <- trait_ref); } } - self.encode_inherent_implementations(def_id); - 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); @@ -1699,10 +1730,18 @@ impl EncodeContext<'a, 'tcx> { } fn encode_info_for_foreign_item(&mut self, def_id: DefId, nitem: &hir::ForeignItem<'_>) { - let tcx = self.tcx; - debug!("EncodeContext::encode_info_for_foreign_item({:?})", def_id); + self.encode_info_for_item_foreign_item_common( + def_id, + nitem.attrs, + nitem.ident, + true, + matches!(nitem.kind, hir::ForeignItemKind::Fn(..)), + matches!(nitem.kind, hir::ForeignItemKind::Fn(..)), + true, + ); + record!(self.tables.kind[def_id] <- match nitem.kind { hir::ForeignItemKind::Fn(_, ref names, _) => { let data = FnData { @@ -1720,22 +1759,6 @@ impl EncodeContext<'a, 'tcx> { hir::ForeignItemKind::Static(_, hir::Mutability::Not) => EntryKind::ForeignImmStatic, hir::ForeignItemKind::Type => EntryKind::ForeignType, }); - record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); - record!(self.tables.span[def_id] <- nitem.span); - record!(self.tables.attributes[def_id] <- nitem.attrs); - self.encode_ident_span(def_id, nitem.ident); - self.encode_stability(def_id); - self.encode_const_stability(def_id); - self.encode_deprecation(def_id); - self.encode_item_type(def_id); - self.encode_inherent_implementations(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); } } From b813d821089b1fcabbe4196bd3225e07710971cf Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Sun, 25 Oct 2020 09:20:18 -0600 Subject: [PATCH 2/3] Remove boolean flags --- compiler/rustc_metadata/src/rmeta/encoder.rs | 122 ++++++++----------- 1 file changed, 54 insertions(+), 68 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index d340588508915..f422450e494e6 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1205,10 +1205,6 @@ impl EncodeContext<'a, 'tcx> { def_id: DefId, attrs: &'_ [ast::Attribute], ident: Ident, - should_encode_item_type: bool, - should_encode_fn_sig: bool, - should_encode_variances: bool, - should_encode_generics_explicit_predicates_inferred_outlives: bool, ) { record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); @@ -1217,69 +1213,17 @@ impl EncodeContext<'a, 'tcx> { self.encode_stability(def_id); self.encode_const_stability(def_id); self.encode_deprecation(def_id); - if should_encode_item_type { - self.encode_item_type(def_id); - } self.encode_inherent_implementations(def_id); - if should_encode_fn_sig { - record!(self.tables.fn_sig[def_id] <- self.tcx.fn_sig(def_id)); - } - if should_encode_variances { - self.encode_variances_of(def_id); - } - if should_encode_generics_explicit_predicates_inferred_outlives { - self.encode_generics(def_id); - self.encode_explicit_predicates(def_id); - self.encode_inferred_outlives(def_id); - } } fn encode_info_for_item(&mut self, def_id: DefId, item: &'tcx hir::Item<'tcx>) { + let tcx = self.tcx; + debug!("EncodeContext::encode_info_for_item({:?})", def_id); // Must be at top of function, so ident span encoding happens early // Otherwise later operations panic with 'Missing ident span ...' - self.encode_info_for_item_foreign_item_common( - def_id, - item.attrs, - item.ident, - matches!( - item.kind, - hir::ItemKind::Static(..) - | hir::ItemKind::Const(..) - | hir::ItemKind::Fn(..) - | hir::ItemKind::TyAlias(..) - | hir::ItemKind::OpaqueTy(..) - | hir::ItemKind::Enum(..) - | hir::ItemKind::Struct(..) - | hir::ItemKind::Union(..) - | hir::ItemKind::Impl { .. } - ), - matches!(item.kind, hir::ItemKind::Fn(..)), - matches!( - item.kind, - hir::ItemKind::Enum(..) - | hir::ItemKind::Struct(..) - | hir::ItemKind::Union(..) - | hir::ItemKind::Fn(..) - ), - matches!( - 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(..) - ), - ); - - let tcx = self.tcx; + self.encode_info_for_item_foreign_item_common(def_id, item.attrs, item.ident); record!(self.tables.kind[def_id] <- match item.kind { hir::ItemKind::Static(_, hir::Mutability::Mut, _) => EntryKind::MutStatic, @@ -1426,11 +1370,51 @@ impl EncodeContext<'a, 'tcx> { } _ => {} } + match item.kind { + hir::ItemKind::Static(..) + | hir::ItemKind::Const(..) + | hir::ItemKind::Fn(..) + | hir::ItemKind::TyAlias(..) + | hir::ItemKind::OpaqueTy(..) + | hir::ItemKind::Enum(..) + | hir::ItemKind::Struct(..) + | hir::ItemKind::Union(..) + | hir::ItemKind::Impl { .. } => self.encode_item_type(def_id), + _ => {} + } + if let hir::ItemKind::Fn(..) = item.kind { + record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id)); + } if let hir::ItemKind::Impl { .. } = item.kind { if let Some(trait_ref) = self.tcx.impl_trait_ref(def_id) { 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); @@ -1730,17 +1714,11 @@ impl EncodeContext<'a, 'tcx> { } fn encode_info_for_foreign_item(&mut self, def_id: DefId, nitem: &hir::ForeignItem<'_>) { + let tcx = self.tcx; + debug!("EncodeContext::encode_info_for_foreign_item({:?})", def_id); - self.encode_info_for_item_foreign_item_common( - def_id, - nitem.attrs, - nitem.ident, - true, - matches!(nitem.kind, hir::ForeignItemKind::Fn(..)), - matches!(nitem.kind, hir::ForeignItemKind::Fn(..)), - true, - ); + self.encode_info_for_item_foreign_item_common(def_id, nitem.attrs, nitem.ident); record!(self.tables.kind[def_id] <- match nitem.kind { hir::ForeignItemKind::Fn(_, ref names, _) => { @@ -1759,6 +1737,14 @@ impl EncodeContext<'a, 'tcx> { hir::ForeignItemKind::Static(_, hir::Mutability::Not) => EntryKind::ForeignImmStatic, hir::ForeignItemKind::Type => EntryKind::ForeignType, }); + 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); } } From c5d0a64861058d357f1e5ff263720e44e367ae0e Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Sun, 25 Oct 2020 11:33:02 -0600 Subject: [PATCH 3/3] Dedupe encode_bounds Remove `encode_explicit_predicates` and `encode_inferred_outlives`, they were only used in `encode_bounds` after this change --- compiler/rustc_metadata/src/rmeta/encoder.rs | 45 ++++++-------------- 1 file changed, 12 insertions(+), 33 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index f422450e494e6..02ebaa8502f46 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -758,9 +758,7 @@ impl EncodeContext<'a, 'tcx> { // 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); + self.encode_bounds(def_id); self.encode_optimized_mir(def_id.expect_local()); self.encode_promoted_mir(def_id.expect_local()); } @@ -789,9 +787,7 @@ impl EncodeContext<'a, 'tcx> { 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); + self.encode_bounds(def_id); self.encode_optimized_mir(def_id.expect_local()); self.encode_promoted_mir(def_id.expect_local()); } @@ -868,9 +864,7 @@ impl EncodeContext<'a, 'tcx> { self.encode_stability(def_id); self.encode_deprecation(def_id); self.encode_item_type(def_id); - self.encode_generics(def_id); - self.encode_explicit_predicates(def_id); - self.encode_inferred_outlives(def_id); + self.encode_bounds(def_id); } fn encode_struct_ctor(&mut self, adt_def: &ty::AdtDef, def_id: DefId) { @@ -896,9 +890,7 @@ impl EncodeContext<'a, 'tcx> { 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); + self.encode_bounds(def_id); self.encode_optimized_mir(def_id.expect_local()); self.encode_promoted_mir(def_id.expect_local()); } @@ -908,14 +900,11 @@ impl EncodeContext<'a, 'tcx> { 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); + fn encode_bounds(&mut self, def_id: DefId) { + debug!("EncodeContext::encode_bounds({:?})", def_id); + self.encode_generics(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); @@ -1013,9 +1002,7 @@ impl EncodeContext<'a, 'tcx> { 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); + self.encode_bounds(def_id); // This should be kept in sync with `PrefetchVisitor.visit_trait_item`. self.encode_optimized_mir(def_id.expect_local()); @@ -1086,9 +1073,7 @@ impl EncodeContext<'a, 'tcx> { 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); + self.encode_bounds(def_id); // The following part should be kept in sync with `PrefetchVisitor.visit_impl_item`. @@ -1409,9 +1394,7 @@ impl EncodeContext<'a, 'tcx> { | 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); + self.encode_bounds(def_id); } _ => {} } @@ -1501,9 +1484,7 @@ impl EncodeContext<'a, 'tcx> { record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst(qualifs, const_data)); record!(self.tables.span[def_id.to_def_id()] <- self.tcx.def_span(def_id)); 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()); + self.encode_bounds(def_id.to_def_id()); self.encode_optimized_mir(def_id); self.encode_promoted_mir(def_id); } @@ -1742,9 +1723,7 @@ impl EncodeContext<'a, 'tcx> { 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); + self.encode_bounds(def_id); } }