Skip to content

Commit 337c11e

Browse files
committed
Auto merge of #141842 - jhpratt:rollup-r7ldrl2, r=jhpratt
Rollup of 6 pull requests Successful merges: - #141072 (Stabilize feature `result_flattening`) - #141215 (std: clarify Clone trait documentation about duplication semantics) - #141277 (Miri CI: test aarch64-apple-darwin in PRs instead of the x86_64 target) - #141521 (Add `const` support for float rounding methods) - #141812 (Fix "consider borrowing" for else-if) - #141832 (library: explain TOCTOU races in `fs::remove_dir_all`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents f0999ff + 7f306d5 commit 337c11e

File tree

31 files changed

+551
-148
lines changed

31 files changed

+551
-148
lines changed

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,103 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
518518
sym::fabsf64 => self.float_abs_intrinsic::<Double>(args, dest)?,
519519
sym::fabsf128 => self.float_abs_intrinsic::<Quad>(args, dest)?,
520520

521+
sym::floorf16 => self.float_round_intrinsic::<Half>(
522+
args,
523+
dest,
524+
rustc_apfloat::Round::TowardNegative,
525+
)?,
526+
sym::floorf32 => self.float_round_intrinsic::<Single>(
527+
args,
528+
dest,
529+
rustc_apfloat::Round::TowardNegative,
530+
)?,
531+
sym::floorf64 => self.float_round_intrinsic::<Double>(
532+
args,
533+
dest,
534+
rustc_apfloat::Round::TowardNegative,
535+
)?,
536+
sym::floorf128 => self.float_round_intrinsic::<Quad>(
537+
args,
538+
dest,
539+
rustc_apfloat::Round::TowardNegative,
540+
)?,
541+
542+
sym::ceilf16 => self.float_round_intrinsic::<Half>(
543+
args,
544+
dest,
545+
rustc_apfloat::Round::TowardPositive,
546+
)?,
547+
sym::ceilf32 => self.float_round_intrinsic::<Single>(
548+
args,
549+
dest,
550+
rustc_apfloat::Round::TowardPositive,
551+
)?,
552+
sym::ceilf64 => self.float_round_intrinsic::<Double>(
553+
args,
554+
dest,
555+
rustc_apfloat::Round::TowardPositive,
556+
)?,
557+
sym::ceilf128 => self.float_round_intrinsic::<Quad>(
558+
args,
559+
dest,
560+
rustc_apfloat::Round::TowardPositive,
561+
)?,
562+
563+
sym::truncf16 => {
564+
self.float_round_intrinsic::<Half>(args, dest, rustc_apfloat::Round::TowardZero)?
565+
}
566+
sym::truncf32 => {
567+
self.float_round_intrinsic::<Single>(args, dest, rustc_apfloat::Round::TowardZero)?
568+
}
569+
sym::truncf64 => {
570+
self.float_round_intrinsic::<Double>(args, dest, rustc_apfloat::Round::TowardZero)?
571+
}
572+
sym::truncf128 => {
573+
self.float_round_intrinsic::<Quad>(args, dest, rustc_apfloat::Round::TowardZero)?
574+
}
575+
576+
sym::roundf16 => self.float_round_intrinsic::<Half>(
577+
args,
578+
dest,
579+
rustc_apfloat::Round::NearestTiesToAway,
580+
)?,
581+
sym::roundf32 => self.float_round_intrinsic::<Single>(
582+
args,
583+
dest,
584+
rustc_apfloat::Round::NearestTiesToAway,
585+
)?,
586+
sym::roundf64 => self.float_round_intrinsic::<Double>(
587+
args,
588+
dest,
589+
rustc_apfloat::Round::NearestTiesToAway,
590+
)?,
591+
sym::roundf128 => self.float_round_intrinsic::<Quad>(
592+
args,
593+
dest,
594+
rustc_apfloat::Round::NearestTiesToAway,
595+
)?,
596+
597+
sym::round_ties_even_f16 => self.float_round_intrinsic::<Half>(
598+
args,
599+
dest,
600+
rustc_apfloat::Round::NearestTiesToEven,
601+
)?,
602+
sym::round_ties_even_f32 => self.float_round_intrinsic::<Single>(
603+
args,
604+
dest,
605+
rustc_apfloat::Round::NearestTiesToEven,
606+
)?,
607+
sym::round_ties_even_f64 => self.float_round_intrinsic::<Double>(
608+
args,
609+
dest,
610+
rustc_apfloat::Round::NearestTiesToEven,
611+
)?,
612+
sym::round_ties_even_f128 => self.float_round_intrinsic::<Quad>(
613+
args,
614+
dest,
615+
rustc_apfloat::Round::NearestTiesToEven,
616+
)?,
617+
521618
// Unsupported intrinsic: skip the return_to_block below.
522619
_ => return interp_ok(false),
523620
}
@@ -900,4 +997,20 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
900997
self.write_scalar(x.abs(), dest)?;
901998
interp_ok(())
902999
}
1000+
1001+
fn float_round_intrinsic<F>(
1002+
&mut self,
1003+
args: &[OpTy<'tcx, M::Provenance>],
1004+
dest: &PlaceTy<'tcx, M::Provenance>,
1005+
mode: rustc_apfloat::Round,
1006+
) -> InterpResult<'tcx, ()>
1007+
where
1008+
F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
1009+
{
1010+
let x: F = self.read_scalar(&args[0])?.to_float()?;
1011+
let res = x.round_to_integral(mode).value;
1012+
let res = self.adjust_nan(res, &[x]);
1013+
self.write_scalar(res, dest)?;
1014+
interp_ok(())
1015+
}
9031016
}

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#![feature(decl_macro)]
1313
#![feature(panic_backtrace_config)]
1414
#![feature(panic_update_hook)]
15-
#![feature(result_flattening)]
1615
#![feature(rustdoc_internals)]
1716
#![feature(try_blocks)]
1817
// tidy-alphabetical-end

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2713,6 +2713,31 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
27132713
));
27142714
}
27152715

2716+
// Don't try to suggest ref/deref on an `if` expression, because:
2717+
// - The `if` could be part of a desugared `if else` statement,
2718+
// which would create impossible suggestions such as `if ... { ... } else &if { ... } else { ... }`.
2719+
// - In general the suggestions it creates such as `&if ... { ... } else { ... }` are not very helpful.
2720+
// We try to generate a suggestion such as `if ... { &... } else { &... }` instead.
2721+
if let hir::ExprKind::If(_c, then, els) = expr.kind {
2722+
// The `then` of a `Expr::If` always contains a block, and that block may have a final expression that we can borrow
2723+
// If the block does not have a final expression, it will return () and we do not make a suggestion to borrow that.
2724+
let ExprKind::Block(then, _) = then.kind else { return None };
2725+
let Some(then) = then.expr else { return None };
2726+
let (mut suggs, help, app, verbose, mutref) =
2727+
self.suggest_deref_or_ref(then, checked_ty, expected)?;
2728+
2729+
// If there is no `else`, the return type of this `if` will be (), so suggesting to change the `then` block is useless
2730+
let els_expr = match els?.kind {
2731+
ExprKind::Block(block, _) => block.expr?,
2732+
_ => els?,
2733+
};
2734+
let (else_suggs, ..) =
2735+
self.suggest_deref_or_ref(els_expr, checked_ty, expected)?;
2736+
suggs.extend(else_suggs);
2737+
2738+
return Some((suggs, help, app, verbose, mutref));
2739+
}
2740+
27162741
if let Some((sugg, msg)) = self.can_use_as_ref(expr) {
27172742
return Some((
27182743
sugg,

library/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.

library/core/src/intrinsics/mod.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,86 +2240,86 @@ pub unsafe fn fmuladdf128(a: f128, b: f128, c: f128) -> f128;
22402240
/// [`f16::floor`](../../std/primitive.f16.html#method.floor)
22412241
#[rustc_intrinsic]
22422242
#[rustc_nounwind]
2243-
pub unsafe fn floorf16(x: f16) -> f16;
2243+
pub const unsafe fn floorf16(x: f16) -> f16;
22442244
/// Returns the largest integer less than or equal to an `f32`.
22452245
///
22462246
/// The stabilized version of this intrinsic is
22472247
/// [`f32::floor`](../../std/primitive.f32.html#method.floor)
22482248
#[rustc_intrinsic]
22492249
#[rustc_nounwind]
2250-
pub unsafe fn floorf32(x: f32) -> f32;
2250+
pub const unsafe fn floorf32(x: f32) -> f32;
22512251
/// Returns the largest integer less than or equal to an `f64`.
22522252
///
22532253
/// The stabilized version of this intrinsic is
22542254
/// [`f64::floor`](../../std/primitive.f64.html#method.floor)
22552255
#[rustc_intrinsic]
22562256
#[rustc_nounwind]
2257-
pub unsafe fn floorf64(x: f64) -> f64;
2257+
pub const unsafe fn floorf64(x: f64) -> f64;
22582258
/// Returns the largest integer less than or equal to an `f128`.
22592259
///
22602260
/// The stabilized version of this intrinsic is
22612261
/// [`f128::floor`](../../std/primitive.f128.html#method.floor)
22622262
#[rustc_intrinsic]
22632263
#[rustc_nounwind]
2264-
pub unsafe fn floorf128(x: f128) -> f128;
2264+
pub const unsafe fn floorf128(x: f128) -> f128;
22652265

22662266
/// Returns the smallest integer greater than or equal to an `f16`.
22672267
///
22682268
/// The stabilized version of this intrinsic is
22692269
/// [`f16::ceil`](../../std/primitive.f16.html#method.ceil)
22702270
#[rustc_intrinsic]
22712271
#[rustc_nounwind]
2272-
pub unsafe fn ceilf16(x: f16) -> f16;
2272+
pub const unsafe fn ceilf16(x: f16) -> f16;
22732273
/// Returns the smallest integer greater than or equal to an `f32`.
22742274
///
22752275
/// The stabilized version of this intrinsic is
22762276
/// [`f32::ceil`](../../std/primitive.f32.html#method.ceil)
22772277
#[rustc_intrinsic]
22782278
#[rustc_nounwind]
2279-
pub unsafe fn ceilf32(x: f32) -> f32;
2279+
pub const unsafe fn ceilf32(x: f32) -> f32;
22802280
/// Returns the smallest integer greater than or equal to an `f64`.
22812281
///
22822282
/// The stabilized version of this intrinsic is
22832283
/// [`f64::ceil`](../../std/primitive.f64.html#method.ceil)
22842284
#[rustc_intrinsic]
22852285
#[rustc_nounwind]
2286-
pub unsafe fn ceilf64(x: f64) -> f64;
2286+
pub const unsafe fn ceilf64(x: f64) -> f64;
22872287
/// Returns the smallest integer greater than or equal to an `f128`.
22882288
///
22892289
/// The stabilized version of this intrinsic is
22902290
/// [`f128::ceil`](../../std/primitive.f128.html#method.ceil)
22912291
#[rustc_intrinsic]
22922292
#[rustc_nounwind]
2293-
pub unsafe fn ceilf128(x: f128) -> f128;
2293+
pub const unsafe fn ceilf128(x: f128) -> f128;
22942294

22952295
/// Returns the integer part of an `f16`.
22962296
///
22972297
/// The stabilized version of this intrinsic is
22982298
/// [`f16::trunc`](../../std/primitive.f16.html#method.trunc)
22992299
#[rustc_intrinsic]
23002300
#[rustc_nounwind]
2301-
pub unsafe fn truncf16(x: f16) -> f16;
2301+
pub const unsafe fn truncf16(x: f16) -> f16;
23022302
/// Returns the integer part of an `f32`.
23032303
///
23042304
/// The stabilized version of this intrinsic is
23052305
/// [`f32::trunc`](../../std/primitive.f32.html#method.trunc)
23062306
#[rustc_intrinsic]
23072307
#[rustc_nounwind]
2308-
pub unsafe fn truncf32(x: f32) -> f32;
2308+
pub const unsafe fn truncf32(x: f32) -> f32;
23092309
/// Returns the integer part of an `f64`.
23102310
///
23112311
/// The stabilized version of this intrinsic is
23122312
/// [`f64::trunc`](../../std/primitive.f64.html#method.trunc)
23132313
#[rustc_intrinsic]
23142314
#[rustc_nounwind]
2315-
pub unsafe fn truncf64(x: f64) -> f64;
2315+
pub const unsafe fn truncf64(x: f64) -> f64;
23162316
/// Returns the integer part of an `f128`.
23172317
///
23182318
/// The stabilized version of this intrinsic is
23192319
/// [`f128::trunc`](../../std/primitive.f128.html#method.trunc)
23202320
#[rustc_intrinsic]
23212321
#[rustc_nounwind]
2322-
pub unsafe fn truncf128(x: f128) -> f128;
2322+
pub const unsafe fn truncf128(x: f128) -> f128;
23232323

23242324
/// Returns the nearest integer to an `f16`. Rounds half-way cases to the number with an even
23252325
/// least significant digit.
@@ -2328,7 +2328,7 @@ pub unsafe fn truncf128(x: f128) -> f128;
23282328
/// [`f16::round_ties_even`](../../std/primitive.f16.html#method.round_ties_even)
23292329
#[rustc_intrinsic]
23302330
#[rustc_nounwind]
2331-
pub fn round_ties_even_f16(x: f16) -> f16;
2331+
pub const fn round_ties_even_f16(x: f16) -> f16;
23322332

23332333
/// Returns the nearest integer to an `f32`. Rounds half-way cases to the number with an even
23342334
/// least significant digit.
@@ -2337,7 +2337,7 @@ pub fn round_ties_even_f16(x: f16) -> f16;
23372337
/// [`f32::round_ties_even`](../../std/primitive.f32.html#method.round_ties_even)
23382338
#[rustc_intrinsic]
23392339
#[rustc_nounwind]
2340-
pub fn round_ties_even_f32(x: f32) -> f32;
2340+
pub const fn round_ties_even_f32(x: f32) -> f32;
23412341

23422342
/// Returns the nearest integer to an `f64`. Rounds half-way cases to the number with an even
23432343
/// least significant digit.
@@ -2346,7 +2346,7 @@ pub fn round_ties_even_f32(x: f32) -> f32;
23462346
/// [`f64::round_ties_even`](../../std/primitive.f64.html#method.round_ties_even)
23472347
#[rustc_intrinsic]
23482348
#[rustc_nounwind]
2349-
pub fn round_ties_even_f64(x: f64) -> f64;
2349+
pub const fn round_ties_even_f64(x: f64) -> f64;
23502350

23512351
/// Returns the nearest integer to an `f128`. Rounds half-way cases to the number with an even
23522352
/// least significant digit.
@@ -2355,36 +2355,36 @@ pub fn round_ties_even_f64(x: f64) -> f64;
23552355
/// [`f128::round_ties_even`](../../std/primitive.f128.html#method.round_ties_even)
23562356
#[rustc_intrinsic]
23572357
#[rustc_nounwind]
2358-
pub fn round_ties_even_f128(x: f128) -> f128;
2358+
pub const fn round_ties_even_f128(x: f128) -> f128;
23592359

23602360
/// Returns the nearest integer to an `f16`. Rounds half-way cases away from zero.
23612361
///
23622362
/// The stabilized version of this intrinsic is
23632363
/// [`f16::round`](../../std/primitive.f16.html#method.round)
23642364
#[rustc_intrinsic]
23652365
#[rustc_nounwind]
2366-
pub unsafe fn roundf16(x: f16) -> f16;
2366+
pub const unsafe fn roundf16(x: f16) -> f16;
23672367
/// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
23682368
///
23692369
/// The stabilized version of this intrinsic is
23702370
/// [`f32::round`](../../std/primitive.f32.html#method.round)
23712371
#[rustc_intrinsic]
23722372
#[rustc_nounwind]
2373-
pub unsafe fn roundf32(x: f32) -> f32;
2373+
pub const unsafe fn roundf32(x: f32) -> f32;
23742374
/// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
23752375
///
23762376
/// The stabilized version of this intrinsic is
23772377
/// [`f64::round`](../../std/primitive.f64.html#method.round)
23782378
#[rustc_intrinsic]
23792379
#[rustc_nounwind]
2380-
pub unsafe fn roundf64(x: f64) -> f64;
2380+
pub const unsafe fn roundf64(x: f64) -> f64;
23812381
/// Returns the nearest integer to an `f128`. Rounds half-way cases away from zero.
23822382
///
23832383
/// The stabilized version of this intrinsic is
23842384
/// [`f128::round`](../../std/primitive.f128.html#method.round)
23852385
#[rustc_intrinsic]
23862386
#[rustc_nounwind]
2387-
pub unsafe fn roundf128(x: f128) -> f128;
2387+
pub const unsafe fn roundf128(x: f128) -> f128;
23882388

23892389
/// Float addition that allows optimizations based on algebraic rules.
23902390
/// May assume inputs are finite.

0 commit comments

Comments
 (0)