Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit f03b18b

Browse files
committed
Add is_null helper
This is cheaper than creating a null-`ScalarInt` and comparing and then just throwing it away.
1 parent 0347ca7 commit f03b18b

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ impl ScalarInt {
188188
Self { data: 0, size: size.bytes() as u8 }
189189
}
190190

191+
#[inline]
192+
pub fn is_null(self) -> bool {
193+
self.data == 0
194+
}
195+
191196
pub(crate) fn ptr_sized_op<'tcx>(
192197
self,
193198
dl: &TargetDataLayout,

compiler/rustc_mir/src/const_eval/machine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_middle::mir;
22
use rustc_middle::ty::layout::HasTyCtxt;
3-
use rustc_middle::ty::{self, ScalarInt, Ty};
3+
use rustc_middle::ty::{self, Ty};
44
use std::borrow::Borrow;
55
use std::collections::hash_map::Entry;
66
use std::hash::Hash;
@@ -199,7 +199,7 @@ impl<'mir, 'tcx: 'mir> CompileTimeEvalContext<'mir, 'tcx> {
199199
// is in bounds, because if they are in bounds, the pointer can't be null.
200200
// Inequality with integers other than null can never be known for sure.
201201
(Scalar::Int(int), Scalar::Ptr(ptr)) | (Scalar::Ptr(ptr), Scalar::Int(int)) => {
202-
int == ScalarInt::null(int.size()) && !self.memory.ptr_may_be_null(ptr)
202+
int.is_null() && !self.memory.ptr_may_be_null(ptr)
203203
}
204204
// FIXME: return `true` for at least some comparisons where we can reliably
205205
// determine the result of runtime inequality tests at compile-time.

0 commit comments

Comments
 (0)