Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 7 pull requests #71556

Merged
merged 19 commits into from
Apr 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
6cdb825
Implement BitOr and BitOrAssign for the NonZero integer types
Mar 8, 2020
3f1a588
stabilize BTreeMap::remove_entry
DutchGhost Apr 2, 2020
cdb6bef
Deprecate `Box::into_raw_non_null`
SimonSapin Apr 15, 2020
b359fe1
Deprecate `Rc::into_raw_non_null` and `Arc::into_raw_non_null`
SimonSapin Apr 15, 2020
9a1c7db
Apply suggestions from code review
SimonSapin Apr 15, 2020
7709d20
Implement `Box::into_raw` based on `Box::leak`
SimonSapin Apr 15, 2020
4282776
Replace filter_map().next() calls with find_map()
cuviper Apr 25, 2020
19c29c4
Fix comment in docstring example for Error::kind
Askaholic Apr 25, 2020
b1fbd79
Add missing Send and Sync bounds for linked list Cursor and CursorMut.
crlf0710 Apr 25, 2020
df99de5
proc_macro: Stabilize `Span::resolved_at` and `Span::located_at`
petrochenkov Feb 10, 2020
966a295
Add a test for `Span::resolved_at` and `Span::located_at`
petrochenkov Apr 18, 2020
78a034d
Use the correct bound for `Cursor` `Send`
crlf0710 Apr 25, 2020
7b7c63c
Rollup merge of #69041 - petrochenkov:stabmodispan, r=Amanieu
Dylan-DPC Apr 25, 2020
b6e03c4
Rollup merge of #69813 - thomcc:nonzero-bitor, r=Amanieu
Dylan-DPC Apr 25, 2020
29fd528
Rollup merge of #70712 - :stabilize-remove-entry, r=Amanieu
Dylan-DPC Apr 25, 2020
939c932
Rollup merge of #71168 - SimonSapin:into_raw_non_null, r=Amanieu
Dylan-DPC Apr 25, 2020
9709785
Rollup merge of #71544 - cuviper:filter_map_next, r=Mark-Simulacrum
Dylan-DPC Apr 25, 2020
ecef6c7
Rollup merge of #71545 - Askaholic:patch-1, r=jonas-schievink
Dylan-DPC Apr 25, 2020
82642d7
Rollup merge of #71548 - crlf0710:cursor_bounds, r=Amanieu
Dylan-DPC Apr 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,12 @@ impl<T: ?Sized> Box<T> {
#[stable(feature = "box_raw", since = "1.4.0")]
#[inline]
pub fn into_raw(b: Box<T>) -> *mut T {
Box::into_raw_non_null(b).as_ptr()
// Box is recognized as a "unique pointer" by Stacked Borrows, but internally it is a
// raw pointer for the type system. Turning it directly into a raw pointer would not be
// recognized as "releasing" the unique pointer to permit aliased raw accesses,
// so all raw pointer methods go through `leak` which creates a (unique)
// mutable reference. Turning *that* to a raw pointer behaves correctly.
Box::leak(b) as *mut T
}

/// Consumes the `Box`, returning the wrapped pointer as `NonNull<T>`.
Expand All @@ -451,6 +456,7 @@ impl<T: ?Sized> Box<T> {
///
/// ```
/// #![feature(box_into_raw_non_null)]
/// #![allow(deprecated)]
///
/// let x = Box::new(5);
/// let ptr = Box::into_raw_non_null(x);
Expand All @@ -460,24 +466,34 @@ impl<T: ?Sized> Box<T> {
/// let x = unsafe { Box::from_raw(ptr.as_ptr()) };
/// ```
#[unstable(feature = "box_into_raw_non_null", issue = "47336")]
#[rustc_deprecated(
since = "1.44.0",
reason = "use `Box::leak(b).into()` or `NonNull::from(Box::leak(b))` instead"
)]
#[inline]
pub fn into_raw_non_null(b: Box<T>) -> NonNull<T> {
Box::into_unique(b).into()
}

#[unstable(feature = "ptr_internals", issue = "none", reason = "use into_raw_non_null instead")]
// Box is recognized as a "unique pointer" by Stacked Borrows, but internally it is a
// raw pointer for the type system. Turning it directly into a raw pointer would not be
// recognized as "releasing" the unique pointer to permit aliased raw accesses,
// so all raw pointer methods go through `leak` which creates a (unique)
// mutable reference. Turning *that* to a raw pointer behaves correctly.
Box::leak(b).into()
}

#[unstable(
feature = "ptr_internals",
issue = "none",
reason = "use `Box::leak(b).into()` or `Unique::from(Box::leak(b))` instead"
)]
#[inline]
#[doc(hidden)]
pub fn into_unique(b: Box<T>) -> Unique<T> {
let b = mem::ManuallyDrop::new(b);
let mut unique = b.0;
// Box is kind-of a library type, but recognized as a "unique pointer" by
// Stacked Borrows. This function here corresponds to "reborrowing to
// a raw pointer", but there is no actual reborrow here -- so
// without some care, the pointer we are returning here still carries
// the tag of `b`, with `Unique` permission.
// We round-trip through a mutable reference to avoid that.
unsafe { Unique::new_unchecked(unique.as_mut() as *mut T) }
// Box is recognized as a "unique pointer" by Stacked Borrows, but internally it is a
// raw pointer for the type system. Turning it directly into a raw pointer would not be
// recognized as "releasing" the unique pointer to permit aliased raw accesses,
// so all raw pointer methods go through `leak` which creates a (unique)
// mutable reference. Turning *that* to a raw pointer behaves correctly.
Box::leak(b).into()
}

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

/// Converts a `Box<T>` into a `Pin<Box<T>>`
Expand Down
3 changes: 1 addition & 2 deletions src/liballoc/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -923,15 +923,14 @@ impl<K: Ord, V> BTreeMap<K, V> {
/// Basic usage:
///
/// ```
/// #![feature(btreemap_remove_entry)]
/// use std::collections::BTreeMap;
///
/// let mut map = BTreeMap::new();
/// map.insert(1, "a");
/// assert_eq!(map.remove_entry(&1), Some((1, "a")));
/// assert_eq!(map.remove_entry(&1), None);
/// ```
#[unstable(feature = "btreemap_remove_entry", issue = "66714")]
#[stable(feature = "btreemap_remove_entry", since = "1.44.0")]
pub fn remove_entry<Q: ?Sized>(&mut self, key: &Q) -> Option<(K, V)>
where
K: Borrow<Q>,
Expand Down
28 changes: 19 additions & 9 deletions src/liballoc/collections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl<T> LinkedList<T> {
unsafe {
node.next = self.head;
node.prev = None;
let node = Some(Box::into_raw_non_null(node));
let node = Some(Box::leak(node).into());

match self.head {
None => self.tail = node,
Expand Down Expand Up @@ -184,7 +184,7 @@ impl<T> LinkedList<T> {
unsafe {
node.next = None;
node.prev = self.tail;
let node = Some(Box::into_raw_non_null(node));
let node = Some(Box::leak(node).into());

match self.tail {
None => self.head = node,
Expand Down Expand Up @@ -1133,11 +1133,9 @@ impl<T> IterMut<'_, T> {
Some(prev) => prev,
};

let node = Some(Box::into_raw_non_null(box Node {
next: Some(head),
prev: Some(prev),
element,
}));
let node = Some(
Box::leak(box Node { next: Some(head), prev: Some(prev), element }).into(),
);

// Not creating references to entire nodes to not invalidate the
// reference to `element` we handed to the user.
Expand Down Expand Up @@ -1450,7 +1448,7 @@ impl<'a, T> CursorMut<'a, T> {
#[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn insert_after(&mut self, item: T) {
unsafe {
let spliced_node = Box::into_raw_non_null(Box::new(Node::new(item)));
let spliced_node = Box::leak(Box::new(Node::new(item))).into();
let node_next = match self.current {
None => self.list.head,
Some(node) => node.as_ref().next,
Expand All @@ -1470,7 +1468,7 @@ impl<'a, T> CursorMut<'a, T> {
#[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn insert_before(&mut self, item: T) {
unsafe {
let spliced_node = Box::into_raw_non_null(Box::new(Node::new(item)));
let spliced_node = Box::leak(Box::new(Node::new(item))).into();
let node_prev = match self.current {
None => self.list.tail,
Some(node) => node.as_ref().prev,
Expand Down Expand Up @@ -1843,3 +1841,15 @@ unsafe impl<T: Send> Send for IterMut<'_, T> {}

#[stable(feature = "rust1", since = "1.0.0")]
unsafe impl<T: Sync> Sync for IterMut<'_, T> {}

#[unstable(feature = "linked_list_cursors", issue = "58533")]
unsafe impl<T: Sync> Send for Cursor<'_, T> {}

#[unstable(feature = "linked_list_cursors", issue = "58533")]
unsafe impl<T: Sync> Sync for Cursor<'_, T> {}

#[unstable(feature = "linked_list_cursors", issue = "58533")]
unsafe impl<T: Send> Send for CursorMut<'_, T> {}

#[unstable(feature = "linked_list_cursors", issue = "58533")]
unsafe impl<T: Sync> Sync for CursorMut<'_, T> {}
1 change: 0 additions & 1 deletion src/liballoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
#![feature(allocator_api)]
#![feature(allow_internal_unstable)]
#![feature(arbitrary_self_types)]
#![feature(box_into_raw_non_null)]
#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(cfg_sanitize)]
Expand Down
10 changes: 5 additions & 5 deletions src/liballoc/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,9 @@ impl<T> Rc<T> {
// pointers, which ensures that the weak destructor never frees
// the allocation while the strong destructor is running, even
// if the weak pointer is stored inside the strong one.
Self::from_inner(Box::into_raw_non_null(box RcBox {
strong: Cell::new(1),
weak: Cell::new(1),
value,
}))
Self::from_inner(
Box::leak(box RcBox { strong: Cell::new(1), weak: Cell::new(1), value }).into(),
)
}

/// Constructs a new `Rc` with uninitialized contents.
Expand Down Expand Up @@ -662,6 +660,7 @@ impl<T: ?Sized> Rc<T> {
///
/// ```
/// #![feature(rc_into_raw_non_null)]
/// #![allow(deprecated)]
///
/// use std::rc::Rc;
///
Expand All @@ -671,6 +670,7 @@ impl<T: ?Sized> Rc<T> {
/// assert_eq!(deref, "hello");
/// ```
#[unstable(feature = "rc_into_raw_non_null", issue = "47336")]
#[rustc_deprecated(since = "1.44.0", reason = "use `Rc::into_raw` instead")]
#[inline]
pub fn into_raw_non_null(this: Self) -> NonNull<T> {
// safe because Rc guarantees its pointer is non-null
Expand Down
4 changes: 3 additions & 1 deletion src/liballoc/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ impl<T> Arc<T> {
weak: atomic::AtomicUsize::new(1),
data,
};
Self::from_inner(Box::into_raw_non_null(x))
Self::from_inner(Box::leak(x).into())
}

/// Constructs a new `Arc` with uninitialized contents.
Expand Down Expand Up @@ -659,6 +659,7 @@ impl<T: ?Sized> Arc<T> {
///
/// ```
/// #![feature(rc_into_raw_non_null)]
/// #![allow(deprecated)]
///
/// use std::sync::Arc;
///
Expand All @@ -668,6 +669,7 @@ impl<T: ?Sized> Arc<T> {
/// assert_eq!(deref, "hello");
/// ```
#[unstable(feature = "rc_into_raw_non_null", issue = "47336")]
#[rustc_deprecated(since = "1.44.0", reason = "use `Arc::into_raw` instead")]
#[inline]
pub fn into_raw_non_null(this: Self) -> NonNull<T> {
// safe because Arc guarantees its pointer is non-null
Expand Down
52 changes: 52 additions & 0 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::convert::Infallible;
use crate::fmt;
use crate::intrinsics;
use crate::mem;
use crate::ops::{BitOr, BitOrAssign};
use crate::str::FromStr;

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

#[stable(feature = "nonzero_bitor", since = "1.43.0")]
impl BitOr for $Ty {
type Output = Self;
#[inline]
fn bitor(self, rhs: Self) -> Self::Output {
// Safety: since `self` and `rhs` are both nonzero, the
// result of the bitwise-or will be nonzero.
unsafe { $Ty::new_unchecked(self.get() | rhs.get()) }
}
}

#[stable(feature = "nonzero_bitor", since = "1.43.0")]
impl BitOr<$Int> for $Ty {
type Output = Self;
#[inline]
fn bitor(self, rhs: $Int) -> Self::Output {
// Safety: since `self` is nonzero, the result of the
// bitwise-or will be nonzero regardless of the value of
// `rhs`.
unsafe { $Ty::new_unchecked(self.get() | rhs) }
}
}

#[stable(feature = "nonzero_bitor", since = "1.43.0")]
impl BitOr<$Ty> for $Int {
type Output = $Ty;
#[inline]
fn bitor(self, rhs: $Ty) -> Self::Output {
// Safety: since `rhs` is nonzero, the result of the
// bitwise-or will be nonzero regardless of the value of
// `self`.
unsafe { $Ty::new_unchecked(self | rhs.get()) }
}
}

#[stable(feature = "nonzero_bitor", since = "1.43.0")]
impl BitOrAssign for $Ty {
#[inline]
fn bitor_assign(&mut self, rhs: Self) {
*self = *self | rhs;
}
}

#[stable(feature = "nonzero_bitor", since = "1.43.0")]
impl BitOrAssign<$Int> for $Ty {
#[inline]
fn bitor_assign(&mut self, rhs: $Int) {
*self = *self | rhs;
}
}

impl_nonzero_fmt! {
#[$stability] (Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty
}
Expand Down
35 changes: 35 additions & 0 deletions src/libcore/tests/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,38 @@ fn test_from_str() {
Some(IntErrorKind::Overflow)
);
}

#[test]
fn test_nonzero_bitor() {
let nz_alt = NonZeroU8::new(0b1010_1010).unwrap();
let nz_low = NonZeroU8::new(0b0000_1111).unwrap();

let both_nz: NonZeroU8 = nz_alt | nz_low;
assert_eq!(both_nz.get(), 0b1010_1111);

let rhs_int: NonZeroU8 = nz_low | 0b1100_0000u8;
assert_eq!(rhs_int.get(), 0b1100_1111);

let rhs_zero: NonZeroU8 = nz_alt | 0u8;
assert_eq!(rhs_zero.get(), 0b1010_1010);

let lhs_int: NonZeroU8 = 0b0110_0110u8 | nz_alt;
assert_eq!(lhs_int.get(), 0b1110_1110);

let lhs_zero: NonZeroU8 = 0u8 | nz_low;
assert_eq!(lhs_zero.get(), 0b0000_1111);
}

#[test]
fn test_nonzero_bitor_assign() {
let mut target = NonZeroU8::new(0b1010_1010).unwrap();

target |= NonZeroU8::new(0b0000_1111).unwrap();
assert_eq!(target.get(), 0b1010_1111);

target |= 0b0001_0000;
assert_eq!(target.get(), 0b1011_1111);

target |= 0;
assert_eq!(target.get(), 0b1011_1111);
}
4 changes: 2 additions & 2 deletions src/libproc_macro/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,14 @@ impl Span {

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

/// Creates a new span with the same name resolution behavior as `self` but
/// with the line/column information of `other`.
#[unstable(feature = "proc_macro_span", issue = "54725")]
#[stable(feature = "proc_macro_span_located_at", since = "1.43.0")]
pub fn located_at(&self, other: Span) -> Span {
other.resolved_at(*self)
}
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_codegen_ssa/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1127,11 +1127,7 @@ fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> {
}

let formats = tcx.dependency_formats(LOCAL_CRATE);
let deps = formats
.iter()
.filter_map(|(t, list)| if *t == crate_type { Some(list) } else { None })
.next()
.unwrap();
let deps = formats.iter().find_map(|(t, list)| (*t == crate_type).then_some(list)).unwrap();

for (index, dep_format) in deps.iter().enumerate() {
let cnum = CrateNum::new(index + 1);
Expand Down
23 changes: 10 additions & 13 deletions src/librustc_errors/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,21 +285,18 @@ pub trait Emitter {
let has_macro_spans = iter::once(&*span)
.chain(children.iter().map(|child| &child.span))
.flat_map(|span| span.primary_spans())
.copied()
.flat_map(|sp| {
sp.macro_backtrace().filter_map(|expn_data| {
match expn_data.kind {
ExpnKind::Root => None,
.flat_map(|sp| sp.macro_backtrace())
.find_map(|expn_data| {
match expn_data.kind {
ExpnKind::Root => None,

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

ExpnKind::Macro(macro_kind, _) => Some(macro_kind),
}
})
})
.next();
ExpnKind::Macro(macro_kind, _) => Some(macro_kind),
}
});

if !backtrace {
self.fix_multispans_in_extern_macros(source_map, span, children);
Expand Down
Loading