Skip to content

Rollup of 8 pull requests #142142

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 17 commits into from
Jun 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
112 changes: 43 additions & 69 deletions compiler/rustc_hir_typeck/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,20 +380,21 @@ impl<'a, 'tcx> CastCheck<'tcx> {
err.span_label(self.span, "invalid cast");
if self.expr_ty.is_numeric() {
if self.expr_ty == fcx.tcx.types.u32 {
match fcx.tcx.sess.source_map().span_to_snippet(self.expr.span) {
Ok(snippet) => err.span_suggestion(
self.span,
"try `char::from_u32` instead",
format!("char::from_u32({snippet})"),
Applicability::MachineApplicable,
),

Err(_) => err.span_help(self.span, "try `char::from_u32` instead"),
};
err.multipart_suggestion(
"consider using `char::from_u32` instead",
vec![
(self.expr_span.shrink_to_lo(), "char::from_u32(".to_string()),
(self.expr_span.shrink_to_hi().to(self.cast_span), ")".to_string()),
],
Applicability::MachineApplicable,
);
} else if self.expr_ty == fcx.tcx.types.i8 {
err.span_help(self.span, "try casting from `u8` instead");
err.span_help(self.span, "consider casting from `u8` instead");
} else {
err.span_help(self.span, "try `char::from_u32` instead (via a `u32`)");
err.span_help(
self.span,
"consider using `char::from_u32` instead (via a `u32`)",
);
};
}
err.emit();
Expand Down Expand Up @@ -494,38 +495,31 @@ impl<'a, 'tcx> CastCheck<'tcx> {
self.cast_ty.kind(),
ty::FnDef(..) | ty::FnPtr(..) | ty::Closure(..)
) {
let mut label = true;
// Check `impl From<self.expr_ty> for self.cast_ty {}` for accurate suggestion:
if let Ok(snippet) = fcx.tcx.sess.source_map().span_to_snippet(self.expr_span)
&& let Some(from_trait) = fcx.tcx.get_diagnostic_item(sym::From)
{
if let Some(from_trait) = fcx.tcx.get_diagnostic_item(sym::From) {
let ty = fcx.resolve_vars_if_possible(self.cast_ty);
let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
if fcx
.infcx
.type_implements_trait(from_trait, [ty, expr_ty], fcx.param_env)
.must_apply_modulo_regions()
{
label = false;
if let ty::Adt(def, args) = self.cast_ty.kind() {
err.span_suggestion_verbose(
self.span,
"consider using the `From` trait instead",
format!(
"{}::from({})",
fcx.tcx.value_path_str_with_args(def.did(), args),
snippet
),
Applicability::MaybeIncorrect,
);
let to_ty = if let ty::Adt(def, args) = self.cast_ty.kind() {
fcx.tcx.value_path_str_with_args(def.did(), args)
} else {
err.span_suggestion(
self.span,
"consider using the `From` trait instead",
format!("{}::from({})", self.cast_ty, snippet),
Applicability::MaybeIncorrect,
);
self.cast_ty.to_string()
};
err.multipart_suggestion(
"consider using the `From` trait instead",
vec![
(self.expr_span.shrink_to_lo(), format!("{to_ty}::from(")),
(
self.expr_span.shrink_to_hi().to(self.cast_span),
")".to_string(),
),
],
Applicability::MaybeIncorrect,
);
}
}

Expand All @@ -548,11 +542,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
)
};

if label {
err.span_label(self.span, msg);
} else {
err.note(msg);
}
err.span_label(self.span, msg);

if let Some(note) = note {
err.note(note);
Expand Down Expand Up @@ -654,38 +644,22 @@ impl<'a, 'tcx> CastCheck<'tcx> {
match self.expr_ty.kind() {
ty::Ref(_, _, mt) => {
let mtstr = mt.prefix_str();
match fcx.tcx.sess.source_map().span_to_snippet(self.cast_span) {
Ok(s) => {
err.span_suggestion(
self.cast_span,
"try casting to a reference instead",
format!("&{mtstr}{s}"),
Applicability::MachineApplicable,
);
}
Err(_) => {
let msg = format!("did you mean `&{mtstr}{tstr}`?");
err.span_help(self.cast_span, msg);
}
}
err.span_suggestion_verbose(
self.cast_span.shrink_to_lo(),
"consider casting to a reference instead",
format!("&{mtstr}"),
Applicability::MachineApplicable,
);
}
ty::Adt(def, ..) if def.is_box() => {
match fcx.tcx.sess.source_map().span_to_snippet(self.cast_span) {
Ok(s) => {
err.span_suggestion(
self.cast_span,
"you can cast to a `Box` instead",
format!("Box<{s}>"),
Applicability::MachineApplicable,
);
}
Err(_) => {
err.span_help(
self.cast_span,
format!("you might have meant `Box<{tstr}>`"),
);
}
}
err.multipart_suggestion(
"you can cast to a `Box` instead",
vec![
(self.cast_span.shrink_to_lo(), "Box<".to_string()),
(self.cast_span.shrink_to_hi(), ">".to_string()),
],
Applicability::MachineApplicable,
);
}
_ => {
err.span_help(self.expr_span, "consider using a box or reference as appropriate");
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#![feature(negative_impls)]
#![feature(never_type)]
#![feature(ptr_alignment_type)]
#![feature(round_char_boundary)]
#![feature(rustc_attrs)]
#![feature(rustdoc_internals)]
#![feature(trusted_len)]
Expand Down
26 changes: 25 additions & 1 deletion compiler/rustc_middle/src/mir/mono.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::fmt;
use std::hash::Hash;

Expand Down Expand Up @@ -468,6 +469,29 @@ impl<'tcx> CodegenUnit<'tcx> {
hash.as_u128().to_base_fixed_len(CASE_INSENSITIVE)
}

pub fn shorten_name(human_readable_name: &str) -> Cow<'_, str> {
// Set a limit a somewhat below the common platform limits for file names.
const MAX_CGU_NAME_LENGTH: usize = 200;
const TRUNCATED_NAME_PREFIX: &str = "-trunc-";
if human_readable_name.len() > MAX_CGU_NAME_LENGTH {
let mangled_name = Self::mangle_name(human_readable_name);
// Determine a safe byte offset to truncate the name to
let truncate_to = human_readable_name.floor_char_boundary(
MAX_CGU_NAME_LENGTH - TRUNCATED_NAME_PREFIX.len() - mangled_name.len(),
);
format!(
"{}{}{}",
&human_readable_name[..truncate_to],
TRUNCATED_NAME_PREFIX,
mangled_name
)
.into()
} else {
// If the name is short enough, we can just return it as is.
human_readable_name.into()
}
}

pub fn compute_size_estimate(&mut self) {
// The size of a codegen unit as the sum of the sizes of the items
// within it.
Expand Down Expand Up @@ -604,7 +628,7 @@ impl<'tcx> CodegenUnitNameBuilder<'tcx> {
let cgu_name = self.build_cgu_name_no_mangle(cnum, components, special_suffix);

if self.tcx.sess.opts.unstable_opts.human_readable_cgu_names {
cgu_name
Symbol::intern(&CodegenUnit::shorten_name(cgu_name.as_str()))
} else {
Symbol::intern(&CodegenUnit::mangle_name(cgu_name.as_str()))
}
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_monomorphize/src/partitioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,15 +461,15 @@ fn merge_codegen_units<'tcx>(

for cgu in codegen_units.iter_mut() {
if let Some(new_cgu_name) = new_cgu_names.get(&cgu.name()) {
if cx.tcx.sess.opts.unstable_opts.human_readable_cgu_names {
cgu.set_name(Symbol::intern(new_cgu_name));
let new_cgu_name = if cx.tcx.sess.opts.unstable_opts.human_readable_cgu_names {
Symbol::intern(&CodegenUnit::shorten_name(new_cgu_name))
} else {
// If we don't require CGU names to be human-readable,
// we use a fixed length hash of the composite CGU name
// instead.
let new_cgu_name = CodegenUnit::mangle_name(new_cgu_name);
cgu.set_name(Symbol::intern(&new_cgu_name));
}
Symbol::intern(&CodegenUnit::mangle_name(new_cgu_name))
};
cgu.set_name(new_cgu_name);
}
}

Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_target/src/spec/base/apple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,13 @@ pub(crate) fn base(
// to v4, so we do the same.
// https://github.com/llvm/llvm-project/blob/378778a0d10c2f8d5df8ceff81f95b6002984a4b/clang/lib/Driver/ToolChains/Darwin.cpp#L1203
default_dwarf_version: 4,
frame_pointer: FramePointer::Always,
frame_pointer: match arch {
// clang ignores `-fomit-frame-pointer` for Armv7, it only accepts `-momit-leaf-frame-pointer`
Armv7k | Armv7s => FramePointer::Always,
// clang supports omitting frame pointers for the rest, but... don't?
Arm64 | Arm64e | Arm64_32 => FramePointer::NonLeaf,
I386 | I686 | X86_64 | X86_64h => FramePointer::Always,
},
has_rpath: true,
dll_suffix: ".dylib".into(),
archive_format: "darwin".into(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::spec::base::apple::{Arch, TargetAbi, base};
use crate::spec::{FramePointer, SanitizerSet, Target, TargetMetadata, TargetOptions};
use crate::spec::{SanitizerSet, Target, TargetMetadata, TargetOptions};

pub(crate) fn target() -> Target {
let (opts, llvm_target, arch) = base("macos", Arch::Arm64, TargetAbi::Normal);
Expand All @@ -17,7 +17,6 @@ pub(crate) fn target() -> Target {
arch,
options: TargetOptions {
mcount: "\u{1}mcount".into(),
frame_pointer: FramePointer::NonLeaf,
cpu: "apple-m1".into(),
max_atomic_width: Some(128),
// FIXME: The leak sanitizer currently fails the tests, see #88132.
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_target/src/spec/targets/aarch64_apple_ios.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::spec::base::apple::{Arch, TargetAbi, base};
use crate::spec::{FramePointer, SanitizerSet, Target, TargetMetadata, TargetOptions};
use crate::spec::{SanitizerSet, Target, TargetMetadata, TargetOptions};

pub(crate) fn target() -> Target {
let (opts, llvm_target, arch) = base("ios", Arch::Arm64, TargetAbi::Normal);
Expand All @@ -18,7 +18,6 @@ pub(crate) fn target() -> Target {
options: TargetOptions {
features: "+neon,+fp-armv8,+apple-a7".into(),
max_atomic_width: Some(128),
frame_pointer: FramePointer::NonLeaf,
supported_sanitizers: SanitizerSet::ADDRESS | SanitizerSet::THREAD,
..opts
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::spec::base::apple::{Arch, TargetAbi, base};
use crate::spec::{FramePointer, SanitizerSet, Target, TargetMetadata, TargetOptions};
use crate::spec::{SanitizerSet, Target, TargetMetadata, TargetOptions};

pub(crate) fn target() -> Target {
let (opts, llvm_target, arch) = base("ios", Arch::Arm64, TargetAbi::MacCatalyst);
Expand All @@ -18,7 +18,6 @@ pub(crate) fn target() -> Target {
options: TargetOptions {
features: "+neon,+fp-armv8,+apple-a12".into(),
max_atomic_width: Some(128),
frame_pointer: FramePointer::NonLeaf,
supported_sanitizers: SanitizerSet::ADDRESS | SanitizerSet::LEAK | SanitizerSet::THREAD,
..opts
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::spec::base::apple::{Arch, TargetAbi, base};
use crate::spec::{FramePointer, SanitizerSet, Target, TargetMetadata, TargetOptions};
use crate::spec::{SanitizerSet, Target, TargetMetadata, TargetOptions};

pub(crate) fn target() -> Target {
let (opts, llvm_target, arch) = base("ios", Arch::Arm64, TargetAbi::Simulator);
Expand All @@ -18,7 +18,6 @@ pub(crate) fn target() -> Target {
options: TargetOptions {
features: "+neon,+fp-armv8,+apple-a7".into(),
max_atomic_width: Some(128),
frame_pointer: FramePointer::NonLeaf,
supported_sanitizers: SanitizerSet::ADDRESS | SanitizerSet::THREAD,
..opts
},
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_target/src/spec/targets/aarch64_apple_tvos.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::spec::base::apple::{Arch, TargetAbi, base};
use crate::spec::{FramePointer, Target, TargetMetadata, TargetOptions};
use crate::spec::{Target, TargetMetadata, TargetOptions};

pub(crate) fn target() -> Target {
let (opts, llvm_target, arch) = base("tvos", Arch::Arm64, TargetAbi::Normal);
Expand All @@ -18,7 +18,6 @@ pub(crate) fn target() -> Target {
options: TargetOptions {
features: "+neon,+fp-armv8,+apple-a7".into(),
max_atomic_width: Some(128),
frame_pointer: FramePointer::NonLeaf,
..opts
},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::spec::base::apple::{Arch, TargetAbi, base};
use crate::spec::{FramePointer, Target, TargetMetadata, TargetOptions};
use crate::spec::{Target, TargetMetadata, TargetOptions};

pub(crate) fn target() -> Target {
let (opts, llvm_target, arch) = base("tvos", Arch::Arm64, TargetAbi::Simulator);
Expand All @@ -18,7 +18,6 @@ pub(crate) fn target() -> Target {
options: TargetOptions {
features: "+neon,+fp-armv8,+apple-a7".into(),
max_atomic_width: Some(128),
frame_pointer: FramePointer::NonLeaf,
..opts
},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::spec::base::apple::{Arch, TargetAbi, base};
use crate::spec::{FramePointer, SanitizerSet, Target, TargetMetadata, TargetOptions};
use crate::spec::{SanitizerSet, Target, TargetMetadata, TargetOptions};

pub(crate) fn target() -> Target {
let (opts, llvm_target, arch) = base("visionos", Arch::Arm64, TargetAbi::Normal);
Expand All @@ -18,7 +18,6 @@ pub(crate) fn target() -> Target {
options: TargetOptions {
features: "+neon,+fp-armv8,+apple-a16".into(),
max_atomic_width: Some(128),
frame_pointer: FramePointer::NonLeaf,
supported_sanitizers: SanitizerSet::ADDRESS | SanitizerSet::THREAD,
..opts
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::spec::base::apple::{Arch, TargetAbi, base};
use crate::spec::{FramePointer, SanitizerSet, Target, TargetMetadata, TargetOptions};
use crate::spec::{SanitizerSet, Target, TargetMetadata, TargetOptions};

pub(crate) fn target() -> Target {
let (opts, llvm_target, arch) = base("visionos", Arch::Arm64, TargetAbi::Simulator);
Expand All @@ -18,7 +18,6 @@ pub(crate) fn target() -> Target {
options: TargetOptions {
features: "+neon,+fp-armv8,+apple-a16".into(),
max_atomic_width: Some(128),
frame_pointer: FramePointer::NonLeaf,
supported_sanitizers: SanitizerSet::ADDRESS | SanitizerSet::THREAD,
..opts
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::spec::base::apple::{Arch, TargetAbi, base};
use crate::spec::{FramePointer, Target, TargetMetadata, TargetOptions};
use crate::spec::{Target, TargetMetadata, TargetOptions};

pub(crate) fn target() -> Target {
let (opts, llvm_target, arch) = base("watchos", Arch::Arm64, TargetAbi::Simulator);
Expand All @@ -18,7 +18,6 @@ pub(crate) fn target() -> Target {
options: TargetOptions {
features: "+neon,+fp-armv8,+apple-a7".into(),
max_atomic_width: Some(128),
frame_pointer: FramePointer::NonLeaf,
..opts
},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::spec::base::apple::{Arch, TargetAbi, base};
use crate::spec::{FramePointer, SanitizerSet, Target, TargetMetadata, TargetOptions};
use crate::spec::{SanitizerSet, Target, TargetMetadata, TargetOptions};

pub(crate) fn target() -> Target {
let (opts, llvm_target, arch) = base("macos", Arch::Arm64e, TargetAbi::Normal);
Expand All @@ -17,7 +17,6 @@ pub(crate) fn target() -> Target {
arch,
options: TargetOptions {
mcount: "\u{1}mcount".into(),
frame_pointer: FramePointer::NonLeaf,
cpu: "apple-m1".into(),
max_atomic_width: Some(128),
// FIXME: The leak sanitizer currently fails the tests, see #88132.
Expand Down
Loading
Loading