Implement Display
for DisambiguatedDefPathData
and DefPathData
This commit is contained in:
parent
f1878d19fa
commit
9f50c49117
5 changed files with 33 additions and 58 deletions
|
@ -15,7 +15,7 @@ use rustc_index::vec::IndexVec;
|
||||||
use rustc_span::hygiene::ExpnId;
|
use rustc_span::hygiene::ExpnId;
|
||||||
use rustc_span::symbol::{kw, sym, Symbol};
|
use rustc_span::symbol::{kw, sym, Symbol};
|
||||||
|
|
||||||
use std::fmt::Write;
|
use std::fmt::{self, Write};
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
|
@ -155,6 +155,23 @@ pub struct DisambiguatedDefPathData {
|
||||||
pub disambiguator: u32,
|
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)]
|
#[derive(Clone, Debug, Encodable, Decodable)]
|
||||||
pub struct DefPath {
|
pub struct DefPath {
|
||||||
/// The path leading from the crate root to the item.
|
/// The path leading from the crate root to the item.
|
||||||
|
@ -202,35 +219,7 @@ impl DefPath {
|
||||||
let mut s = String::with_capacity(self.data.len() * 16);
|
let mut s = String::with_capacity(self.data.len() * 16);
|
||||||
|
|
||||||
for component in &self.data {
|
for component in &self.data {
|
||||||
match component.data.get_name() {
|
write!(s, "::{}", component).unwrap();
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s
|
s
|
||||||
|
@ -246,13 +235,9 @@ impl DefPath {
|
||||||
for component in &self.data {
|
for component in &self.data {
|
||||||
s.extend(opt_delimiter);
|
s.extend(opt_delimiter);
|
||||||
opt_delimiter = Some('-');
|
opt_delimiter = Some('-');
|
||||||
match component.data.get_name() {
|
write!(s, "{}", component).unwrap();
|
||||||
DefPathDataName::Named(name) => write!(s, "{}", name).unwrap(),
|
|
||||||
DefPathDataName::Anon { namespace } => {
|
|
||||||
write!(s, "{{{}#{}}}", namespace, component.disambiguator).unwrap()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s
|
s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -465,11 +450,13 @@ impl DefPathData {
|
||||||
ImplTrait => DefPathDataName::Anon { namespace: sym::opaque },
|
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() {
|
match self.get_name() {
|
||||||
DefPathDataName::Named(name) => name.to_string(),
|
DefPathDataName::Named(name) => f.write_str(&name.as_str()),
|
||||||
DefPathDataName::Anon { namespace } => format!("{{{{{}}}}}", namespace),
|
DefPathDataName::Anon { namespace } => write!(f, "{{{{{}}}}}", namespace),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -531,7 +531,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
||||||
disambiguated_data: &DisambiguatedDefPathData,
|
disambiguated_data: &DisambiguatedDefPathData,
|
||||||
) -> Result<Self::Path, Self::Error> {
|
) -> Result<Self::Path, Self::Error> {
|
||||||
let mut path = print_prefix(self)?;
|
let mut path = print_prefix(self)?;
|
||||||
path.push(disambiguated_data.data.to_string());
|
path.push(disambiguated_data.to_string());
|
||||||
Ok(path)
|
Ok(path)
|
||||||
}
|
}
|
||||||
fn path_generic_args(
|
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);
|
let def_id = map.local_def_id(id);
|
||||||
tcx.def_path_str(def_id.to_def_id())
|
tcx.def_path_str(def_id.to_def_id())
|
||||||
} else if let Some(path) = map.def_path_from_hir_id(id) {
|
} else if let Some(path) = map.def_path_from_hir_id(id) {
|
||||||
path.data
|
path.data.into_iter().map(|elem| elem.to_string()).collect::<Vec<_>>().join("::")
|
||||||
.into_iter()
|
|
||||||
.map(|elem| elem.data.to_string())
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("::")
|
|
||||||
} else {
|
} else {
|
||||||
String::from("<missing path>")
|
String::from("<missing path>")
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use rustc_hir::def_id::CrateNum;
|
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::mir::interpret::Allocation;
|
||||||
use rustc_middle::ty::{
|
use rustc_middle::ty::{
|
||||||
self,
|
self,
|
||||||
|
@ -132,14 +132,8 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
|
||||||
return Ok(self);
|
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)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
||||||
use rustc_hir::def_id::CrateNum;
|
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::ich::NodeIdHashingMode;
|
||||||
use rustc_middle::mir::interpret::{ConstValue, Scalar};
|
use rustc_middle::mir::interpret::{ConstValue, Scalar};
|
||||||
use rustc_middle::ty::print::{PrettyPrinter, Print, Printer};
|
use rustc_middle::ty::print::{PrettyPrinter, Print, Printer};
|
||||||
|
@ -316,10 +316,8 @@ impl Printer<'tcx> for SymbolPrinter<'tcx> {
|
||||||
self.path.finalize_pending_component();
|
self.path.finalize_pending_component();
|
||||||
}
|
}
|
||||||
|
|
||||||
match disambiguated_data.data.get_name() {
|
write!(self, "{}", disambiguated_data.data)?;
|
||||||
DefPathDataName::Named(name) => self.write_str(&name.as_str())?,
|
|
||||||
DefPathDataName::Anon { namespace } => write!(self, "{{{{{}}}}}", namespace)?,
|
|
||||||
}
|
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
fn path_generic_args(
|
fn path_generic_args(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue