From b4a11d4dd971be4a2e055e4f92fece030615a692 Mon Sep 17 00:00:00 2001 From: cohenarthur Date: Thu, 23 Apr 2020 22:22:27 +0200 Subject: [PATCH 1/9] doc-example: Refactor pointer name to avoid confusion Changed raw pointer name from ptr to raw_pointer to avoid confusion with the `use std::ptr` statement a few lines above. This way the crate name and pointer name are well differenciated. --- src/libcore/ptr/mod.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/libcore/ptr/mod.rs b/src/libcore/ptr/mod.rs index 84f28488c74b6..54d44222d3f57 100644 --- a/src/libcore/ptr/mod.rs +++ b/src/libcore/ptr/mod.rs @@ -65,8 +65,6 @@ //! [`write_volatile`]: ./fn.write_volatile.html //! [`NonNull::dangling`]: ./struct.NonNull.html#method.dangling -// ignore-tidy-undocumented-unsafe - #![stable(feature = "rust1", since = "1.0.0")] use crate::cmp::Ordering; @@ -248,8 +246,8 @@ pub(crate) struct FatPtr { /// /// // create a slice pointer when starting out with a pointer to the first element /// let x = [5, 6, 7]; -/// let ptr = x.as_ptr(); -/// let slice = ptr::slice_from_raw_parts(ptr, 3); +/// let raw_pointer = x.as_ptr(); +/// let slice = ptr::slice_from_raw_parts(raw_pointer, 3); /// assert_eq!(unsafe { &*slice }[2], 7); /// ``` #[inline] From 7d6aef65d80581b4fe726db7fcc592e87072a33d Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Sun, 26 Apr 2020 11:50:58 +0200 Subject: [PATCH 2/9] test iterator chain type length blowup --- .../issue-58952-filter-type-length.rs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/test/ui/iterators/issue-58952-filter-type-length.rs diff --git a/src/test/ui/iterators/issue-58952-filter-type-length.rs b/src/test/ui/iterators/issue-58952-filter-type-length.rs new file mode 100644 index 0000000000000..046e37840849e --- /dev/null +++ b/src/test/ui/iterators/issue-58952-filter-type-length.rs @@ -0,0 +1,31 @@ +// run-pass +//! This snippet causes the type length to blowup exponentially, +//! so check that we don't accidentially exceed the type length limit. +// FIXME: Once the size of iterator adaptors is further reduced, +// increase the complexity of this test. + +fn main() { + let c = 2; + let bv = vec![2]; + let b = bv + .iter() + .filter(|a| **a == c); + + let _a = vec![1, 2, 3] + .into_iter() + .filter(|a| b.clone().any(|b| *b == *a)) + .filter(|a| b.clone().any(|b| *b == *a)) + .filter(|a| b.clone().any(|b| *b == *a)) + .filter(|a| b.clone().any(|b| *b == *a)) + .filter(|a| b.clone().any(|b| *b == *a)) + .filter(|a| b.clone().any(|b| *b == *a)) + .filter(|a| b.clone().any(|b| *b == *a)) + .filter(|a| b.clone().any(|b| *b == *a)) + .filter(|a| b.clone().any(|b| *b == *a)) + .filter(|a| b.clone().any(|b| *b == *a)) + .filter(|a| b.clone().any(|b| *b == *a)) + .filter(|a| b.clone().any(|b| *b == *a)) + .filter(|a| b.clone().any(|b| *b == *a)) + .filter(|a| b.clone().any(|b| *b == *a)) + .collect::>(); +} From 1f0a864570d79807c7843ebb995fdcdd384ff3c9 Mon Sep 17 00:00:00 2001 From: Samrat Man Singh Date: Tue, 28 Apr 2020 00:13:37 +0530 Subject: [PATCH 3/9] Suggest `into` instead of `try_into` if possible with int types If it is possible to convert an integer type into another using `into`, don't suggest `try_into`. This commit changes the suggested method to convert from one integer type to another for the following cases: - u{n} -> i{m} where n < m - u8 -> isize - i{n} -> isize where n <= 16 - u{n} -> usize where n <= 16 --- src/librustc_typeck/check/demand.rs | 11 +++++- src/test/ui/suggestions/integer-into.rs | 17 ++++++++ src/test/ui/suggestions/integer-into.stderr | 43 +++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/suggestions/integer-into.rs create mode 100644 src/test/ui/suggestions/integer-into.stderr diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs index c75283e419a6d..980aefa710ff5 100644 --- a/src/librustc_typeck/check/demand.rs +++ b/src/librustc_typeck/check/demand.rs @@ -767,7 +767,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { suggest_to_change_suffix_or_into(err, is_fallible); true } - (&ty::Int(_), &ty::Uint(_)) | (&ty::Uint(_), &ty::Int(_)) => { + (&ty::Int(exp), &ty::Uint(found)) => { + let is_fallible = match (exp.bit_width(), found.bit_width()) { + (Some(exp), Some(found)) if found < exp => false, + (None, Some(found)) if found <= 16 => false, + _ => true + }; + suggest_to_change_suffix_or_into(err, is_fallible); + true + }, + (&ty::Uint(_), &ty::Int(_)) => { suggest_to_change_suffix_or_into(err, true); true } diff --git a/src/test/ui/suggestions/integer-into.rs b/src/test/ui/suggestions/integer-into.rs new file mode 100644 index 0000000000000..409b27a4eab31 --- /dev/null +++ b/src/test/ui/suggestions/integer-into.rs @@ -0,0 +1,17 @@ +fn main() { + let a = 1u8; + let _: i64 = a; + //~^ ERROR mismatched types + + let b = 1i8; + let _: isize = b; + //~^ ERROR mismatched types + + let c = 1u8; + let _: isize = c; + //~^ ERROR mismatched types + + let d = 1u8; + let _: usize = d; + //~^ ERROR mismatched types +} diff --git a/src/test/ui/suggestions/integer-into.stderr b/src/test/ui/suggestions/integer-into.stderr new file mode 100644 index 0000000000000..a15cf81f71fe7 --- /dev/null +++ b/src/test/ui/suggestions/integer-into.stderr @@ -0,0 +1,43 @@ +error[E0308]: mismatched types + --> $DIR/integer-into.rs:3:18 + | +LL | let _: i64 = a; + | --- ^ + | | | + | | expected `i64`, found `u8` + | | help: you can convert an `u8` to `i64`: `a.into()` + | expected due to this + +error[E0308]: mismatched types + --> $DIR/integer-into.rs:7:20 + | +LL | let _: isize = b; + | ----- ^ + | | | + | | expected `isize`, found `i8` + | | help: you can convert an `i8` to `isize`: `b.into()` + | expected due to this + +error[E0308]: mismatched types + --> $DIR/integer-into.rs:11:20 + | +LL | let _: isize = c; + | ----- ^ + | | | + | | expected `isize`, found `u8` + | | help: you can convert an `u8` to `isize`: `c.into()` + | expected due to this + +error[E0308]: mismatched types + --> $DIR/integer-into.rs:15:20 + | +LL | let _: usize = d; + | ----- ^ + | | | + | | expected `usize`, found `u8` + | | help: you can convert an `u8` to `usize`: `d.into()` + | expected due to this + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0308`. From 8d2f301283ff2dcf10221045d8345e3a38f6f99d Mon Sep 17 00:00:00 2001 From: Donough Liu Date: Tue, 28 Apr 2020 08:05:24 +0800 Subject: [PATCH 4/9] Fix wrong argument in autoderef process --- src/librustc_typeck/check/autoderef.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc_typeck/check/autoderef.rs b/src/librustc_typeck/check/autoderef.rs index 0dab172230ef1..2bb00553232ad 100644 --- a/src/librustc_typeck/check/autoderef.rs +++ b/src/librustc_typeck/check/autoderef.rs @@ -114,10 +114,10 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> { let tcx = self.infcx.tcx; - // + // let trait_ref = TraitRef { def_id: tcx.lang_items().deref_trait()?, - substs: tcx.mk_substs_trait(self.cur_ty, &[]), + substs: tcx.mk_substs_trait(ty, &[]), }; let cause = traits::ObligationCause::misc(self.span, self.body_id); From 57dd22baad83d3e5a8ec81a0bf64ad54464988bc Mon Sep 17 00:00:00 2001 From: Samrat Man Singh Date: Tue, 28 Apr 2020 21:27:14 +0530 Subject: [PATCH 5/9] Suggest `into` to convert into `isize` only if uint is of width 8 Since Into is not implemented for uint of width greater than 8 --- src/librustc_typeck/check/demand.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs index 980aefa710ff5..aa36bec6e1e88 100644 --- a/src/librustc_typeck/check/demand.rs +++ b/src/librustc_typeck/check/demand.rs @@ -770,12 +770,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { (&ty::Int(exp), &ty::Uint(found)) => { let is_fallible = match (exp.bit_width(), found.bit_width()) { (Some(exp), Some(found)) if found < exp => false, - (None, Some(found)) if found <= 16 => false, - _ => true + (None, Some(8)) => false, + _ => true, }; suggest_to_change_suffix_or_into(err, is_fallible); true - }, + } (&ty::Uint(_), &ty::Int(_)) => { suggest_to_change_suffix_or_into(err, true); true From a6033e33e7199646b269c0f3504591b108670fef Mon Sep 17 00:00:00 2001 From: Samrat Man Singh Date: Tue, 28 Apr 2020 21:32:36 +0530 Subject: [PATCH 6/9] Fix numeric-cast tests for new `into` suggestion Remove `integer-into.rs` since the numeric-cast tests already cover these cases. --- src/test/ui/numeric/numeric-cast-2.stderr | 22 +++---- src/test/ui/numeric/numeric-cast.fixed | 14 ++--- src/test/ui/numeric/numeric-cast.stderr | 70 +++++++++------------ src/test/ui/suggestions/integer-into.rs | 17 ----- src/test/ui/suggestions/integer-into.stderr | 43 ------------- 5 files changed, 43 insertions(+), 123 deletions(-) delete mode 100644 src/test/ui/suggestions/integer-into.rs delete mode 100644 src/test/ui/suggestions/integer-into.stderr diff --git a/src/test/ui/numeric/numeric-cast-2.stderr b/src/test/ui/numeric/numeric-cast-2.stderr index 465b507b788fd..3f900062cbb6e 100644 --- a/src/test/ui/numeric/numeric-cast-2.stderr +++ b/src/test/ui/numeric/numeric-cast-2.stderr @@ -15,27 +15,21 @@ error[E0308]: mismatched types --> $DIR/numeric-cast-2.rs:7:18 | LL | let y: i64 = x + x; - | --- ^^^^^ expected `i64`, found `u16` - | | + | --- ^^^^^ + | | | + | | expected `i64`, found `u16` + | | help: you can convert an `u16` to `i64`: `(x + x).into()` | expected due to this - | -help: you can convert an `u16` to `i64` and panic if the converted value wouldn't fit - | -LL | let y: i64 = (x + x).try_into().unwrap(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0308]: mismatched types --> $DIR/numeric-cast-2.rs:9:18 | LL | let z: i32 = x + x; - | --- ^^^^^ expected `i32`, found `u16` - | | + | --- ^^^^^ + | | | + | | expected `i32`, found `u16` + | | help: you can convert an `u16` to `i32`: `(x + x).into()` | expected due to this - | -help: you can convert an `u16` to `i32` and panic if the converted value wouldn't fit - | -LL | let z: i32 = (x + x).try_into().unwrap(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: aborting due to 3 previous errors diff --git a/src/test/ui/numeric/numeric-cast.fixed b/src/test/ui/numeric/numeric-cast.fixed index 31acdb8faf6a2..cf0560a107772 100644 --- a/src/test/ui/numeric/numeric-cast.fixed +++ b/src/test/ui/numeric/numeric-cast.fixed @@ -49,7 +49,7 @@ fn main() { //~^ ERROR mismatched types foo::(x_u16.try_into().unwrap()); //~^ ERROR mismatched types - foo::(x_u8.try_into().unwrap()); + foo::(x_u8.into()); //~^ ERROR mismatched types foo::(x_isize); foo::(x_i64.try_into().unwrap()); @@ -89,11 +89,11 @@ fn main() { //~^ ERROR mismatched types foo::(x_u64.try_into().unwrap()); //~^ ERROR mismatched types - foo::(x_u32.try_into().unwrap()); + foo::(x_u32.into()); //~^ ERROR mismatched types - foo::(x_u16.try_into().unwrap()); + foo::(x_u16.into()); //~^ ERROR mismatched types - foo::(x_u8.try_into().unwrap()); + foo::(x_u8.into()); //~^ ERROR mismatched types foo::(x_isize.try_into().unwrap()); //~^ ERROR mismatched types @@ -135,9 +135,9 @@ fn main() { //~^ ERROR mismatched types foo::(x_u32.try_into().unwrap()); //~^ ERROR mismatched types - foo::(x_u16.try_into().unwrap()); + foo::(x_u16.into()); //~^ ERROR mismatched types - foo::(x_u8.try_into().unwrap()); + foo::(x_u8.into()); //~^ ERROR mismatched types foo::(x_isize.try_into().unwrap()); //~^ ERROR mismatched types @@ -181,7 +181,7 @@ fn main() { //~^ ERROR mismatched types foo::(x_u16.try_into().unwrap()); //~^ ERROR mismatched types - foo::(x_u8.try_into().unwrap()); + foo::(x_u8.into()); //~^ ERROR mismatched types foo::(x_isize.try_into().unwrap()); //~^ ERROR mismatched types diff --git a/src/test/ui/numeric/numeric-cast.stderr b/src/test/ui/numeric/numeric-cast.stderr index ff92a86c3a7b4..cc1aa72d21451 100644 --- a/src/test/ui/numeric/numeric-cast.stderr +++ b/src/test/ui/numeric/numeric-cast.stderr @@ -141,12 +141,10 @@ error[E0308]: mismatched types --> $DIR/numeric-cast.rs:52:18 | LL | foo::(x_u8); - | ^^^^ expected `isize`, found `u8` - | -help: you can convert an `u8` to `isize` and panic if the converted value wouldn't fit - | -LL | foo::(x_u8.try_into().unwrap()); - | ^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^ + | | + | expected `isize`, found `u8` + | help: you can convert an `u8` to `isize`: `x_u8.into()` error[E0308]: mismatched types --> $DIR/numeric-cast.rs:55:18 @@ -307,34 +305,28 @@ error[E0308]: mismatched types --> $DIR/numeric-cast.rs:92:16 | LL | foo::(x_u32); - | ^^^^^ expected `i64`, found `u32` - | -help: you can convert an `u32` to `i64` and panic if the converted value wouldn't fit - | -LL | foo::(x_u32.try_into().unwrap()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^ + | | + | expected `i64`, found `u32` + | help: you can convert an `u32` to `i64`: `x_u32.into()` error[E0308]: mismatched types --> $DIR/numeric-cast.rs:94:16 | LL | foo::(x_u16); - | ^^^^^ expected `i64`, found `u16` - | -help: you can convert an `u16` to `i64` and panic if the converted value wouldn't fit - | -LL | foo::(x_u16.try_into().unwrap()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^ + | | + | expected `i64`, found `u16` + | help: you can convert an `u16` to `i64`: `x_u16.into()` error[E0308]: mismatched types --> $DIR/numeric-cast.rs:96:16 | LL | foo::(x_u8); - | ^^^^ expected `i64`, found `u8` - | -help: you can convert an `u8` to `i64` and panic if the converted value wouldn't fit - | -LL | foo::(x_u8.try_into().unwrap()); - | ^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^ + | | + | expected `i64`, found `u8` + | help: you can convert an `u8` to `i64`: `x_u8.into()` error[E0308]: mismatched types --> $DIR/numeric-cast.rs:98:16 @@ -506,23 +498,19 @@ error[E0308]: mismatched types --> $DIR/numeric-cast.rs:138:16 | LL | foo::(x_u16); - | ^^^^^ expected `i32`, found `u16` - | -help: you can convert an `u16` to `i32` and panic if the converted value wouldn't fit - | -LL | foo::(x_u16.try_into().unwrap()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^ + | | + | expected `i32`, found `u16` + | help: you can convert an `u16` to `i32`: `x_u16.into()` error[E0308]: mismatched types --> $DIR/numeric-cast.rs:140:16 | LL | foo::(x_u8); - | ^^^^ expected `i32`, found `u8` - | -help: you can convert an `u8` to `i32` and panic if the converted value wouldn't fit - | -LL | foo::(x_u8.try_into().unwrap()); - | ^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^ + | | + | expected `i32`, found `u8` + | help: you can convert an `u8` to `i32`: `x_u8.into()` error[E0308]: mismatched types --> $DIR/numeric-cast.rs:142:16 @@ -709,12 +697,10 @@ error[E0308]: mismatched types --> $DIR/numeric-cast.rs:184:16 | LL | foo::(x_u8); - | ^^^^ expected `i16`, found `u8` - | -help: you can convert an `u8` to `i16` and panic if the converted value wouldn't fit - | -LL | foo::(x_u8.try_into().unwrap()); - | ^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^ + | | + | expected `i16`, found `u8` + | help: you can convert an `u8` to `i16`: `x_u8.into()` error[E0308]: mismatched types --> $DIR/numeric-cast.rs:186:16 diff --git a/src/test/ui/suggestions/integer-into.rs b/src/test/ui/suggestions/integer-into.rs deleted file mode 100644 index 409b27a4eab31..0000000000000 --- a/src/test/ui/suggestions/integer-into.rs +++ /dev/null @@ -1,17 +0,0 @@ -fn main() { - let a = 1u8; - let _: i64 = a; - //~^ ERROR mismatched types - - let b = 1i8; - let _: isize = b; - //~^ ERROR mismatched types - - let c = 1u8; - let _: isize = c; - //~^ ERROR mismatched types - - let d = 1u8; - let _: usize = d; - //~^ ERROR mismatched types -} diff --git a/src/test/ui/suggestions/integer-into.stderr b/src/test/ui/suggestions/integer-into.stderr deleted file mode 100644 index a15cf81f71fe7..0000000000000 --- a/src/test/ui/suggestions/integer-into.stderr +++ /dev/null @@ -1,43 +0,0 @@ -error[E0308]: mismatched types - --> $DIR/integer-into.rs:3:18 - | -LL | let _: i64 = a; - | --- ^ - | | | - | | expected `i64`, found `u8` - | | help: you can convert an `u8` to `i64`: `a.into()` - | expected due to this - -error[E0308]: mismatched types - --> $DIR/integer-into.rs:7:20 - | -LL | let _: isize = b; - | ----- ^ - | | | - | | expected `isize`, found `i8` - | | help: you can convert an `i8` to `isize`: `b.into()` - | expected due to this - -error[E0308]: mismatched types - --> $DIR/integer-into.rs:11:20 - | -LL | let _: isize = c; - | ----- ^ - | | | - | | expected `isize`, found `u8` - | | help: you can convert an `u8` to `isize`: `c.into()` - | expected due to this - -error[E0308]: mismatched types - --> $DIR/integer-into.rs:15:20 - | -LL | let _: usize = d; - | ----- ^ - | | | - | | expected `usize`, found `u8` - | | help: you can convert an `u8` to `usize`: `d.into()` - | expected due to this - -error: aborting due to 4 previous errors - -For more information about this error, try `rustc --explain E0308`. From 8558ccd5c4d3d7ca78aaff4a81c48f7a04b0f1f2 Mon Sep 17 00:00:00 2001 From: cohenarthur Date: Thu, 23 Apr 2020 22:32:20 +0200 Subject: [PATCH 7/9] safety-ptr: Add SAFETY on some unsafe blocks from libcore/ptr Add documentation example to slice_from_raw_parts_mut() Add SAFETY explanations to some unsafe blocks in libcore/ptr * libcore/ptr/mod.rs * libcore/ptr/unique.rs * libcore/ptr/non_null.rs safety-mod.rs: Add SAFETY to slice_from_raw_parts(), slice_from_raw_parts_mut() slice_from_raw_parts_mut: Add documentation example safety-ptr-unique.rs: Add SAFETY to new() and cast() safety-ptr-non_null.rs: Add SAFETY to new() safety-ptr-non_null.rs: Add SAFETY to cast() safety-ptr-non_null.rs: Add SAFETY to from() impls safety-ptr-unique.rs: Add SAFETY to from() impls safety-ptr-non_null.rs: Add SAFETY to new() safety-ptr-unique.rs: Add SAFETY to new() safety-ptr-mod.rs: Fix safety explanation https://github.com/rust-lang/rust/pull/71507#discussion_r414488884 safety-prt-non_null.rs: Fix SAFETY comment syntax safety-ptr-unique.rs: Fix syntax for empty() safety-ptr-non_null.rs: Fix misuse of non-null for align_of() safety-ptr-non_null.rs: Remove incorrect SAFETY comment safety-ptr-unique.rs: Remove unsound SAFETY comment safety-ptr-mod.rs: Add std comment on slice_from_raw_parts guarantee safety-ptr-unique.rs: Remove incorrect safety comment Creating a Unique from a NonNull has strict guarantees that the current implementation does not guarantee https://github.com/rust-lang/rust/pull/71507#discussion_r415035952 safety-ptr: Re-adding ignore-tidy directive --- src/libcore/ptr/mod.rs | 21 +++++++++++++++++++++ src/libcore/ptr/non_null.rs | 18 +++++++++++++++--- src/libcore/ptr/unique.rs | 7 +++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/libcore/ptr/mod.rs b/src/libcore/ptr/mod.rs index 54d44222d3f57..58f779106f716 100644 --- a/src/libcore/ptr/mod.rs +++ b/src/libcore/ptr/mod.rs @@ -254,6 +254,9 @@ pub(crate) struct FatPtr { #[stable(feature = "slice_from_raw_parts", since = "1.42.0")] #[rustc_const_unstable(feature = "const_slice_from_raw_parts", issue = "67456")] pub const fn slice_from_raw_parts(data: *const T, len: usize) -> *const [T] { + // SAFETY: Accessing the value from the `Repr` union is safe since *const [T] + // and FatPtr have the same memory layouts. Only std can make this + // guarantee. unsafe { Repr { raw: FatPtr { data, len } }.rust } } @@ -267,10 +270,28 @@ pub const fn slice_from_raw_parts(data: *const T, len: usize) -> *const [T] { /// /// [`slice_from_raw_parts`]: fn.slice_from_raw_parts.html /// [`from_raw_parts_mut`]: ../../std/slice/fn.from_raw_parts_mut.html +/// +/// # Examples +/// +/// ```rust +/// use std::ptr; +/// +/// let x = &mut [5, 6, 7]; +/// let raw_pointer = x.as_mut_ptr(); +/// let slice = ptr::slice_from_raw_parts_mut(raw_pointer, 3); +/// +/// unsafe { +/// (*slice)[2] = 99; // assign a value at an index in the slice +/// }; +/// +/// assert_eq!(unsafe { &*slice }[2], 99); +/// ``` #[inline] #[stable(feature = "slice_from_raw_parts", since = "1.42.0")] #[rustc_const_unstable(feature = "const_slice_from_raw_parts", issue = "67456")] pub const fn slice_from_raw_parts_mut(data: *mut T, len: usize) -> *mut [T] { + // SAFETY: Accessing the value from the `Repr` union is safe since *mut [T] + // and FatPtr have the same memory layouts unsafe { Repr { raw: FatPtr { data, len } }.rust_mut } } diff --git a/src/libcore/ptr/non_null.rs b/src/libcore/ptr/non_null.rs index 626e58d49306e..7d08503215ed0 100644 --- a/src/libcore/ptr/non_null.rs +++ b/src/libcore/ptr/non_null.rs @@ -7,8 +7,6 @@ use crate::mem; use crate::ops::{CoerceUnsized, DispatchFromDyn}; use crate::ptr::Unique; -// ignore-tidy-undocumented-unsafe - /// `*mut T` but non-zero and covariant. /// /// This is often the correct thing to use when building data structures using @@ -69,6 +67,9 @@ impl NonNull { #[rustc_const_stable(feature = "const_nonnull_dangling", since = "1.32.0")] #[inline] pub const fn dangling() -> Self { + // SAFETY: mem::align_of() returns a non-zero usize which is then casted + // to a *mut T. Therefore, `ptr` is not null and the conditions for + // calling new_unchecked() are respected. unsafe { let ptr = mem::align_of::() as *mut T; NonNull::new_unchecked(ptr) @@ -93,7 +94,12 @@ impl NonNull { #[stable(feature = "nonnull", since = "1.25.0")] #[inline] pub fn new(ptr: *mut T) -> Option { - if !ptr.is_null() { Some(unsafe { Self::new_unchecked(ptr) }) } else { None } + if !ptr.is_null() { + // SAFETY: The pointer is already checked and is not null + Some(unsafe { Self::new_unchecked(ptr) }) + } else { + None + } } /// Acquires the underlying `*mut` pointer. @@ -131,6 +137,7 @@ impl NonNull { #[rustc_const_stable(feature = "const_nonnull_cast", since = "1.32.0")] #[inline] pub const fn cast(self) -> NonNull { + // SAFETY: `self` is a `NonNull` pointer which is necessarily non-null unsafe { NonNull::new_unchecked(self.as_ptr() as *mut U) } } } @@ -205,6 +212,8 @@ impl hash::Hash for NonNull { impl From> for NonNull { #[inline] fn from(unique: Unique) -> Self { + // SAFETY: A Unique pointer cannot be null, so the conditions for + // new_unchecked() are respected. unsafe { NonNull::new_unchecked(unique.as_ptr()) } } } @@ -213,6 +222,7 @@ impl From> for NonNull { impl From<&mut T> for NonNull { #[inline] fn from(reference: &mut T) -> Self { + // SAFETY: A mutable reference cannot be null. unsafe { NonNull { pointer: reference as *mut T } } } } @@ -221,6 +231,8 @@ impl From<&mut T> for NonNull { impl From<&T> for NonNull { #[inline] fn from(reference: &T) -> Self { + // SAFETY: A reference cannot be null, so the conditions for + // new_unchecked() are respected. unsafe { NonNull { pointer: reference as *const T } } } } diff --git a/src/libcore/ptr/unique.rs b/src/libcore/ptr/unique.rs index 87b56d951c6ce..7d823f91fecb8 100644 --- a/src/libcore/ptr/unique.rs +++ b/src/libcore/ptr/unique.rs @@ -74,6 +74,8 @@ impl Unique { // FIXME: rename to dangling() to match NonNull? #[inline] pub const fn empty() -> Self { + // SAFETY: mem::align_of() returns a valid, non-null pointer. The + // conditions to call new_unchecked() are thus respected. unsafe { Unique::new_unchecked(mem::align_of::() as *mut T) } } } @@ -94,6 +96,7 @@ impl Unique { #[inline] pub fn new(ptr: *mut T) -> Option { if !ptr.is_null() { + // SAFETY: The pointer has already been checked and is not null. Some(unsafe { Unique { pointer: ptr as _, _marker: PhantomData } }) } else { None @@ -129,6 +132,9 @@ impl Unique { /// Casts to a pointer of another type. #[inline] pub const fn cast(self) -> Unique { + // SAFETY: Unique::new_unchecked() creates a new unique and needs + // the given pointer to not be null. + // Since we are passing self as a pointer, it cannot be null. unsafe { Unique::new_unchecked(self.as_ptr() as *mut U) } } } @@ -168,6 +174,7 @@ impl fmt::Pointer for Unique { impl From<&mut T> for Unique { #[inline] fn from(reference: &mut T) -> Self { + // SAFETY: A mutable reference cannot be null unsafe { Unique { pointer: reference as *mut T, _marker: PhantomData } } } } From d6336dfe01744209b1aa0987c0a7a6c1d79a8ff4 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 29 Apr 2020 08:16:39 -0700 Subject: [PATCH 8/9] Add an index page for nightly rustc docs. --- src/bootstrap/doc.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/bootstrap/doc.rs b/src/bootstrap/doc.rs index fc217a707db94..7eab92ddc92a9 100644 --- a/src/bootstrap/doc.rs +++ b/src/bootstrap/doc.rs @@ -478,7 +478,11 @@ impl Step for Rustc { // Build cargo command. let mut cargo = builder.cargo(compiler, Mode::Rustc, target, "doc"); - cargo.env("RUSTDOCFLAGS", "--document-private-items"); + cargo.env( + "RUSTDOCFLAGS", + "--document-private-items \ + --enable-index-page -Zunstable-options", + ); compile::rustc_cargo(builder, &mut cargo, target); // Only include compiler crates, no dependencies of those, such as `libc`. From f408a4e9bdfae6334a26f169d4502156cf469056 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Wed, 29 Apr 2020 12:06:32 -0400 Subject: [PATCH 9/9] Fix doc link to Eq trait from PartialEq trait The `Eq` link was incorrectly going to the `eq` method of `PartialEq` instead of to the `Eq` trait. --- src/libcore/cmp.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 335969b3ef04e..9856efc6bd8a4 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -191,6 +191,7 @@ use self::Ordering::*; /// assert_eq!(x.eq(&y), false); /// ``` /// +/// [`Eq`]: Eq /// [`eq`]: PartialEq::eq /// [`ne`]: PartialEq::ne #[lang = "eq"]