about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authormarmeladema <xademax@gmail.com>2020-08-31 23:26:15 +0100
committermarmeladema <xademax@gmail.com>2020-09-25 22:46:15 +0100
commit2708ad8bb441fb500bbd2486779c215ffd57bcd2 (patch)
tree25b502c1ef735d9c9671f8135306afa90944a04f /compiler
parent9f50c49117684c2806c54e94ee6aff7d11ca724f (diff)
downloadrust-2708ad8bb441fb500bbd2486779c215ffd57bcd2.tar.gz
rust-2708ad8bb441fb500bbd2486779c215ffd57bcd2.zip
Fix pretty-printing of `DisambiguatedDefPathData`
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_hir/src/definitions.rs28
-rw-r--r--compiler/rustc_middle/src/ty/print/pretty.rs19
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<F: fmt::Write> 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;
         }