Implement Display
for DisambiguatedDefPathData
and DefPathData
This commit is contained in:
parent
f1878d19fa
commit
9f50c49117
@ -15,7 +15,7 @@
|
||||
use rustc_span::hygiene::ExpnId;
|
||||
use rustc_span::symbol::{kw, sym, Symbol};
|
||||
|
||||
use std::fmt::Write;
|
||||
use std::fmt::{self, Write};
|
||||
use std::hash::Hash;
|
||||
use tracing::debug;
|
||||
|
||||
@ -155,6 +155,23 @@ pub struct DisambiguatedDefPathData {
|
||||
pub disambiguator: u32,
|
||||
}
|
||||
|
||||
impl fmt::Display for DisambiguatedDefPathData {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self.data.get_name() {
|
||||
DefPathDataName::Named(name) => {
|
||||
if self.disambiguator == 0 {
|
||||
f.write_str(&name.as_str())
|
||||
} else {
|
||||
write!(f, "{}#{}", name, self.disambiguator)
|
||||
}
|
||||
}
|
||||
DefPathDataName::Anon { namespace } => {
|
||||
write!(f, "{{{}#{}}}", namespace, self.disambiguator)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Encodable, Decodable)]
|
||||
pub struct DefPath {
|
||||
/// The path leading from the crate root to the item.
|
||||
@ -202,35 +219,7 @@ pub fn to_string_no_crate(&self) -> String {
|
||||
let mut s = String::with_capacity(self.data.len() * 16);
|
||||
|
||||
for component in &self.data {
|
||||
match component.data.get_name() {
|
||||
DefPathDataName::Named(name) => write!(s, "::{}", name).unwrap(),
|
||||
DefPathDataName::Anon { namespace } => {
|
||||
write!(s, "::{{{}#{}}}", namespace, component.disambiguator).unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s
|
||||
}
|
||||
|
||||
/// Returns a filename-friendly string for the `DefPath`, with the
|
||||
/// crate-prefix.
|
||||
pub fn to_string_friendly<F>(&self, crate_imported_name: F) -> String
|
||||
where
|
||||
F: FnOnce(CrateNum) -> Symbol,
|
||||
{
|
||||
let crate_name_str = crate_imported_name(self.krate).as_str();
|
||||
let mut s = String::with_capacity(crate_name_str.len() + self.data.len() * 16);
|
||||
|
||||
write!(s, "::{}", crate_name_str).unwrap();
|
||||
|
||||
for component in &self.data {
|
||||
match component.data.get_name() {
|
||||
DefPathDataName::Named(name) => write!(s, "::{}", name).unwrap(),
|
||||
DefPathDataName::Anon { namespace } => {
|
||||
write!(s, "{{{}#{}}}", namespace, component.disambiguator).unwrap()
|
||||
}
|
||||
}
|
||||
write!(s, "::{}", component).unwrap();
|
||||
}
|
||||
|
||||
s
|
||||
@ -246,13 +235,9 @@ pub fn to_filename_friendly_no_crate(&self) -> String {
|
||||
for component in &self.data {
|
||||
s.extend(opt_delimiter);
|
||||
opt_delimiter = Some('-');
|
||||
match component.data.get_name() {
|
||||
DefPathDataName::Named(name) => write!(s, "{}", name).unwrap(),
|
||||
DefPathDataName::Anon { namespace } => {
|
||||
write!(s, "{{{}#{}}}", namespace, component.disambiguator).unwrap()
|
||||
}
|
||||
}
|
||||
write!(s, "{}", component).unwrap();
|
||||
}
|
||||
|
||||
s
|
||||
}
|
||||
}
|
||||
@ -465,11 +450,13 @@ pub fn get_name(&self) -> DefPathDataName {
|
||||
ImplTrait => DefPathDataName::Anon { namespace: sym::opaque },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_string(&self) -> String {
|
||||
impl fmt::Display for DefPathData {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self.get_name() {
|
||||
DefPathDataName::Named(name) => name.to_string(),
|
||||
DefPathDataName::Anon { namespace } => format!("{{{{{}}}}}", namespace),
|
||||
DefPathDataName::Named(name) => f.write_str(&name.as_str()),
|
||||
DefPathDataName::Anon { namespace } => write!(f, "{{{{{}}}}}", namespace),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -531,7 +531,7 @@ fn path_append(
|
||||
disambiguated_data: &DisambiguatedDefPathData,
|
||||
) -> Result<Self::Path, Self::Error> {
|
||||
let mut path = print_prefix(self)?;
|
||||
path.push(disambiguated_data.data.to_string());
|
||||
path.push(disambiguated_data.to_string());
|
||||
Ok(path)
|
||||
}
|
||||
fn path_generic_args(
|
||||
|
@ -1002,11 +1002,7 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {
|
||||
let def_id = map.local_def_id(id);
|
||||
tcx.def_path_str(def_id.to_def_id())
|
||||
} else if let Some(path) = map.def_path_from_hir_id(id) {
|
||||
path.data
|
||||
.into_iter()
|
||||
.map(|elem| elem.data.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join("::")
|
||||
path.data.into_iter().map(|elem| elem.to_string()).collect::<Vec<_>>().join("::")
|
||||
} else {
|
||||
String::from("<missing path>")
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
use rustc_hir::def_id::CrateNum;
|
||||
use rustc_hir::definitions::{DefPathData, DefPathDataName, DisambiguatedDefPathData};
|
||||
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
|
||||
use rustc_middle::mir::interpret::Allocation;
|
||||
use rustc_middle::ty::{
|
||||
self,
|
||||
@ -132,14 +132,8 @@ fn path_append(
|
||||
return Ok(self);
|
||||
}
|
||||
|
||||
self.path.push_str("::");
|
||||
write!(self.path, "::{}", disambiguated_data.data).unwrap();
|
||||
|
||||
match disambiguated_data.data.get_name() {
|
||||
DefPathDataName::Named(name) => self.path.write_str(&name.as_str()).unwrap(),
|
||||
DefPathDataName::Anon { namespace } => {
|
||||
write!(self.path, "{{{{{}}}}}", namespace).unwrap()
|
||||
}
|
||||
}
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
||||
use rustc_hir::def_id::CrateNum;
|
||||
use rustc_hir::definitions::{DefPathData, DefPathDataName, DisambiguatedDefPathData};
|
||||
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
|
||||
use rustc_middle::ich::NodeIdHashingMode;
|
||||
use rustc_middle::mir::interpret::{ConstValue, Scalar};
|
||||
use rustc_middle::ty::print::{PrettyPrinter, Print, Printer};
|
||||
@ -316,10 +316,8 @@ fn path_append(
|
||||
self.path.finalize_pending_component();
|
||||
}
|
||||
|
||||
match disambiguated_data.data.get_name() {
|
||||
DefPathDataName::Named(name) => self.write_str(&name.as_str())?,
|
||||
DefPathDataName::Anon { namespace } => write!(self, "{{{{{}}}}}", namespace)?,
|
||||
}
|
||||
write!(self, "{}", disambiguated_data.data)?;
|
||||
|
||||
Ok(self)
|
||||
}
|
||||
fn path_generic_args(
|
||||
|
Loading…
Reference in New Issue
Block a user