Skip to content

Commit 4f0ca92

Browse files
committed
Auto merge of #52123 - Mark-Simulacrum:rollup, r=Mark-Simulacrum
Rollup of 9 pull requests Successful merges: - #51901 (Rc: remove unused allocation and fix segfault in Weak::new()) - #52058 (Use of unimplemented!() causing ICE with NLL) - #52067 (Visit the mir basic blocks in reverse-postfix order) - #52083 (Dont run ast borrowck on mir mode) - #52099 (fix typo in stable `--edition` error message) - #52103 (Stabilize rc_downcast) - #52104 (Remove unnecessary feature gate.) - #52117 (Dedupe filetime) - #52120 (ARM: expose the "mclass" target feature) Failed merges: r? @ghost
2 parents 99b0ddb + cd7ddae commit 4f0ca92

35 files changed

+354
-175
lines changed

src/Cargo.lock

+6-17
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ dependencies = [
147147
"build_helper 0.1.0",
148148
"cc 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)",
149149
"cmake 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)",
150-
"filetime 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
150+
"filetime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
151151
"getopts 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)",
152152
"lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
153153
"libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -416,7 +416,7 @@ version = "0.0.0"
416416
dependencies = [
417417
"diff 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
418418
"env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)",
419-
"filetime 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
419+
"filetime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
420420
"getopts 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)",
421421
"lazy_static 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
422422
"libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -693,16 +693,6 @@ dependencies = [
693693
name = "features"
694694
version = "0.1.0"
695695

696-
[[package]]
697-
name = "filetime"
698-
version = "0.1.15"
699-
source = "registry+https://github.com/rust-lang/crates.io-index"
700-
dependencies = [
701-
"cfg-if 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
702-
"libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)",
703-
"redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
704-
]
705-
706696
[[package]]
707697
name = "filetime"
708698
version = "0.2.1"
@@ -1139,11 +1129,11 @@ dependencies = [
11391129

11401130
[[package]]
11411131
name = "lzma-sys"
1142-
version = "0.1.9"
1132+
version = "0.1.10"
11431133
source = "registry+https://github.com/rust-lang/crates.io-index"
11441134
dependencies = [
11451135
"cc 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)",
1146-
"filetime 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
1136+
"filetime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
11471137
"libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)",
11481138
"pkg-config 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)",
11491139
]
@@ -3075,7 +3065,7 @@ name = "xz2"
30753065
version = "0.1.5"
30763066
source = "registry+https://github.com/rust-lang/crates.io-index"
30773067
dependencies = [
3078-
"lzma-sys 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
3068+
"lzma-sys 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
30793069
]
30803070

30813071
[[package]]
@@ -3134,7 +3124,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
31343124
"checksum error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff511d5dc435d703f4971bc399647c9bc38e20cb41452e3b9feb4765419ed3f3"
31353125
"checksum failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "934799b6c1de475a012a02dab0ace1ace43789ee4b99bcfbf1a2e3e8ced5de82"
31363126
"checksum failure_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c7cdda555bb90c9bb67a3b670a0f42de8e73f5981524123ad8578aafec8ddb8b"
3137-
"checksum filetime 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "714653f3e34871534de23771ac7b26e999651a0a228f47beb324dfdf1dd4b10f"
31383127
"checksum filetime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "da4b9849e77b13195302c174324b5ba73eec9b236b24c221a61000daefb95c5f"
31393128
"checksum fixedbitset 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "86d4de0081402f5e88cdac65c8dcdcc73118c1a7a465e2a05f0da05843a8ea33"
31403129
"checksum flate2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9fac2277e84e5e858483756647a9d0aa8d9a2b7cba517fd84325a0aaa69a0909"
@@ -3179,7 +3168,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
31793168
"checksum log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b"
31803169
"checksum log 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "61bd98ae7f7b754bc53dca7d44b604f733c6bba044ea6f41bc8d89272d8161d2"
31813170
"checksum log_settings 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "19af41f0565d7c19b2058153ad0b42d4d5ce89ec4dbf06ed6741114a8b63e7cd"
3182-
"checksum lzma-sys 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c1b93b78f89e8737dac81837fc8f5521ac162abcba902e1a3db949d55346d1da"
3171+
"checksum lzma-sys 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "d1eaa027402541975218bb0eec67d6b0412f6233af96e0d096d31dbdfd22e614"
31833172
"checksum mac 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c41e0c4fef86961ac6d6f8a82609f55f31b05e4fce149ac5710e439df7619ba4"
31843173
"checksum maplit 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "08cbb6b4fef96b6d77bfc40ec491b1690c779e77b05cd9f07f787ed376fd4c43"
31853174
"checksum markup5ever 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bfedc97d5a503e96816d10fedcd5b42f760b2e525ce2f7ec71f6a41780548475"

src/bootstrap/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ test = false
3636
[dependencies]
3737
build_helper = { path = "../build_helper" }
3838
cmake = "0.1.23"
39-
filetime = "0.1"
39+
filetime = "0.2"
4040
num_cpus = "1.0"
4141
getopts = "0.2"
4242
cc = "1.0.1"

src/liballoc/rc.rs

+42-25
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ use core::hash::{Hash, Hasher};
253253
use core::intrinsics::abort;
254254
use core::marker;
255255
use core::marker::{Unsize, PhantomData};
256-
use core::mem::{self, align_of_val, forget, size_of_val, uninitialized};
256+
use core::mem::{self, align_of_val, forget, size_of_val};
257257
use core::ops::Deref;
258258
use core::ops::CoerceUnsized;
259259
use core::ptr::{self, NonNull};
@@ -620,13 +620,12 @@ impl<T: Clone> Rc<T> {
620620

621621
impl Rc<Any> {
622622
#[inline]
623-
#[unstable(feature = "rc_downcast", issue = "44608")]
623+
#[stable(feature = "rc_downcast", since = "1.29.0")]
624624
/// Attempt to downcast the `Rc<Any>` to a concrete type.
625625
///
626626
/// # Examples
627627
///
628628
/// ```
629-
/// #![feature(rc_downcast)]
630629
/// use std::any::Any;
631630
/// use std::rc::Rc;
632631
///
@@ -1153,6 +1152,10 @@ impl<T> From<Vec<T>> for Rc<[T]> {
11531152
/// [`None`]: ../../std/option/enum.Option.html#variant.None
11541153
#[stable(feature = "rc_weak", since = "1.4.0")]
11551154
pub struct Weak<T: ?Sized> {
1155+
// This is a `NonNull` to allow optimizing the size of this type in enums,
1156+
// but it is not necessarily a valid pointer.
1157+
// `Weak::new` sets this to a dangling pointer so that it doesn’t need
1158+
// to allocate space on the heap.
11561159
ptr: NonNull<RcBox<T>>,
11571160
}
11581161

@@ -1165,8 +1168,8 @@ impl<T: ?Sized> !marker::Sync for Weak<T> {}
11651168
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Weak<U>> for Weak<T> {}
11661169

11671170
impl<T> Weak<T> {
1168-
/// Constructs a new `Weak<T>`, allocating memory for `T` without initializing
1169-
/// it. Calling [`upgrade`] on the return value always gives [`None`].
1171+
/// Constructs a new `Weak<T>`, without allocating any memory.
1172+
/// Calling [`upgrade`] on the return value always gives [`None`].
11701173
///
11711174
/// [`upgrade`]: struct.Weak.html#method.upgrade
11721175
/// [`None`]: ../../std/option/enum.Option.html
@@ -1181,18 +1184,18 @@ impl<T> Weak<T> {
11811184
/// ```
11821185
#[stable(feature = "downgraded_weak", since = "1.10.0")]
11831186
pub fn new() -> Weak<T> {
1184-
unsafe {
1185-
Weak {
1186-
ptr: Box::into_raw_non_null(box RcBox {
1187-
strong: Cell::new(0),
1188-
weak: Cell::new(1),
1189-
value: uninitialized(),
1190-
}),
1191-
}
1187+
Weak {
1188+
ptr: NonNull::dangling(),
11921189
}
11931190
}
11941191
}
11951192

1193+
pub(crate) fn is_dangling<T: ?Sized>(ptr: NonNull<T>) -> bool {
1194+
let address = ptr.as_ptr() as *mut () as usize;
1195+
let align = align_of_val(unsafe { ptr.as_ref() });
1196+
address == align
1197+
}
1198+
11961199
impl<T: ?Sized> Weak<T> {
11971200
/// Attempts to upgrade the `Weak` pointer to an [`Rc`], extending
11981201
/// the lifetime of the value if successful.
@@ -1222,13 +1225,25 @@ impl<T: ?Sized> Weak<T> {
12221225
/// ```
12231226
#[stable(feature = "rc_weak", since = "1.4.0")]
12241227
pub fn upgrade(&self) -> Option<Rc<T>> {
1225-
if self.strong() == 0 {
1228+
let inner = self.inner()?;
1229+
if inner.strong() == 0 {
12261230
None
12271231
} else {
1228-
self.inc_strong();
1232+
inner.inc_strong();
12291233
Some(Rc { ptr: self.ptr, phantom: PhantomData })
12301234
}
12311235
}
1236+
1237+
/// Return `None` when the pointer is dangling and there is no allocated `RcBox`,
1238+
/// i.e. this `Weak` was created by `Weak::new`
1239+
#[inline]
1240+
fn inner(&self) -> Option<&RcBox<T>> {
1241+
if is_dangling(self.ptr) {
1242+
None
1243+
} else {
1244+
Some(unsafe { self.ptr.as_ref() })
1245+
}
1246+
}
12321247
}
12331248

12341249
#[stable(feature = "rc_weak", since = "1.4.0")]
@@ -1258,12 +1273,14 @@ impl<T: ?Sized> Drop for Weak<T> {
12581273
/// assert!(other_weak_foo.upgrade().is_none());
12591274
/// ```
12601275
fn drop(&mut self) {
1261-
unsafe {
1262-
self.dec_weak();
1276+
if let Some(inner) = self.inner() {
1277+
inner.dec_weak();
12631278
// the weak count starts at 1, and will only go to zero if all
12641279
// the strong pointers have disappeared.
1265-
if self.weak() == 0 {
1266-
Global.dealloc(self.ptr.cast(), Layout::for_value(self.ptr.as_ref()));
1280+
if inner.weak() == 0 {
1281+
unsafe {
1282+
Global.dealloc(self.ptr.cast(), Layout::for_value(self.ptr.as_ref()));
1283+
}
12671284
}
12681285
}
12691286
}
@@ -1284,7 +1301,9 @@ impl<T: ?Sized> Clone for Weak<T> {
12841301
/// ```
12851302
#[inline]
12861303
fn clone(&self) -> Weak<T> {
1287-
self.inc_weak();
1304+
if let Some(inner) = self.inner() {
1305+
inner.inc_weak()
1306+
}
12881307
Weak { ptr: self.ptr }
12891308
}
12901309
}
@@ -1317,7 +1336,7 @@ impl<T> Default for Weak<T> {
13171336
}
13181337
}
13191338

1320-
// NOTE: We checked_add here to deal with mem::forget safety. In particular
1339+
// NOTE: We checked_add here to deal with mem::forget safely. In particular
13211340
// if you mem::forget Rcs (or Weaks), the ref-count can overflow, and then
13221341
// you can free the allocation while outstanding Rcs (or Weaks) exist.
13231342
// We abort because this is such a degenerate scenario that we don't care about
@@ -1370,12 +1389,10 @@ impl<T: ?Sized> RcBoxPtr<T> for Rc<T> {
13701389
}
13711390
}
13721391

1373-
impl<T: ?Sized> RcBoxPtr<T> for Weak<T> {
1392+
impl<T: ?Sized> RcBoxPtr<T> for RcBox<T> {
13741393
#[inline(always)]
13751394
fn inner(&self) -> &RcBox<T> {
1376-
unsafe {
1377-
self.ptr.as_ref()
1378-
}
1395+
self
13791396
}
13801397
}
13811398

src/liballoc/sync.rs

+25-23
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use core::convert::From;
3434

3535
use alloc::{Global, Alloc, Layout, box_free, handle_alloc_error};
3636
use boxed::Box;
37+
use rc::is_dangling;
3738
use string::String;
3839
use vec::Vec;
3940

@@ -43,9 +44,6 @@ use vec::Vec;
4344
/// necessarily) at _exactly_ `MAX_REFCOUNT + 1` references.
4445
const MAX_REFCOUNT: usize = (isize::MAX) as usize;
4546

46-
/// A sentinel value that is used for the pointer of `Weak::new()`.
47-
const WEAK_EMPTY: usize = 1;
48-
4947
/// A thread-safe reference-counting pointer. 'Arc' stands for 'Atomically
5048
/// Reference Counted'.
5149
///
@@ -239,9 +237,9 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Arc<U>> for Arc<T> {}
239237
#[stable(feature = "arc_weak", since = "1.4.0")]
240238
pub struct Weak<T: ?Sized> {
241239
// This is a `NonNull` to allow optimizing the size of this type in enums,
242-
// but it is actually not truly "non-null". A `Weak::new()` will set this
243-
// to a sentinel value, instead of needing to allocate some space in the
244-
// heap.
240+
// but it is not necessarily a valid pointer.
241+
// `Weak::new` sets this to a dangling pointer so that it doesn’t need
242+
// to allocate space on the heap.
245243
ptr: NonNull<ArcInner<T>>,
246244
}
247245

@@ -982,13 +980,12 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Arc<T> {
982980

983981
impl Arc<Any + Send + Sync> {
984982
#[inline]
985-
#[unstable(feature = "rc_downcast", issue = "44608")]
983+
#[stable(feature = "rc_downcast", since = "1.29.0")]
986984
/// Attempt to downcast the `Arc<Any + Send + Sync>` to a concrete type.
987985
///
988986
/// # Examples
989987
///
990988
/// ```
991-
/// #![feature(rc_downcast)]
992989
/// use std::any::Any;
993990
/// use std::sync::Arc;
994991
///
@@ -1035,10 +1032,8 @@ impl<T> Weak<T> {
10351032
/// ```
10361033
#[stable(feature = "downgraded_weak", since = "1.10.0")]
10371034
pub fn new() -> Weak<T> {
1038-
unsafe {
1039-
Weak {
1040-
ptr: NonNull::new_unchecked(WEAK_EMPTY as *mut _),
1041-
}
1035+
Weak {
1036+
ptr: NonNull::dangling(),
10421037
}
10431038
}
10441039
}
@@ -1074,11 +1069,7 @@ impl<T: ?Sized> Weak<T> {
10741069
pub fn upgrade(&self) -> Option<Arc<T>> {
10751070
// We use a CAS loop to increment the strong count instead of a
10761071
// fetch_add because once the count hits 0 it must never be above 0.
1077-
let inner = if self.ptr.as_ptr() as *const u8 as usize == WEAK_EMPTY {
1078-
return None;
1079-
} else {
1080-
unsafe { self.ptr.as_ref() }
1081-
};
1072+
let inner = self.inner()?;
10821073

10831074
// Relaxed load because any write of 0 that we can observe
10841075
// leaves the field in a permanently zero state (so a
@@ -1109,6 +1100,17 @@ impl<T: ?Sized> Weak<T> {
11091100
}
11101101
}
11111102
}
1103+
1104+
/// Return `None` when the pointer is dangling and there is no allocated `ArcInner`,
1105+
/// i.e. this `Weak` was created by `Weak::new`
1106+
#[inline]
1107+
fn inner(&self) -> Option<&ArcInner<T>> {
1108+
if is_dangling(self.ptr) {
1109+
None
1110+
} else {
1111+
Some(unsafe { self.ptr.as_ref() })
1112+
}
1113+
}
11121114
}
11131115

11141116
#[stable(feature = "arc_weak", since = "1.4.0")]
@@ -1126,10 +1128,10 @@ impl<T: ?Sized> Clone for Weak<T> {
11261128
/// ```
11271129
#[inline]
11281130
fn clone(&self) -> Weak<T> {
1129-
let inner = if self.ptr.as_ptr() as *const u8 as usize == WEAK_EMPTY {
1130-
return Weak { ptr: self.ptr };
1131+
let inner = if let Some(inner) = self.inner() {
1132+
inner
11311133
} else {
1132-
unsafe { self.ptr.as_ref() }
1134+
return Weak { ptr: self.ptr };
11331135
};
11341136
// See comments in Arc::clone() for why this is relaxed. This can use a
11351137
// fetch_add (ignoring the lock) because the weak count is only locked
@@ -1204,10 +1206,10 @@ impl<T: ?Sized> Drop for Weak<T> {
12041206
// weak count can only be locked if there was precisely one weak ref,
12051207
// meaning that drop could only subsequently run ON that remaining weak
12061208
// ref, which can only happen after the lock is released.
1207-
let inner = if self.ptr.as_ptr() as *const u8 as usize == WEAK_EMPTY {
1208-
return;
1209+
let inner = if let Some(inner) = self.inner() {
1210+
inner
12091211
} else {
1210-
unsafe { self.ptr.as_ref() }
1212+
return
12111213
};
12121214

12131215
if inner.weak.fetch_sub(1, Release) == 1 {

0 commit comments

Comments
 (0)