avoid unnecessary type sanity checks

This commit is contained in:
Ralf Jung 2025-07-29 17:22:22 +02:00
parent dbc030e034
commit e2cc7757e1
4 changed files with 11 additions and 15 deletions

View file

@ -151,17 +151,13 @@ impl<'tcx> Value<'tcx> {
/// or an aggregate).
#[inline]
pub fn try_to_bits(self, tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>) -> Option<u128> {
let scalar = self.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;
Some(scalar.to_bits(size))
}
pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
let (ty::Bool | ty::Char | ty::Uint(_) | ty::Int(_) | ty::Float(_)) = self.ty.kind() else {
return None;
};
self.valtree.try_to_scalar_int()
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;
Some(scalar.to_bits(size))
}
pub fn try_to_bool(self) -> Option<bool> {

View file

@ -160,7 +160,7 @@ impl<'a, 'tcx> ParseCtxt<'a, 'tcx> {
});
}
};
values.push(value.try_to_scalar_int().unwrap().to_bits_unchecked());
values.push(value.valtree.unwrap_leaf().to_bits_unchecked());
targets.push(self.parse_block(arm.body)?);
}

View file

@ -113,7 +113,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let switch_targets = SwitchTargets::new(
target_blocks.iter().filter_map(|(&branch, &block)| {
if let TestBranch::Constant(value) = branch {
let bits = value.try_to_scalar_int().unwrap().to_bits_unchecked();
let bits = value.valtree.unwrap_leaf().to_bits_unchecked();
Some((bits, block))
} else {
None

View file

@ -525,7 +525,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
}
ty::Char | ty::Int(_) | ty::Uint(_) => {
ctor = {
let bits = value.try_to_scalar_int().unwrap().to_bits_unchecked();
let bits = value.valtree.unwrap_leaf().to_bits_unchecked();
let x = match *ty.kind() {
ty::Int(ity) => {
let size = Integer::from_int_ty(&cx.tcx, ity).size().bits();
@ -540,7 +540,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
}
ty::Float(ty::FloatTy::F16) => {
use rustc_apfloat::Float;
let bits = value.try_to_scalar_int().unwrap().to_u16();
let bits = value.valtree.unwrap_leaf().to_u16();
let value = rustc_apfloat::ieee::Half::from_bits(bits.into());
ctor = F16Range(value, value, RangeEnd::Included);
fields = vec![];
@ -548,7 +548,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
}
ty::Float(ty::FloatTy::F32) => {
use rustc_apfloat::Float;
let bits = value.try_to_scalar_int().unwrap().to_u32();
let bits = value.valtree.unwrap_leaf().to_u32();
let value = rustc_apfloat::ieee::Single::from_bits(bits.into());
ctor = F32Range(value, value, RangeEnd::Included);
fields = vec![];
@ -556,7 +556,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
}
ty::Float(ty::FloatTy::F64) => {
use rustc_apfloat::Float;
let bits = value.try_to_scalar_int().unwrap().to_u64();
let bits = value.valtree.unwrap_leaf().to_u64();
let value = rustc_apfloat::ieee::Double::from_bits(bits.into());
ctor = F64Range(value, value, RangeEnd::Included);
fields = vec![];
@ -564,7 +564,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
}
ty::Float(ty::FloatTy::F128) => {
use rustc_apfloat::Float;
let bits = value.try_to_scalar_int().unwrap().to_u128();
let bits = value.valtree.unwrap_leaf().to_u128();
let value = rustc_apfloat::ieee::Quad::from_bits(bits);
ctor = F128Range(value, value, RangeEnd::Included);
fields = vec![];