Skip to content

Commit 298fb8a

Browse files
committed
Add name and trimmed_name methods to DefId
1 parent 1c3b035 commit 298fb8a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

compiler/stable_mir/src/crate_def.rs

+21
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,27 @@ use crate::{Crate, Symbol, with};
1010
#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize)]
1111
pub struct DefId(pub(crate) usize);
1212

13+
impl DefId {
14+
/// Return fully qualified name of this definition
15+
pub fn name(&self) -> Symbol {
16+
with(|cx| cx.def_name(*self, false))
17+
}
18+
19+
/// Return a trimmed name of this definition.
20+
///
21+
/// This can be used to print more user friendly diagnostic messages.
22+
///
23+
/// If a symbol name can only be imported from one place for a type, and as
24+
/// long as it was not glob-imported anywhere in the current crate, we trim its
25+
/// path and print only the name.
26+
///
27+
/// For example, this function may shorten `std::vec::Vec` to just `Vec`,
28+
/// as long as there is no other `Vec` importable anywhere.
29+
pub fn trimmed_name(&self) -> Symbol {
30+
with(|cx| cx.def_name(*self, true))
31+
}
32+
}
33+
1334
/// A trait for retrieving information about a particular definition.
1435
///
1536
/// Implementors must provide the implementation of `def_id` which will be used to retrieve

0 commit comments

Comments
 (0)