Skip to content

Commit 9f407ae

Browse files
committed
Do not expose fallible to_int operation on Scalar.
Any use of it has been shown to be a bug in the past.
1 parent 0dd5a1b commit 9f407ae

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

compiler/rustc_middle/src/mir/interpret/value.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl<'tcx> ConstValue<'tcx> {
7878
}
7979

8080
pub fn try_to_scalar_int(&self) -> Option<ScalarInt> {
81-
self.try_to_scalar()?.to_int().ok()
81+
Some(self.try_to_scalar()?.assert_int())
8282
}
8383

8484
pub fn try_to_bits(&self, size: Size) -> Option<u128> {
@@ -367,13 +367,16 @@ impl<'tcx, Tag> Scalar<Tag> {
367367
#[inline]
368368
fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
369369
assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
370-
self.to_int()?.to_bits(target_size).map_err(|size| {
371-
err_ub!(ScalarSizeMismatch {
372-
target_size: target_size.bytes(),
373-
data_size: size.bytes(),
374-
})
375-
.into()
376-
})
370+
match self {
371+
Scalar::Int(int) => int.to_bits(target_size).map_err(|size| {
372+
err_ub!(ScalarSizeMismatch {
373+
target_size: target_size.bytes(),
374+
data_size: size.bytes(),
375+
})
376+
.into()
377+
}),
378+
Scalar::Ptr(_) => throw_unsup!(ReadPointerAsBytes),
379+
}
377380
}
378381

379382
#[inline(always)]
@@ -383,7 +386,10 @@ impl<'tcx, Tag> Scalar<Tag> {
383386

384387
#[inline]
385388
pub fn assert_int(self) -> ScalarInt {
386-
self.to_int().expect("expected an int but got an abstract pointer")
389+
match self {
390+
Scalar::Ptr(_) => bug!("expected an int but got an abstract pointer"),
391+
Scalar::Int(int) => int,
392+
}
387393
}
388394

389395
#[inline]
@@ -518,14 +524,6 @@ impl<Tag> From<Pointer<Tag>> for Scalar<Tag> {
518524
}
519525
}
520526

521-
impl TryFrom<Scalar> for ScalarInt {
522-
type Error = super::InterpErrorInfo<'static>;
523-
#[inline]
524-
fn try_from(scalar: Scalar) -> InterpResult<'static, Self> {
525-
scalar.to_int()
526-
}
527-
}
528-
529527
impl<Tag> From<ScalarInt> for Scalar<Tag> {
530528
#[inline(always)]
531529
fn from(ptr: ScalarInt) -> Self {

compiler/rustc_middle/src/mir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2473,7 +2473,7 @@ impl ConstantKind<'tcx> {
24732473

24742474
#[inline]
24752475
pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
2476-
self.try_to_value()?.try_to_scalar()?.to_int().ok()
2476+
Some(self.try_to_value()?.try_to_scalar()?.assert_int())
24772477
}
24782478

24792479
#[inline]

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<'tcx> ConstKind<'tcx> {
5757

5858
#[inline]
5959
pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
60-
self.try_to_value()?.try_to_scalar()?.to_int().ok()
60+
Some(self.try_to_value()?.try_to_scalar()?.assert_int())
6161
}
6262

6363
#[inline]

0 commit comments

Comments
 (0)