Skip to content

Commit 2708ad8

Browse files
committed
Fix pretty-printing of DisambiguatedDefPathData
1 parent 9f50c49 commit 2708ad8

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

compiler/rustc_hir/src/definitions.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_data_structures::fx::FxHashMap;
1313
use rustc_data_structures::stable_hasher::StableHasher;
1414
use rustc_index::vec::IndexVec;
1515
use rustc_span::hygiene::ExpnId;
16-
use rustc_span::symbol::{kw, sym, Symbol};
16+
use rustc_span::symbol::{kw, sym, Ident, Symbol};
1717

1818
use std::fmt::{self, Write};
1919
use std::hash::Hash;
@@ -155,23 +155,32 @@ 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 {
158+
impl DisambiguatedDefPathData {
159+
pub fn fmt_maybe_verbose(&self, writer: &mut impl Write, verbose: bool) -> fmt::Result {
160160
match self.data.get_name() {
161161
DefPathDataName::Named(name) => {
162-
if self.disambiguator == 0 {
163-
f.write_str(&name.as_str())
162+
if Ident::with_dummy_span(name).is_raw_guess() {
163+
writer.write_str("r#")?;
164+
}
165+
if self.disambiguator == 0 || !verbose {
166+
writer.write_str(&name.as_str())
164167
} else {
165-
write!(f, "{}#{}", name, self.disambiguator)
168+
write!(writer, "{}#{}", name, self.disambiguator)
166169
}
167170
}
168171
DefPathDataName::Anon { namespace } => {
169-
write!(f, "{{{}#{}}}", namespace, self.disambiguator)
172+
write!(writer, "{{{}#{}}}", namespace, self.disambiguator)
170173
}
171174
}
172175
}
173176
}
174177

178+
impl fmt::Display for DisambiguatedDefPathData {
179+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180+
self.fmt_maybe_verbose(f, true)
181+
}
182+
}
183+
175184
#[derive(Clone, Debug, Encodable, Decodable)]
176185
pub struct DefPath {
177186
/// The path leading from the crate root to the item.
@@ -419,6 +428,7 @@ impl Definitions {
419428
}
420429
}
421430

431+
#[derive(Copy, Clone, PartialEq, Debug)]
422432
pub enum DefPathDataName {
423433
Named(Symbol),
424434
Anon { namespace: Symbol },
@@ -434,7 +444,7 @@ impl DefPathData {
434444
}
435445
}
436446

437-
pub fn get_name(&self) -> DefPathDataName {
447+
pub fn name(&self) -> DefPathDataName {
438448
use self::DefPathData::*;
439449
match *self {
440450
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => {
@@ -454,7 +464,7 @@ impl DefPathData {
454464

455465
impl fmt::Display for DefPathData {
456466
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
457-
match self.get_name() {
467+
match self.name() {
458468
DefPathDataName::Named(name) => f.write_str(&name.as_str()),
459469
DefPathDataName::Anon { namespace } => write!(f, "{{{{{}}}}}", namespace),
460470
}

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,27 +1496,16 @@ impl<F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
14961496
return Ok(self);
14971497
}
14981498

1499-
let name = match disambiguated_data.data.get_name() {
1500-
DefPathDataName::Named(name) => name,
1501-
DefPathDataName::Anon { namespace } => namespace,
1502-
};
1503-
15041499
// FIXME(eddyb) `name` should never be empty, but it
15051500
// currently is for `extern { ... }` "foreign modules".
1506-
if name != kw::Invalid {
1501+
let name = disambiguated_data.data.get_name();
1502+
if name != DefPathDataName::Named(kw::Invalid) {
15071503
if !self.empty_path {
15081504
write!(self, "::")?;
15091505
}
1510-
if Ident::with_dummy_span(name).is_raw_guess() {
1511-
write!(self, "r#")?;
1512-
}
15131506

1514-
match disambiguated_data.data.get_name() {
1515-
DefPathDataName::Named(name) => self.write_str(&name.as_str())?,
1516-
DefPathDataName::Anon { namespace } => {
1517-
write!(self, "{{{}#{}}}", namespace, disambiguated_data.disambiguator)?
1518-
}
1519-
}
1507+
let verbose = self.tcx.sess.verbose();
1508+
disambiguated_data.fmt_maybe_verbose(&mut self, verbose)?;
15201509

15211510
self.empty_path = false;
15221511
}

0 commit comments

Comments
 (0)