Skip to content

Commit fa51f81

Browse files
committed
Auto merge of #71689 - Dylan-DPC:rollup-8nyuwm1, r=Dylan-DPC
Rollup of 6 pull requests Successful merges: - #71507 (Document unsafety in core::ptr) - #71572 (test iterator chain type length blowup) - #71617 (Suggest `into` instead of `try_into` if possible with int types) - #71627 (Fix wrong argument in autoderef process) - #71678 (Add an index page for nightly rustc docs.) - #71680 (Fix doc link to Eq trait from PartialEq trait) Failed merges: - #71597 (Rename Unique::empty() -> Unique::dangling()) r? @ghost
2 parents c50bd7e + d11b559 commit fa51f81

File tree

11 files changed

+137
-74
lines changed

11 files changed

+137
-74
lines changed

src/bootstrap/doc.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,11 @@ impl Step for Rustc {
478478

479479
// Build cargo command.
480480
let mut cargo = builder.cargo(compiler, Mode::Rustc, target, "doc");
481-
cargo.env("RUSTDOCFLAGS", "--document-private-items");
481+
cargo.env(
482+
"RUSTDOCFLAGS",
483+
"--document-private-items \
484+
--enable-index-page -Zunstable-options",
485+
);
482486
compile::rustc_cargo(builder, &mut cargo, target);
483487

484488
// Only include compiler crates, no dependencies of those, such as `libc`.

src/libcore/cmp.rs

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ use self::Ordering::*;
191191
/// assert_eq!(x.eq(&y), false);
192192
/// ```
193193
///
194+
/// [`Eq`]: Eq
194195
/// [`eq`]: PartialEq::eq
195196
/// [`ne`]: PartialEq::ne
196197
#[lang = "eq"]

src/libcore/ptr/mod.rs

+23-4
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@
6565
//! [`write_volatile`]: ./fn.write_volatile.html
6666
//! [`NonNull::dangling`]: ./struct.NonNull.html#method.dangling
6767
68-
// ignore-tidy-undocumented-unsafe
69-
7068
#![stable(feature = "rust1", since = "1.0.0")]
7169

7270
use crate::cmp::Ordering;
@@ -248,14 +246,17 @@ pub(crate) struct FatPtr<T> {
248246
///
249247
/// // create a slice pointer when starting out with a pointer to the first element
250248
/// let x = [5, 6, 7];
251-
/// let ptr = x.as_ptr();
252-
/// let slice = ptr::slice_from_raw_parts(ptr, 3);
249+
/// let raw_pointer = x.as_ptr();
250+
/// let slice = ptr::slice_from_raw_parts(raw_pointer, 3);
253251
/// assert_eq!(unsafe { &*slice }[2], 7);
254252
/// ```
255253
#[inline]
256254
#[stable(feature = "slice_from_raw_parts", since = "1.42.0")]
257255
#[rustc_const_unstable(feature = "const_slice_from_raw_parts", issue = "67456")]
258256
pub const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
257+
// SAFETY: Accessing the value from the `Repr` union is safe since *const [T]
258+
// and FatPtr have the same memory layouts. Only std can make this
259+
// guarantee.
259260
unsafe { Repr { raw: FatPtr { data, len } }.rust }
260261
}
261262

@@ -269,10 +270,28 @@ pub const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
269270
///
270271
/// [`slice_from_raw_parts`]: fn.slice_from_raw_parts.html
271272
/// [`from_raw_parts_mut`]: ../../std/slice/fn.from_raw_parts_mut.html
273+
///
274+
/// # Examples
275+
///
276+
/// ```rust
277+
/// use std::ptr;
278+
///
279+
/// let x = &mut [5, 6, 7];
280+
/// let raw_pointer = x.as_mut_ptr();
281+
/// let slice = ptr::slice_from_raw_parts_mut(raw_pointer, 3);
282+
///
283+
/// unsafe {
284+
/// (*slice)[2] = 99; // assign a value at an index in the slice
285+
/// };
286+
///
287+
/// assert_eq!(unsafe { &*slice }[2], 99);
288+
/// ```
272289
#[inline]
273290
#[stable(feature = "slice_from_raw_parts", since = "1.42.0")]
274291
#[rustc_const_unstable(feature = "const_slice_from_raw_parts", issue = "67456")]
275292
pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
293+
// SAFETY: Accessing the value from the `Repr` union is safe since *mut [T]
294+
// and FatPtr have the same memory layouts
276295
unsafe { Repr { raw: FatPtr { data, len } }.rust_mut }
277296
}
278297

src/libcore/ptr/non_null.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use crate::mem;
77
use crate::ops::{CoerceUnsized, DispatchFromDyn};
88
use crate::ptr::Unique;
99

10-
// ignore-tidy-undocumented-unsafe
11-
1210
/// `*mut T` but non-zero and covariant.
1311
///
1412
/// This is often the correct thing to use when building data structures using
@@ -69,6 +67,9 @@ impl<T: Sized> NonNull<T> {
6967
#[rustc_const_stable(feature = "const_nonnull_dangling", since = "1.32.0")]
7068
#[inline]
7169
pub const fn dangling() -> Self {
70+
// SAFETY: mem::align_of() returns a non-zero usize which is then casted
71+
// to a *mut T. Therefore, `ptr` is not null and the conditions for
72+
// calling new_unchecked() are respected.
7273
unsafe {
7374
let ptr = mem::align_of::<T>() as *mut T;
7475
NonNull::new_unchecked(ptr)
@@ -93,7 +94,12 @@ impl<T: ?Sized> NonNull<T> {
9394
#[stable(feature = "nonnull", since = "1.25.0")]
9495
#[inline]
9596
pub fn new(ptr: *mut T) -> Option<Self> {
96-
if !ptr.is_null() { Some(unsafe { Self::new_unchecked(ptr) }) } else { None }
97+
if !ptr.is_null() {
98+
// SAFETY: The pointer is already checked and is not null
99+
Some(unsafe { Self::new_unchecked(ptr) })
100+
} else {
101+
None
102+
}
97103
}
98104

99105
/// Acquires the underlying `*mut` pointer.
@@ -131,6 +137,7 @@ impl<T: ?Sized> NonNull<T> {
131137
#[rustc_const_stable(feature = "const_nonnull_cast", since = "1.32.0")]
132138
#[inline]
133139
pub const fn cast<U>(self) -> NonNull<U> {
140+
// SAFETY: `self` is a `NonNull` pointer which is necessarily non-null
134141
unsafe { NonNull::new_unchecked(self.as_ptr() as *mut U) }
135142
}
136143
}
@@ -205,6 +212,8 @@ impl<T: ?Sized> hash::Hash for NonNull<T> {
205212
impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
206213
#[inline]
207214
fn from(unique: Unique<T>) -> Self {
215+
// SAFETY: A Unique pointer cannot be null, so the conditions for
216+
// new_unchecked() are respected.
208217
unsafe { NonNull::new_unchecked(unique.as_ptr()) }
209218
}
210219
}
@@ -213,6 +222,7 @@ impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
213222
impl<T: ?Sized> From<&mut T> for NonNull<T> {
214223
#[inline]
215224
fn from(reference: &mut T) -> Self {
225+
// SAFETY: A mutable reference cannot be null.
216226
unsafe { NonNull { pointer: reference as *mut T } }
217227
}
218228
}
@@ -221,6 +231,8 @@ impl<T: ?Sized> From<&mut T> for NonNull<T> {
221231
impl<T: ?Sized> From<&T> for NonNull<T> {
222232
#[inline]
223233
fn from(reference: &T) -> Self {
234+
// SAFETY: A reference cannot be null, so the conditions for
235+
// new_unchecked() are respected.
224236
unsafe { NonNull { pointer: reference as *const T } }
225237
}
226238
}

src/libcore/ptr/unique.rs

+7
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ impl<T: Sized> Unique<T> {
7373
// FIXME: rename to dangling() to match NonNull?
7474
#[inline]
7575
pub const fn empty() -> Self {
76+
// SAFETY: mem::align_of() returns a valid, non-null pointer. The
77+
// conditions to call new_unchecked() are thus respected.
7678
unsafe { Unique::new_unchecked(mem::align_of::<T>() as *mut T) }
7779
}
7880
}
@@ -93,6 +95,7 @@ impl<T: ?Sized> Unique<T> {
9395
#[inline]
9496
pub fn new(ptr: *mut T) -> Option<Self> {
9597
if !ptr.is_null() {
98+
// SAFETY: The pointer has already been checked and is not null.
9699
Some(unsafe { Unique { pointer: ptr as _, _marker: PhantomData } })
97100
} else {
98101
None
@@ -128,6 +131,9 @@ impl<T: ?Sized> Unique<T> {
128131
/// Casts to a pointer of another type.
129132
#[inline]
130133
pub const fn cast<U>(self) -> Unique<U> {
134+
// SAFETY: Unique::new_unchecked() creates a new unique and needs
135+
// the given pointer to not be null.
136+
// Since we are passing self as a pointer, it cannot be null.
131137
unsafe { Unique::new_unchecked(self.as_ptr() as *mut U) }
132138
}
133139
}
@@ -167,6 +173,7 @@ impl<T: ?Sized> fmt::Pointer for Unique<T> {
167173
impl<T: ?Sized> From<&mut T> for Unique<T> {
168174
#[inline]
169175
fn from(reference: &mut T) -> Self {
176+
// SAFETY: A mutable reference cannot be null
170177
unsafe { Unique { pointer: reference as *mut T, _marker: PhantomData } }
171178
}
172179
}

src/librustc_typeck/check/autoderef.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> {
114114

115115
let tcx = self.infcx.tcx;
116116

117-
// <cur_ty as Deref>
117+
// <ty as Deref>
118118
let trait_ref = TraitRef {
119119
def_id: tcx.lang_items().deref_trait()?,
120-
substs: tcx.mk_substs_trait(self.cur_ty, &[]),
120+
substs: tcx.mk_substs_trait(ty, &[]),
121121
};
122122

123123
let cause = traits::ObligationCause::misc(self.span, self.body_id);

src/librustc_typeck/check/demand.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
767767
suggest_to_change_suffix_or_into(err, is_fallible);
768768
true
769769
}
770-
(&ty::Int(_), &ty::Uint(_)) | (&ty::Uint(_), &ty::Int(_)) => {
770+
(&ty::Int(exp), &ty::Uint(found)) => {
771+
let is_fallible = match (exp.bit_width(), found.bit_width()) {
772+
(Some(exp), Some(found)) if found < exp => false,
773+
(None, Some(8)) => false,
774+
_ => true,
775+
};
776+
suggest_to_change_suffix_or_into(err, is_fallible);
777+
true
778+
}
779+
(&ty::Uint(_), &ty::Int(_)) => {
771780
suggest_to_change_suffix_or_into(err, true);
772781
true
773782
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// run-pass
2+
//! This snippet causes the type length to blowup exponentially,
3+
//! so check that we don't accidentially exceed the type length limit.
4+
// FIXME: Once the size of iterator adaptors is further reduced,
5+
// increase the complexity of this test.
6+
7+
fn main() {
8+
let c = 2;
9+
let bv = vec![2];
10+
let b = bv
11+
.iter()
12+
.filter(|a| **a == c);
13+
14+
let _a = vec![1, 2, 3]
15+
.into_iter()
16+
.filter(|a| b.clone().any(|b| *b == *a))
17+
.filter(|a| b.clone().any(|b| *b == *a))
18+
.filter(|a| b.clone().any(|b| *b == *a))
19+
.filter(|a| b.clone().any(|b| *b == *a))
20+
.filter(|a| b.clone().any(|b| *b == *a))
21+
.filter(|a| b.clone().any(|b| *b == *a))
22+
.filter(|a| b.clone().any(|b| *b == *a))
23+
.filter(|a| b.clone().any(|b| *b == *a))
24+
.filter(|a| b.clone().any(|b| *b == *a))
25+
.filter(|a| b.clone().any(|b| *b == *a))
26+
.filter(|a| b.clone().any(|b| *b == *a))
27+
.filter(|a| b.clone().any(|b| *b == *a))
28+
.filter(|a| b.clone().any(|b| *b == *a))
29+
.filter(|a| b.clone().any(|b| *b == *a))
30+
.collect::<Vec<_>>();
31+
}

src/test/ui/numeric/numeric-cast-2.stderr

+8-14
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,21 @@ error[E0308]: mismatched types
1515
--> $DIR/numeric-cast-2.rs:7:18
1616
|
1717
LL | let y: i64 = x + x;
18-
| --- ^^^^^ expected `i64`, found `u16`
19-
| |
18+
| --- ^^^^^
19+
| | |
20+
| | expected `i64`, found `u16`
21+
| | help: you can convert an `u16` to `i64`: `(x + x).into()`
2022
| expected due to this
21-
|
22-
help: you can convert an `u16` to `i64` and panic if the converted value wouldn't fit
23-
|
24-
LL | let y: i64 = (x + x).try_into().unwrap();
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2623

2724
error[E0308]: mismatched types
2825
--> $DIR/numeric-cast-2.rs:9:18
2926
|
3027
LL | let z: i32 = x + x;
31-
| --- ^^^^^ expected `i32`, found `u16`
32-
| |
28+
| --- ^^^^^
29+
| | |
30+
| | expected `i32`, found `u16`
31+
| | help: you can convert an `u16` to `i32`: `(x + x).into()`
3332
| expected due to this
34-
|
35-
help: you can convert an `u16` to `i32` and panic if the converted value wouldn't fit
36-
|
37-
LL | let z: i32 = (x + x).try_into().unwrap();
38-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
3933

4034
error: aborting due to 3 previous errors
4135

src/test/ui/numeric/numeric-cast.fixed

+7-7
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn main() {
4949
//~^ ERROR mismatched types
5050
foo::<isize>(x_u16.try_into().unwrap());
5151
//~^ ERROR mismatched types
52-
foo::<isize>(x_u8.try_into().unwrap());
52+
foo::<isize>(x_u8.into());
5353
//~^ ERROR mismatched types
5454
foo::<isize>(x_isize);
5555
foo::<isize>(x_i64.try_into().unwrap());
@@ -89,11 +89,11 @@ fn main() {
8989
//~^ ERROR mismatched types
9090
foo::<i64>(x_u64.try_into().unwrap());
9191
//~^ ERROR mismatched types
92-
foo::<i64>(x_u32.try_into().unwrap());
92+
foo::<i64>(x_u32.into());
9393
//~^ ERROR mismatched types
94-
foo::<i64>(x_u16.try_into().unwrap());
94+
foo::<i64>(x_u16.into());
9595
//~^ ERROR mismatched types
96-
foo::<i64>(x_u8.try_into().unwrap());
96+
foo::<i64>(x_u8.into());
9797
//~^ ERROR mismatched types
9898
foo::<i64>(x_isize.try_into().unwrap());
9999
//~^ ERROR mismatched types
@@ -135,9 +135,9 @@ fn main() {
135135
//~^ ERROR mismatched types
136136
foo::<i32>(x_u32.try_into().unwrap());
137137
//~^ ERROR mismatched types
138-
foo::<i32>(x_u16.try_into().unwrap());
138+
foo::<i32>(x_u16.into());
139139
//~^ ERROR mismatched types
140-
foo::<i32>(x_u8.try_into().unwrap());
140+
foo::<i32>(x_u8.into());
141141
//~^ ERROR mismatched types
142142
foo::<i32>(x_isize.try_into().unwrap());
143143
//~^ ERROR mismatched types
@@ -181,7 +181,7 @@ fn main() {
181181
//~^ ERROR mismatched types
182182
foo::<i16>(x_u16.try_into().unwrap());
183183
//~^ ERROR mismatched types
184-
foo::<i16>(x_u8.try_into().unwrap());
184+
foo::<i16>(x_u8.into());
185185
//~^ ERROR mismatched types
186186
foo::<i16>(x_isize.try_into().unwrap());
187187
//~^ ERROR mismatched types

0 commit comments

Comments
 (0)