Skip to content

Commit e20fa18

Browse files
authored
Unrolled build for rust-lang#130822
Rollup merge of rust-lang#130822 - bjoernager:non-null-from-ref, r=dtolnay Add `from_ref` and `from_mut` constructors to `core::ptr::NonNull`. Relevant tracking issue: rust-lang#130823 The `core::ptr::NonNull` type should have the convenience constructors `from_ref` and `from_mut` for parity with `core::ptr::from_ref` and `core::ptr::from_mut`. Although the type in question already implements `From<&T>` and `From<&mut T>`, these new functions also carry the ability to be used in constant expressions (due to not being behind a trait).
2 parents bed75e7 + 94ab726 commit e20fa18

8 files changed

+97
-68
lines changed

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
#![feature(isqrt)]
154154
#![feature(lazy_get)]
155155
#![feature(link_cfg)]
156+
#![feature(non_null_from_ref)]
156157
#![feature(offset_of_enum)]
157158
#![feature(panic_internals)]
158159
#![feature(ptr_alignment_type)]

library/core/src/ptr/non_null.rs

+22-6
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,24 @@ impl<T: ?Sized> NonNull<T> {
230230
}
231231
}
232232

233+
/// Converts a reference to a `NonNull` pointer.
234+
#[unstable(feature = "non_null_from_ref", issue = "130823")]
235+
#[rustc_const_unstable(feature = "non_null_from_ref", issue = "130823")]
236+
#[inline]
237+
pub const fn from_ref(r: &T) -> Self {
238+
// SAFETY: A reference cannot be null.
239+
unsafe { NonNull { pointer: r as *const T } }
240+
}
241+
242+
/// Converts a mutable reference to a `NonNull` pointer.
243+
#[unstable(feature = "non_null_from_ref", issue = "130823")]
244+
#[rustc_const_unstable(feature = "non_null_from_ref", issue = "130823")]
245+
#[inline]
246+
pub const fn from_mut(r: &mut T) -> Self {
247+
// SAFETY: A mutable reference cannot be null.
248+
unsafe { NonNull { pointer: r as *mut T } }
249+
}
250+
233251
/// Performs the same functionality as [`std::ptr::from_raw_parts`], except that a
234252
/// `NonNull` pointer is returned, as opposed to a raw `*const` pointer.
235253
///
@@ -1749,9 +1767,8 @@ impl<T: ?Sized> From<&mut T> for NonNull<T> {
17491767
///
17501768
/// This conversion is safe and infallible since references cannot be null.
17511769
#[inline]
1752-
fn from(reference: &mut T) -> Self {
1753-
// SAFETY: A mutable reference cannot be null.
1754-
unsafe { NonNull { pointer: reference as *mut T } }
1770+
fn from(r: &mut T) -> Self {
1771+
NonNull::from_mut(r)
17551772
}
17561773
}
17571774

@@ -1761,8 +1778,7 @@ impl<T: ?Sized> From<&T> for NonNull<T> {
17611778
///
17621779
/// This conversion is safe and infallible since references cannot be null.
17631780
#[inline]
1764-
fn from(reference: &T) -> Self {
1765-
// SAFETY: A reference cannot be null.
1766-
unsafe { NonNull { pointer: reference as *const T } }
1781+
fn from(r: &T) -> Self {
1782+
NonNull::from_ref(r)
17671783
}
17681784
}

tests/mir-opt/pre-codegen/slice_iter.enumerated_loop.PreCodegen.after.panic-abort.mir

+21-19
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,30 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
1919
debug i => _22;
2020
debug x => _23;
2121
}
22-
scope 17 (inlined <Enumerate<std::slice::Iter<'_, T>> as Iterator>::next) {
22+
scope 18 (inlined <Enumerate<std::slice::Iter<'_, T>> as Iterator>::next) {
2323
let mut _14: &mut std::slice::Iter<'_, T>;
2424
let mut _15: std::option::Option<&T>;
2525
let mut _19: (usize, bool);
2626
let mut _20: (usize, &T);
27-
scope 18 {
27+
scope 19 {
2828
let _18: usize;
29-
scope 23 {
29+
scope 24 {
3030
}
3131
}
32-
scope 19 {
33-
scope 20 {
34-
scope 26 (inlined <Option<(usize, &T)> as FromResidual<Option<Infallible>>>::from_residual) {
32+
scope 20 {
33+
scope 21 {
34+
scope 27 (inlined <Option<(usize, &T)> as FromResidual<Option<Infallible>>>::from_residual) {
3535
}
3636
}
3737
}
38-
scope 21 {
39-
scope 22 {
38+
scope 22 {
39+
scope 23 {
4040
}
4141
}
42-
scope 24 (inlined <Option<&T> as Try>::branch) {
42+
scope 25 (inlined <Option<&T> as Try>::branch) {
4343
let mut _16: isize;
4444
let _17: &T;
45-
scope 25 {
45+
scope 26 {
4646
}
4747
}
4848
}
@@ -59,29 +59,31 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
5959
let _9: *const T;
6060
scope 7 {
6161
}
62-
scope 11 (inlined without_provenance::<T>) {
62+
scope 12 (inlined without_provenance::<T>) {
6363
}
64-
scope 12 (inlined NonNull::<T>::as_ptr) {
64+
scope 13 (inlined NonNull::<T>::as_ptr) {
6565
}
66-
scope 13 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
66+
scope 14 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
6767
}
6868
}
6969
scope 8 (inlined <NonNull<[T]> as From<&[T]>>::from) {
70-
let mut _4: *const [T];
70+
scope 9 (inlined NonNull::<[T]>::from_ref) {
71+
let mut _4: *const [T];
72+
}
7173
}
72-
scope 9 (inlined NonNull::<[T]>::cast::<T>) {
74+
scope 10 (inlined NonNull::<[T]>::cast::<T>) {
7375
let mut _5: *const T;
74-
scope 10 (inlined NonNull::<[T]>::as_ptr) {
76+
scope 11 (inlined NonNull::<[T]>::as_ptr) {
7577
}
7678
}
7779
}
7880
}
7981
}
80-
scope 14 (inlined <std::slice::Iter<'_, T> as Iterator>::enumerate) {
81-
scope 15 (inlined Enumerate::<std::slice::Iter<'_, T>>::new) {
82+
scope 15 (inlined <std::slice::Iter<'_, T> as Iterator>::enumerate) {
83+
scope 16 (inlined Enumerate::<std::slice::Iter<'_, T>>::new) {
8284
}
8385
}
84-
scope 16 (inlined <Enumerate<std::slice::Iter<'_, T>> as IntoIterator>::into_iter) {
86+
scope 17 (inlined <Enumerate<std::slice::Iter<'_, T>> as IntoIterator>::into_iter) {
8587
}
8688

8789
bb0: {

tests/mir-opt/pre-codegen/slice_iter.enumerated_loop.PreCodegen.after.panic-unwind.mir

+11-9
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,31 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
3434
let _9: *const T;
3535
scope 7 {
3636
}
37-
scope 11 (inlined without_provenance::<T>) {
37+
scope 12 (inlined without_provenance::<T>) {
3838
}
39-
scope 12 (inlined NonNull::<T>::as_ptr) {
39+
scope 13 (inlined NonNull::<T>::as_ptr) {
4040
}
41-
scope 13 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
41+
scope 14 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
4242
}
4343
}
4444
scope 8 (inlined <NonNull<[T]> as From<&[T]>>::from) {
45-
let mut _4: *const [T];
45+
scope 9 (inlined NonNull::<[T]>::from_ref) {
46+
let mut _4: *const [T];
47+
}
4648
}
47-
scope 9 (inlined NonNull::<[T]>::cast::<T>) {
49+
scope 10 (inlined NonNull::<[T]>::cast::<T>) {
4850
let mut _5: *const T;
49-
scope 10 (inlined NonNull::<[T]>::as_ptr) {
51+
scope 11 (inlined NonNull::<[T]>::as_ptr) {
5052
}
5153
}
5254
}
5355
}
5456
}
55-
scope 14 (inlined <std::slice::Iter<'_, T> as Iterator>::enumerate) {
56-
scope 15 (inlined Enumerate::<std::slice::Iter<'_, T>>::new) {
57+
scope 15 (inlined <std::slice::Iter<'_, T> as Iterator>::enumerate) {
58+
scope 16 (inlined Enumerate::<std::slice::Iter<'_, T>>::new) {
5759
}
5860
}
59-
scope 16 (inlined <Enumerate<std::slice::Iter<'_, T>> as IntoIterator>::into_iter) {
61+
scope 17 (inlined <Enumerate<std::slice::Iter<'_, T>> as IntoIterator>::into_iter) {
6062
}
6163

6264
bb0: {

tests/mir-opt/pre-codegen/slice_iter.forward_loop.PreCodegen.after.panic-abort.mir

+9-7
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,27 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
3131
let _9: *const T;
3232
scope 7 {
3333
}
34-
scope 11 (inlined without_provenance::<T>) {
34+
scope 12 (inlined without_provenance::<T>) {
3535
}
36-
scope 12 (inlined NonNull::<T>::as_ptr) {
36+
scope 13 (inlined NonNull::<T>::as_ptr) {
3737
}
38-
scope 13 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
38+
scope 14 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
3939
}
4040
}
4141
scope 8 (inlined <NonNull<[T]> as From<&[T]>>::from) {
42-
let mut _4: *const [T];
42+
scope 9 (inlined NonNull::<[T]>::from_ref) {
43+
let mut _4: *const [T];
44+
}
4345
}
44-
scope 9 (inlined NonNull::<[T]>::cast::<T>) {
46+
scope 10 (inlined NonNull::<[T]>::cast::<T>) {
4547
let mut _5: *const T;
46-
scope 10 (inlined NonNull::<[T]>::as_ptr) {
48+
scope 11 (inlined NonNull::<[T]>::as_ptr) {
4749
}
4850
}
4951
}
5052
}
5153
}
52-
scope 14 (inlined <std::slice::Iter<'_, T> as IntoIterator>::into_iter) {
54+
scope 15 (inlined <std::slice::Iter<'_, T> as IntoIterator>::into_iter) {
5355
}
5456

5557
bb0: {

tests/mir-opt/pre-codegen/slice_iter.forward_loop.PreCodegen.after.panic-unwind.mir

+9-7
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,27 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
3131
let _9: *const T;
3232
scope 7 {
3333
}
34-
scope 11 (inlined without_provenance::<T>) {
34+
scope 12 (inlined without_provenance::<T>) {
3535
}
36-
scope 12 (inlined NonNull::<T>::as_ptr) {
36+
scope 13 (inlined NonNull::<T>::as_ptr) {
3737
}
38-
scope 13 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
38+
scope 14 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
3939
}
4040
}
4141
scope 8 (inlined <NonNull<[T]> as From<&[T]>>::from) {
42-
let mut _4: *const [T];
42+
scope 9 (inlined NonNull::<[T]>::from_ref) {
43+
let mut _4: *const [T];
44+
}
4345
}
44-
scope 9 (inlined NonNull::<[T]>::cast::<T>) {
46+
scope 10 (inlined NonNull::<[T]>::cast::<T>) {
4547
let mut _5: *const T;
46-
scope 10 (inlined NonNull::<[T]>::as_ptr) {
48+
scope 11 (inlined NonNull::<[T]>::as_ptr) {
4749
}
4850
}
4951
}
5052
}
5153
}
52-
scope 14 (inlined <std::slice::Iter<'_, T> as IntoIterator>::into_iter) {
54+
scope 15 (inlined <std::slice::Iter<'_, T> as IntoIterator>::into_iter) {
5355
}
5456

5557
bb0: {

tests/mir-opt/pre-codegen/slice_iter.reverse_loop.PreCodegen.after.panic-abort.mir

+12-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
1818
scope 2 {
1919
debug x => _17;
2020
}
21-
scope 17 (inlined <Rev<std::slice::Iter<'_, T>> as Iterator>::next) {
21+
scope 18 (inlined <Rev<std::slice::Iter<'_, T>> as Iterator>::next) {
2222
let mut _14: &mut std::slice::Iter<'_, T>;
2323
}
2424
}
@@ -34,29 +34,31 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
3434
let _9: *const T;
3535
scope 7 {
3636
}
37-
scope 11 (inlined without_provenance::<T>) {
37+
scope 12 (inlined without_provenance::<T>) {
3838
}
39-
scope 12 (inlined NonNull::<T>::as_ptr) {
39+
scope 13 (inlined NonNull::<T>::as_ptr) {
4040
}
41-
scope 13 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
41+
scope 14 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
4242
}
4343
}
4444
scope 8 (inlined <NonNull<[T]> as From<&[T]>>::from) {
45-
let mut _4: *const [T];
45+
scope 9 (inlined NonNull::<[T]>::from_ref) {
46+
let mut _4: *const [T];
47+
}
4648
}
47-
scope 9 (inlined NonNull::<[T]>::cast::<T>) {
49+
scope 10 (inlined NonNull::<[T]>::cast::<T>) {
4850
let mut _5: *const T;
49-
scope 10 (inlined NonNull::<[T]>::as_ptr) {
51+
scope 11 (inlined NonNull::<[T]>::as_ptr) {
5052
}
5153
}
5254
}
5355
}
5456
}
55-
scope 14 (inlined <std::slice::Iter<'_, T> as Iterator>::rev) {
56-
scope 15 (inlined Rev::<std::slice::Iter<'_, T>>::new) {
57+
scope 15 (inlined <std::slice::Iter<'_, T> as Iterator>::rev) {
58+
scope 16 (inlined Rev::<std::slice::Iter<'_, T>>::new) {
5759
}
5860
}
59-
scope 16 (inlined <Rev<std::slice::Iter<'_, T>> as IntoIterator>::into_iter) {
61+
scope 17 (inlined <Rev<std::slice::Iter<'_, T>> as IntoIterator>::into_iter) {
6062
}
6163

6264
bb0: {

tests/mir-opt/pre-codegen/slice_iter.reverse_loop.PreCodegen.after.panic-unwind.mir

+12-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
1818
scope 2 {
1919
debug x => _17;
2020
}
21-
scope 17 (inlined <Rev<std::slice::Iter<'_, T>> as Iterator>::next) {
21+
scope 18 (inlined <Rev<std::slice::Iter<'_, T>> as Iterator>::next) {
2222
let mut _14: &mut std::slice::Iter<'_, T>;
2323
}
2424
}
@@ -34,29 +34,31 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
3434
let _9: *const T;
3535
scope 7 {
3636
}
37-
scope 11 (inlined without_provenance::<T>) {
37+
scope 12 (inlined without_provenance::<T>) {
3838
}
39-
scope 12 (inlined NonNull::<T>::as_ptr) {
39+
scope 13 (inlined NonNull::<T>::as_ptr) {
4040
}
41-
scope 13 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
41+
scope 14 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
4242
}
4343
}
4444
scope 8 (inlined <NonNull<[T]> as From<&[T]>>::from) {
45-
let mut _4: *const [T];
45+
scope 9 (inlined NonNull::<[T]>::from_ref) {
46+
let mut _4: *const [T];
47+
}
4648
}
47-
scope 9 (inlined NonNull::<[T]>::cast::<T>) {
49+
scope 10 (inlined NonNull::<[T]>::cast::<T>) {
4850
let mut _5: *const T;
49-
scope 10 (inlined NonNull::<[T]>::as_ptr) {
51+
scope 11 (inlined NonNull::<[T]>::as_ptr) {
5052
}
5153
}
5254
}
5355
}
5456
}
55-
scope 14 (inlined <std::slice::Iter<'_, T> as Iterator>::rev) {
56-
scope 15 (inlined Rev::<std::slice::Iter<'_, T>>::new) {
57+
scope 15 (inlined <std::slice::Iter<'_, T> as Iterator>::rev) {
58+
scope 16 (inlined Rev::<std::slice::Iter<'_, T>>::new) {
5759
}
5860
}
59-
scope 16 (inlined <Rev<std::slice::Iter<'_, T>> as IntoIterator>::into_iter) {
61+
scope 17 (inlined <Rev<std::slice::Iter<'_, T>> as IntoIterator>::into_iter) {
6062
}
6163

6264
bb0: {

0 commit comments

Comments
 (0)