Skip to content

Commit 0862458

Browse files
committed
Auto merge of rust-lang#71556 - Dylan-DPC:rollup-9ll4shr, r=Dylan-DPC
Rollup of 7 pull requests Successful merges: - rust-lang#69041 (proc_macro: Stabilize `Span::resolved_at` and `Span::located_at`) - rust-lang#69813 (Implement BitOr and BitOrAssign for the NonZero integer types) - rust-lang#70712 (stabilize BTreeMap::remove_entry) - rust-lang#71168 (Deprecate `{Box,Rc,Arc}::into_raw_non_null`) - rust-lang#71544 (Replace filter_map().next() calls with find_map()) - rust-lang#71545 (Fix comment in docstring example for Error::kind) - rust-lang#71548 (Add missing Send and Sync impls for linked list Cursor and CursorMut.) Failed merges: r? @ghost
2 parents 659951c + 82642d7 commit 0862458

File tree

34 files changed

+314
-188
lines changed

34 files changed

+314
-188
lines changed

src/liballoc/boxed.rs

+31-15
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,12 @@ impl<T: ?Sized> Box<T> {
428428
#[stable(feature = "box_raw", since = "1.4.0")]
429429
#[inline]
430430
pub fn into_raw(b: Box<T>) -> *mut T {
431-
Box::into_raw_non_null(b).as_ptr()
431+
// Box is recognized as a "unique pointer" by Stacked Borrows, but internally it is a
432+
// raw pointer for the type system. Turning it directly into a raw pointer would not be
433+
// recognized as "releasing" the unique pointer to permit aliased raw accesses,
434+
// so all raw pointer methods go through `leak` which creates a (unique)
435+
// mutable reference. Turning *that* to a raw pointer behaves correctly.
436+
Box::leak(b) as *mut T
432437
}
433438

434439
/// Consumes the `Box`, returning the wrapped pointer as `NonNull<T>`.
@@ -451,6 +456,7 @@ impl<T: ?Sized> Box<T> {
451456
///
452457
/// ```
453458
/// #![feature(box_into_raw_non_null)]
459+
/// #![allow(deprecated)]
454460
///
455461
/// let x = Box::new(5);
456462
/// let ptr = Box::into_raw_non_null(x);
@@ -460,24 +466,34 @@ impl<T: ?Sized> Box<T> {
460466
/// let x = unsafe { Box::from_raw(ptr.as_ptr()) };
461467
/// ```
462468
#[unstable(feature = "box_into_raw_non_null", issue = "47336")]
469+
#[rustc_deprecated(
470+
since = "1.44.0",
471+
reason = "use `Box::leak(b).into()` or `NonNull::from(Box::leak(b))` instead"
472+
)]
463473
#[inline]
464474
pub fn into_raw_non_null(b: Box<T>) -> NonNull<T> {
465-
Box::into_unique(b).into()
466-
}
467-
468-
#[unstable(feature = "ptr_internals", issue = "none", reason = "use into_raw_non_null instead")]
475+
// Box is recognized as a "unique pointer" by Stacked Borrows, but internally it is a
476+
// raw pointer for the type system. Turning it directly into a raw pointer would not be
477+
// recognized as "releasing" the unique pointer to permit aliased raw accesses,
478+
// so all raw pointer methods go through `leak` which creates a (unique)
479+
// mutable reference. Turning *that* to a raw pointer behaves correctly.
480+
Box::leak(b).into()
481+
}
482+
483+
#[unstable(
484+
feature = "ptr_internals",
485+
issue = "none",
486+
reason = "use `Box::leak(b).into()` or `Unique::from(Box::leak(b))` instead"
487+
)]
469488
#[inline]
470489
#[doc(hidden)]
471490
pub fn into_unique(b: Box<T>) -> Unique<T> {
472-
let b = mem::ManuallyDrop::new(b);
473-
let mut unique = b.0;
474-
// Box is kind-of a library type, but recognized as a "unique pointer" by
475-
// Stacked Borrows. This function here corresponds to "reborrowing to
476-
// a raw pointer", but there is no actual reborrow here -- so
477-
// without some care, the pointer we are returning here still carries
478-
// the tag of `b`, with `Unique` permission.
479-
// We round-trip through a mutable reference to avoid that.
480-
unsafe { Unique::new_unchecked(unique.as_mut() as *mut T) }
491+
// Box is recognized as a "unique pointer" by Stacked Borrows, but internally it is a
492+
// raw pointer for the type system. Turning it directly into a raw pointer would not be
493+
// recognized as "releasing" the unique pointer to permit aliased raw accesses,
494+
// so all raw pointer methods go through `leak` which creates a (unique)
495+
// mutable reference. Turning *that* to a raw pointer behaves correctly.
496+
Box::leak(b).into()
481497
}
482498

483499
/// Consumes and leaks the `Box`, returning a mutable reference,
@@ -523,7 +539,7 @@ impl<T: ?Sized> Box<T> {
523539
where
524540
T: 'a, // Technically not needed, but kept to be explicit.
525541
{
526-
unsafe { &mut *Box::into_raw(b) }
542+
unsafe { &mut *mem::ManuallyDrop::new(b).0.as_ptr() }
527543
}
528544

529545
/// Converts a `Box<T>` into a `Pin<Box<T>>`

src/liballoc/collections/btree/map.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -923,15 +923,14 @@ impl<K: Ord, V> BTreeMap<K, V> {
923923
/// Basic usage:
924924
///
925925
/// ```
926-
/// #![feature(btreemap_remove_entry)]
927926
/// use std::collections::BTreeMap;
928927
///
929928
/// let mut map = BTreeMap::new();
930929
/// map.insert(1, "a");
931930
/// assert_eq!(map.remove_entry(&1), Some((1, "a")));
932931
/// assert_eq!(map.remove_entry(&1), None);
933932
/// ```
934-
#[unstable(feature = "btreemap_remove_entry", issue = "66714")]
933+
#[stable(feature = "btreemap_remove_entry", since = "1.44.0")]
935934
pub fn remove_entry<Q: ?Sized>(&mut self, key: &Q) -> Option<(K, V)>
936935
where
937936
K: Borrow<Q>,

src/liballoc/collections/linked_list.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl<T> LinkedList<T> {
143143
unsafe {
144144
node.next = self.head;
145145
node.prev = None;
146-
let node = Some(Box::into_raw_non_null(node));
146+
let node = Some(Box::leak(node).into());
147147

148148
match self.head {
149149
None => self.tail = node,
@@ -184,7 +184,7 @@ impl<T> LinkedList<T> {
184184
unsafe {
185185
node.next = None;
186186
node.prev = self.tail;
187-
let node = Some(Box::into_raw_non_null(node));
187+
let node = Some(Box::leak(node).into());
188188

189189
match self.tail {
190190
None => self.head = node,
@@ -1133,11 +1133,9 @@ impl<T> IterMut<'_, T> {
11331133
Some(prev) => prev,
11341134
};
11351135

1136-
let node = Some(Box::into_raw_non_null(box Node {
1137-
next: Some(head),
1138-
prev: Some(prev),
1139-
element,
1140-
}));
1136+
let node = Some(
1137+
Box::leak(box Node { next: Some(head), prev: Some(prev), element }).into(),
1138+
);
11411139

11421140
// Not creating references to entire nodes to not invalidate the
11431141
// reference to `element` we handed to the user.
@@ -1450,7 +1448,7 @@ impl<'a, T> CursorMut<'a, T> {
14501448
#[unstable(feature = "linked_list_cursors", issue = "58533")]
14511449
pub fn insert_after(&mut self, item: T) {
14521450
unsafe {
1453-
let spliced_node = Box::into_raw_non_null(Box::new(Node::new(item)));
1451+
let spliced_node = Box::leak(Box::new(Node::new(item))).into();
14541452
let node_next = match self.current {
14551453
None => self.list.head,
14561454
Some(node) => node.as_ref().next,
@@ -1470,7 +1468,7 @@ impl<'a, T> CursorMut<'a, T> {
14701468
#[unstable(feature = "linked_list_cursors", issue = "58533")]
14711469
pub fn insert_before(&mut self, item: T) {
14721470
unsafe {
1473-
let spliced_node = Box::into_raw_non_null(Box::new(Node::new(item)));
1471+
let spliced_node = Box::leak(Box::new(Node::new(item))).into();
14741472
let node_prev = match self.current {
14751473
None => self.list.tail,
14761474
Some(node) => node.as_ref().prev,
@@ -1843,3 +1841,15 @@ unsafe impl<T: Send> Send for IterMut<'_, T> {}
18431841

18441842
#[stable(feature = "rust1", since = "1.0.0")]
18451843
unsafe impl<T: Sync> Sync for IterMut<'_, T> {}
1844+
1845+
#[unstable(feature = "linked_list_cursors", issue = "58533")]
1846+
unsafe impl<T: Sync> Send for Cursor<'_, T> {}
1847+
1848+
#[unstable(feature = "linked_list_cursors", issue = "58533")]
1849+
unsafe impl<T: Sync> Sync for Cursor<'_, T> {}
1850+
1851+
#[unstable(feature = "linked_list_cursors", issue = "58533")]
1852+
unsafe impl<T: Send> Send for CursorMut<'_, T> {}
1853+
1854+
#[unstable(feature = "linked_list_cursors", issue = "58533")]
1855+
unsafe impl<T: Sync> Sync for CursorMut<'_, T> {}

src/liballoc/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
#![feature(allocator_api)]
7878
#![feature(allow_internal_unstable)]
7979
#![feature(arbitrary_self_types)]
80-
#![feature(box_into_raw_non_null)]
8180
#![feature(box_patterns)]
8281
#![feature(box_syntax)]
8382
#![feature(cfg_sanitize)]

src/liballoc/rc.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,9 @@ impl<T> Rc<T> {
323323
// pointers, which ensures that the weak destructor never frees
324324
// the allocation while the strong destructor is running, even
325325
// if the weak pointer is stored inside the strong one.
326-
Self::from_inner(Box::into_raw_non_null(box RcBox {
327-
strong: Cell::new(1),
328-
weak: Cell::new(1),
329-
value,
330-
}))
326+
Self::from_inner(
327+
Box::leak(box RcBox { strong: Cell::new(1), weak: Cell::new(1), value }).into(),
328+
)
331329
}
332330

333331
/// Constructs a new `Rc` with uninitialized contents.
@@ -661,6 +659,7 @@ impl<T: ?Sized> Rc<T> {
661659
///
662660
/// ```
663661
/// #![feature(rc_into_raw_non_null)]
662+
/// #![allow(deprecated)]
664663
///
665664
/// use std::rc::Rc;
666665
///
@@ -670,6 +669,7 @@ impl<T: ?Sized> Rc<T> {
670669
/// assert_eq!(deref, "hello");
671670
/// ```
672671
#[unstable(feature = "rc_into_raw_non_null", issue = "47336")]
672+
#[rustc_deprecated(since = "1.44.0", reason = "use `Rc::into_raw` instead")]
673673
#[inline]
674674
pub fn into_raw_non_null(this: Self) -> NonNull<T> {
675675
// safe because Rc guarantees its pointer is non-null

src/liballoc/sync.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ impl<T> Arc<T> {
324324
weak: atomic::AtomicUsize::new(1),
325325
data,
326326
};
327-
Self::from_inner(Box::into_raw_non_null(x))
327+
Self::from_inner(Box::leak(x).into())
328328
}
329329

330330
/// Constructs a new `Arc` with uninitialized contents.
@@ -658,6 +658,7 @@ impl<T: ?Sized> Arc<T> {
658658
///
659659
/// ```
660660
/// #![feature(rc_into_raw_non_null)]
661+
/// #![allow(deprecated)]
661662
///
662663
/// use std::sync::Arc;
663664
///
@@ -667,6 +668,7 @@ impl<T: ?Sized> Arc<T> {
667668
/// assert_eq!(deref, "hello");
668669
/// ```
669670
#[unstable(feature = "rc_into_raw_non_null", issue = "47336")]
671+
#[rustc_deprecated(since = "1.44.0", reason = "use `Arc::into_raw` instead")]
670672
#[inline]
671673
pub fn into_raw_non_null(this: Self) -> NonNull<T> {
672674
// safe because Arc guarantees its pointer is non-null

src/libcore/num/mod.rs

+52
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::convert::Infallible;
88
use crate::fmt;
99
use crate::intrinsics;
1010
use crate::mem;
11+
use crate::ops::{BitOr, BitOrAssign};
1112
use crate::str::FromStr;
1213

1314
// Used because the `?` operator is not allowed in a const context.
@@ -110,6 +111,57 @@ assert_eq!(size_of::<Option<core::num::", stringify!($Ty), ">>(), size_of::<", s
110111
}
111112
}
112113

114+
#[stable(feature = "nonzero_bitor", since = "1.43.0")]
115+
impl BitOr for $Ty {
116+
type Output = Self;
117+
#[inline]
118+
fn bitor(self, rhs: Self) -> Self::Output {
119+
// Safety: since `self` and `rhs` are both nonzero, the
120+
// result of the bitwise-or will be nonzero.
121+
unsafe { $Ty::new_unchecked(self.get() | rhs.get()) }
122+
}
123+
}
124+
125+
#[stable(feature = "nonzero_bitor", since = "1.43.0")]
126+
impl BitOr<$Int> for $Ty {
127+
type Output = Self;
128+
#[inline]
129+
fn bitor(self, rhs: $Int) -> Self::Output {
130+
// Safety: since `self` is nonzero, the result of the
131+
// bitwise-or will be nonzero regardless of the value of
132+
// `rhs`.
133+
unsafe { $Ty::new_unchecked(self.get() | rhs) }
134+
}
135+
}
136+
137+
#[stable(feature = "nonzero_bitor", since = "1.43.0")]
138+
impl BitOr<$Ty> for $Int {
139+
type Output = $Ty;
140+
#[inline]
141+
fn bitor(self, rhs: $Ty) -> Self::Output {
142+
// Safety: since `rhs` is nonzero, the result of the
143+
// bitwise-or will be nonzero regardless of the value of
144+
// `self`.
145+
unsafe { $Ty::new_unchecked(self | rhs.get()) }
146+
}
147+
}
148+
149+
#[stable(feature = "nonzero_bitor", since = "1.43.0")]
150+
impl BitOrAssign for $Ty {
151+
#[inline]
152+
fn bitor_assign(&mut self, rhs: Self) {
153+
*self = *self | rhs;
154+
}
155+
}
156+
157+
#[stable(feature = "nonzero_bitor", since = "1.43.0")]
158+
impl BitOrAssign<$Int> for $Ty {
159+
#[inline]
160+
fn bitor_assign(&mut self, rhs: $Int) {
161+
*self = *self | rhs;
162+
}
163+
}
164+
113165
impl_nonzero_fmt! {
114166
#[$stability] (Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty
115167
}

src/libcore/tests/nonzero.rs

+35
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,38 @@ fn test_from_str() {
141141
Some(IntErrorKind::Overflow)
142142
);
143143
}
144+
145+
#[test]
146+
fn test_nonzero_bitor() {
147+
let nz_alt = NonZeroU8::new(0b1010_1010).unwrap();
148+
let nz_low = NonZeroU8::new(0b0000_1111).unwrap();
149+
150+
let both_nz: NonZeroU8 = nz_alt | nz_low;
151+
assert_eq!(both_nz.get(), 0b1010_1111);
152+
153+
let rhs_int: NonZeroU8 = nz_low | 0b1100_0000u8;
154+
assert_eq!(rhs_int.get(), 0b1100_1111);
155+
156+
let rhs_zero: NonZeroU8 = nz_alt | 0u8;
157+
assert_eq!(rhs_zero.get(), 0b1010_1010);
158+
159+
let lhs_int: NonZeroU8 = 0b0110_0110u8 | nz_alt;
160+
assert_eq!(lhs_int.get(), 0b1110_1110);
161+
162+
let lhs_zero: NonZeroU8 = 0u8 | nz_low;
163+
assert_eq!(lhs_zero.get(), 0b0000_1111);
164+
}
165+
166+
#[test]
167+
fn test_nonzero_bitor_assign() {
168+
let mut target = NonZeroU8::new(0b1010_1010).unwrap();
169+
170+
target |= NonZeroU8::new(0b0000_1111).unwrap();
171+
assert_eq!(target.get(), 0b1010_1111);
172+
173+
target |= 0b0001_0000;
174+
assert_eq!(target.get(), 0b1011_1111);
175+
176+
target |= 0;
177+
assert_eq!(target.get(), 0b1011_1111);
178+
}

src/libproc_macro/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -351,14 +351,14 @@ impl Span {
351351

352352
/// Creates a new span with the same line/column information as `self` but
353353
/// that resolves symbols as though it were at `other`.
354-
#[unstable(feature = "proc_macro_span", issue = "54725")]
354+
#[stable(feature = "proc_macro_span_resolved_at", since = "1.43.0")]
355355
pub fn resolved_at(&self, other: Span) -> Span {
356356
Span(self.0.resolved_at(other.0))
357357
}
358358

359359
/// Creates a new span with the same name resolution behavior as `self` but
360360
/// with the line/column information of `other`.
361-
#[unstable(feature = "proc_macro_span", issue = "54725")]
361+
#[stable(feature = "proc_macro_span_located_at", since = "1.43.0")]
362362
pub fn located_at(&self, other: Span) -> Span {
363363
other.resolved_at(*self)
364364
}

src/librustc_codegen_ssa/back/linker.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1127,11 +1127,7 @@ fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> {
11271127
}
11281128

11291129
let formats = tcx.dependency_formats(LOCAL_CRATE);
1130-
let deps = formats
1131-
.iter()
1132-
.filter_map(|(t, list)| if *t == crate_type { Some(list) } else { None })
1133-
.next()
1134-
.unwrap();
1130+
let deps = formats.iter().find_map(|(t, list)| (*t == crate_type).then_some(list)).unwrap();
11351131

11361132
for (index, dep_format) in deps.iter().enumerate() {
11371133
let cnum = CrateNum::new(index + 1);

src/librustc_errors/emitter.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -285,21 +285,18 @@ pub trait Emitter {
285285
let has_macro_spans = iter::once(&*span)
286286
.chain(children.iter().map(|child| &child.span))
287287
.flat_map(|span| span.primary_spans())
288-
.copied()
289-
.flat_map(|sp| {
290-
sp.macro_backtrace().filter_map(|expn_data| {
291-
match expn_data.kind {
292-
ExpnKind::Root => None,
288+
.flat_map(|sp| sp.macro_backtrace())
289+
.find_map(|expn_data| {
290+
match expn_data.kind {
291+
ExpnKind::Root => None,
293292

294-
// Skip past non-macro entries, just in case there
295-
// are some which do actually involve macros.
296-
ExpnKind::Desugaring(..) | ExpnKind::AstPass(..) => None,
293+
// Skip past non-macro entries, just in case there
294+
// are some which do actually involve macros.
295+
ExpnKind::Desugaring(..) | ExpnKind::AstPass(..) => None,
297296

298-
ExpnKind::Macro(macro_kind, _) => Some(macro_kind),
299-
}
300-
})
301-
})
302-
.next();
297+
ExpnKind::Macro(macro_kind, _) => Some(macro_kind),
298+
}
299+
});
303300

304301
if !backtrace {
305302
self.fix_multispans_in_extern_macros(source_map, span, children);

0 commit comments

Comments
 (0)