From c60cc439858a028d8ecc2be1e7468c04cc36e8f2 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 5 Feb 2023 16:28:12 +0400 Subject: [PATCH] rustc_metadata: Encode/decode some `LazyArray`s without an `Option` Also add asserts to decoding `LazyArray`s with `Option` --- compiler/rustc_metadata/src/rmeta/decoder.rs | 23 +++++++--------- .../src/rmeta/decoder/cstore_impl.rs | 13 +++++++-- compiler/rustc_metadata/src/rmeta/encoder.rs | 27 ++++++++++--------- compiler/rustc_metadata/src/rmeta/mod.rs | 8 +++--- compiler/rustc_ty_utils/src/assoc.rs | 9 ++++--- 5 files changed, 45 insertions(+), 35 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 3125111b2bd..06c8c32c4b3 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -864,7 +864,7 @@ fn get_variant(self, kind: &DefKind, index: DefIndex, parent_did: DefId) -> ty:: .tables .children .get(self, index) - .unwrap_or_else(LazyArray::default) + .expect("fields are not encoded for a variant") .decode(self) .map(|index| ty::FieldDef { did: self.local_def_id(index), @@ -896,7 +896,7 @@ fn get_adt_def(self, item_id: DefIndex, tcx: TyCtxt<'tcx>) -> ty::AdtDef<'tcx> { .tables .children .get(self, item_id) - .unwrap_or_else(LazyArray::default) + .expect("variants are not encoded for an enum") .decode(self) .filter_map(|index| { let kind = self.def_kind(index); @@ -1045,7 +1045,7 @@ fn get_fn_has_self_parameter(self, id: DefIndex, sess: &'a Session) -> bool { .tables .fn_arg_names .get(self, id) - .unwrap_or_else(LazyArray::default) + .expect("argument names not encoded for a function") .decode((self, sess)) .nth(0) .map_or(false, |ident| ident.name == kw::SelfLower) @@ -1060,7 +1060,7 @@ fn get_associated_item_def_ids( .tables .children .get(self, id) - .unwrap_or_else(LazyArray::default) + .expect("associated items not encoded for an item") .decode((self, sess)) .map(move |child_index| self.local_def_id(child_index)) } @@ -1068,13 +1068,12 @@ fn get_associated_item_def_ids( fn get_associated_item(self, id: DefIndex, sess: &'a Session) -> ty::AssocItem { let name = self.item_name(id); - let kind = match self.def_kind(id) { - DefKind::AssocConst => ty::AssocKind::Const, - DefKind::AssocFn => ty::AssocKind::Fn, - DefKind::AssocTy => ty::AssocKind::Type, + let (kind, has_self) = match self.def_kind(id) { + DefKind::AssocConst => (ty::AssocKind::Const, false), + DefKind::AssocFn => (ty::AssocKind::Fn, self.get_fn_has_self_parameter(id, sess)), + DefKind::AssocTy => (ty::AssocKind::Type, false), _ => bug!("cannot get associated-item of `{:?}`", self.def_key(id)), }; - let has_self = self.get_fn_has_self_parameter(id, sess); let container = self.root.tables.assoc_container.get(self, id).unwrap(); ty::AssocItem { @@ -1131,7 +1130,7 @@ fn get_struct_field_names( .tables .children .get(self, id) - .unwrap_or_else(LazyArray::default) + .expect("fields not encoded for a struct") .decode(self) .map(move |index| respan(self.get_span(index, sess), self.item_name(index))) } @@ -1144,7 +1143,7 @@ fn get_struct_field_visibilities( .tables .children .get(self, id) - .unwrap_or_else(LazyArray::default) + .expect("fields not encoded for a struct") .decode(self) .map(move |field_index| self.get_visibility(field_index)) } @@ -1159,7 +1158,6 @@ fn get_inherent_implementations_for_type( .tables .inherent_impls .get(self, id) - .unwrap_or_else(LazyArray::default) .decode(self) .map(|index| self.local_def_id(index)), ) @@ -1174,7 +1172,6 @@ fn get_inherent_impls(self) -> impl Iterator + 'a { .tables .inherent_impls .get(self, ty_index) - .unwrap_or_else(LazyArray::default) .decode(self) .map(move |impl_index| (ty_def_id, self.local_def_id(impl_index))) }) diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 9b1401f4a44..07cc84ab953 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -1,6 +1,7 @@ use crate::creader::{CStore, LoadedMacro}; use crate::foreign_modules; use crate::native_libs; +use crate::rmeta::table::IsDefault; use crate::rmeta::AttrFlags; use rustc_ast as ast; @@ -88,6 +89,14 @@ macro_rules! provide_one { } } }; + ($tcx:ident, $def_id:ident, $other:ident, $cdata:ident, $name:ident => { table_defaulted_array }) => { + provide_one! { + $tcx, $def_id, $other, $cdata, $name => { + let lazy = $cdata.root.tables.$name.get($cdata, $def_id.index); + if lazy.is_default() { &[] } else { $tcx.arena.alloc_from_iter(lazy.decode(($cdata, $tcx))) } + } + } + }; ($tcx:ident, $def_id:ident, $other:ident, $cdata:ident, $name:ident => { table_direct }) => { provide_one! { $tcx, $def_id, $other, $cdata, $name => { @@ -187,10 +196,10 @@ fn into_args(self) -> (DefId, SimplifiedType) { } provide! { tcx, def_id, other, cdata, - explicit_item_bounds => { table } + explicit_item_bounds => { table_defaulted_array } explicit_predicates_of => { table } generics_of => { table } - inferred_outlives_of => { table } + inferred_outlives_of => { table_defaulted_array } super_predicates_of => { table } type_of => { table } variances_of => { table } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index ed63e940965..07c37b29621 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -389,6 +389,16 @@ macro_rules! record_array { }}; } +macro_rules! record_defaulted_array { + ($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{ + { + let value = $value; + let lazy = $self.lazy_array(value); + $self.$tables.$table.set($def_id.index, lazy); + } + }}; +} + impl<'a, 'tcx> EncodeContext<'a, 'tcx> { fn emit_lazy_distance(&mut self, position: NonZeroUsize) { let pos = position.get(); @@ -1190,9 +1200,7 @@ fn encode_def_ids(&mut self) { record!(self.tables.generics_of[def_id] <- g); record!(self.tables.explicit_predicates_of[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_array!(self.tables.inferred_outlives_of[def_id] <- inferred_outlives); - } + record_defaulted_array!(self.tables.inferred_outlives_of[def_id] <- inferred_outlives); } if should_encode_type(tcx, local_id, def_kind) { record!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id)); @@ -1213,15 +1221,12 @@ fn encode_def_ids(&mut self) { record!(self.tables.trait_impl_trait_tys[def_id] <- table); } } + let inherent_impls = tcx.with_stable_hashing_context(|hcx| { tcx.crate_inherent_impls(()).inherent_impls.to_sorted(&hcx, true) }); - - for (def_id, implementations) in inherent_impls { - if implementations.is_empty() { - continue; - } - record_array!(self.tables.inherent_impls[def_id.to_def_id()] <- implementations.iter().map(|&def_id| { + for (def_id, impls) in inherent_impls { + record_defaulted_array!(self.tables.inherent_impls[def_id.to_def_id()] <- impls.iter().map(|def_id| { assert!(def_id.is_local()); def_id.index })); @@ -1330,9 +1335,7 @@ fn encode_info_for_mod(&mut self, local_def_id: LocalDefId, md: &hir::Mod<'_>) { 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); - if !bounds.is_empty() { - record_array!(self.tables.explicit_item_bounds[def_id] <- bounds); - } + record_defaulted_array!(self.tables.explicit_item_bounds[def_id] <- bounds); } fn encode_info_for_trait_item(&mut self, def_id: DefId) { diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index d8c778b935e..f0b9bf04537 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -350,6 +350,9 @@ fn encode(&self, buf: &mut FileEncoder) -> LazyTables { is_macro_rules: Table, is_type_alias_impl_trait: Table, attr_flags: Table, + explicit_item_bounds: Table, Span)>>, + inferred_outlives_of: Table, Span)>>, + inherent_impls: Table>, - optional: attributes: Table>, @@ -362,12 +365,8 @@ fn encode(&self, buf: &mut FileEncoder) -> LazyTables { lookup_const_stability: Table>, lookup_default_body_stability: Table>, lookup_deprecation_entry: Table>, - // As an optimization, a missing entry indicates an empty `&[]`. - explicit_item_bounds: Table, Span)>>, explicit_predicates_of: Table>>, generics_of: Table>, - // As an optimization, a missing entry indicates an empty `&[]`. - inferred_outlives_of: Table, Span)>>, super_predicates_of: Table>>, type_of: Table>>, variances_of: Table>, @@ -395,7 +394,6 @@ fn encode(&self, buf: &mut FileEncoder) -> LazyTables { generator_kind: Table>, trait_def: Table>, trait_item_def_id: Table, - inherent_impls: Table>, expn_that_defined: Table>, unused_generic_params: Table>, params_in_repr: Table>>, diff --git a/compiler/rustc_ty_utils/src/assoc.rs b/compiler/rustc_ty_utils/src/assoc.rs index 424b52309d3..a6e0f13f698 100644 --- a/compiler/rustc_ty_utils/src/assoc.rs +++ b/compiler/rustc_ty_utils/src/assoc.rs @@ -22,14 +22,17 @@ fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] { hir::ItemKind::Impl(ref impl_) => tcx.arena.alloc_from_iter( impl_.items.iter().map(|impl_item_ref| impl_item_ref.id.owner_id.to_def_id()), ), - hir::ItemKind::TraitAlias(..) => &[], _ => span_bug!(item.span, "associated_item_def_ids: not impl or trait"), } } fn associated_items(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItems<'_> { - let items = tcx.associated_item_def_ids(def_id).iter().map(|did| tcx.associated_item(*did)); - ty::AssocItems::new(items) + if tcx.is_trait_alias(def_id) { + ty::AssocItems::new(Vec::new()) + } else { + let items = tcx.associated_item_def_ids(def_id).iter().map(|did| tcx.associated_item(*did)); + ty::AssocItems::new(items) + } } fn impl_item_implementor_ids(tcx: TyCtxt<'_>, impl_id: DefId) -> FxHashMap {