diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index 6708ae6056284..1a584cf289004 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -521,6 +521,10 @@ impl<'tcx> Const<'tcx> { self.try_to_valtree()?.try_to_scalar() } + pub fn try_to_bool(self) -> Option { + self.try_to_scalar()?.to_bool().ok() + } + #[inline] pub fn try_to_target_usize(self, tcx: TyCtxt<'tcx>) -> Option { self.try_to_valtree()?.try_to_target_usize(tcx) diff --git a/compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/encode.rs b/compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/encode.rs index a0a0dd058ffc8..9c2f869f35768 100644 --- a/compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/encode.rs +++ b/compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/encode.rs @@ -145,7 +145,7 @@ fn encode_const<'tcx>( let _ = write!(s, "{val}"); } ty::Bool => { - let val = c.try_eval_bool(tcx, ty::ParamEnv::reveal_all()).unwrap(); + let val = c.try_to_bool().expect("expected monomorphic const in cfi"); let _ = write!(s, "{val}"); } _ => { @@ -411,7 +411,7 @@ pub fn encode_ty<'tcx>( ty::Array(ty0, len) => { // A - let len = len.eval_target_usize(tcx, ty::ParamEnv::reveal_all()); + let len = len.try_to_target_usize(tcx).expect("expected monomorphic const in cfi"); let mut s = String::from("A"); let _ = write!(s, "{len}"); s.push_str(&encode_ty(tcx, *ty0, dict, options));