Skip to content

Commit 9f50c49

Browse files
committed
Implement Display for DisambiguatedDefPathData and DefPathData
1 parent f1878d1 commit 9f50c49

File tree

5 files changed

+33
-58
lines changed

5 files changed

+33
-58
lines changed

compiler/rustc_hir/src/definitions.rs

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_index::vec::IndexVec;
1515
use rustc_span::hygiene::ExpnId;
1616
use rustc_span::symbol::{kw, sym, Symbol};
1717

18-
use std::fmt::Write;
18+
use std::fmt::{self, Write};
1919
use std::hash::Hash;
2020
use tracing::debug;
2121

@@ -155,6 +155,23 @@ pub struct DisambiguatedDefPathData {
155155
pub disambiguator: u32,
156156
}
157157

158+
impl fmt::Display for DisambiguatedDefPathData {
159+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
160+
match self.data.get_name() {
161+
DefPathDataName::Named(name) => {
162+
if self.disambiguator == 0 {
163+
f.write_str(&name.as_str())
164+
} else {
165+
write!(f, "{}#{}", name, self.disambiguator)
166+
}
167+
}
168+
DefPathDataName::Anon { namespace } => {
169+
write!(f, "{{{}#{}}}", namespace, self.disambiguator)
170+
}
171+
}
172+
}
173+
}
174+
158175
#[derive(Clone, Debug, Encodable, Decodable)]
159176
pub struct DefPath {
160177
/// The path leading from the crate root to the item.
@@ -202,35 +219,7 @@ impl DefPath {
202219
let mut s = String::with_capacity(self.data.len() * 16);
203220

204221
for component in &self.data {
205-
match component.data.get_name() {
206-
DefPathDataName::Named(name) => write!(s, "::{}", name).unwrap(),
207-
DefPathDataName::Anon { namespace } => {
208-
write!(s, "::{{{}#{}}}", namespace, component.disambiguator).unwrap()
209-
}
210-
}
211-
}
212-
213-
s
214-
}
215-
216-
/// Returns a filename-friendly string for the `DefPath`, with the
217-
/// crate-prefix.
218-
pub fn to_string_friendly<F>(&self, crate_imported_name: F) -> String
219-
where
220-
F: FnOnce(CrateNum) -> Symbol,
221-
{
222-
let crate_name_str = crate_imported_name(self.krate).as_str();
223-
let mut s = String::with_capacity(crate_name_str.len() + self.data.len() * 16);
224-
225-
write!(s, "::{}", crate_name_str).unwrap();
226-
227-
for component in &self.data {
228-
match component.data.get_name() {
229-
DefPathDataName::Named(name) => write!(s, "::{}", name).unwrap(),
230-
DefPathDataName::Anon { namespace } => {
231-
write!(s, "{{{}#{}}}", namespace, component.disambiguator).unwrap()
232-
}
233-
}
222+
write!(s, "::{}", component).unwrap();
234223
}
235224

236225
s
@@ -246,13 +235,9 @@ impl DefPath {
246235
for component in &self.data {
247236
s.extend(opt_delimiter);
248237
opt_delimiter = Some('-');
249-
match component.data.get_name() {
250-
DefPathDataName::Named(name) => write!(s, "{}", name).unwrap(),
251-
DefPathDataName::Anon { namespace } => {
252-
write!(s, "{{{}#{}}}", namespace, component.disambiguator).unwrap()
253-
}
254-
}
238+
write!(s, "{}", component).unwrap();
255239
}
240+
256241
s
257242
}
258243
}
@@ -465,11 +450,13 @@ impl DefPathData {
465450
ImplTrait => DefPathDataName::Anon { namespace: sym::opaque },
466451
}
467452
}
453+
}
468454

469-
pub fn to_string(&self) -> String {
455+
impl fmt::Display for DefPathData {
456+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
470457
match self.get_name() {
471-
DefPathDataName::Named(name) => name.to_string(),
472-
DefPathDataName::Anon { namespace } => format!("{{{{{}}}}}", namespace),
458+
DefPathDataName::Named(name) => f.write_str(&name.as_str()),
459+
DefPathDataName::Anon { namespace } => write!(f, "{{{{{}}}}}", namespace),
473460
}
474461
}
475462
}

compiler/rustc_infer/src/infer/error_reporting/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
531531
disambiguated_data: &DisambiguatedDefPathData,
532532
) -> Result<Self::Path, Self::Error> {
533533
let mut path = print_prefix(self)?;
534-
path.push(disambiguated_data.data.to_string());
534+
path.push(disambiguated_data.to_string());
535535
Ok(path)
536536
}
537537
fn path_generic_args(

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,11 +1002,7 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {
10021002
let def_id = map.local_def_id(id);
10031003
tcx.def_path_str(def_id.to_def_id())
10041004
} else if let Some(path) = map.def_path_from_hir_id(id) {
1005-
path.data
1006-
.into_iter()
1007-
.map(|elem| elem.data.to_string())
1008-
.collect::<Vec<_>>()
1009-
.join("::")
1005+
path.data.into_iter().map(|elem| elem.to_string()).collect::<Vec<_>>().join("::")
10101006
} else {
10111007
String::from("<missing path>")
10121008
}

compiler/rustc_mir/src/interpret/intrinsics/type_name.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_hir::def_id::CrateNum;
2-
use rustc_hir::definitions::{DefPathData, DefPathDataName, DisambiguatedDefPathData};
2+
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
33
use rustc_middle::mir::interpret::Allocation;
44
use rustc_middle::ty::{
55
self,
@@ -132,14 +132,8 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
132132
return Ok(self);
133133
}
134134

135-
self.path.push_str("::");
135+
write!(self.path, "::{}", disambiguated_data.data).unwrap();
136136

137-
match disambiguated_data.data.get_name() {
138-
DefPathDataName::Named(name) => self.path.write_str(&name.as_str()).unwrap(),
139-
DefPathDataName::Anon { namespace } => {
140-
write!(self.path, "{{{{{}}}}}", namespace).unwrap()
141-
}
142-
}
143137
Ok(self)
144138
}
145139

compiler/rustc_symbol_mangling/src/legacy.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
22
use rustc_hir::def_id::CrateNum;
3-
use rustc_hir::definitions::{DefPathData, DefPathDataName, DisambiguatedDefPathData};
3+
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
44
use rustc_middle::ich::NodeIdHashingMode;
55
use rustc_middle::mir::interpret::{ConstValue, Scalar};
66
use rustc_middle::ty::print::{PrettyPrinter, Print, Printer};
@@ -316,10 +316,8 @@ impl Printer<'tcx> for SymbolPrinter<'tcx> {
316316
self.path.finalize_pending_component();
317317
}
318318

319-
match disambiguated_data.data.get_name() {
320-
DefPathDataName::Named(name) => self.write_str(&name.as_str())?,
321-
DefPathDataName::Anon { namespace } => write!(self, "{{{{{}}}}}", namespace)?,
322-
}
319+
write!(self, "{}", disambiguated_data.data)?;
320+
323321
Ok(self)
324322
}
325323
fn path_generic_args(

0 commit comments

Comments
 (0)