Skip to content

Commit

Permalink
Rc: Add assumptions that the pointer is non-null
Browse files Browse the repository at this point in the history
Since the snapshot compiler is still using an older LLVM version, omit
the call in stage0, because compile times explode otherwise.
  • Loading branch information
Aatch authored and dotdash committed Feb 3, 2015
1 parent 7858cb4 commit 40b6e34
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/liballoc/rc.rs
Expand Up @@ -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;

Expand Down Expand Up @@ -769,12 +770,34 @@ trait RcBoxPtr<T> {

impl<T> RcBoxPtr<T> for Rc<T> {
#[inline(always)]
fn inner(&self) -> &RcBox<T> { unsafe { &(**self._ptr) } }
fn inner(&self) -> &RcBox<T> {
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<T> RcBoxPtr<T> for Weak<T> {
#[inline(always)]
fn inner(&self) -> &RcBox<T> { unsafe { &(**self._ptr) } }
fn inner(&self) -> &RcBox<T> {
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)]
Expand Down

0 comments on commit 40b6e34

Please sign in to comment.