diff options
| author | Lukas Markeffsky <@> | 2025-01-27 11:16:19 +0000 |
|---|---|---|
| committer | Lukas Markeffsky <@> | 2025-01-30 18:13:16 +0100 |
| commit | 0055fb92db2fcfb8c74d1bf9063f14f65d5dcce0 (patch) | |
| tree | ef85e6eb90f4d25876ca6d5a64ea027f3a86dccc /compiler/rustc_middle/src | |
| parent | 10fc0b159ee6e5281bf38f65680082961dd7bec3 (diff) | |
check the types in `ty::Value` to value conversion
and remove `ty::Const::try_to_scalar` because it becomes redundant
Diffstat (limited to 'compiler/rustc_middle/src')
| -rw-r--r-- | compiler/rustc_middle/src/ty/consts.rs | 7 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/consts/valtree.rs | 12 |
2 files changed, 11 insertions, 8 deletions
diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index 6875532e28a..4ad5d8443b2 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -5,7 +5,6 @@ use rustc_error_messages::MultiSpan; use rustc_macros::HashStable; use rustc_type_ir::{self as ir, TypeFlags, WithCachedTypeInfo}; -use crate::mir::interpret::Scalar; use crate::ty::{self, Ty, TyCtxt}; mod int; @@ -231,12 +230,6 @@ impl<'tcx> Const<'tcx> { } #[inline] - pub fn try_to_scalar(self) -> Option<(Scalar, Ty<'tcx>)> { - let cv = self.try_to_value()?; - Some((cv.valtree.try_to_scalar()?, cv.ty)) - } - - #[inline] pub fn try_to_target_usize(self, tcx: TyCtxt<'tcx>) -> Option<u64> { self.try_to_value()?.try_to_target_usize(tcx) } diff --git a/compiler/rustc_middle/src/ty/consts/valtree.rs b/compiler/rustc_middle/src/ty/consts/valtree.rs index 425b68a951c..d914b7576dc 100644 --- a/compiler/rustc_middle/src/ty/consts/valtree.rs +++ b/compiler/rustc_middle/src/ty/consts/valtree.rs @@ -117,9 +117,13 @@ pub struct Value<'tcx> { impl<'tcx> Value<'tcx> { /// Attempts to extract the raw bits from the constant. /// - /// Fails if the value can't be represented as bits (e.g. because it is an aggregate). + /// Fails if the value can't be represented as bits (e.g. because it is a reference + /// or an aggregate). #[inline] pub fn try_to_bits(self, tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>) -> Option<u128> { + let (ty::Bool | ty::Char | ty::Uint(_) | ty::Int(_) | ty::Float(_)) = self.ty.kind() else { + return None; + }; let scalar = self.valtree.try_to_scalar_int()?; let input = typing_env.with_post_analysis_normalized(tcx).as_query_input(self.ty); let size = tcx.layout_of(input).ok()?.size; @@ -127,10 +131,16 @@ impl<'tcx> Value<'tcx> { } pub fn try_to_bool(self) -> Option<bool> { + if !self.ty.is_bool() { + return None; + } self.valtree.try_to_scalar_int()?.try_to_bool().ok() } pub fn try_to_target_usize(self, tcx: TyCtxt<'tcx>) -> Option<u64> { + if !self.ty.is_usize() { + return None; + } self.valtree.try_to_scalar_int().map(|s| s.to_target_usize(tcx)) } } |
