about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNotLebedev <notlebedev@gmail.com>2025-03-01 09:57:04 +0300
committerNotLebedev <notlebedev@gmail.com>2025-03-02 12:11:14 +0300
commit298fb8af65cdbecd81ed4a51d3602641caaad909 (patch)
tree8e5d62fff21c64b862ee6274f94e287fb503e477
parent1c3b035542775e9a5decc93167d351b062942d32 (diff)
downloadrust-298fb8af65cdbecd81ed4a51d3602641caaad909.tar.gz
rust-298fb8af65cdbecd81ed4a51d3602641caaad909.zip
Add name and trimmed_name methods to DefId
-rw-r--r--compiler/stable_mir/src/crate_def.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/compiler/stable_mir/src/crate_def.rs b/compiler/stable_mir/src/crate_def.rs
index 8c6fd99f98a..469e8a7cf89 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