diff options
| author | bors <bors@rust-lang.org> | 2018-07-24 03:01:11 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-07-24 03:01:11 +0000 |
| commit | baba5007bf857b4577a8d26de454a03d7afef3ac (patch) | |
| tree | 0e552f1865570b25ddabb354b6c6119f8979b866 /src/liballoc | |
| parent | e842dea7a3d9babc7a19bd201711f4243840fab0 (diff) | |
| parent | 3efc612a930822ea1b9c6749a3d146235c324a83 (diff) | |
| download | rust-baba5007bf857b4577a8d26de454a03d7afef3ac.tar.gz rust-baba5007bf857b4577a8d26de454a03d7afef3ac.zip | |
Auto merge of #52655 - kennytm:rollup, r=kennytm
Rollup of 10 pull requests Successful merges: - #52538 (Remove obsolete flags in the i586_musl Dockerfile) - #52548 (Cursor: update docs to clarify Cursor only works with in-memory buffers) - #52605 (Do not suggest using `to_owned()` on `&str += &str`) - #52621 (Fix color detection for Windows msys terminals.) - #52622 (Use MultiSpan in E0707 and E709) - #52627 (Compile rustc before building tests for rustdoc) - #52637 (Don't use NonNull::dangling as sentinel value in Rc, Arc) - #52640 (Forget Waker when cloning LocalWaker) - #52641 (Simplify 2 functions in rustc_mir/dataflow) - #52642 (Replace a few expect+format combos with unwrap_or_else+panic) Failed merges: r? @ghost
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/rc.rs | 13 | ||||
| -rw-r--r-- | src/liballoc/sync.rs | 13 |
2 files changed, 17 insertions, 9 deletions
diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index d76acb28df9..be049eb6e5e 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -258,6 +258,7 @@ use core::ops::Deref; use core::ops::CoerceUnsized; use core::ptr::{self, NonNull}; use core::convert::From; +use core::usize; use alloc::{Global, Alloc, Layout, box_free, handle_alloc_error}; use string::String; @@ -449,6 +450,8 @@ impl<T: ?Sized> Rc<T> { #[stable(feature = "rc_weak", since = "1.4.0")] pub fn downgrade(this: &Self) -> Weak<T> { this.inc_weak(); + // Make sure we do not create a dangling Weak + debug_assert!(!is_dangling(this.ptr)); Weak { ptr: this.ptr } } @@ -1154,8 +1157,9 @@ impl<T> From<Vec<T>> for Rc<[T]> { pub struct Weak<T: ?Sized> { // This is a `NonNull` to allow optimizing the size of this type in enums, // but it is not necessarily a valid pointer. - // `Weak::new` sets this to a dangling pointer so that it doesn’t need - // to allocate space on the heap. + // `Weak::new` sets this to `usize::MAX` so that it doesn’t need + // to allocate space on the heap. That's not a value a real pointer + // will ever have because RcBox has alignment at least 2. ptr: NonNull<RcBox<T>>, } @@ -1185,15 +1189,14 @@ impl<T> Weak<T> { #[stable(feature = "downgraded_weak", since = "1.10.0")] pub fn new() -> Weak<T> { Weak { - ptr: NonNull::dangling(), + ptr: NonNull::new(usize::MAX as *mut RcBox<T>).expect("MAX is not 0"), } } } pub(crate) fn is_dangling<T: ?Sized>(ptr: NonNull<T>) -> bool { let address = ptr.as_ptr() as *mut () as usize; - let align = align_of_val(unsafe { ptr.as_ref() }); - address == align + address == usize::MAX } impl<T: ?Sized> Weak<T> { diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 5def0237e7e..a00b6b4e435 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -238,8 +238,9 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Arc<U>> for Arc<T> {} pub struct Weak<T: ?Sized> { // This is a `NonNull` to allow optimizing the size of this type in enums, // but it is not necessarily a valid pointer. - // `Weak::new` sets this to a dangling pointer so that it doesn’t need - // to allocate space on the heap. + // `Weak::new` sets this to `usize::MAX` so that it doesn’t need + // to allocate space on the heap. That's not a value a real pointer + // will ever have because RcBox has alignment at least 2. ptr: NonNull<ArcInner<T>>, } @@ -442,7 +443,11 @@ impl<T: ?Sized> Arc<T> { // synchronize with the write coming from `is_unique`, so that the // events prior to that write happen before this read. match this.inner().weak.compare_exchange_weak(cur, cur + 1, Acquire, Relaxed) { - Ok(_) => return Weak { ptr: this.ptr }, + Ok(_) => { + // Make sure we do not create a dangling Weak + debug_assert!(!is_dangling(this.ptr)); + return Weak { ptr: this.ptr }; + } Err(old) => cur = old, } } @@ -1033,7 +1038,7 @@ impl<T> Weak<T> { #[stable(feature = "downgraded_weak", since = "1.10.0")] pub fn new() -> Weak<T> { Weak { - ptr: NonNull::dangling(), + ptr: NonNull::new(usize::MAX as *mut ArcInner<T>).expect("MAX is not 0"), } } } |
