diff --git a/compiler/rustc_transmute/src/lib.rs b/compiler/rustc_transmute/src/lib.rs index a50cc8f5932..f9537f708ef 100644 --- a/compiler/rustc_transmute/src/lib.rs +++ b/compiler/rustc_transmute/src/lib.rs @@ -84,7 +84,7 @@ mod rustc { use rustc_infer::infer::InferCtxt; use rustc_macros::TypeVisitable; use rustc_middle::traits::ObligationCause; - use rustc_middle::ty::{Const, ParamEnv, Ty, TyCtxt, ValTree}; + use rustc_middle::ty::{Const, ParamEnv, Ty, TyCtxt}; use super::*; @@ -139,25 +139,21 @@ mod rustc { let adt_def = cv.ty.ty_adt_def()?; - assert_eq!( - tcx.require_lang_item(LangItem::TransmuteOpts, None), - adt_def.did(), - "The given `Const` was not marked with the `{}` lang item.", - LangItem::TransmuteOpts.name(), - ); + if !tcx.is_lang_item(adt_def.did(), LangItem::TransmuteOpts) { + tcx.dcx().delayed_bug(format!( + "The given `const` was not marked with the `{}` lang item.", + LangItem::TransmuteOpts.name() + )); + return Some(Self { + alignment: true, + lifetimes: true, + safety: true, + validity: true, + }); + } let variant = adt_def.non_enum_variant(); - let fields = match cv.valtree { - ValTree::Branch(branch) => branch, - _ => { - return Some(Self { - alignment: true, - lifetimes: true, - safety: true, - validity: true, - }); - } - }; + let fields = cv.valtree.unwrap_branch(); let get_field = |name| { let (field_idx, _) = variant diff --git a/tests/ui/transmutability/malformed-program-gracefulness/wrong-adt-assume.rs b/tests/ui/transmutability/malformed-program-gracefulness/wrong-adt-assume.rs new file mode 100644 index 00000000000..2340df25025 --- /dev/null +++ b/tests/ui/transmutability/malformed-program-gracefulness/wrong-adt-assume.rs @@ -0,0 +1,20 @@ +//! Test that we don't ICE when passing the wrong ADT to ASSUME. + +#![feature(adt_const_params)] +#![feature(transmutability)] + +use std::marker::ConstParamTy; +use std::mem::TransmuteFrom; + +#[derive(ConstParamTy, PartialEq, Eq)] +struct NotAssume; + +fn foo() +where + u8: TransmuteFrom, //~ ERROR the constant `ASSUME` is not of type `Assume` +{ +} + +fn main() { + foo::<{ NotAssume }>(); +} diff --git a/tests/ui/transmutability/malformed-program-gracefulness/wrong-adt-assume.stderr b/tests/ui/transmutability/malformed-program-gracefulness/wrong-adt-assume.stderr new file mode 100644 index 00000000000..cae700ecfdc --- /dev/null +++ b/tests/ui/transmutability/malformed-program-gracefulness/wrong-adt-assume.stderr @@ -0,0 +1,11 @@ +error: the constant `ASSUME` is not of type `Assume` + --> $DIR/wrong-adt-assume.rs:14:9 + | +LL | u8: TransmuteFrom, + | ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Assume`, found `NotAssume` + | +note: required by a const generic parameter in `TransmuteFrom` + --> $SRC_DIR/core/src/mem/transmutability.rs:LL:COL + +error: aborting due to 1 previous error +