Skip to content

Commit c6410f5

Browse files
authored
Rollup merge of #129829 - compiler-errors:decode-non-optional, r=lcnr
Make decoding non-optional `LazyArray` panic if not set Tables may be [defined](https://github.com/rust-lang/rust/blob/9649706eada1b2c68cf6504356efb058f68ad739/compiler/rustc_metadata/src/rmeta/mod.rs#L377) as `optional:` or `defaulted:`. If optional, if we try to read a value from a key that was never encoded, we should panic. This has high value in ensuring correctness over a defaulted table, so the tradeoff is worth considering, since it signals the compiler has a buggy encode impl, rather than just defaulting to a value. HOWEVER, `optional:` arrays were side-stepping this. So this PR fixes that, and makes `optional:` tables of `LazyArray` act like `LazyValue`, and panic if it's not assigned a value during encoding. During this PR, I found that `deduced_param_attrs` has buggy (?? i think??) implementation where it will refuse to encode cross-crate `deduced_param_attrs` unless we're codegening, we're optimizing the library, and incremental is disabled. This seems incredibly wrong, but I don't want to fix it in this PR. https://github.com/rust-lang/rust/blob/9649706eada1b2c68cf6504356efb058f68ad739/compiler/rustc_metadata/src/rmeta/encoder.rs#L1733-L1747
2 parents 003ddec + 175238b commit c6410f5

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

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

+18-5
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ impl<'a, 'tcx, T: Copy + Decodable<DecodeContext<'a, 'tcx>>> ProcessQueryValue<'
7171
for Option<DecodeIterator<'a, 'tcx, T>>
7272
{
7373
#[inline(always)]
74-
fn process_decoded(self, tcx: TyCtxt<'tcx>, _err: impl Fn() -> !) -> &'tcx [T] {
75-
if let Some(iter) = self { tcx.arena.alloc_from_iter(iter) } else { &[] }
74+
fn process_decoded(self, tcx: TyCtxt<'tcx>, err: impl Fn() -> !) -> &'tcx [T] {
75+
if let Some(iter) = self { tcx.arena.alloc_from_iter(iter) } else { err() }
7676
}
7777
}
7878

@@ -84,12 +84,12 @@ impl<'a, 'tcx, T: Copy + Decodable<DecodeContext<'a, 'tcx>>>
8484
fn process_decoded(
8585
self,
8686
tcx: TyCtxt<'tcx>,
87-
_err: impl Fn() -> !,
87+
err: impl Fn() -> !,
8888
) -> ty::EarlyBinder<'tcx, &'tcx [T]> {
8989
ty::EarlyBinder::bind(if let Some(iter) = self {
9090
tcx.arena.alloc_from_iter(iter)
9191
} else {
92-
&[]
92+
err()
9393
})
9494
}
9595
}
@@ -300,7 +300,20 @@ provide! { tcx, def_id, other, cdata,
300300
.unwrap_or_else(|| panic!("{def_id:?} does not have eval_static_initializer")))
301301
}
302302
trait_def => { table }
303-
deduced_param_attrs => { table }
303+
deduced_param_attrs => {
304+
// FIXME: `deduced_param_attrs` has some sketchy encoding settings,
305+
// where we don't encode unless we're optimizing, doing codegen,
306+
// and not incremental (see `encoder.rs`). I don't think this is right!
307+
cdata
308+
.root
309+
.tables
310+
.deduced_param_attrs
311+
.get(cdata, def_id.index)
312+
.map(|lazy| {
313+
&*tcx.arena.alloc_from_iter(lazy.decode((cdata, tcx)))
314+
})
315+
.unwrap_or_default()
316+
}
304317
is_type_alias_impl_trait => {
305318
debug_assert_eq!(tcx.def_kind(def_id), DefKind::OpaqueTy);
306319
cdata.root.tables.is_type_alias_impl_trait.get(cdata, def_id.index)

0 commit comments

Comments
 (0)