Skip to content

Commit dcf1077

Browse files
committed
Auto merge of #75308 - JohnTitor:rollup-vnnny43, r=JohnTitor
Rollup of 15 pull requests Successful merges: - #74712 (Update E0271 explanation) - #74842 (adjust remaining targets) - #75151 (Consistent variable name alloc for raw_vec) - #75162 (Fix the documentation for move about Fn traits implementations) - #75248 (Add `as_mut_ptr` to `NonNull<[T]>`) - #75262 (Show multi extension example for Path in doctests) - #75266 (Add safety section to `NonNull::as_*` method docs) - #75284 (Show relative example for Path ancestors) - #75285 (Separate example for Path strip_prefix) - #75287 (Show Path extension example change multi extension) - #75288 (Use assert! for Path exists example to check bool) - #75289 (Remove ambiguity from PathBuf pop example) - #75290 (fix `min_const_generics` version) - #75291 (Clean up E0750) - #75292 (Clean up E0502) Failed merges: r? @ghost
2 parents ceedf1d + e6dfd30 commit dcf1077

File tree

11 files changed

+134
-58
lines changed

11 files changed

+134
-58
lines changed

library/alloc/src/raw_vec.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,15 @@ impl<T, A: AllocRef> RawVec<T, A> {
203203
///
204204
/// # Safety
205205
///
206-
/// The `ptr` must be allocated (via the given allocator `a`), and with the given `capacity`.
206+
/// The `ptr` must be allocated (via the given allocator `alloc`), and with the given
207+
/// `capacity`.
207208
/// The `capacity` cannot exceed `isize::MAX` for sized types. (only a concern on 32-bit
208209
/// systems). ZST vectors may have a capacity up to `usize::MAX`.
209-
/// If the `ptr` and `capacity` come from a `RawVec` created via `a`, then this is guaranteed.
210+
/// If the `ptr` and `capacity` come from a `RawVec` created via `alloc`, then this is
211+
/// guaranteed.
210212
#[inline]
211-
pub unsafe fn from_raw_parts_in(ptr: *mut T, capacity: usize, a: A) -> Self {
212-
Self { ptr: unsafe { Unique::new_unchecked(ptr) }, cap: capacity, alloc: a }
213+
pub unsafe fn from_raw_parts_in(ptr: *mut T, capacity: usize, alloc: A) -> Self {
214+
Self { ptr: unsafe { Unique::new_unchecked(ptr) }, cap: capacity, alloc }
213215
}
214216

215217
/// Gets a raw pointer to the start of the allocation. Note that this is

library/core/src/ptr/non_null.rs

+54
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,24 @@ impl<T: ?Sized> NonNull<T> {
117117
/// The resulting lifetime is bound to self so this behaves "as if"
118118
/// it were actually an instance of T that is getting borrowed. If a longer
119119
/// (unbound) lifetime is needed, use `&*my_ptr.as_ptr()`.
120+
///
121+
/// # Safety
122+
///
123+
/// When calling this method, you have to ensure that all of the following is true:
124+
/// - `self` is properly aligned
125+
/// - `self` must point to an initialized instance of T; in particular, the pointer must be
126+
/// "dereferencable" in the sense defined [here].
127+
///
128+
/// This applies even if the result of this method is unused!
129+
/// (The part about being initialized is not yet fully decided, but until
130+
/// it is, the only safe approach is to ensure that they are indeed initialized.)
131+
///
132+
/// Additionally, the lifetime of `self` does not necessarily reflect the actual
133+
/// lifetime of the data. *You* must enforce Rust's aliasing rules. In particular,
134+
/// for the duration of this lifetime, the memory the pointer points to must not
135+
/// get mutated (except inside `UnsafeCell`).
136+
///
137+
/// [here]: crate::ptr#safety
120138
#[stable(feature = "nonnull", since = "1.25.0")]
121139
#[inline]
122140
pub unsafe fn as_ref(&self) -> &T {
@@ -130,6 +148,24 @@ impl<T: ?Sized> NonNull<T> {
130148
/// The resulting lifetime is bound to self so this behaves "as if"
131149
/// it were actually an instance of T that is getting borrowed. If a longer
132150
/// (unbound) lifetime is needed, use `&mut *my_ptr.as_ptr()`.
151+
///
152+
/// # Safety
153+
///
154+
/// When calling this method, you have to ensure that all of the following is true:
155+
/// - `self` is properly aligned
156+
/// - `self` must point to an initialized instance of T; in particular, the pointer must be
157+
/// "dereferenceable" in the sense defined [here].
158+
///
159+
/// This applies even if the result of this method is unused!
160+
/// (The part about being initialized is not yet fully decided, but until
161+
/// it is the only safe approach is to ensure that they are indeed initialized.)
162+
///
163+
/// Additionally, the lifetime of `self` does not necessarily reflect the actual
164+
/// lifetime of the data. *You* must enforce Rust's aliasing rules. In particular,
165+
/// for the duration of this lifetime, the memory this pointer points to must not
166+
/// get accessed (read or written) through any other pointer.
167+
///
168+
/// [here]: crate::ptr#safety
133169
#[stable(feature = "nonnull", since = "1.25.0")]
134170
#[inline]
135171
pub unsafe fn as_mut(&mut self) -> &mut T {
@@ -224,6 +260,24 @@ impl<T> NonNull<[T]> {
224260
unsafe { NonNull::new_unchecked(self.as_ptr().as_mut_ptr()) }
225261
}
226262

263+
/// Returns a raw pointer to the slice's buffer.
264+
///
265+
/// # Examples
266+
///
267+
/// ```rust
268+
/// #![feature(slice_ptr_get, nonnull_slice_from_raw_parts)]
269+
/// use std::ptr::NonNull;
270+
///
271+
/// let slice: NonNull<[i8]> = NonNull::slice_from_raw_parts(NonNull::dangling(), 3);
272+
/// assert_eq!(slice.as_mut_ptr(), 1 as *mut i8);
273+
/// ```
274+
#[inline]
275+
#[unstable(feature = "slice_ptr_get", issue = "74265")]
276+
#[rustc_const_unstable(feature = "slice_ptr_get", issue = "74265")]
277+
pub const fn as_mut_ptr(self) -> *mut T {
278+
self.as_non_null_ptr().as_ptr()
279+
}
280+
227281
/// Returns a raw pointer to an element or subslice, without doing bounds
228282
/// checking.
229283
///

library/std/src/keyword_docs.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -943,8 +943,7 @@ mod mod_keyword {}
943943
/// Capture a [closure]'s environment by value.
944944
///
945945
/// `move` converts any variables captured by reference or mutable reference
946-
/// to owned by value variables. The three [`Fn` trait]'s mirror the ways to capture
947-
/// variables, when `move` is used, the closures is represented by the `FnOnce` trait.
946+
/// to owned by value variables.
948947
///
949948
/// ```rust
950949
/// let capture = "hello";
@@ -953,6 +952,23 @@ mod mod_keyword {}
953952
/// };
954953
/// ```
955954
///
955+
/// Note: `move` closures may still implement [`Fn`] or [`FnMut`], even though
956+
/// they capture variables by `move`. This is because the traits implemented by
957+
/// a closure type are determined by *what* the closure does with captured
958+
/// values, not *how* it captures them:
959+
///
960+
/// ```rust
961+
/// fn create_fn() -> impl Fn() {
962+
/// let text = "Fn".to_owned();
963+
///
964+
/// move || println!("This is a: {}", text)
965+
/// }
966+
///
967+
/// let fn_plain = create_fn();
968+
///
969+
/// fn_plain();
970+
/// ```
971+
///
956972
/// `move` is often used when [threads] are involved.
957973
///
958974
/// ```rust

library/std/src/path.rs

+19-11
Original file line numberDiff line numberDiff line change
@@ -1232,10 +1232,10 @@ impl PathBuf {
12321232
/// ```
12331233
/// use std::path::{Path, PathBuf};
12341234
///
1235-
/// let mut p = PathBuf::from("/test/test.rs");
1235+
/// let mut p = PathBuf::from("/spirited/away.rs");
12361236
///
12371237
/// p.pop();
1238-
/// assert_eq!(Path::new("/test"), p);
1238+
/// assert_eq!(Path::new("/spirited"), p);
12391239
/// p.pop();
12401240
/// assert_eq!(Path::new("/"), p);
12411241
/// ```
@@ -1992,6 +1992,13 @@ impl Path {
19921992
/// assert_eq!(ancestors.next(), Some(Path::new("/foo")));
19931993
/// assert_eq!(ancestors.next(), Some(Path::new("/")));
19941994
/// assert_eq!(ancestors.next(), None);
1995+
///
1996+
/// let mut ancestors = Path::new("../foo/bar").ancestors();
1997+
/// assert_eq!(ancestors.next(), Some(Path::new("../foo/bar")));
1998+
/// assert_eq!(ancestors.next(), Some(Path::new("../foo")));
1999+
/// assert_eq!(ancestors.next(), Some(Path::new("..")));
2000+
/// assert_eq!(ancestors.next(), Some(Path::new("")));
2001+
/// assert_eq!(ancestors.next(), None);
19952002
/// ```
19962003
///
19972004
/// [`None`]: ../../std/option/enum.Option.html#variant.None
@@ -2053,8 +2060,9 @@ impl Path {
20532060
/// assert_eq!(path.strip_prefix("/test/"), Ok(Path::new("haha/foo.txt")));
20542061
/// assert_eq!(path.strip_prefix("/test/haha/foo.txt"), Ok(Path::new("")));
20552062
/// assert_eq!(path.strip_prefix("/test/haha/foo.txt/"), Ok(Path::new("")));
2056-
/// assert_eq!(path.strip_prefix("test").is_ok(), false);
2057-
/// assert_eq!(path.strip_prefix("/haha").is_ok(), false);
2063+
///
2064+
/// assert!(path.strip_prefix("test").is_err());
2065+
/// assert!(path.strip_prefix("/haha").is_err());
20582066
///
20592067
/// let prefix = PathBuf::from("/test/");
20602068
/// assert_eq!(path.strip_prefix(prefix), Ok(Path::new("haha/foo.txt")));
@@ -2140,9 +2148,8 @@ impl Path {
21402148
/// ```
21412149
/// use std::path::Path;
21422150
///
2143-
/// let path = Path::new("foo.rs");
2144-
///
2145-
/// assert_eq!("foo", path.file_stem().unwrap());
2151+
/// assert_eq!("foo", Path::new("foo.rs").file_stem().unwrap());
2152+
/// assert_eq!("foo.tar", Path::new("foo.tar.gz").file_stem().unwrap());
21462153
/// ```
21472154
#[stable(feature = "rust1", since = "1.0.0")]
21482155
pub fn file_stem(&self) -> Option<&OsStr> {
@@ -2166,9 +2173,8 @@ impl Path {
21662173
/// ```
21672174
/// use std::path::Path;
21682175
///
2169-
/// let path = Path::new("foo.rs");
2170-
///
2171-
/// assert_eq!("rs", path.extension().unwrap());
2176+
/// assert_eq!("rs", Path::new("foo.rs").extension().unwrap());
2177+
/// assert_eq!("gz", Path::new("foo.tar.gz").extension().unwrap());
21722178
/// ```
21732179
#[stable(feature = "rust1", since = "1.0.0")]
21742180
pub fn extension(&self) -> Option<&OsStr> {
@@ -2247,6 +2253,8 @@ impl Path {
22472253
///
22482254
/// let path = Path::new("foo.tar.gz");
22492255
/// assert_eq!(path.with_extension(""), PathBuf::from("foo.tar"));
2256+
/// assert_eq!(path.with_extension("xz"), PathBuf::from("foo.tar.xz"));
2257+
/// assert_eq!(path.with_extension("").with_extension("txt"), PathBuf::from("foo.txt"));
22502258
/// ```
22512259
#[stable(feature = "rust1", since = "1.0.0")]
22522260
pub fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
@@ -2473,7 +2481,7 @@ impl Path {
24732481
///
24742482
/// ```no_run
24752483
/// use std::path::Path;
2476-
/// assert_eq!(Path::new("does_not_exist.txt").exists(), false);
2484+
/// assert!(!Path::new("does_not_exist.txt").exists());
24772485
/// ```
24782486
///
24792487
/// # See Also

library/std/src/sys/hermit/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub unsafe extern "C" fn runtime_entry(
106106
argv: *const *const c_char,
107107
env: *const *const c_char,
108108
) -> ! {
109-
use crate::sys::hermit::fast_thread_local::run_dtors;
109+
use crate::sys::hermit::thread_local_dtor::run_dtors;
110110
extern "C" {
111111
fn main(argc: isize, argv: *const *const c_char) -> i32;
112112
}

library/std/src/sys/hermit/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::ffi::CStr;
44
use crate::io;
55
use crate::mem;
66
use crate::sys::hermit::abi;
7-
use crate::sys::hermit::fast_thread_local::run_dtors;
7+
use crate::sys::hermit::thread_local_dtor::run_dtors;
88
use crate::time::Duration;
99

1010
pub type Tid = abi::Tid;

src/librustc_error_codes/error_codes/E0271.md

+13-30
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,6 @@ Erroneous code example:
55
```compile_fail,E0271
66
trait Trait { type AssociatedType; }
77
8-
fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
9-
println!("in foo");
10-
}
11-
12-
impl Trait for i8 { type AssociatedType = &'static str; }
13-
14-
foo(3_i8);
15-
```
16-
17-
This is because of a type mismatch between the associated type of some
18-
trait (e.g., `T::Bar`, where `T` implements `trait Quux { type Bar; }`)
19-
and another type `U` that is required to be equal to `T::Bar`, but is not.
20-
Examples follow.
21-
22-
Here is that same example again, with some explanatory comments:
23-
24-
```compile_fail,E0271
25-
trait Trait { type AssociatedType; }
26-
278
fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
289
// ~~~~~~~~ ~~~~~~~~~~~~~~~~~~
2910
// | |
@@ -56,11 +37,9 @@ foo(3_i8);
5637
// therefore the type-checker complains with this error code.
5738
```
5839

59-
To avoid those issues, you have to make the types match correctly.
60-
So we can fix the previous examples like this:
61-
40+
The issue can be resolved by changing the associated type:
41+
1) in the `foo` implementation:
6242
```
63-
// Basic Example:
6443
trait Trait { type AssociatedType; }
6544
6645
fn foo<T>(t: T) where T: Trait<AssociatedType = &'static str> {
@@ -70,13 +49,17 @@ fn foo<T>(t: T) where T: Trait<AssociatedType = &'static str> {
7049
impl Trait for i8 { type AssociatedType = &'static str; }
7150
7251
foo(3_i8);
52+
```
7353

74-
// For-Loop Example:
75-
let vs = vec![1, 2, 3, 4];
76-
for v in &vs {
77-
match v {
78-
&1 => {}
79-
_ => {}
80-
}
54+
2) in the `Trait` implementation for `i8`:
55+
```
56+
trait Trait { type AssociatedType; }
57+
58+
fn foo<T>(t: T) where T: Trait<AssociatedType = u32> {
59+
println!("in foo");
8160
}
61+
62+
impl Trait for i8 { type AssociatedType = u32; }
63+
64+
foo(3_i8);
8265
```

src/librustc_error_codes/error_codes/E0502.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Erroneous code example:
55
```compile_fail,E0502
66
fn bar(x: &mut i32) {}
77
fn foo(a: &mut i32) {
8-
let ref y = a; // a is borrowed as immutable.
8+
let y = &a; // a is borrowed as immutable.
99
bar(a); // error: cannot borrow `*a` as mutable because `a` is also borrowed
1010
// as immutable
1111
println!("{}", y);
@@ -19,7 +19,7 @@ variable before trying to access it mutably:
1919
fn bar(x: &mut i32) {}
2020
fn foo(a: &mut i32) {
2121
bar(a);
22-
let ref y = a; // ok!
22+
let y = &a; // ok!
2323
println!("{}", y);
2424
}
2525
```
+18-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
Negative impls cannot be default impls. A default impl supplies
2-
default values for the items within to be used by other impls, whereas
3-
a negative impl declares that there are no other impls. These don't
4-
make sense to combine.
1+
A negative impl was made default impl.
2+
3+
Erroneous code example:
4+
5+
```compile_fail,E0750
6+
# #![feature(negative_impls)]
7+
# #![feature(specialization)]
8+
trait MyTrait {
9+
type Foo;
10+
}
11+
12+
default impl !MyTrait for u32 {} // error!
13+
# fn main() {}
14+
```
15+
16+
Negative impls cannot be default impls. A default impl supplies default values
17+
for the items within to be used by other impls, whereas a negative impl declares
18+
that there are no other impls. Combining it does not make sense.

src/librustc_feature/active.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ declare_features! (
580580
(active, const_fn_transmute, "1.46.0", Some(53605), None),
581581

582582
/// The smallest useful subset of `const_generics`.
583-
(active, min_const_generics, "1.46.0", Some(74878), None),
583+
(active, min_const_generics, "1.47.0", Some(74878), None),
584584

585585
// -------------------------------------------------------------------------
586586
// feature-group-end: actual feature gates

src/tools/tidy/src/error_codes_check.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ const EXEMPTED_FROM_TEST: &[&str] = &[
1616
];
1717

1818
// Some error codes don't have any tests apparently...
19-
const IGNORE_EXPLANATION_CHECK: &[&str] =
20-
&["E0570", "E0601", "E0602", "E0639", "E0729", "E0749", "E0750"];
19+
const IGNORE_EXPLANATION_CHECK: &[&str] = &["E0570", "E0601", "E0602", "E0639", "E0729", "E0749"];
2120

2221
fn check_error_code_explanation(
2322
f: &str,

0 commit comments

Comments
 (0)