Skip to content

Commit c60cc43

Browse files
committed
rustc_metadata: Encode/decode some LazyArrays without an Option
Also add asserts to decoding `LazyArray`s with `Option`
1 parent eb5f2d3 commit c60cc43

File tree

5 files changed

+45
-35
lines changed

5 files changed

+45
-35
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
864864
.tables
865865
.children
866866
.get(self, index)
867-
.unwrap_or_else(LazyArray::default)
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::default)
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::default)
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::default)
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::default)
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::default)
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::default)
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::default)
11781175
.decode(self)
11791176
.map(move |impl_index| (ty_def_id, self.local_def_id(impl_index)))
11801177
})

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 }

compiler/rustc_metadata/src/rmeta/encoder.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,16 @@ macro_rules! record_array {
389389
}};
390390
}
391391

392+
macro_rules! record_defaulted_array {
393+
($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{
394+
{
395+
let value = $value;
396+
let lazy = $self.lazy_array(value);
397+
$self.$tables.$table.set($def_id.index, lazy);
398+
}
399+
}};
400+
}
401+
392402
impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
393403
fn emit_lazy_distance(&mut self, position: NonZeroUsize) {
394404
let pos = position.get();
@@ -1190,9 +1200,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11901200
record!(self.tables.generics_of[def_id] <- g);
11911201
record!(self.tables.explicit_predicates_of[def_id] <- self.tcx.explicit_predicates_of(def_id));
11921202
let inferred_outlives = self.tcx.inferred_outlives_of(def_id);
1193-
if !inferred_outlives.is_empty() {
1194-
record_array!(self.tables.inferred_outlives_of[def_id] <- inferred_outlives);
1195-
}
1203+
record_defaulted_array!(self.tables.inferred_outlives_of[def_id] <- inferred_outlives);
11961204
}
11971205
if should_encode_type(tcx, local_id, def_kind) {
11981206
record!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id));
@@ -1213,15 +1221,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12131221
record!(self.tables.trait_impl_trait_tys[def_id] <- table);
12141222
}
12151223
}
1224+
12161225
let inherent_impls = tcx.with_stable_hashing_context(|hcx| {
12171226
tcx.crate_inherent_impls(()).inherent_impls.to_sorted(&hcx, true)
12181227
});
1219-
1220-
for (def_id, implementations) in inherent_impls {
1221-
if implementations.is_empty() {
1222-
continue;
1223-
}
1224-
record_array!(self.tables.inherent_impls[def_id.to_def_id()] <- implementations.iter().map(|&def_id| {
1228+
for (def_id, impls) in inherent_impls {
1229+
record_defaulted_array!(self.tables.inherent_impls[def_id.to_def_id()] <- impls.iter().map(|def_id| {
12251230
assert!(def_id.is_local());
12261231
def_id.index
12271232
}));
@@ -1330,9 +1335,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13301335
fn encode_explicit_item_bounds(&mut self, def_id: DefId) {
13311336
debug!("EncodeContext::encode_explicit_item_bounds({:?})", def_id);
13321337
let bounds = self.tcx.explicit_item_bounds(def_id);
1333-
if !bounds.is_empty() {
1334-
record_array!(self.tables.explicit_item_bounds[def_id] <- bounds);
1335-
}
1338+
record_defaulted_array!(self.tables.explicit_item_bounds[def_id] <- bounds);
13361339
}
13371340

13381341
fn encode_info_for_trait_item(&mut self, def_id: DefId) {

compiler/rustc_metadata/src/rmeta/mod.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ define_tables! {
350350
is_macro_rules: Table<DefIndex, bool>,
351351
is_type_alias_impl_trait: Table<DefIndex, bool>,
352352
attr_flags: Table<DefIndex, AttrFlags>,
353+
explicit_item_bounds: Table<DefIndex, LazyArray<(ty::Predicate<'static>, Span)>>,
354+
inferred_outlives_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
355+
inherent_impls: Table<DefIndex, LazyArray<DefIndex>>,
353356

354357
- optional:
355358
attributes: Table<DefIndex, LazyArray<ast::Attribute>>,
@@ -362,12 +365,8 @@ define_tables! {
362365
lookup_const_stability: Table<DefIndex, LazyValue<attr::ConstStability>>,
363366
lookup_default_body_stability: Table<DefIndex, LazyValue<attr::DefaultBodyStability>>,
364367
lookup_deprecation_entry: Table<DefIndex, LazyValue<attr::Deprecation>>,
365-
// As an optimization, a missing entry indicates an empty `&[]`.
366-
explicit_item_bounds: Table<DefIndex, LazyArray<(ty::Predicate<'static>, Span)>>,
367368
explicit_predicates_of: Table<DefIndex, LazyValue<ty::GenericPredicates<'static>>>,
368369
generics_of: Table<DefIndex, LazyValue<ty::Generics>>,
369-
// As an optimization, a missing entry indicates an empty `&[]`.
370-
inferred_outlives_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
371370
super_predicates_of: Table<DefIndex, LazyValue<ty::GenericPredicates<'static>>>,
372371
type_of: Table<DefIndex, LazyValue<Ty<'static>>>,
373372
variances_of: Table<DefIndex, LazyArray<ty::Variance>>,
@@ -395,7 +394,6 @@ define_tables! {
395394
generator_kind: Table<DefIndex, LazyValue<hir::GeneratorKind>>,
396395
trait_def: Table<DefIndex, LazyValue<ty::TraitDef>>,
397396
trait_item_def_id: Table<DefIndex, RawDefId>,
398-
inherent_impls: Table<DefIndex, LazyArray<DefIndex>>,
399397
expn_that_defined: Table<DefIndex, LazyValue<ExpnId>>,
400398
unused_generic_params: Table<DefIndex, LazyValue<UnusedGenericParams>>,
401399
params_in_repr: Table<DefIndex, LazyValue<BitSet<u32>>>,

compiler/rustc_ty_utils/src/assoc.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] {
2222
hir::ItemKind::Impl(ref impl_) => tcx.arena.alloc_from_iter(
2323
impl_.items.iter().map(|impl_item_ref| impl_item_ref.id.owner_id.to_def_id()),
2424
),
25-
hir::ItemKind::TraitAlias(..) => &[],
2625
_ => span_bug!(item.span, "associated_item_def_ids: not impl or trait"),
2726
}
2827
}
2928

3029
fn associated_items(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItems<'_> {
31-
let items = tcx.associated_item_def_ids(def_id).iter().map(|did| tcx.associated_item(*did));
32-
ty::AssocItems::new(items)
30+
if tcx.is_trait_alias(def_id) {
31+
ty::AssocItems::new(Vec::new())
32+
} else {
33+
let items = tcx.associated_item_def_ids(def_id).iter().map(|did| tcx.associated_item(*did));
34+
ty::AssocItems::new(items)
35+
}
3336
}
3437

3538
fn impl_item_implementor_ids(tcx: TyCtxt<'_>, impl_id: DefId) -> FxHashMap<DefId, DefId> {

0 commit comments

Comments
 (0)