From 2708ad8bb441fb500bbd2486779c215ffd57bcd2 Mon Sep 17 00:00:00 2001 From: marmeladema Date: Mon, 31 Aug 2020 23:26:15 +0100 Subject: [PATCH] Fix pretty-printing of `DisambiguatedDefPathData` --- compiler/rustc_hir/src/definitions.rs | 28 +++++++++++++------- compiler/rustc_middle/src/ty/print/pretty.rs | 19 +++---------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/compiler/rustc_hir/src/definitions.rs b/compiler/rustc_hir/src/definitions.rs index 4b803447eed..ae2ce6f176a 100644 --- a/compiler/rustc_hir/src/definitions.rs +++ b/compiler/rustc_hir/src/definitions.rs @@ -13,7 +13,7 @@ use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::stable_hasher::StableHasher; use rustc_index::vec::IndexVec; use rustc_span::hygiene::ExpnId; -use rustc_span::symbol::{kw, sym, Symbol}; +use rustc_span::symbol::{kw, sym, Ident, Symbol}; use std::fmt::{self, Write}; use std::hash::Hash; @@ -155,23 +155,32 @@ pub struct DisambiguatedDefPathData { pub disambiguator: u32, } -impl fmt::Display for DisambiguatedDefPathData { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { +impl DisambiguatedDefPathData { + pub fn fmt_maybe_verbose(&self, writer: &mut impl Write, verbose: bool) -> fmt::Result { match self.data.get_name() { DefPathDataName::Named(name) => { - if self.disambiguator == 0 { - f.write_str(&name.as_str()) + if Ident::with_dummy_span(name).is_raw_guess() { + writer.write_str("r#")?; + } + if self.disambiguator == 0 || !verbose { + writer.write_str(&name.as_str()) } else { - write!(f, "{}#{}", name, self.disambiguator) + write!(writer, "{}#{}", name, self.disambiguator) } } DefPathDataName::Anon { namespace } => { - write!(f, "{{{}#{}}}", namespace, self.disambiguator) + write!(writer, "{{{}#{}}}", namespace, self.disambiguator) } } } } +impl fmt::Display for DisambiguatedDefPathData { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.fmt_maybe_verbose(f, true) + } +} + #[derive(Clone, Debug, Encodable, Decodable)] pub struct DefPath { /// The path leading from the crate root to the item. @@ -419,6 +428,7 @@ impl Definitions { } } +#[derive(Copy, Clone, PartialEq, Debug)] pub enum DefPathDataName { Named(Symbol), Anon { namespace: Symbol }, @@ -434,7 +444,7 @@ impl DefPathData { } } - pub fn get_name(&self) -> DefPathDataName { + pub fn name(&self) -> DefPathDataName { use self::DefPathData::*; match *self { TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => { @@ -454,7 +464,7 @@ impl DefPathData { impl fmt::Display for DefPathData { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self.get_name() { + match self.name() { DefPathDataName::Named(name) => f.write_str(&name.as_str()), DefPathDataName::Anon { namespace } => write!(f, "{{{{{}}}}}", namespace), } diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 91986289099..7ec14d43892 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -1496,27 +1496,16 @@ impl Printer<'tcx> for FmtPrinter<'_, 'tcx, F> { return Ok(self); } - let name = match disambiguated_data.data.get_name() { - DefPathDataName::Named(name) => name, - DefPathDataName::Anon { namespace } => namespace, - }; - // FIXME(eddyb) `name` should never be empty, but it // currently is for `extern { ... }` "foreign modules". - if name != kw::Invalid { + let name = disambiguated_data.data.get_name(); + if name != DefPathDataName::Named(kw::Invalid) { if !self.empty_path { write!(self, "::")?; } - if Ident::with_dummy_span(name).is_raw_guess() { - write!(self, "r#")?; - } - match disambiguated_data.data.get_name() { - DefPathDataName::Named(name) => self.write_str(&name.as_str())?, - DefPathDataName::Anon { namespace } => { - write!(self, "{{{}#{}}}", namespace, disambiguated_data.disambiguator)? - } - } + let verbose = self.tcx.sess.verbose(); + disambiguated_data.fmt_maybe_verbose(&mut self, verbose)?; self.empty_path = false; }