diff --git a/compiler/rustc_hir/src/definitions.rs b/compiler/rustc_hir/src/definitions.rs index c825042bdcd..4b803447eed 100644 --- a/compiler/rustc_hir/src/definitions.rs +++ b/compiler/rustc_hir/src/definitions.rs @@ -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(&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), } } } diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index a99193c972b..795c5a64d26 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -531,7 +531,7 @@ fn path_append( disambiguated_data: &DisambiguatedDefPathData, ) -> Result { 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( diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 1e57411f9c5..ceb873adf5c 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -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::>() - .join("::") + path.data.into_iter().map(|elem| elem.to_string()).collect::>().join("::") } else { String::from("") } diff --git a/compiler/rustc_mir/src/interpret/intrinsics/type_name.rs b/compiler/rustc_mir/src/interpret/intrinsics/type_name.rs index 8cf768a006b..554ada1ab25 100644 --- a/compiler/rustc_mir/src/interpret/intrinsics/type_name.rs +++ b/compiler/rustc_mir/src/interpret/intrinsics/type_name.rs @@ -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) } diff --git a/compiler/rustc_symbol_mangling/src/legacy.rs b/compiler/rustc_symbol_mangling/src/legacy.rs index 15eab8bb857..b96e318bd3e 100644 --- a/compiler/rustc_symbol_mangling/src/legacy.rs +++ b/compiler/rustc_symbol_mangling/src/legacy.rs @@ -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(