Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions compiler/stable_mir/src/crate_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,24 +40,17 @@ 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 {
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.
///
/// 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 {
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.
Expand Down
5 changes: 1 addition & 4 deletions compiler/stable_mir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
Loading