From bf26f2442365bbdc6c0785d884a042a824e1d37f Mon Sep 17 00:00:00 2001 From: Frank Steffahn Date: Thu, 20 Feb 2025 17:24:09 +0100 Subject: [PATCH 01/12] Clarify/update comments in `BufRead::read_line`'s default body with where to *actually* look for more details --- library/std/src/io/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 980ea1478e084..7f610bc88bfd7 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -2534,7 +2534,7 @@ pub trait BufRead: Read { fn read_line(&mut self, buf: &mut String) -> Result { // Note that we are not calling the `.read_until` method here, but // rather our hardcoded implementation. For more details as to why, see - // the comments in `read_to_end`. + // the comments in `default_read_to_string`. unsafe { append_to_string(buf, |b| read_until(self, b'\n', b)) } } From 33ee398fdaa849e096e4935dba7cf599b9f6b832 Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Tue, 25 Feb 2025 13:02:48 -0800 Subject: [PATCH 02/12] More precisely document `Global::deallocate()`'s safety. There is a subtlety which "other conditions must be upheld by the caller" does not capture: `GlobalAlloc`/`alloc::dealloc()` require that the provided layout will be *equal*, not just that it "fits", the layout used to allocate. This is always true here due to how `allocate()`, `grow()`, and `shrink()` are implemented (they never return a larger allocation than requested), but that is a non-local property of the implementation, so it should be documented explicitly. --- library/alloc/src/alloc.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/library/alloc/src/alloc.rs b/library/alloc/src/alloc.rs index e686a02f29b0c..3bfdc68dcdaf6 100644 --- a/library/alloc/src/alloc.rs +++ b/library/alloc/src/alloc.rs @@ -264,8 +264,14 @@ unsafe impl Allocator for Global { #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces unsafe fn deallocate(&self, ptr: NonNull, layout: Layout) { if layout.size() != 0 { - // SAFETY: `layout` is non-zero in size, - // other conditions must be upheld by the caller + // SAFETY: + // * We have checked that `layout` is non-zero in size. + // * The caller is obligated to provide a layout that "fits", and in this case, + // "fit" always means a layout that is equal to the original, because our + // `allocate()`, `grow()`, and `shrink()` implementations never returns a larger + // allocation than requested. + // * Other conditions must be upheld by the caller, as per `Allocator::deallocate()`'s + // safety documentation. unsafe { dealloc(ptr.as_ptr(), layout) } } } From 775cb23dbc58f991f300cea5d3f66b088a3950d0 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 27 Feb 2025 23:07:50 +0100 Subject: [PATCH 03/12] doc: update Wasmtime flags Wasmtime's `--wasm-features` and `--wasi-modules` flags have been renamed since these docs were initially written. Additionally, from my testing I don't believe `--wasm threads` is needed if `--wasi threads` is passed already. --- src/doc/rustc/src/platform-support/wasm32-wasip1-threads.md | 2 +- src/doc/rustc/src/platform-support/wasm64-unknown-unknown.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/doc/rustc/src/platform-support/wasm32-wasip1-threads.md b/src/doc/rustc/src/platform-support/wasm32-wasip1-threads.md index 994c0f4bbb3e9..1b0a312ca9c71 100644 --- a/src/doc/rustc/src/platform-support/wasm32-wasip1-threads.md +++ b/src/doc/rustc/src/platform-support/wasm32-wasip1-threads.md @@ -100,7 +100,7 @@ This target is not a stable target. This means that there are a few engines which implement the `wasi-threads` feature and if they do they're likely behind a flag, for example: -* Wasmtime - `--wasm-features=threads --wasi-modules=experimental-wasi-threads` +* Wasmtime - `--wasi threads` * [WAMR](https://github.com/bytecodealliance/wasm-micro-runtime) - needs to be built with WAMR_BUILD_LIB_WASI_THREADS=1 ## Building the target diff --git a/src/doc/rustc/src/platform-support/wasm64-unknown-unknown.md b/src/doc/rustc/src/platform-support/wasm64-unknown-unknown.md index 6932e6a5764bc..a717f5dad7979 100644 --- a/src/doc/rustc/src/platform-support/wasm64-unknown-unknown.md +++ b/src/doc/rustc/src/platform-support/wasm64-unknown-unknown.md @@ -36,7 +36,7 @@ which implement the `memory64` feature and if they do they're likely behind a flag, for example: * Nodejs - `--experimental-wasm-memory64` -* Wasmtime - `--wasm-features memory64` +* Wasmtime - `--wasm memory64` Also note that at this time the `wasm64-unknown-unknown` target assumes the presence of other merged wasm proposals such as (with their LLVM feature flags): From 3a6f269f26454835aabac9055ef0cda820b048ec Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Sat, 1 Mar 2025 12:20:30 +0100 Subject: [PATCH 04/12] improve error message and testing of using an unsigned simd mask --- compiler/rustc_codegen_ssa/messages.ftl | 3 +- compiler/rustc_codegen_ssa/src/errors.rs | 1 + tests/ui/simd/intrinsic/generic-gather.rs | 52 +++++++++++++++++++ tests/ui/simd/intrinsic/generic-gather.stderr | 39 ++++++++++++++ tests/ui/simd/intrinsic/generic-select.rs | 4 +- tests/ui/simd/intrinsic/generic-select.stderr | 8 ++- 6 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 tests/ui/simd/intrinsic/generic-gather.rs create mode 100644 tests/ui/simd/intrinsic/generic-gather.stderr diff --git a/compiler/rustc_codegen_ssa/messages.ftl b/compiler/rustc_codegen_ssa/messages.ftl index 22e262546c3a7..f15d6fba5066c 100644 --- a/compiler/rustc_codegen_ssa/messages.ftl +++ b/compiler/rustc_codegen_ssa/messages.ftl @@ -119,7 +119,8 @@ codegen_ssa_invalid_monomorphization_inserted_type = invalid monomorphization of codegen_ssa_invalid_monomorphization_invalid_bitmask = invalid monomorphization of `{$name}` intrinsic: invalid bitmask `{$mask_ty}`, expected `u{$expected_int_bits}` or `[u8; {$expected_bytes}]` -codegen_ssa_invalid_monomorphization_mask_type = invalid monomorphization of `{$name}` intrinsic: mask element type is `{$ty}`, expected `i_` +codegen_ssa_invalid_monomorphization_mask_type = invalid monomorphization of `{$name}` intrinsic: found mask element type is `{$ty}`, expected a signed integer type + .note = the mask may be widened, which only has the correct behavior for signed integers codegen_ssa_invalid_monomorphization_mismatched_lengths = invalid monomorphization of `{$name}` intrinsic: mismatched lengths: mask length `{$m_len}` != other vector length `{$v_len}` diff --git a/compiler/rustc_codegen_ssa/src/errors.rs b/compiler/rustc_codegen_ssa/src/errors.rs index 7e28961599f01..ccf6d12977f6a 100644 --- a/compiler/rustc_codegen_ssa/src/errors.rs +++ b/compiler/rustc_codegen_ssa/src/errors.rs @@ -957,6 +957,7 @@ pub enum InvalidMonomorphization<'tcx> { }, #[diag(codegen_ssa_invalid_monomorphization_mask_type, code = E0511)] + #[note] MaskType { #[primary_span] span: Span, diff --git a/tests/ui/simd/intrinsic/generic-gather.rs b/tests/ui/simd/intrinsic/generic-gather.rs new file mode 100644 index 0000000000000..118d802948305 --- /dev/null +++ b/tests/ui/simd/intrinsic/generic-gather.rs @@ -0,0 +1,52 @@ +//@ build-fail +//@ ignore-emscripten + +// Test that the simd_{gather,scatter} intrinsics produce ok-ish error +// messages when misused. + +#![feature(repr_simd, core_intrinsics)] +#![allow(non_camel_case_types)] + +use std::intrinsics::simd::{simd_gather, simd_scatter}; + +#[repr(simd)] +#[derive(Copy, Clone, PartialEq, Debug)] +struct x4(pub [T; 4]); + +fn main() { + let mut x = [0_f32, 1., 2., 3., 4., 5., 6., 7.]; + + let default = x4([-3_f32, -3., -3., -3.]); + let s_strided = x4([0_f32, 2., -3., 6.]); + + let mask = x4([-1_i32, -1, 0, -1]); + let umask = x4([0u16; 4]); + let fmask = x4([0_f32; 4]); + + let pointer = x.as_mut_ptr(); + let pointers = + unsafe { x4([pointer.offset(0), pointer.offset(2), pointer.offset(4), pointer.offset(6)]) }; + + unsafe { + simd_gather(default, mask, mask); + //~^ ERROR expected element type `i32` of second argument `x4` to be a pointer to the element type `f32` + + simd_gather(default, pointers, umask); + //~^ ERROR expected element type `u16` of third argument `x4` to be a signed integer type + + simd_gather(default, pointers, fmask); + //~^ ERROR expected element type `f32` of third argument `x4` to be a signed integer type + } + + unsafe { + let values = x4([42_f32, 43_f32, 44_f32, 45_f32]); + simd_scatter(values, mask, mask); + //~^ ERROR expected element type `i32` of second argument `x4` to be a pointer to the element type `f32` of the first argument `x4`, found `i32` != `*mut f32` + + simd_scatter(values, pointers, umask); + //~^ ERROR expected element type `u16` of third argument `x4` to be a signed integer type + + simd_scatter(values, pointers, fmask); + //~^ ERROR expected element type `f32` of third argument `x4` to be a signed integer type + } +} diff --git a/tests/ui/simd/intrinsic/generic-gather.stderr b/tests/ui/simd/intrinsic/generic-gather.stderr new file mode 100644 index 0000000000000..81e10fc987523 --- /dev/null +++ b/tests/ui/simd/intrinsic/generic-gather.stderr @@ -0,0 +1,39 @@ +error[E0511]: invalid monomorphization of `simd_gather` intrinsic: expected element type `i32` of second argument `x4` to be a pointer to the element type `f32` of the first argument `x4`, found `i32` != `*_ f32` + --> $DIR/generic-gather.rs:31:9 + | +LL | simd_gather(default, mask, mask); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0511]: invalid monomorphization of `simd_gather` intrinsic: expected element type `u16` of third argument `x4` to be a signed integer type + --> $DIR/generic-gather.rs:34:9 + | +LL | simd_gather(default, pointers, umask); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0511]: invalid monomorphization of `simd_gather` intrinsic: expected element type `f32` of third argument `x4` to be a signed integer type + --> $DIR/generic-gather.rs:37:9 + | +LL | simd_gather(default, pointers, fmask); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0511]: invalid monomorphization of `simd_scatter` intrinsic: expected element type `i32` of second argument `x4` to be a pointer to the element type `f32` of the first argument `x4`, found `i32` != `*mut f32` + --> $DIR/generic-gather.rs:43:9 + | +LL | simd_scatter(values, mask, mask); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0511]: invalid monomorphization of `simd_scatter` intrinsic: expected element type `u16` of third argument `x4` to be a signed integer type + --> $DIR/generic-gather.rs:46:9 + | +LL | simd_scatter(values, pointers, umask); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0511]: invalid monomorphization of `simd_scatter` intrinsic: expected element type `f32` of third argument `x4` to be a signed integer type + --> $DIR/generic-gather.rs:49:9 + | +LL | simd_scatter(values, pointers, fmask); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 6 previous errors + +For more information about this error, try `rustc --explain E0511`. diff --git a/tests/ui/simd/intrinsic/generic-select.rs b/tests/ui/simd/intrinsic/generic-select.rs index 917ad3ca6046d..db14032f1f204 100644 --- a/tests/ui/simd/intrinsic/generic-select.rs +++ b/tests/ui/simd/intrinsic/generic-select.rs @@ -37,10 +37,10 @@ fn main() { //~^ ERROR mismatched lengths: mask length `8` != other vector length `4` simd_select(x, x, x); - //~^ ERROR mask element type is `u32`, expected `i_` + //~^ ERROR mask element type is `u32`, expected a signed integer type simd_select(z, z, z); - //~^ ERROR mask element type is `f32`, expected `i_` + //~^ ERROR mask element type is `f32`, expected a signed integer type simd_select(m4, 0u32, 1u32); //~^ ERROR found non-SIMD `u32` diff --git a/tests/ui/simd/intrinsic/generic-select.stderr b/tests/ui/simd/intrinsic/generic-select.stderr index c46584d117ce8..b9af86515fdef 100644 --- a/tests/ui/simd/intrinsic/generic-select.stderr +++ b/tests/ui/simd/intrinsic/generic-select.stderr @@ -4,17 +4,21 @@ error[E0511]: invalid monomorphization of `simd_select` intrinsic: mismatched le LL | simd_select(m8, x, x); | ^^^^^^^^^^^^^^^^^^^^^ -error[E0511]: invalid monomorphization of `simd_select` intrinsic: mask element type is `u32`, expected `i_` +error[E0511]: invalid monomorphization of `simd_select` intrinsic: found mask element type is `u32`, expected a signed integer type --> $DIR/generic-select.rs:39:9 | LL | simd_select(x, x, x); | ^^^^^^^^^^^^^^^^^^^^ + | + = note: the mask may be widened, which only has the correct behavior for signed integers -error[E0511]: invalid monomorphization of `simd_select` intrinsic: mask element type is `f32`, expected `i_` +error[E0511]: invalid monomorphization of `simd_select` intrinsic: found mask element type is `f32`, expected a signed integer type --> $DIR/generic-select.rs:42:9 | LL | simd_select(z, z, z); | ^^^^^^^^^^^^^^^^^^^^ + | + = note: the mask may be widened, which only has the correct behavior for signed integers error[E0511]: invalid monomorphization of `simd_select` intrinsic: expected SIMD argument type, found non-SIMD `u32` --> $DIR/generic-select.rs:45:9 From 6f7fd11474615975fe3f57cfdd9d639892e54e8d Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sun, 2 Mar 2025 03:03:52 +0900 Subject: [PATCH 05/12] rustc_target: Add msync target feature and enable it on powerpcspe targets --- .../src/spec/targets/powerpc_unknown_linux_gnuspe.rs | 2 +- .../src/spec/targets/powerpc_unknown_linux_muslspe.rs | 1 + compiler/rustc_target/src/target_features.rs | 1 + tests/ui/check-cfg/target_feature.stderr | 1 + 4 files changed, 4 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_target/src/spec/targets/powerpc_unknown_linux_gnuspe.rs b/compiler/rustc_target/src/spec/targets/powerpc_unknown_linux_gnuspe.rs index 03bae9b597778..3c1d18e077719 100644 --- a/compiler/rustc_target/src/spec/targets/powerpc_unknown_linux_gnuspe.rs +++ b/compiler/rustc_target/src/spec/targets/powerpc_unknown_linux_gnuspe.rs @@ -24,7 +24,7 @@ pub(crate) fn target() -> Target { options: TargetOptions { abi: "spe".into(), endian: Endian::Big, - features: "+secure-plt".into(), + features: "+secure-plt,+msync".into(), mcount: "_mcount".into(), ..base }, diff --git a/compiler/rustc_target/src/spec/targets/powerpc_unknown_linux_muslspe.rs b/compiler/rustc_target/src/spec/targets/powerpc_unknown_linux_muslspe.rs index df4fd75b0bded..30d0d9cb60a77 100644 --- a/compiler/rustc_target/src/spec/targets/powerpc_unknown_linux_muslspe.rs +++ b/compiler/rustc_target/src/spec/targets/powerpc_unknown_linux_muslspe.rs @@ -26,6 +26,7 @@ pub(crate) fn target() -> Target { options: TargetOptions { abi: "spe".into(), endian: Endian::Big, + features: "+msync".into(), mcount: "_mcount".into(), ..base }, diff --git a/compiler/rustc_target/src/target_features.rs b/compiler/rustc_target/src/target_features.rs index 65a85151befa5..d05466bb48431 100644 --- a/compiler/rustc_target/src/target_features.rs +++ b/compiler/rustc_target/src/target_features.rs @@ -461,6 +461,7 @@ const HEXAGON_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[ static POWERPC_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[ // tidy-alphabetical-start ("altivec", Unstable(sym::powerpc_target_feature), &[]), + ("msync", Unstable(sym::powerpc_target_feature), &[]), ("partword-atomics", Unstable(sym::powerpc_target_feature), &[]), ("power10-vector", Unstable(sym::powerpc_target_feature), &["power9-vector"]), ("power8-altivec", Unstable(sym::powerpc_target_feature), &["altivec"]), diff --git a/tests/ui/check-cfg/target_feature.stderr b/tests/ui/check-cfg/target_feature.stderr index 8675f7a61c79a..5b82d3f539fe1 100644 --- a/tests/ui/check-cfg/target_feature.stderr +++ b/tests/ui/check-cfg/target_feature.stderr @@ -151,6 +151,7 @@ LL | cfg!(target_feature = "_UNEXPECTED_VALUE"); `mp` `mp1e2` `msa` +`msync` `mte` `multivalue` `mutable-globals` From 596c14ade49d94bd1cfaa26aa34f7ef016f632d6 Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Sat, 1 Mar 2025 14:38:07 -0700 Subject: [PATCH 06/12] fix `RangeBounds::is_empty` documentation One-sided ranges are never empty --- library/core/src/ops/range.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/ops/range.rs b/library/core/src/ops/range.rs index e0c442e529215..1935268cda891 100644 --- a/library/core/src/ops/range.rs +++ b/library/core/src/ops/range.rs @@ -827,7 +827,7 @@ pub trait RangeBounds { } /// Returns `true` if the range contains no items. - /// One-sided ranges (`RangeFrom`, etc) always return `true`. + /// One-sided ranges (`RangeFrom`, etc) always return `false`. /// /// # Examples /// From c51b229140c885cac757a405a328a07e90d5bca9 Mon Sep 17 00:00:00 2001 From: Trevor Gross Date: Sat, 1 Mar 2025 23:05:19 +0000 Subject: [PATCH 07/12] Disable `f16` on Aarch64 without `neon` LLVM has crashes at some `half` operations when built with assertions enabled if fp-armv8 is not available [1]. Things seem to usually work, but we are reaching LLVM undefined behavior so this needs to be disabled. [1]: https://github.com/llvm/llvm-project/issues/129394 --- library/std/build.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/library/std/build.rs b/library/std/build.rs index 723d1eb02e07e..9df35ce3cc852 100644 --- a/library/std/build.rs +++ b/library/std/build.rs @@ -12,6 +12,11 @@ fn main() { .expect("CARGO_CFG_TARGET_POINTER_WIDTH was not set") .parse() .unwrap(); + let target_features: Vec<_> = env::var("CARGO_CFG_TARGET_FEATURE") + .unwrap_or_default() + .split(",") + .map(ToOwned::to_owned) + .collect(); let is_miri = env::var_os("CARGO_CFG_MIRI").is_some(); println!("cargo:rustc-check-cfg=cfg(netbsd10)"); @@ -101,6 +106,8 @@ fn main() { ("s390x", _) => false, // Unsupported ("arm64ec", _) => false, + // LLVM crash + ("aarch64", _) if !target_features.iter().any(|f| f == "neon") => false, // MinGW ABI bugs ("x86_64", "windows") if target_env == "gnu" && target_abi != "llvm" => false, // Infinite recursion From 60798272d6aa5e9bd45341c637ce55338e349496 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sun, 2 Mar 2025 14:27:53 +1100 Subject: [PATCH 08/12] Adjust triagebot.toml entries for `rustc_mir_build/src/builder/` --- triagebot.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/triagebot.toml b/triagebot.toml index f9eb09ff34381..7aafe85d9f945 100644 --- a/triagebot.toml +++ b/triagebot.toml @@ -771,7 +771,7 @@ cc = ["@fmease"] message = "Some changes occurred in diagnostic error codes" cc = ["@GuillaumeGomez"] -[mentions."compiler/rustc_mir_build/src/build/matches"] +[mentions."compiler/rustc_mir_build/src/builder/matches"] message = "Some changes occurred in match lowering" cc = ["@Nadrieril"] @@ -1031,7 +1031,7 @@ cc = ["@rust-lang/project-exploit-mitigations", "@rcvalle"] message = "Some changes occurred in coverage instrumentation." cc = ["@Zalathar"] -[mentions."compiler/rustc_mir_build/src/build/coverageinfo.rs"] +[mentions."compiler/rustc_mir_build/src/builder/coverageinfo.rs"] message = "Some changes occurred in coverage instrumentation." cc = ["@Zalathar"] @@ -1260,7 +1260,7 @@ project-exploit-mitigations = [ "/compiler/rustc_middle/src/ty" = ["compiler", "types"] "/compiler/rustc_const_eval/src/interpret" = ["compiler", "mir"] "/compiler/rustc_const_eval/src/transform" = ["compiler", "mir-opt"] -"/compiler/rustc_mir_build/src/build" = ["compiler", "mir"] +"/compiler/rustc_mir_build/src/builder" = ["compiler", "mir"] "/compiler/rustc_mir_transform" = ["compiler", "mir", "mir-opt"] "/compiler/rustc_smir" = ["project-stable-mir"] "/compiler/rustc_parse" = ["compiler", "parser"] From 4d0a1af920c088f4a56def2c2c4d97cb7c60f1ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jana=20D=C3=B6nszelmann?= Date: Sun, 2 Mar 2025 09:59:28 +0100 Subject: [PATCH 09/12] edit mailmap --- .mailmap | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.mailmap b/.mailmap index eb6a9fcabca99..a791daa681d47 100644 --- a/.mailmap +++ b/.mailmap @@ -292,6 +292,9 @@ James Hinshelwood James Miller James Perry James Sanderson +Jana Dönszelmann +Jana Dönszelmann +Jana Dönszelmann Jan-Erik Rediger Jaro Fietz Jason Fager From 298fb8af65cdbecd81ed4a51d3602641caaad909 Mon Sep 17 00:00:00 2001 From: NotLebedev Date: Sat, 1 Mar 2025 09:57:04 +0300 Subject: [PATCH 10/12] Add name and trimmed_name methods to DefId --- compiler/stable_mir/src/crate_def.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/compiler/stable_mir/src/crate_def.rs b/compiler/stable_mir/src/crate_def.rs index 8c6fd99f98a1c..469e8a7cf8996 100644 --- a/compiler/stable_mir/src/crate_def.rs +++ b/compiler/stable_mir/src/crate_def.rs @@ -10,6 +10,27 @@ use crate::{Crate, Symbol, with}; #[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize)] pub struct DefId(pub(crate) usize); +impl DefId { + /// Return fully qualified name of this definition + pub fn name(&self) -> Symbol { + with(|cx| cx.def_name(*self, false)) + } + + /// Return a trimmed name of this definition. + /// + /// This can be used to print more user friendly diagnostic messages. + /// + /// If a symbol name can only be imported from one place for a type, and as + /// long as it was not glob-imported anywhere in the current crate, we trim its + /// path and print only the name. + /// + /// For example, this function may shorten `std::vec::Vec` to just `Vec`, + /// as long as there is no other `Vec` importable anywhere. + pub fn trimmed_name(&self) -> Symbol { + with(|cx| cx.def_name(*self, true)) + } +} + /// A trait for retrieving information about a particular definition. /// /// Implementors must provide the implementation of `def_id` which will be used to retrieve From 141d2f3f025c649e92e71dc1f22198cb265c435a Mon Sep 17 00:00:00 2001 From: NotLebedev Date: Sat, 1 Mar 2025 10:57:17 +0300 Subject: [PATCH 11/12] Replace usages of `Context.def_name` Use `DefId.name` and `DefId.trimmed_name` instead --- compiler/stable_mir/src/crate_def.rs | 6 ++---- compiler/stable_mir/src/lib.rs | 5 +---- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/compiler/stable_mir/src/crate_def.rs b/compiler/stable_mir/src/crate_def.rs index 469e8a7cf8996..d36426d5a9903 100644 --- a/compiler/stable_mir/src/crate_def.rs +++ b/compiler/stable_mir/src/crate_def.rs @@ -41,8 +41,7 @@ pub trait CrateDef { /// Return the fully qualified name of the current definition. fn name(&self) -> Symbol { - let def_id = self.def_id(); - with(|cx| cx.def_name(def_id, false)) + self.def_id().name() } /// Return a trimmed name of this definition. @@ -56,8 +55,7 @@ pub trait CrateDef { /// For example, this function may shorten `std::vec::Vec` to just `Vec`, /// as long as there is no other `Vec` importable anywhere. fn trimmed_name(&self) -> Symbol { - let def_id = self.def_id(); - with(|cx| cx.def_name(def_id, true)) + self.def_id().trimmed_name() } /// Return information about the crate where this definition is declared. diff --git a/compiler/stable_mir/src/lib.rs b/compiler/stable_mir/src/lib.rs index 0b4cebadad1d4..8df36e23c4a21 100644 --- a/compiler/stable_mir/src/lib.rs +++ b/compiler/stable_mir/src/lib.rs @@ -48,10 +48,7 @@ pub type CrateNum = usize; impl Debug for DefId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("DefId") - .field("id", &self.0) - .field("name", &with(|cx| cx.def_name(*self, false))) - .finish() + f.debug_struct("DefId").field("id", &self.0).field("name", &self.name()).finish() } } From a3378f59388ced7f6011e508bb7dff5f90362e00 Mon Sep 17 00:00:00 2001 From: NotLebedev Date: Sun, 2 Mar 2025 13:02:20 +0300 Subject: [PATCH 12/12] Remove duplication in `name`/`trimmed_anem` docs Reference `DefId` in `CrateDef` docs to avoid duplicating long description of `trimmed_name` --- compiler/stable_mir/src/crate_def.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/compiler/stable_mir/src/crate_def.rs b/compiler/stable_mir/src/crate_def.rs index d36426d5a9903..2577c281ca4f2 100644 --- a/compiler/stable_mir/src/crate_def.rs +++ b/compiler/stable_mir/src/crate_def.rs @@ -40,20 +40,15 @@ pub trait CrateDef { fn def_id(&self) -> DefId; /// Return the fully qualified name of the current definition. + /// + /// See [`DefId::name`] for more details fn name(&self) -> Symbol { self.def_id().name() } /// Return a trimmed name of this definition. /// - /// This can be used to print more user friendly diagnostic messages. - /// - /// If a symbol name can only be imported from one place for a type, and as - /// long as it was not glob-imported anywhere in the current crate, we trim its - /// path and print only the name. - /// - /// For example, this function may shorten `std::vec::Vec` to just `Vec`, - /// as long as there is no other `Vec` importable anywhere. + /// See [`DefId::trimmed_name`] for more details fn trimmed_name(&self) -> Symbol { self.def_id().trimmed_name() }