Skip to content

Commit 256ab94

Browse files
author
The Miri Cronjob Bot
committed
Merge from rustc
2 parents 11b7a2c + 60374c4 commit 256ab94

File tree

20 files changed

+462
-123
lines changed

20 files changed

+462
-123
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

alloc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ bench = false
1616

1717
[dependencies]
1818
core = { path = "../core", public = true }
19-
compiler_builtins = { version = "=0.1.159", features = ['rustc-dep-of-std'] }
19+
compiler_builtins = { version = "=0.1.160", features = ['rustc-dep-of-std'] }
2020

2121
[features]
2222
compiler-builtins-mem = ['compiler_builtins/mem']

core/src/clone.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,16 @@
3838

3939
mod uninit;
4040

41-
/// A common trait for the ability to explicitly duplicate an object.
41+
/// A common trait that allows explicit creation of a duplicate value.
42+
///
43+
/// Calling [`clone`] always produces a new value.
44+
/// However, for types that are references to other data (such as smart pointers or references),
45+
/// the new value may still point to the same underlying data, rather than duplicating it.
46+
/// See [`Clone::clone`] for more details.
47+
///
48+
/// This distinction is especially important when using `#[derive(Clone)]` on structs containing
49+
/// smart pointers like `Arc<Mutex<T>>` - the cloned struct will share mutable state with the
50+
/// original.
4251
///
4352
/// Differs from [`Copy`] in that [`Copy`] is implicit and an inexpensive bit-wise copy, while
4453
/// `Clone` is always explicit and may or may not be expensive. In order to enforce
@@ -147,7 +156,16 @@ mod uninit;
147156
#[rustc_diagnostic_item = "Clone"]
148157
#[rustc_trivial_field_reads]
149158
pub trait Clone: Sized {
150-
/// Returns a copy of the value.
159+
/// Returns a duplicate of the value.
160+
///
161+
/// Note that what "duplicate" means varies by type:
162+
/// - For most types, this creates a deep, independent copy
163+
/// - For reference types like `&T`, this creates another reference to the same value
164+
/// - For smart pointers like [`Arc`] or [`Rc`], this increments the reference count
165+
/// but still points to the same underlying data
166+
///
167+
/// [`Arc`]: ../../std/sync/struct.Arc.html
168+
/// [`Rc`]: ../../std/rc/struct.Rc.html
151169
///
152170
/// # Examples
153171
///
@@ -157,6 +175,23 @@ pub trait Clone: Sized {
157175
///
158176
/// assert_eq!("Hello", hello.clone());
159177
/// ```
178+
///
179+
/// Example with a reference-counted type:
180+
///
181+
/// ```
182+
/// use std::sync::{Arc, Mutex};
183+
///
184+
/// let data = Arc::new(Mutex::new(vec![1, 2, 3]));
185+
/// let data_clone = data.clone(); // Creates another Arc pointing to the same Mutex
186+
///
187+
/// {
188+
/// let mut lock = data.lock().unwrap();
189+
/// lock.push(4);
190+
/// }
191+
///
192+
/// // Changes are visible through the clone because they share the same underlying data
193+
/// assert_eq!(*data_clone.lock().unwrap(), vec![1, 2, 3, 4]);
194+
/// ```
160195
#[stable(feature = "rust1", since = "1.0.0")]
161196
#[must_use = "cloning is often expensive and is not expected to have side effects"]
162197
// Clone::clone is special because the compiler generates MIR to implement it for some types.

core/src/intrinsics/bounds.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//! Various traits used to restrict intrinsics to not-completely-wrong types.
2+
3+
/// Types with a built-in dereference operator in runtime MIR,
4+
/// aka references and raw pointers.
5+
///
6+
/// # Safety
7+
/// Must actually *be* such a type.
8+
pub unsafe trait BuiltinDeref: Sized {
9+
type Pointee: ?Sized;
10+
}
11+
12+
unsafe impl<T: ?Sized> BuiltinDeref for &mut T {
13+
type Pointee = T;
14+
}
15+
unsafe impl<T: ?Sized> BuiltinDeref for &T {
16+
type Pointee = T;
17+
}
18+
unsafe impl<T: ?Sized> BuiltinDeref for *mut T {
19+
type Pointee = T;
20+
}
21+
unsafe impl<T: ?Sized> BuiltinDeref for *const T {
22+
type Pointee = T;
23+
}
24+
25+
pub trait ChangePointee<U: ?Sized>: BuiltinDeref {
26+
type Output;
27+
}
28+
impl<'a, T: ?Sized + 'a, U: ?Sized + 'a> ChangePointee<U> for &'a mut T {
29+
type Output = &'a mut U;
30+
}
31+
impl<'a, T: ?Sized + 'a, U: ?Sized + 'a> ChangePointee<U> for &'a T {
32+
type Output = &'a U;
33+
}
34+
impl<T: ?Sized, U: ?Sized> ChangePointee<U> for *mut T {
35+
type Output = *mut U;
36+
}
37+
impl<T: ?Sized, U: ?Sized> ChangePointee<U> for *const T {
38+
type Output = *const U;
39+
}

core/src/intrinsics/mod.rs

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
use crate::marker::{ConstParamTy, DiscriminantKind, Tuple};
5454
use crate::ptr;
5555

56+
mod bounds;
5657
pub mod fallback;
5758
pub mod mir;
5859
pub mod simd;
@@ -1730,7 +1731,7 @@ pub const fn needs_drop<T: ?Sized>() -> bool;
17301731
#[rustc_intrinsic_const_stable_indirect]
17311732
#[rustc_nounwind]
17321733
#[rustc_intrinsic]
1733-
pub const unsafe fn offset<Ptr, Delta>(dst: Ptr, offset: Delta) -> Ptr;
1734+
pub const unsafe fn offset<Ptr: bounds::BuiltinDeref, Delta>(dst: Ptr, offset: Delta) -> Ptr;
17341735

17351736
/// Calculates the offset from a pointer, potentially wrapping.
17361737
///
@@ -1751,6 +1752,33 @@ pub const unsafe fn offset<Ptr, Delta>(dst: Ptr, offset: Delta) -> Ptr;
17511752
#[rustc_intrinsic]
17521753
pub const unsafe fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
17531754

1755+
/// Projects to the `index`-th element of `slice_ptr`, as the same kind of pointer
1756+
/// as the slice was provided -- so `&mut [T] → &mut T`, `&[T] → &T`,
1757+
/// `*mut [T] → *mut T`, or `*const [T] → *const T` -- without a bounds check.
1758+
///
1759+
/// This is exposed via `<usize as SliceIndex>::get(_unchecked)(_mut)`,
1760+
/// and isn't intended to be used elsewhere.
1761+
///
1762+
/// Expands in MIR to `{&, &mut, &raw const, &raw mut} (*slice_ptr)[index]`,
1763+
/// depending on the types involved, so no backend support is needed.
1764+
///
1765+
/// # Safety
1766+
///
1767+
/// - `index < PtrMetadata(slice_ptr)`, so the indexing is in-bounds for the slice
1768+
/// - the resulting offsetting is in-bounds of the allocated object, which is
1769+
/// always the case for references, but needs to be upheld manually for pointers
1770+
#[cfg(not(bootstrap))]
1771+
#[rustc_nounwind]
1772+
#[rustc_intrinsic]
1773+
pub const unsafe fn slice_get_unchecked<
1774+
ItemPtr: bounds::ChangePointee<[T], Pointee = T, Output = SlicePtr>,
1775+
SlicePtr,
1776+
T,
1777+
>(
1778+
slice_ptr: SlicePtr,
1779+
index: usize,
1780+
) -> ItemPtr;
1781+
17541782
/// Masks out bits of the pointer according to a mask.
17551783
///
17561784
/// Note that, unlike most intrinsics, this is safe to call;
@@ -2212,86 +2240,86 @@ pub unsafe fn fmuladdf128(a: f128, b: f128, c: f128) -> f128;
22122240
/// [`f16::floor`](../../std/primitive.f16.html#method.floor)
22132241
#[rustc_intrinsic]
22142242
#[rustc_nounwind]
2215-
pub unsafe fn floorf16(x: f16) -> f16;
2243+
pub const unsafe fn floorf16(x: f16) -> f16;
22162244
/// Returns the largest integer less than or equal to an `f32`.
22172245
///
22182246
/// The stabilized version of this intrinsic is
22192247
/// [`f32::floor`](../../std/primitive.f32.html#method.floor)
22202248
#[rustc_intrinsic]
22212249
#[rustc_nounwind]
2222-
pub unsafe fn floorf32(x: f32) -> f32;
2250+
pub const unsafe fn floorf32(x: f32) -> f32;
22232251
/// Returns the largest integer less than or equal to an `f64`.
22242252
///
22252253
/// The stabilized version of this intrinsic is
22262254
/// [`f64::floor`](../../std/primitive.f64.html#method.floor)
22272255
#[rustc_intrinsic]
22282256
#[rustc_nounwind]
2229-
pub unsafe fn floorf64(x: f64) -> f64;
2257+
pub const unsafe fn floorf64(x: f64) -> f64;
22302258
/// Returns the largest integer less than or equal to an `f128`.
22312259
///
22322260
/// The stabilized version of this intrinsic is
22332261
/// [`f128::floor`](../../std/primitive.f128.html#method.floor)
22342262
#[rustc_intrinsic]
22352263
#[rustc_nounwind]
2236-
pub unsafe fn floorf128(x: f128) -> f128;
2264+
pub const unsafe fn floorf128(x: f128) -> f128;
22372265

22382266
/// Returns the smallest integer greater than or equal to an `f16`.
22392267
///
22402268
/// The stabilized version of this intrinsic is
22412269
/// [`f16::ceil`](../../std/primitive.f16.html#method.ceil)
22422270
#[rustc_intrinsic]
22432271
#[rustc_nounwind]
2244-
pub unsafe fn ceilf16(x: f16) -> f16;
2272+
pub const unsafe fn ceilf16(x: f16) -> f16;
22452273
/// Returns the smallest integer greater than or equal to an `f32`.
22462274
///
22472275
/// The stabilized version of this intrinsic is
22482276
/// [`f32::ceil`](../../std/primitive.f32.html#method.ceil)
22492277
#[rustc_intrinsic]
22502278
#[rustc_nounwind]
2251-
pub unsafe fn ceilf32(x: f32) -> f32;
2279+
pub const unsafe fn ceilf32(x: f32) -> f32;
22522280
/// Returns the smallest integer greater than or equal to an `f64`.
22532281
///
22542282
/// The stabilized version of this intrinsic is
22552283
/// [`f64::ceil`](../../std/primitive.f64.html#method.ceil)
22562284
#[rustc_intrinsic]
22572285
#[rustc_nounwind]
2258-
pub unsafe fn ceilf64(x: f64) -> f64;
2286+
pub const unsafe fn ceilf64(x: f64) -> f64;
22592287
/// Returns the smallest integer greater than or equal to an `f128`.
22602288
///
22612289
/// The stabilized version of this intrinsic is
22622290
/// [`f128::ceil`](../../std/primitive.f128.html#method.ceil)
22632291
#[rustc_intrinsic]
22642292
#[rustc_nounwind]
2265-
pub unsafe fn ceilf128(x: f128) -> f128;
2293+
pub const unsafe fn ceilf128(x: f128) -> f128;
22662294

22672295
/// Returns the integer part of an `f16`.
22682296
///
22692297
/// The stabilized version of this intrinsic is
22702298
/// [`f16::trunc`](../../std/primitive.f16.html#method.trunc)
22712299
#[rustc_intrinsic]
22722300
#[rustc_nounwind]
2273-
pub unsafe fn truncf16(x: f16) -> f16;
2301+
pub const unsafe fn truncf16(x: f16) -> f16;
22742302
/// Returns the integer part of an `f32`.
22752303
///
22762304
/// The stabilized version of this intrinsic is
22772305
/// [`f32::trunc`](../../std/primitive.f32.html#method.trunc)
22782306
#[rustc_intrinsic]
22792307
#[rustc_nounwind]
2280-
pub unsafe fn truncf32(x: f32) -> f32;
2308+
pub const unsafe fn truncf32(x: f32) -> f32;
22812309
/// Returns the integer part of an `f64`.
22822310
///
22832311
/// The stabilized version of this intrinsic is
22842312
/// [`f64::trunc`](../../std/primitive.f64.html#method.trunc)
22852313
#[rustc_intrinsic]
22862314
#[rustc_nounwind]
2287-
pub unsafe fn truncf64(x: f64) -> f64;
2315+
pub const unsafe fn truncf64(x: f64) -> f64;
22882316
/// Returns the integer part of an `f128`.
22892317
///
22902318
/// The stabilized version of this intrinsic is
22912319
/// [`f128::trunc`](../../std/primitive.f128.html#method.trunc)
22922320
#[rustc_intrinsic]
22932321
#[rustc_nounwind]
2294-
pub unsafe fn truncf128(x: f128) -> f128;
2322+
pub const unsafe fn truncf128(x: f128) -> f128;
22952323

22962324
/// Returns the nearest integer to an `f16`. Rounds half-way cases to the number with an even
22972325
/// least significant digit.
@@ -2300,7 +2328,7 @@ pub unsafe fn truncf128(x: f128) -> f128;
23002328
/// [`f16::round_ties_even`](../../std/primitive.f16.html#method.round_ties_even)
23012329
#[rustc_intrinsic]
23022330
#[rustc_nounwind]
2303-
pub fn round_ties_even_f16(x: f16) -> f16;
2331+
pub const fn round_ties_even_f16(x: f16) -> f16;
23042332

23052333
/// Returns the nearest integer to an `f32`. Rounds half-way cases to the number with an even
23062334
/// least significant digit.
@@ -2309,7 +2337,7 @@ pub fn round_ties_even_f16(x: f16) -> f16;
23092337
/// [`f32::round_ties_even`](../../std/primitive.f32.html#method.round_ties_even)
23102338
#[rustc_intrinsic]
23112339
#[rustc_nounwind]
2312-
pub fn round_ties_even_f32(x: f32) -> f32;
2340+
pub const fn round_ties_even_f32(x: f32) -> f32;
23132341

23142342
/// Returns the nearest integer to an `f64`. Rounds half-way cases to the number with an even
23152343
/// least significant digit.
@@ -2318,7 +2346,7 @@ pub fn round_ties_even_f32(x: f32) -> f32;
23182346
/// [`f64::round_ties_even`](../../std/primitive.f64.html#method.round_ties_even)
23192347
#[rustc_intrinsic]
23202348
#[rustc_nounwind]
2321-
pub fn round_ties_even_f64(x: f64) -> f64;
2349+
pub const fn round_ties_even_f64(x: f64) -> f64;
23222350

23232351
/// Returns the nearest integer to an `f128`. Rounds half-way cases to the number with an even
23242352
/// least significant digit.
@@ -2327,36 +2355,36 @@ pub fn round_ties_even_f64(x: f64) -> f64;
23272355
/// [`f128::round_ties_even`](../../std/primitive.f128.html#method.round_ties_even)
23282356
#[rustc_intrinsic]
23292357
#[rustc_nounwind]
2330-
pub fn round_ties_even_f128(x: f128) -> f128;
2358+
pub const fn round_ties_even_f128(x: f128) -> f128;
23312359

23322360
/// Returns the nearest integer to an `f16`. Rounds half-way cases away from zero.
23332361
///
23342362
/// The stabilized version of this intrinsic is
23352363
/// [`f16::round`](../../std/primitive.f16.html#method.round)
23362364
#[rustc_intrinsic]
23372365
#[rustc_nounwind]
2338-
pub unsafe fn roundf16(x: f16) -> f16;
2366+
pub const unsafe fn roundf16(x: f16) -> f16;
23392367
/// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
23402368
///
23412369
/// The stabilized version of this intrinsic is
23422370
/// [`f32::round`](../../std/primitive.f32.html#method.round)
23432371
#[rustc_intrinsic]
23442372
#[rustc_nounwind]
2345-
pub unsafe fn roundf32(x: f32) -> f32;
2373+
pub const unsafe fn roundf32(x: f32) -> f32;
23462374
/// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
23472375
///
23482376
/// The stabilized version of this intrinsic is
23492377
/// [`f64::round`](../../std/primitive.f64.html#method.round)
23502378
#[rustc_intrinsic]
23512379
#[rustc_nounwind]
2352-
pub unsafe fn roundf64(x: f64) -> f64;
2380+
pub const unsafe fn roundf64(x: f64) -> f64;
23532381
/// Returns the nearest integer to an `f128`. Rounds half-way cases away from zero.
23542382
///
23552383
/// The stabilized version of this intrinsic is
23562384
/// [`f128::round`](../../std/primitive.f128.html#method.round)
23572385
#[rustc_intrinsic]
23582386
#[rustc_nounwind]
2359-
pub unsafe fn roundf128(x: f128) -> f128;
2387+
pub const unsafe fn roundf128(x: f128) -> f128;
23602388

23612389
/// Float addition that allows optimizations based on algebraic rules.
23622390
/// May assume inputs are finite.
@@ -3575,18 +3603,9 @@ pub const fn type_id<T: ?Sized + 'static>() -> u128;
35753603
#[unstable(feature = "core_intrinsics", issue = "none")]
35763604
#[rustc_intrinsic_const_stable_indirect]
35773605
#[rustc_intrinsic]
3578-
pub const fn aggregate_raw_ptr<P: AggregateRawPtr<D, Metadata = M>, D, M>(data: D, meta: M) -> P;
3579-
3580-
#[unstable(feature = "core_intrinsics", issue = "none")]
3581-
pub trait AggregateRawPtr<D> {
3582-
type Metadata: Copy;
3583-
}
3584-
impl<P: ?Sized, T: ptr::Thin> AggregateRawPtr<*const T> for *const P {
3585-
type Metadata = <P as ptr::Pointee>::Metadata;
3586-
}
3587-
impl<P: ?Sized, T: ptr::Thin> AggregateRawPtr<*mut T> for *mut P {
3588-
type Metadata = <P as ptr::Pointee>::Metadata;
3589-
}
3606+
pub const fn aggregate_raw_ptr<P: bounds::BuiltinDeref, D, M>(data: D, meta: M) -> P
3607+
where
3608+
<P as bounds::BuiltinDeref>::Pointee: ptr::Pointee<Metadata = M>;
35903609

35913610
/// Lowers in MIR to `Rvalue::UnaryOp` with `UnOp::PtrMetadata`.
35923611
///

0 commit comments

Comments
 (0)