Skip to content

Commit cbb5fc1

Browse files
committed
avoid unnecessary type sanity checks
1 parent 5e53d76 commit cbb5fc1

File tree

4 files changed

+11
-15
lines changed

4 files changed

+11
-15
lines changed

compiler/rustc_middle/src/ty/consts/valtree.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,13 @@ impl<'tcx> Value<'tcx> {
151151
/// or an aggregate).
152152
#[inline]
153153
pub fn try_to_bits(self, tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>) -> Option<u128> {
154-
let scalar = self.try_to_scalar_int()?;
155-
let input = typing_env.with_post_analysis_normalized(tcx).as_query_input(self.ty);
156-
let size = tcx.layout_of(input).ok()?.size;
157-
Some(scalar.to_bits(size))
158-
}
159-
160-
pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
161154
let (ty::Bool | ty::Char | ty::Uint(_) | ty::Int(_) | ty::Float(_)) = self.ty.kind() else {
162155
return None;
163156
};
164-
self.valtree.try_to_scalar_int()
157+
let scalar = self.valtree.try_to_scalar_int()?;
158+
let input = typing_env.with_post_analysis_normalized(tcx).as_query_input(self.ty);
159+
let size = tcx.layout_of(input).ok()?.size;
160+
Some(scalar.to_bits(size))
165161
}
166162

167163
pub fn try_to_bool(self) -> Option<bool> {

compiler/rustc_mir_build/src/builder/custom/parse/instruction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl<'a, 'tcx> ParseCtxt<'a, 'tcx> {
160160
});
161161
}
162162
};
163-
values.push(value.try_to_scalar_int().unwrap().to_bits_unchecked());
163+
values.push(value.valtree.unwrap_leaf().to_bits_unchecked());
164164
targets.push(self.parse_block(arm.body)?);
165165
}
166166

compiler/rustc_mir_build/src/builder/matches/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
113113
let switch_targets = SwitchTargets::new(
114114
target_blocks.iter().filter_map(|(&branch, &block)| {
115115
if let TestBranch::Constant(value) = branch {
116-
let bits = value.try_to_scalar_int().unwrap().to_bits_unchecked();
116+
let bits = value.valtree.unwrap_leaf().to_bits_unchecked();
117117
Some((bits, block))
118118
} else {
119119
None

compiler/rustc_pattern_analysis/src/rustc.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
525525
}
526526
ty::Char | ty::Int(_) | ty::Uint(_) => {
527527
ctor = {
528-
let bits = value.try_to_scalar_int().unwrap().to_bits_unchecked();
528+
let bits = value.valtree.unwrap_leaf().to_bits_unchecked();
529529
let x = match *ty.kind() {
530530
ty::Int(ity) => {
531531
let size = Integer::from_int_ty(&cx.tcx, ity).size().bits();
@@ -540,31 +540,31 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
540540
}
541541
ty::Float(ty::FloatTy::F16) => {
542542
use rustc_apfloat::Float;
543-
let bits = value.try_to_scalar_int().unwrap().to_u16();
543+
let bits = value.valtree.unwrap_leaf().to_u16();
544544
let value = rustc_apfloat::ieee::Half::from_bits(bits.into());
545545
ctor = F16Range(value, value, RangeEnd::Included);
546546
fields = vec![];
547547
arity = 0;
548548
}
549549
ty::Float(ty::FloatTy::F32) => {
550550
use rustc_apfloat::Float;
551-
let bits = value.try_to_scalar_int().unwrap().to_u32();
551+
let bits = value.valtree.unwrap_leaf().to_u32();
552552
let value = rustc_apfloat::ieee::Single::from_bits(bits.into());
553553
ctor = F32Range(value, value, RangeEnd::Included);
554554
fields = vec![];
555555
arity = 0;
556556
}
557557
ty::Float(ty::FloatTy::F64) => {
558558
use rustc_apfloat::Float;
559-
let bits = value.try_to_scalar_int().unwrap().to_u64();
559+
let bits = value.valtree.unwrap_leaf().to_u64();
560560
let value = rustc_apfloat::ieee::Double::from_bits(bits.into());
561561
ctor = F64Range(value, value, RangeEnd::Included);
562562
fields = vec![];
563563
arity = 0;
564564
}
565565
ty::Float(ty::FloatTy::F128) => {
566566
use rustc_apfloat::Float;
567-
let bits = value.try_to_scalar_int().unwrap().to_u128();
567+
let bits = value.valtree.unwrap_leaf().to_u128();
568568
let value = rustc_apfloat::ieee::Quad::from_bits(bits);
569569
ctor = F128Range(value, value, RangeEnd::Included);
570570
fields = vec![];

0 commit comments

Comments
 (0)