From f03b18b99b683c25a584344f73cdf9704d77b719 Mon Sep 17 00:00:00 2001 From: oli Date: Sun, 1 Nov 2020 17:17:04 +0000 Subject: [PATCH] Add `is_null` helper This is cheaper than creating a null-`ScalarInt` and comparing and then just throwing it away. --- compiler/rustc_middle/src/ty/consts/int.rs | 5 +++++ compiler/rustc_mir/src/const_eval/machine.rs | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_middle/src/ty/consts/int.rs b/compiler/rustc_middle/src/ty/consts/int.rs index 838e9819b0fcd..89d9ba2c0c34b 100644 --- a/compiler/rustc_middle/src/ty/consts/int.rs +++ b/compiler/rustc_middle/src/ty/consts/int.rs @@ -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, diff --git a/compiler/rustc_mir/src/const_eval/machine.rs b/compiler/rustc_mir/src/const_eval/machine.rs index 5fe393bf43235..c72089ec55a99 100644 --- a/compiler/rustc_mir/src/const_eval/machine.rs +++ b/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; @@ -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.