Skip to content

Commit 93f5e21

Browse files
committed
Auto merge of rust-lang#130631 - GuillaumeGomez:rollup-jpgy1iv, r=GuillaumeGomez
Rollup of 7 pull requests Successful merges: - rust-lang#128209 (Remove macOS 10.10 dynamic linker bug workaround) - rust-lang#130526 (Begin experimental support for pin reborrowing) - rust-lang#130611 (Address diagnostics regression for `const_char_encode_utf8`.) - rust-lang#130614 (Add arm64e-apple-tvos target) - rust-lang#130617 (bail if there are too many non-region infer vars in the query response) - rust-lang#130619 (Fix scraped examples height) - rust-lang#130624 (Add `Vec::as_non_null`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 3e5c662 + 74935ec commit 93f5e21

File tree

3 files changed

+87
-11
lines changed

3 files changed

+87
-11
lines changed

alloc/src/vec/mod.rs

+69-2
Original file line numberDiff line numberDiff line change
@@ -1584,7 +1584,8 @@ impl<T, A: Allocator> Vec<T, A> {
15841584
///
15851585
/// This method guarantees that for the purpose of the aliasing model, this method
15861586
/// does not materialize a reference to the underlying slice, and thus the returned pointer
1587-
/// will remain valid when mixed with other calls to [`as_ptr`] and [`as_mut_ptr`].
1587+
/// will remain valid when mixed with other calls to [`as_ptr`], [`as_mut_ptr`],
1588+
/// and [`as_non_null`].
15881589
/// Note that calling other methods that materialize mutable references to the slice,
15891590
/// or mutable references to specific elements you are planning on accessing through this pointer,
15901591
/// as well as writing to those elements, may still invalidate this pointer.
@@ -1621,6 +1622,7 @@ impl<T, A: Allocator> Vec<T, A> {
16211622
///
16221623
/// [`as_mut_ptr`]: Vec::as_mut_ptr
16231624
/// [`as_ptr`]: Vec::as_ptr
1625+
/// [`as_non_null`]: Vec::as_non_null
16241626
#[stable(feature = "vec_as_ptr", since = "1.37.0")]
16251627
#[rustc_never_returns_null_ptr]
16261628
#[inline]
@@ -1640,7 +1642,8 @@ impl<T, A: Allocator> Vec<T, A> {
16401642
///
16411643
/// This method guarantees that for the purpose of the aliasing model, this method
16421644
/// does not materialize a reference to the underlying slice, and thus the returned pointer
1643-
/// will remain valid when mixed with other calls to [`as_ptr`] and [`as_mut_ptr`].
1645+
/// will remain valid when mixed with other calls to [`as_ptr`], [`as_mut_ptr`],
1646+
/// and [`as_non_null`].
16441647
/// Note that calling other methods that materialize references to the slice,
16451648
/// or references to specific elements you are planning on accessing through this pointer,
16461649
/// may still invalidate this pointer.
@@ -1680,6 +1683,7 @@ impl<T, A: Allocator> Vec<T, A> {
16801683
///
16811684
/// [`as_mut_ptr`]: Vec::as_mut_ptr
16821685
/// [`as_ptr`]: Vec::as_ptr
1686+
/// [`as_non_null`]: Vec::as_non_null
16831687
#[stable(feature = "vec_as_ptr", since = "1.37.0")]
16841688
#[rustc_never_returns_null_ptr]
16851689
#[inline]
@@ -1689,6 +1693,69 @@ impl<T, A: Allocator> Vec<T, A> {
16891693
self.buf.ptr()
16901694
}
16911695

1696+
/// Returns a `NonNull` pointer to the vector's buffer, or a dangling
1697+
/// `NonNull` pointer valid for zero sized reads if the vector didn't allocate.
1698+
///
1699+
/// The caller must ensure that the vector outlives the pointer this
1700+
/// function returns, or else it will end up dangling.
1701+
/// Modifying the vector may cause its buffer to be reallocated,
1702+
/// which would also make any pointers to it invalid.
1703+
///
1704+
/// This method guarantees that for the purpose of the aliasing model, this method
1705+
/// does not materialize a reference to the underlying slice, and thus the returned pointer
1706+
/// will remain valid when mixed with other calls to [`as_ptr`], [`as_mut_ptr`],
1707+
/// and [`as_non_null`].
1708+
/// Note that calling other methods that materialize references to the slice,
1709+
/// or references to specific elements you are planning on accessing through this pointer,
1710+
/// may still invalidate this pointer.
1711+
/// See the second example below for how this guarantee can be used.
1712+
///
1713+
/// # Examples
1714+
///
1715+
/// ```
1716+
/// #![feature(box_vec_non_null)]
1717+
///
1718+
/// // Allocate vector big enough for 4 elements.
1719+
/// let size = 4;
1720+
/// let mut x: Vec<i32> = Vec::with_capacity(size);
1721+
/// let x_ptr = x.as_non_null();
1722+
///
1723+
/// // Initialize elements via raw pointer writes, then set length.
1724+
/// unsafe {
1725+
/// for i in 0..size {
1726+
/// x_ptr.add(i).write(i as i32);
1727+
/// }
1728+
/// x.set_len(size);
1729+
/// }
1730+
/// assert_eq!(&*x, &[0, 1, 2, 3]);
1731+
/// ```
1732+
///
1733+
/// Due to the aliasing guarantee, the following code is legal:
1734+
///
1735+
/// ```rust
1736+
/// #![feature(box_vec_non_null)]
1737+
///
1738+
/// unsafe {
1739+
/// let mut v = vec![0];
1740+
/// let ptr1 = v.as_non_null();
1741+
/// ptr1.write(1);
1742+
/// let ptr2 = v.as_non_null();
1743+
/// ptr2.write(2);
1744+
/// // Notably, the write to `ptr2` did *not* invalidate `ptr1`:
1745+
/// ptr1.write(3);
1746+
/// }
1747+
/// ```
1748+
///
1749+
/// [`as_mut_ptr`]: Vec::as_mut_ptr
1750+
/// [`as_ptr`]: Vec::as_ptr
1751+
/// [`as_non_null`]: Vec::as_non_null
1752+
#[unstable(feature = "box_vec_non_null", reason = "new API", issue = "130364")]
1753+
#[inline]
1754+
pub fn as_non_null(&mut self) -> NonNull<T> {
1755+
// SAFETY: A `Vec` always has a non-null pointer.
1756+
unsafe { NonNull::new_unchecked(self.as_mut_ptr()) }
1757+
}
1758+
16921759
/// Returns a reference to the underlying allocator.
16931760
#[unstable(feature = "allocator_api", issue = "32838")]
16941761
#[inline]

core/src/char/methods.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! impl char {}
22
33
use super::*;
4+
use crate::intrinsics::const_eval_select;
45
use crate::slice;
56
use crate::str::from_utf8_unchecked_mut;
67
use crate::unicode::printable::is_printable;
@@ -1762,6 +1763,15 @@ const fn len_utf8(code: u32) -> usize {
17621763
#[doc(hidden)]
17631764
#[inline]
17641765
pub const fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> &mut [u8] {
1766+
const fn panic_at_const(_code: u32, _len: usize, _dst_len: usize) {
1767+
// Note that we cannot format in constant expressions.
1768+
panic!("encode_utf8: buffer does not have enough bytes to encode code point");
1769+
}
1770+
fn panic_at_rt(code: u32, len: usize, dst_len: usize) {
1771+
panic!(
1772+
"encode_utf8: need {len} bytes to encode U+{code:04X} but buffer has just {dst_len}",
1773+
);
1774+
}
17651775
let len = len_utf8(code);
17661776
match (len, &mut *dst) {
17671777
(1, [a, ..]) => {
@@ -1782,8 +1792,8 @@ pub const fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> &mut [u8] {
17821792
*c = (code >> 6 & 0x3F) as u8 | TAG_CONT;
17831793
*d = (code & 0x3F) as u8 | TAG_CONT;
17841794
}
1785-
// Note that we cannot format in constant expressions.
1786-
_ => panic!("encode_utf8: buffer does not have enough bytes to encode code point"),
1795+
// FIXME(const-hack): We would prefer to have streamlined panics when formatters become const-friendly.
1796+
_ => const_eval_select((code, len, dst.len()), panic_at_const, panic_at_rt),
17871797
};
17881798
// SAFETY: `<&mut [u8]>::as_mut_ptr` is guaranteed to return a valid pointer and `len` has been tested to be within bounds.
17891799
unsafe { slice::from_raw_parts_mut(dst.as_mut_ptr(), len) }

std/src/fs.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -2473,16 +2473,15 @@ pub fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
24732473
/// # Platform-specific behavior
24742474
///
24752475
/// This function currently corresponds to `openat`, `fdopendir`, `unlinkat` and `lstat` functions
2476-
/// on Unix (except for macOS before version 10.10 and REDOX) and the `CreateFileW`,
2477-
/// `GetFileInformationByHandleEx`, `SetFileInformationByHandle`, and `NtCreateFile` functions on
2478-
/// Windows. Note that, this [may change in the future][changes].
2476+
/// on Unix (except for REDOX) and the `CreateFileW`, `GetFileInformationByHandleEx`,
2477+
/// `SetFileInformationByHandle`, and `NtCreateFile` functions on Windows. Note that, this
2478+
/// [may change in the future][changes].
24792479
///
24802480
/// [changes]: io#platform-specific-behavior
24812481
///
2482-
/// On macOS before version 10.10 and REDOX, as well as when running in Miri for any target, this
2483-
/// function is not protected against time-of-check to time-of-use (TOCTOU) race conditions, and
2484-
/// should not be used in security-sensitive code on those platforms. All other platforms are
2485-
/// protected.
2482+
/// On REDOX, as well as when running in Miri for any target, this function is not protected against
2483+
/// time-of-check to time-of-use (TOCTOU) race conditions, and should not be used in
2484+
/// security-sensitive code on those platforms. All other platforms are protected.
24862485
///
24872486
/// # Errors
24882487
///

0 commit comments

Comments
 (0)