Skip to content

Commit 738c4bf

Browse files
authored
Rollup merge of #109013 - Nilstrieb:obscurity-is-not-a-necessity, r=fee1-dead
Give proper error message when tcx wasn't passed to decoder I hit this yesterday and found it very confusing, even though the solution to the problem is very simple.
2 parents 0b0f334 + b7a7077 commit 738c4bf

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,11 @@ impl<T: ParameterizedOverTcx> LazyArray<T> {
311311
impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
312312
#[inline]
313313
fn tcx(&self) -> TyCtxt<'tcx> {
314-
debug_assert!(self.tcx.is_some(), "missing TyCtxt in DecodeContext");
315-
self.tcx.unwrap()
314+
let Some(tcx) = self.tcx else {
315+
bug!("No TyCtxt found for decoding. \
316+
You need to explicitly pass `(crate_metadata_ref, tcx)` to `decode` instead of just `crate_metadata_ref`.");
317+
};
318+
tcx
316319
}
317320

318321
#[inline]
@@ -454,7 +457,12 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for ast::AttrId {
454457
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for SyntaxContext {
455458
fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> SyntaxContext {
456459
let cdata = decoder.cdata();
457-
let sess = decoder.sess.unwrap();
460+
461+
let Some(sess) = decoder.sess else {
462+
bug!("Cannot decode SyntaxContext without Session.\
463+
You need to explicitly pass `(crate_metadata_ref, tcx)` to `decode` instead of just `crate_metadata_ref`.");
464+
};
465+
458466
let cname = cdata.root.name;
459467
rustc_span::hygiene::decode_syntax_context(decoder, &cdata.hygiene_context, |_, id| {
460468
debug!("SpecializedDecoder<SyntaxContext>: decoding {}", id);
@@ -471,7 +479,11 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for SyntaxContext {
471479
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for ExpnId {
472480
fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> ExpnId {
473481
let local_cdata = decoder.cdata();
474-
let sess = decoder.sess.unwrap();
482+
483+
let Some(sess) = decoder.sess else {
484+
bug!("Cannot decode ExpnId without Session. \
485+
You need to explicitly pass `(crate_metadata_ref, tcx)` to `decode` instead of just `crate_metadata_ref`.");
486+
};
475487

476488
let cnum = CrateNum::decode(decoder);
477489
let index = u32::decode(decoder);
@@ -520,7 +532,8 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for Span {
520532
let hi = lo + len;
521533

522534
let Some(sess) = decoder.sess else {
523-
bug!("Cannot decode Span without Session.")
535+
bug!("Cannot decode Span without Session. \
536+
You need to explicitly pass `(crate_metadata_ref, tcx)` to `decode` instead of just `crate_metadata_ref`.")
524537
};
525538

526539
// Index of the file in the corresponding crate's list of encoded files.

0 commit comments

Comments
 (0)