Skip to content

Commit 35d6d70

Browse files
committed
Auto merge of #107693 - petrochenkov:metable, r=oli-obk
rustc_metadata: Encode/decode some `LazyArrays` without an `Option` and a couple of related changes, see individual commits. Addresses comments in #107166 (comment) and #107166 (comment), cc `@cjgillot` `@oli-obk.`
2 parents e1eaa2d + f4e2b95 commit 35d6d70

File tree

7 files changed

+229
-137
lines changed

7 files changed

+229
-137
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+12-15
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ impl<'a, 'tcx, T> Decodable<DecodeContext<'a, 'tcx>> for LazyValue<T> {
654654
impl<'a, 'tcx, T> Decodable<DecodeContext<'a, 'tcx>> for LazyArray<T> {
655655
fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> Self {
656656
let len = decoder.read_usize();
657-
if len == 0 { LazyArray::empty() } else { decoder.read_lazy_array(len) }
657+
if len == 0 { LazyArray::default() } else { decoder.read_lazy_array(len) }
658658
}
659659
}
660660

@@ -864,7 +864,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
864864
.tables
865865
.children
866866
.get(self, index)
867-
.unwrap_or_else(LazyArray::empty)
867+
.expect("fields are not encoded for a variant")
868868
.decode(self)
869869
.map(|index| ty::FieldDef {
870870
did: self.local_def_id(index),
@@ -896,7 +896,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
896896
.tables
897897
.children
898898
.get(self, item_id)
899-
.unwrap_or_else(LazyArray::empty)
899+
.expect("variants are not encoded for an enum")
900900
.decode(self)
901901
.filter_map(|index| {
902902
let kind = self.def_kind(index);
@@ -1045,7 +1045,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10451045
.tables
10461046
.fn_arg_names
10471047
.get(self, id)
1048-
.unwrap_or_else(LazyArray::empty)
1048+
.expect("argument names not encoded for a function")
10491049
.decode((self, sess))
10501050
.nth(0)
10511051
.map_or(false, |ident| ident.name == kw::SelfLower)
@@ -1060,21 +1060,20 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10601060
.tables
10611061
.children
10621062
.get(self, id)
1063-
.unwrap_or_else(LazyArray::empty)
1063+
.expect("associated items not encoded for an item")
10641064
.decode((self, sess))
10651065
.map(move |child_index| self.local_def_id(child_index))
10661066
}
10671067

10681068
fn get_associated_item(self, id: DefIndex, sess: &'a Session) -> ty::AssocItem {
10691069
let name = self.item_name(id);
10701070

1071-
let kind = match self.def_kind(id) {
1072-
DefKind::AssocConst => ty::AssocKind::Const,
1073-
DefKind::AssocFn => ty::AssocKind::Fn,
1074-
DefKind::AssocTy => ty::AssocKind::Type,
1071+
let (kind, has_self) = match self.def_kind(id) {
1072+
DefKind::AssocConst => (ty::AssocKind::Const, false),
1073+
DefKind::AssocFn => (ty::AssocKind::Fn, self.get_fn_has_self_parameter(id, sess)),
1074+
DefKind::AssocTy => (ty::AssocKind::Type, false),
10751075
_ => bug!("cannot get associated-item of `{:?}`", self.def_key(id)),
10761076
};
1077-
let has_self = self.get_fn_has_self_parameter(id, sess);
10781077
let container = self.root.tables.assoc_container.get(self, id).unwrap();
10791078

10801079
ty::AssocItem {
@@ -1131,7 +1130,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11311130
.tables
11321131
.children
11331132
.get(self, id)
1134-
.unwrap_or_else(LazyArray::empty)
1133+
.expect("fields not encoded for a struct")
11351134
.decode(self)
11361135
.map(move |index| respan(self.get_span(index, sess), self.item_name(index)))
11371136
}
@@ -1144,7 +1143,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11441143
.tables
11451144
.children
11461145
.get(self, id)
1147-
.unwrap_or_else(LazyArray::empty)
1146+
.expect("fields not encoded for a struct")
11481147
.decode(self)
11491148
.map(move |field_index| self.get_visibility(field_index))
11501149
}
@@ -1159,7 +1158,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11591158
.tables
11601159
.inherent_impls
11611160
.get(self, id)
1162-
.unwrap_or_else(LazyArray::empty)
11631161
.decode(self)
11641162
.map(|index| self.local_def_id(index)),
11651163
)
@@ -1174,7 +1172,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11741172
.tables
11751173
.inherent_impls
11761174
.get(self, ty_index)
1177-
.unwrap_or_else(LazyArray::empty)
11781175
.decode(self)
11791176
.map(move |impl_index| (ty_def_id, self.local_def_id(impl_index)))
11801177
})
@@ -1322,7 +1319,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
13221319
) -> DefPathHash {
13231320
*def_path_hashes
13241321
.entry(index)
1325-
.or_insert_with(|| self.root.tables.def_path_hashes.get(self, index).unwrap())
1322+
.or_insert_with(|| self.root.tables.def_path_hashes.get(self, index))
13261323
}
13271324

13281325
#[inline]

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::creader::{CStore, LoadedMacro};
22
use crate::foreign_modules;
33
use crate::native_libs;
4+
use crate::rmeta::table::IsDefault;
45
use crate::rmeta::AttrFlags;
56

67
use rustc_ast as ast;
@@ -88,6 +89,14 @@ macro_rules! provide_one {
8889
}
8990
}
9091
};
92+
($tcx:ident, $def_id:ident, $other:ident, $cdata:ident, $name:ident => { table_defaulted_array }) => {
93+
provide_one! {
94+
$tcx, $def_id, $other, $cdata, $name => {
95+
let lazy = $cdata.root.tables.$name.get($cdata, $def_id.index);
96+
if lazy.is_default() { &[] } else { $tcx.arena.alloc_from_iter(lazy.decode(($cdata, $tcx))) }
97+
}
98+
}
99+
};
91100
($tcx:ident, $def_id:ident, $other:ident, $cdata:ident, $name:ident => { table_direct }) => {
92101
provide_one! {
93102
$tcx, $def_id, $other, $cdata, $name => {
@@ -187,10 +196,10 @@ impl IntoArgs for (CrateNum, SimplifiedType) {
187196
}
188197

189198
provide! { tcx, def_id, other, cdata,
190-
explicit_item_bounds => { table }
199+
explicit_item_bounds => { table_defaulted_array }
191200
explicit_predicates_of => { table }
192201
generics_of => { table }
193-
inferred_outlives_of => { table }
202+
inferred_outlives_of => { table_defaulted_array }
194203
super_predicates_of => { table }
195204
type_of => { table }
196205
variances_of => { table }

0 commit comments

Comments
 (0)