Add is_null helper

This is cheaper than creating a null-`ScalarInt` and comparing
and then just throwing it away.
This commit is contained in:
oli 2020-11-01 17:17:04 +00:00
parent 0347ca7d02
commit f03b18b99b
2 changed files with 7 additions and 2 deletions

View File

@ -188,6 +188,11 @@ impl ScalarInt {
Self { data: 0, size: size.bytes() as u8 }
}
#[inline]
pub fn is_null(self) -> bool {
self.data == 0
}
pub(crate) fn ptr_sized_op<'tcx>(
self,
dl: &TargetDataLayout,

View File

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