Skip to content

Commit 59fe0c7

Browse files
authored
Rollup merge of rust-lang#137886 - NotLebedev:stable-mir-91, r=oli-obk
`name()` and `trimmed_name()` for `stable_mir::crate_def::DefId` Resolves rust-lang/project-stable-mir#91 * Added `stable_mir::crate_def::DefId::name()` and `stable_mir::crate_def::DefId::trimmed_name()` methods * Changed `CrateDef` and `DefId` `Debug` implementations to use new methods instead of copy-paste call to `Context::def_name` * Updated docs to avoid duplicating description of what `name` and `trimmed_name` do
2 parents 0ed4856 + a3378f5 commit 59fe0c7

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

compiler/stable_mir/src/crate_def.rs

+26-12
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
@@ -19,24 +40,17 @@ pub trait CrateDef {
1940
fn def_id(&self) -> DefId;
2041

2142
/// Return the fully qualified name of the current definition.
43+
///
44+
/// See [`DefId::name`] for more details
2245
fn name(&self) -> Symbol {
23-
let def_id = self.def_id();
24-
with(|cx| cx.def_name(def_id, false))
46+
self.def_id().name()
2547
}
2648

2749
/// Return a trimmed name of this definition.
2850
///
29-
/// This can be used to print more user friendly diagnostic messages.
30-
///
31-
/// If a symbol name can only be imported from one place for a type, and as
32-
/// long as it was not glob-imported anywhere in the current crate, we trim its
33-
/// path and print only the name.
34-
///
35-
/// For example, this function may shorten `std::vec::Vec` to just `Vec`,
36-
/// as long as there is no other `Vec` importable anywhere.
51+
/// See [`DefId::trimmed_name`] for more details
3752
fn trimmed_name(&self) -> Symbol {
38-
let def_id = self.def_id();
39-
with(|cx| cx.def_name(def_id, true))
53+
self.def_id().trimmed_name()
4054
}
4155

4256
/// Return information about the crate where this definition is declared.

compiler/stable_mir/src/lib.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ pub type CrateNum = usize;
4848

4949
impl Debug for DefId {
5050
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51-
f.debug_struct("DefId")
52-
.field("id", &self.0)
53-
.field("name", &with(|cx| cx.def_name(*self, false)))
54-
.finish()
51+
f.debug_struct("DefId").field("id", &self.0).field("name", &self.name()).finish()
5552
}
5653
}
5754

0 commit comments

Comments
 (0)