From 40b6e34240c6669fdf7fb1b83c3925b0becafd0e Mon Sep 17 00:00:00 2001 From: James Miller Date: Wed, 21 Jan 2015 09:35:24 +1300 Subject: [PATCH] Rc: Add assumptions that the pointer is non-null Since the snapshot compiler is still using an older LLVM version, omit the call in stage0, because compile times explode otherwise. --- src/liballoc/rc.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 464f20e9cac53..80ffa4a0a1910 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -160,6 +160,7 @@ use core::option::Option::{Some, None}; use core::ptr::{self, PtrExt}; use core::result::Result; use core::result::Result::{Ok, Err}; +use core::intrinsics::assume; use heap::deallocate; @@ -769,12 +770,34 @@ trait RcBoxPtr { impl RcBoxPtr for Rc { #[inline(always)] - fn inner(&self) -> &RcBox { unsafe { &(**self._ptr) } } + fn inner(&self) -> &RcBox { + unsafe { + // Safe to assume this here, as if it weren't true, we'd be breaking + // the contract anyway. + // This allows the null check to be elided in the destructor if we + // manipulated the reference count in the same function. + if cfg!(not(stage0)) { // NOTE remove cfg after next snapshot + assume(!self._ptr.is_null()); + } + &(**self._ptr) + } + } } impl RcBoxPtr for Weak { #[inline(always)] - fn inner(&self) -> &RcBox { unsafe { &(**self._ptr) } } + fn inner(&self) -> &RcBox { + unsafe { + // Safe to assume this here, as if it weren't true, we'd be breaking + // the contract anyway. + // This allows the null check to be elided in the destructor if we + // manipulated the reference count in the same function. + if cfg!(not(stage0)) { // NOTE remove cfg after next snapshot + assume(!self._ptr.is_null()); + } + &(**self._ptr) + } + } } #[cfg(test)]