check the types in ty::Value
to value conversion
and remove `ty::Const::try_to_scalar` because it becomes redundant
This commit is contained in:
parent
10fc0b159e
commit
0055fb92db
7 changed files with 75 additions and 22 deletions
|
@ -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;
|
||||
|
@ -230,12 +229,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)
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue