From 298fb8af65cdbecd81ed4a51d3602641caaad909 Mon Sep 17 00:00:00 2001 From: NotLebedev Date: Sat, 1 Mar 2025 09:57:04 +0300 Subject: [PATCH 1/3] 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 2/3] 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 3/3] 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() }