Skip to content

Rollup of 6 pull requests #141842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jun 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions compiler/rustc_const_eval/src/interpret/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,103 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
sym::fabsf64 => self.float_abs_intrinsic::<Double>(args, dest)?,
sym::fabsf128 => self.float_abs_intrinsic::<Quad>(args, dest)?,

sym::floorf16 => self.float_round_intrinsic::<Half>(
args,
dest,
rustc_apfloat::Round::TowardNegative,
)?,
sym::floorf32 => self.float_round_intrinsic::<Single>(
args,
dest,
rustc_apfloat::Round::TowardNegative,
)?,
sym::floorf64 => self.float_round_intrinsic::<Double>(
args,
dest,
rustc_apfloat::Round::TowardNegative,
)?,
sym::floorf128 => self.float_round_intrinsic::<Quad>(
args,
dest,
rustc_apfloat::Round::TowardNegative,
)?,

sym::ceilf16 => self.float_round_intrinsic::<Half>(
args,
dest,
rustc_apfloat::Round::TowardPositive,
)?,
sym::ceilf32 => self.float_round_intrinsic::<Single>(
args,
dest,
rustc_apfloat::Round::TowardPositive,
)?,
sym::ceilf64 => self.float_round_intrinsic::<Double>(
args,
dest,
rustc_apfloat::Round::TowardPositive,
)?,
sym::ceilf128 => self.float_round_intrinsic::<Quad>(
args,
dest,
rustc_apfloat::Round::TowardPositive,
)?,

sym::truncf16 => {
self.float_round_intrinsic::<Half>(args, dest, rustc_apfloat::Round::TowardZero)?
}
sym::truncf32 => {
self.float_round_intrinsic::<Single>(args, dest, rustc_apfloat::Round::TowardZero)?
}
sym::truncf64 => {
self.float_round_intrinsic::<Double>(args, dest, rustc_apfloat::Round::TowardZero)?
}
sym::truncf128 => {
self.float_round_intrinsic::<Quad>(args, dest, rustc_apfloat::Round::TowardZero)?
}

sym::roundf16 => self.float_round_intrinsic::<Half>(
args,
dest,
rustc_apfloat::Round::NearestTiesToAway,
)?,
sym::roundf32 => self.float_round_intrinsic::<Single>(
args,
dest,
rustc_apfloat::Round::NearestTiesToAway,
)?,
sym::roundf64 => self.float_round_intrinsic::<Double>(
args,
dest,
rustc_apfloat::Round::NearestTiesToAway,
)?,
sym::roundf128 => self.float_round_intrinsic::<Quad>(
args,
dest,
rustc_apfloat::Round::NearestTiesToAway,
)?,

sym::round_ties_even_f16 => self.float_round_intrinsic::<Half>(
args,
dest,
rustc_apfloat::Round::NearestTiesToEven,
)?,
sym::round_ties_even_f32 => self.float_round_intrinsic::<Single>(
args,
dest,
rustc_apfloat::Round::NearestTiesToEven,
)?,
sym::round_ties_even_f64 => self.float_round_intrinsic::<Double>(
args,
dest,
rustc_apfloat::Round::NearestTiesToEven,
)?,
sym::round_ties_even_f128 => self.float_round_intrinsic::<Quad>(
args,
dest,
rustc_apfloat::Round::NearestTiesToEven,
)?,

// Unsupported intrinsic: skip the return_to_block below.
_ => return interp_ok(false),
}
Expand Down Expand Up @@ -900,4 +997,20 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
self.write_scalar(x.abs(), dest)?;
interp_ok(())
}

fn float_round_intrinsic<F>(
&mut self,
args: &[OpTy<'tcx, M::Provenance>],
dest: &PlaceTy<'tcx, M::Provenance>,
mode: rustc_apfloat::Round,
) -> InterpResult<'tcx, ()>
where
F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
{
let x: F = self.read_scalar(&args[0])?.to_float()?;
let res = x.round_to_integral(mode).value;
let res = self.adjust_nan(res, &[x]);
self.write_scalar(res, dest)?;
interp_ok(())
}
}
1 change: 0 additions & 1 deletion compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#![feature(decl_macro)]
#![feature(panic_backtrace_config)]
#![feature(panic_update_hook)]
#![feature(result_flattening)]
#![feature(rustdoc_internals)]
#![feature(try_blocks)]
// tidy-alphabetical-end
Expand Down
25 changes: 25 additions & 0 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2713,6 +2713,31 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
));
}

// Don't try to suggest ref/deref on an `if` expression, because:
// - The `if` could be part of a desugared `if else` statement,
// which would create impossible suggestions such as `if ... { ... } else &if { ... } else { ... }`.
// - In general the suggestions it creates such as `&if ... { ... } else { ... }` are not very helpful.
// We try to generate a suggestion such as `if ... { &... } else { &... }` instead.
if let hir::ExprKind::If(_c, then, els) = expr.kind {
// The `then` of a `Expr::If` always contains a block, and that block may have a final expression that we can borrow
// If the block does not have a final expression, it will return () and we do not make a suggestion to borrow that.
let ExprKind::Block(then, _) = then.kind else { return None };
let Some(then) = then.expr else { return None };
let (mut suggs, help, app, verbose, mutref) =
self.suggest_deref_or_ref(then, checked_ty, expected)?;

// If there is no `else`, the return type of this `if` will be (), so suggesting to change the `then` block is useless
let els_expr = match els?.kind {
ExprKind::Block(block, _) => block.expr?,
_ => els?,
};
let (else_suggs, ..) =
self.suggest_deref_or_ref(els_expr, checked_ty, expected)?;
suggs.extend(else_suggs);

return Some((suggs, help, app, verbose, mutref));
}

if let Some((sugg, msg)) = self.can_use_as_ref(expr) {
return Some((
sugg,
Expand Down
39 changes: 37 additions & 2 deletions library/core/src/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@

mod uninit;

/// A common trait for the ability to explicitly duplicate an object.
/// A common trait that allows explicit creation of a duplicate value.
///
/// Calling [`clone`] always produces a new value.
/// However, for types that are references to other data (such as smart pointers or references),
/// the new value may still point to the same underlying data, rather than duplicating it.
/// See [`Clone::clone`] for more details.
///
/// This distinction is especially important when using `#[derive(Clone)]` on structs containing
/// smart pointers like `Arc<Mutex<T>>` - the cloned struct will share mutable state with the
/// original.
///
/// Differs from [`Copy`] in that [`Copy`] is implicit and an inexpensive bit-wise copy, while
/// `Clone` is always explicit and may or may not be expensive. In order to enforce
Expand Down Expand Up @@ -147,7 +156,16 @@ mod uninit;
#[rustc_diagnostic_item = "Clone"]
#[rustc_trivial_field_reads]
pub trait Clone: Sized {
/// Returns a copy of the value.
/// Returns a duplicate of the value.
///
/// Note that what "duplicate" means varies by type:
/// - For most types, this creates a deep, independent copy
/// - For reference types like `&T`, this creates another reference to the same value
/// - For smart pointers like [`Arc`] or [`Rc`], this increments the reference count
/// but still points to the same underlying data
///
/// [`Arc`]: ../../std/sync/struct.Arc.html
/// [`Rc`]: ../../std/rc/struct.Rc.html
///
/// # Examples
///
Expand All @@ -157,6 +175,23 @@ pub trait Clone: Sized {
///
/// assert_eq!("Hello", hello.clone());
/// ```
///
/// Example with a reference-counted type:
///
/// ```
/// use std::sync::{Arc, Mutex};
///
/// let data = Arc::new(Mutex::new(vec![1, 2, 3]));
/// let data_clone = data.clone(); // Creates another Arc pointing to the same Mutex
///
/// {
/// let mut lock = data.lock().unwrap();
/// lock.push(4);
/// }
///
/// // Changes are visible through the clone because they share the same underlying data
/// assert_eq!(*data_clone.lock().unwrap(), vec![1, 2, 3, 4]);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use = "cloning is often expensive and is not expected to have side effects"]
// Clone::clone is special because the compiler generates MIR to implement it for some types.
Expand Down
40 changes: 20 additions & 20 deletions library/core/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2212,86 +2212,86 @@ pub unsafe fn fmuladdf128(a: f128, b: f128, c: f128) -> f128;
/// [`f16::floor`](../../std/primitive.f16.html#method.floor)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn floorf16(x: f16) -> f16;
pub const unsafe fn floorf16(x: f16) -> f16;
/// Returns the largest integer less than or equal to an `f32`.
///
/// The stabilized version of this intrinsic is
/// [`f32::floor`](../../std/primitive.f32.html#method.floor)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn floorf32(x: f32) -> f32;
pub const unsafe fn floorf32(x: f32) -> f32;
/// Returns the largest integer less than or equal to an `f64`.
///
/// The stabilized version of this intrinsic is
/// [`f64::floor`](../../std/primitive.f64.html#method.floor)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn floorf64(x: f64) -> f64;
pub const unsafe fn floorf64(x: f64) -> f64;
/// Returns the largest integer less than or equal to an `f128`.
///
/// The stabilized version of this intrinsic is
/// [`f128::floor`](../../std/primitive.f128.html#method.floor)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn floorf128(x: f128) -> f128;
pub const unsafe fn floorf128(x: f128) -> f128;

/// Returns the smallest integer greater than or equal to an `f16`.
///
/// The stabilized version of this intrinsic is
/// [`f16::ceil`](../../std/primitive.f16.html#method.ceil)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn ceilf16(x: f16) -> f16;
pub const unsafe fn ceilf16(x: f16) -> f16;
/// Returns the smallest integer greater than or equal to an `f32`.
///
/// The stabilized version of this intrinsic is
/// [`f32::ceil`](../../std/primitive.f32.html#method.ceil)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn ceilf32(x: f32) -> f32;
pub const unsafe fn ceilf32(x: f32) -> f32;
/// Returns the smallest integer greater than or equal to an `f64`.
///
/// The stabilized version of this intrinsic is
/// [`f64::ceil`](../../std/primitive.f64.html#method.ceil)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn ceilf64(x: f64) -> f64;
pub const unsafe fn ceilf64(x: f64) -> f64;
/// Returns the smallest integer greater than or equal to an `f128`.
///
/// The stabilized version of this intrinsic is
/// [`f128::ceil`](../../std/primitive.f128.html#method.ceil)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn ceilf128(x: f128) -> f128;
pub const unsafe fn ceilf128(x: f128) -> f128;

/// Returns the integer part of an `f16`.
///
/// The stabilized version of this intrinsic is
/// [`f16::trunc`](../../std/primitive.f16.html#method.trunc)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn truncf16(x: f16) -> f16;
pub const unsafe fn truncf16(x: f16) -> f16;
/// Returns the integer part of an `f32`.
///
/// The stabilized version of this intrinsic is
/// [`f32::trunc`](../../std/primitive.f32.html#method.trunc)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn truncf32(x: f32) -> f32;
pub const unsafe fn truncf32(x: f32) -> f32;
/// Returns the integer part of an `f64`.
///
/// The stabilized version of this intrinsic is
/// [`f64::trunc`](../../std/primitive.f64.html#method.trunc)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn truncf64(x: f64) -> f64;
pub const unsafe fn truncf64(x: f64) -> f64;
/// Returns the integer part of an `f128`.
///
/// The stabilized version of this intrinsic is
/// [`f128::trunc`](../../std/primitive.f128.html#method.trunc)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn truncf128(x: f128) -> f128;
pub const unsafe fn truncf128(x: f128) -> f128;

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

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

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

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

/// Returns the nearest integer to an `f16`. Rounds half-way cases away from zero.
///
/// The stabilized version of this intrinsic is
/// [`f16::round`](../../std/primitive.f16.html#method.round)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn roundf16(x: f16) -> f16;
pub const unsafe fn roundf16(x: f16) -> f16;
/// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
///
/// The stabilized version of this intrinsic is
/// [`f32::round`](../../std/primitive.f32.html#method.round)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn roundf32(x: f32) -> f32;
pub const unsafe fn roundf32(x: f32) -> f32;
/// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
///
/// The stabilized version of this intrinsic is
/// [`f64::round`](../../std/primitive.f64.html#method.round)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn roundf64(x: f64) -> f64;
pub const unsafe fn roundf64(x: f64) -> f64;
/// Returns the nearest integer to an `f128`. Rounds half-way cases away from zero.
///
/// The stabilized version of this intrinsic is
/// [`f128::round`](../../std/primitive.f128.html#method.round)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn roundf128(x: f128) -> f128;
pub const unsafe fn roundf128(x: f128) -> f128;

/// Float addition that allows optimizations based on algebraic rules.
/// May assume inputs are finite.
Expand Down
Loading
Loading