Skip to content

Commit

Permalink
Auto merge of rust-lang#126473 - matthiaskrgr:rollup-8w2xm09, r=matth…
Browse files Browse the repository at this point in the history
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#123769 (Improve escaping of byte, byte str, and c str proc-macro literals)
 - rust-lang#126054 (`E0229`: Suggest Moving Type Constraints to Type Parameter Declaration)
 - rust-lang#126135 (add HermitOS support for vectored read/write operations)
 - rust-lang#126266 (Unify guarantees about the default allocator)
 - rust-lang#126285 (`UniqueRc`: support allocators and `T: ?Sized`.)
 - rust-lang#126399 (extend the check for LLVM build)
 - rust-lang#126426 (const validation: fix ICE on dangling ZST reference)

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Jun 14, 2024
2 parents ca52a2c + a135342 commit c3c1757
Show file tree
Hide file tree
Showing 16 changed files with 369 additions and 137 deletions.
95 changes: 65 additions & 30 deletions alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3516,7 +3516,7 @@ fn data_offset_align(align: usize) -> usize {
layout.size() + layout.padding_needed_for(align)
}

/// A uniquely owned `Rc`
/// A uniquely owned [`Rc`].
///
/// This represents an `Rc` that is known to be uniquely owned -- that is, have exactly one strong
/// reference. Multiple weak pointers can be created, but attempts to upgrade those to strong
Expand Down Expand Up @@ -3554,13 +3554,24 @@ fn data_offset_align(align: usize) -> usize {
/// including fallible or async constructors.
#[unstable(feature = "unique_rc_arc", issue = "112566")]
#[derive(Debug)]
pub struct UniqueRc<T> {
pub struct UniqueRc<
T: ?Sized,
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
> {
ptr: NonNull<RcBox<T>>,
phantom: PhantomData<RcBox<T>>,
alloc: A,
}

#[unstable(feature = "unique_rc_arc", issue = "112566")]
impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<UniqueRc<U, A>>
for UniqueRc<T, A>
{
}

// Depends on A = Global
impl<T> UniqueRc<T> {
/// Creates a new `UniqueRc`
/// Creates a new `UniqueRc`.
///
/// Weak references to this `UniqueRc` can be created with [`UniqueRc::downgrade`]. Upgrading
/// these weak references will fail before the `UniqueRc` has been converted into an [`Rc`].
Expand All @@ -3569,54 +3580,78 @@ impl<T> UniqueRc<T> {
#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "unique_rc_arc", issue = "112566")]
pub fn new(value: T) -> Self {
Self {
ptr: Box::leak(Box::new(RcBox {
Self::new_in(value, Global)
}
}

impl<T, A: Allocator> UniqueRc<T, A> {
/// Creates a new `UniqueRc` in the provided allocator.
///
/// Weak references to this `UniqueRc` can be created with [`UniqueRc::downgrade`]. Upgrading
/// these weak references will fail before the `UniqueRc` has been converted into an [`Rc`].
/// After converting the `UniqueRc` into an [`Rc`], any weak references created beforehand will
/// point to the new [`Rc`].
#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "unique_rc_arc", issue = "112566")]
pub fn new_in(value: T, alloc: A) -> Self {
let (ptr, alloc) = Box::into_unique(Box::new_in(
RcBox {
strong: Cell::new(0),
// keep one weak reference so if all the weak pointers that are created are dropped
// the UniqueRc still stays valid.
weak: Cell::new(1),
value,
}))
.into(),
phantom: PhantomData,
}
}

/// Creates a new weak reference to the `UniqueRc`
///
/// Attempting to upgrade this weak reference will fail before the `UniqueRc` has been converted
/// to a [`Rc`] using [`UniqueRc::into_rc`].
#[unstable(feature = "unique_rc_arc", issue = "112566")]
pub fn downgrade(this: &Self) -> Weak<T> {
// SAFETY: This pointer was allocated at creation time and we guarantee that we only have
// one strong reference before converting to a regular Rc.
unsafe {
this.ptr.as_ref().inc_weak();
}
Weak { ptr: this.ptr, alloc: Global }
},
alloc,
));
Self { ptr: ptr.into(), phantom: PhantomData, alloc }
}
}

/// Converts the `UniqueRc` into a regular [`Rc`]
impl<T: ?Sized, A: Allocator> UniqueRc<T, A> {
/// Converts the `UniqueRc` into a regular [`Rc`].
///
/// This consumes the `UniqueRc` and returns a regular [`Rc`] that contains the `value` that
/// is passed to `into_rc`.
///
/// Any weak references created before this method is called can now be upgraded to strong
/// references.
#[unstable(feature = "unique_rc_arc", issue = "112566")]
pub fn into_rc(this: Self) -> Rc<T> {
pub fn into_rc(this: Self) -> Rc<T, A> {
let mut this = ManuallyDrop::new(this);

// Move the allocator out.
// SAFETY: `this.alloc` will not be accessed again, nor dropped because it is in
// a `ManuallyDrop`.
let alloc: A = unsafe { ptr::read(&this.alloc) };

// SAFETY: This pointer was allocated at creation time so we know it is valid.
unsafe {
// Convert our weak reference into a strong reference
this.ptr.as_mut().strong.set(1);
Rc::from_inner(this.ptr)
Rc::from_inner_in(this.ptr, alloc)
}
}
}

impl<T: ?Sized, A: Allocator + Clone> UniqueRc<T, A> {
/// Creates a new weak reference to the `UniqueRc`.
///
/// Attempting to upgrade this weak reference will fail before the `UniqueRc` has been converted
/// to a [`Rc`] using [`UniqueRc::into_rc`].
#[unstable(feature = "unique_rc_arc", issue = "112566")]
pub fn downgrade(this: &Self) -> Weak<T, A> {
// SAFETY: This pointer was allocated at creation time and we guarantee that we only have
// one strong reference before converting to a regular Rc.
unsafe {
this.ptr.as_ref().inc_weak();
}
Weak { ptr: this.ptr, alloc: this.alloc.clone() }
}
}

#[unstable(feature = "unique_rc_arc", issue = "112566")]
impl<T> Deref for UniqueRc<T> {
impl<T: ?Sized, A: Allocator> Deref for UniqueRc<T, A> {
type Target = T;

fn deref(&self) -> &T {
Expand All @@ -3626,7 +3661,7 @@ impl<T> Deref for UniqueRc<T> {
}

#[unstable(feature = "unique_rc_arc", issue = "112566")]
impl<T> DerefMut for UniqueRc<T> {
impl<T: ?Sized, A: Allocator> DerefMut for UniqueRc<T, A> {
fn deref_mut(&mut self) -> &mut T {
// SAFETY: This pointer was allocated at creation time so we know it is valid. We know we
// have unique ownership and therefore it's safe to make a mutable reference because
Expand All @@ -3636,7 +3671,7 @@ impl<T> DerefMut for UniqueRc<T> {
}

#[unstable(feature = "unique_rc_arc", issue = "112566")]
unsafe impl<#[may_dangle] T> Drop for UniqueRc<T> {
unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for UniqueRc<T, A> {
fn drop(&mut self) {
unsafe {
// destroy the contained object
Expand All @@ -3646,7 +3681,7 @@ unsafe impl<#[may_dangle] T> Drop for UniqueRc<T> {
self.ptr.as_ref().dec_weak();

if self.ptr.as_ref().weak() == 0 {
Global.deallocate(self.ptr.cast(), Layout::for_value_raw(self.ptr.as_ptr()));
self.alloc.deallocate(self.ptr.cast(), Layout::for_value_raw(self.ptr.as_ptr()));
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions alloc/src/rc/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,23 @@ fn test_unique_rc_drops_contents() {
assert!(dropped);
}

/// Exercise the non-default allocator usage.
#[test]
fn test_unique_rc_with_alloc_drops_contents() {
let mut dropped = false;
struct DropMe<'a>(&'a mut bool);
impl Drop for DropMe<'_> {
fn drop(&mut self) {
*self.0 = true;
}
}
{
let rc = UniqueRc::new_in(DropMe(&mut dropped), std::alloc::System);
drop(rc);
}
assert!(dropped);
}

#[test]
fn test_unique_rc_weak_clone_holding_ref() {
let mut v = UniqueRc::new(0u8);
Expand All @@ -614,3 +631,12 @@ fn test_unique_rc_weak_clone_holding_ref() {
let _ = w.clone(); // touch weak count
*r = 123;
}

#[test]
fn test_unique_rc_unsizing_coercion() {
let mut rc: UniqueRc<[u8]> = UniqueRc::new([0u8; 3]);
assert_eq!(rc.len(), 3);
rc[0] = 123;
let rc: Rc<[u8]> = UniqueRc::into_rc(rc);
assert_eq!(*rc, [123, 0, 0]);
}
57 changes: 57 additions & 0 deletions proc_macro/src/escape.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#[derive(Copy, Clone)]
pub(crate) struct EscapeOptions {
/// Produce \'.
pub escape_single_quote: bool,
/// Produce \".
pub escape_double_quote: bool,
/// Produce \x escapes for non-ASCII, and use \x rather than \u for ASCII
/// control characters.
pub escape_nonascii: bool,
}

pub(crate) fn escape_bytes(bytes: &[u8], opt: EscapeOptions) -> String {
let mut repr = String::new();

if opt.escape_nonascii {
for &byte in bytes {
escape_single_byte(byte, opt, &mut repr);
}
} else {
let mut chunks = bytes.utf8_chunks();
while let Some(chunk) = chunks.next() {
for ch in chunk.valid().chars() {
escape_single_char(ch, opt, &mut repr);
}
for &byte in chunk.invalid() {
escape_single_byte(byte, opt, &mut repr);
}
}
}

repr
}

fn escape_single_byte(byte: u8, opt: EscapeOptions, repr: &mut String) {
if byte == b'\0' {
repr.push_str("\\0");
} else if (byte == b'\'' && !opt.escape_single_quote)
|| (byte == b'"' && !opt.escape_double_quote)
{
repr.push(byte as char);
} else {
// Escapes \t, \r, \n, \\, \', \", and uses \x## for non-ASCII and
// for ASCII control characters.
repr.extend(byte.escape_ascii().map(char::from));
}
}

fn escape_single_char(ch: char, opt: EscapeOptions, repr: &mut String) {
if (ch == '\'' && !opt.escape_single_quote) || (ch == '"' && !opt.escape_double_quote) {
repr.push(ch);
} else {
// Escapes \0, \t, \r, \n, \\, \', \", and uses \u{...} for
// non-printable characters and for Grapheme_Extend characters, which
// includes things like U+0300 "Combining Grave Accent".
repr.extend(ch.escape_debug());
}
}
51 changes: 37 additions & 14 deletions proc_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@
pub mod bridge;

mod diagnostic;
mod escape;

#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
pub use diagnostic::{Diagnostic, Level, MultiSpan};

use crate::escape::{escape_bytes, EscapeOptions};
use std::ffi::CStr;
use std::ops::{Range, RangeBounds};
use std::path::PathBuf;
Expand Down Expand Up @@ -1356,40 +1358,61 @@ impl Literal {
/// String literal.
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
pub fn string(string: &str) -> Literal {
let quoted = format!("{:?}", string);
assert!(quoted.starts_with('"') && quoted.ends_with('"'));
let symbol = &quoted[1..quoted.len() - 1];
Literal::new(bridge::LitKind::Str, symbol, None)
let escape = EscapeOptions {
escape_single_quote: false,
escape_double_quote: true,
escape_nonascii: false,
};
let repr = escape_bytes(string.as_bytes(), escape);
Literal::new(bridge::LitKind::Str, &repr, None)
}

/// Character literal.
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
pub fn character(ch: char) -> Literal {
let quoted = format!("{:?}", ch);
assert!(quoted.starts_with('\'') && quoted.ends_with('\''));
let symbol = &quoted[1..quoted.len() - 1];
Literal::new(bridge::LitKind::Char, symbol, None)
let escape = EscapeOptions {
escape_single_quote: true,
escape_double_quote: false,
escape_nonascii: false,
};
let repr = escape_bytes(ch.encode_utf8(&mut [0u8; 4]).as_bytes(), escape);
Literal::new(bridge::LitKind::Char, &repr, None)
}

/// Byte character literal.
#[stable(feature = "proc_macro_byte_character", since = "1.79.0")]
pub fn byte_character(byte: u8) -> Literal {
let string = [byte].escape_ascii().to_string();
Literal::new(bridge::LitKind::Byte, &string, None)
let escape = EscapeOptions {
escape_single_quote: true,
escape_double_quote: false,
escape_nonascii: true,
};
let repr = escape_bytes(&[byte], escape);
Literal::new(bridge::LitKind::Byte, &repr, None)
}

/// Byte string literal.
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
pub fn byte_string(bytes: &[u8]) -> Literal {
let string = bytes.escape_ascii().to_string();
Literal::new(bridge::LitKind::ByteStr, &string, None)
let escape = EscapeOptions {
escape_single_quote: false,
escape_double_quote: true,
escape_nonascii: true,
};
let repr = escape_bytes(bytes, escape);
Literal::new(bridge::LitKind::ByteStr, &repr, None)
}

/// C string literal.
#[stable(feature = "proc_macro_c_str_literals", since = "1.79.0")]
pub fn c_string(string: &CStr) -> Literal {
let string = string.to_bytes().escape_ascii().to_string();
Literal::new(bridge::LitKind::CStr, &string, None)
let escape = EscapeOptions {
escape_single_quote: false,
escape_double_quote: true,
escape_nonascii: false,
};
let repr = escape_bytes(string.to_bytes(), escape);
Literal::new(bridge::LitKind::CStr, &repr, None)
}

/// Returns the span encompassing this literal.
Expand Down
2 changes: 1 addition & 1 deletion std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ dlmalloc = { version = "0.2.4", features = ['rustc-dep-of-std'] }
fortanix-sgx-abi = { version = "0.5.0", features = ['rustc-dep-of-std'], public = true }

[target.'cfg(target_os = "hermit")'.dependencies]
hermit-abi = { version = "0.3.9", features = ['rustc-dep-of-std'], public = true }
hermit-abi = { version = "0.4.0", features = ['rustc-dep-of-std'], public = true }

[target.'cfg(target_os = "wasi")'.dependencies]
wasi = { version = "0.11.0", features = ['rustc-dep-of-std'], default-features = false }
Expand Down
4 changes: 3 additions & 1 deletion std/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ pub use alloc_crate::alloc::*;
/// work, such as to serve alignment requests greater than the alignment
/// provided directly by the backing system allocator.
///
/// This type implements the `GlobalAlloc` trait and Rust programs by default
/// This type implements the [`GlobalAlloc`] trait. Currently the default
/// global allocator is unspecified. Libraries, however, like `cdylib`s and
/// `staticlib`s are guaranteed to use the [`System`] by default and as such
/// work as if they had this definition:
///
/// ```rust
Expand Down
Loading

0 comments on commit c3c1757

Please sign in to comment.