about summary refs log tree commit diff
path: root/compiler/rustc_middle/src/mir
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2024-04-18 08:44:17 +0200
committerRalf Jung <post@ralfj.de>2024-04-19 17:17:31 +0200
commitd3f927db874aaa82480cd2120e15a507e7cb9b15 (patch)
tree977480a87bf4289cef9c3d652147b47e342bf2be /compiler/rustc_middle/src/mir
parent42220f0930a0fb187a75edeeee91d525c326f56d (diff)
downloadrust-d3f927db874aaa82480cd2120e15a507e7cb9b15.tar.gz
rust-d3f927db874aaa82480cd2120e15a507e7cb9b15.zip
avoid PartialOrd on ScalarInt
we don't know their sign so we cannot, in general, order them properly
Diffstat (limited to 'compiler/rustc_middle/src/mir')
-rw-r--r--compiler/rustc_middle/src/mir/consts.rs14
1 files changed, 13 insertions, 1 deletions
diff --git a/compiler/rustc_middle/src/mir/consts.rs b/compiler/rustc_middle/src/mir/consts.rs
index 0af012e90cb..b788cfde85e 100644
--- a/compiler/rustc_middle/src/mir/consts.rs
+++ b/compiler/rustc_middle/src/mir/consts.rs
@@ -244,6 +244,8 @@ impl<'tcx> Const<'tcx> {
             Const::Ty(c) => match c.kind() {
                 ty::ConstKind::Value(valtree) if c.ty().is_primitive() => {
                     // A valtree of a type where leaves directly represent the scalar const value.
+                    // Just checking whether it is a leaf is insufficient as e.g. references are leafs
+                    // but the leaf value is the value they point to, not the reference itself!
                     Some(valtree.unwrap_leaf().into())
                 }
                 _ => None,
@@ -255,7 +257,17 @@ impl<'tcx> Const<'tcx> {
 
     #[inline]
     pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
-        self.try_to_scalar()?.try_to_int().ok()
+        // This is equivalent to `self.try_to_scalar()?.try_to_int().ok()`, but measurably faster.
+        match self {
+            Const::Val(ConstValue::Scalar(Scalar::Int(x)), _) => Some(x),
+            Const::Ty(c) => match c.kind() {
+                ty::ConstKind::Value(valtree) if c.ty().is_primitive() => {
+                    Some(valtree.unwrap_leaf())
+                }
+                _ => None,
+            },
+            _ => None,
+        }
     }
 
     #[inline]