Skip to content

Commit

Permalink
Auto merge of rust-lang#107693 - petrochenkov:metable, r=oli-obk
Browse files Browse the repository at this point in the history
rustc_metadata: Encode/decode some `LazyArrays` without an `Option`

and a couple of related changes, see individual commits.

Addresses comments in rust-lang#107166 (comment) and rust-lang#107166 (comment), cc `@cjgillot` `@oli-obk.`
  • Loading branch information
bors committed Feb 7, 2023
2 parents e1eaa2d + f4e2b95 commit 35d6d70
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 137 deletions.
27 changes: 12 additions & 15 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ impl<'a, 'tcx, T> Decodable<DecodeContext<'a, 'tcx>> for LazyValue<T> {
impl<'a, 'tcx, T> Decodable<DecodeContext<'a, 'tcx>> for LazyArray<T> {
fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> Self {
let len = decoder.read_usize();
if len == 0 { LazyArray::empty() } else { decoder.read_lazy_array(len) }
if len == 0 { LazyArray::default() } else { decoder.read_lazy_array(len) }
}
}

Expand Down Expand Up @@ -864,7 +864,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.tables
.children
.get(self, index)
.unwrap_or_else(LazyArray::empty)
.expect("fields are not encoded for a variant")
.decode(self)
.map(|index| ty::FieldDef {
did: self.local_def_id(index),
Expand Down Expand Up @@ -896,7 +896,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.tables
.children
.get(self, item_id)
.unwrap_or_else(LazyArray::empty)
.expect("variants are not encoded for an enum")
.decode(self)
.filter_map(|index| {
let kind = self.def_kind(index);
Expand Down Expand Up @@ -1045,7 +1045,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.tables
.fn_arg_names
.get(self, id)
.unwrap_or_else(LazyArray::empty)
.expect("argument names not encoded for a function")
.decode((self, sess))
.nth(0)
.map_or(false, |ident| ident.name == kw::SelfLower)
Expand All @@ -1060,21 +1060,20 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.tables
.children
.get(self, id)
.unwrap_or_else(LazyArray::empty)
.expect("associated items not encoded for an item")
.decode((self, sess))
.map(move |child_index| self.local_def_id(child_index))
}

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 {
Expand Down Expand Up @@ -1131,7 +1130,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.tables
.children
.get(self, id)
.unwrap_or_else(LazyArray::empty)
.expect("fields not encoded for a struct")
.decode(self)
.map(move |index| respan(self.get_span(index, sess), self.item_name(index)))
}
Expand All @@ -1144,7 +1143,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.tables
.children
.get(self, id)
.unwrap_or_else(LazyArray::empty)
.expect("fields not encoded for a struct")
.decode(self)
.map(move |field_index| self.get_visibility(field_index))
}
Expand All @@ -1159,7 +1158,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.tables
.inherent_impls
.get(self, id)
.unwrap_or_else(LazyArray::empty)
.decode(self)
.map(|index| self.local_def_id(index)),
)
Expand All @@ -1174,7 +1172,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.tables
.inherent_impls
.get(self, ty_index)
.unwrap_or_else(LazyArray::empty)
.decode(self)
.map(move |impl_index| (ty_def_id, self.local_def_id(impl_index)))
})
Expand Down Expand Up @@ -1322,7 +1319,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
) -> DefPathHash {
*def_path_hashes
.entry(index)
.or_insert_with(|| self.root.tables.def_path_hashes.get(self, index).unwrap())
.or_insert_with(|| self.root.tables.def_path_hashes.get(self, index))
}

#[inline]
Expand Down
13 changes: 11 additions & 2 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -187,10 +196,10 @@ impl IntoArgs for (CrateNum, 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 }
Expand Down
Loading

0 comments on commit 35d6d70

Please sign in to comment.