Skip to content

Commit

Permalink
Add is_null helper
Browse files Browse the repository at this point in the history
This is cheaper than creating a null-`ScalarInt` and comparing
and then just throwing it away.
  • Loading branch information
oli-obk committed Nov 4, 2020
1 parent 0347ca7 commit f03b18b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 5 additions & 0 deletions compiler/rustc_middle/src/ty/consts/int.rs
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir/src/const_eval/machine.rs
@@ -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;
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit f03b18b

Please sign in to comment.