1
Fork 0

Fix pretty-printing of DisambiguatedDefPathData

This commit is contained in:
marmeladema 2020-08-31 23:26:15 +01:00
parent 9f50c49117
commit 2708ad8bb4
2 changed files with 23 additions and 24 deletions

View file

@ -13,7 +13,7 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::StableHasher; use rustc_data_structures::stable_hasher::StableHasher;
use rustc_index::vec::IndexVec; use rustc_index::vec::IndexVec;
use rustc_span::hygiene::ExpnId; 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::fmt::{self, Write};
use std::hash::Hash; use std::hash::Hash;
@ -155,23 +155,32 @@ pub struct DisambiguatedDefPathData {
pub disambiguator: u32, pub disambiguator: u32,
} }
impl fmt::Display for DisambiguatedDefPathData { impl DisambiguatedDefPathData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { pub fn fmt_maybe_verbose(&self, writer: &mut impl Write, verbose: bool) -> fmt::Result {
match self.data.get_name() { match self.data.get_name() {
DefPathDataName::Named(name) => { DefPathDataName::Named(name) => {
if self.disambiguator == 0 { if Ident::with_dummy_span(name).is_raw_guess() {
f.write_str(&name.as_str()) writer.write_str("r#")?;
}
if self.disambiguator == 0 || !verbose {
writer.write_str(&name.as_str())
} else { } else {
write!(f, "{}#{}", name, self.disambiguator) write!(writer, "{}#{}", name, self.disambiguator)
} }
} }
DefPathDataName::Anon { namespace } => { 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)] #[derive(Clone, Debug, Encodable, Decodable)]
pub struct DefPath { pub struct DefPath {
/// The path leading from the crate root to the item. /// The path leading from the crate root to the item.
@ -419,6 +428,7 @@ impl Definitions {
} }
} }
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum DefPathDataName { pub enum DefPathDataName {
Named(Symbol), Named(Symbol),
Anon { namespace: Symbol }, Anon { namespace: Symbol },
@ -434,7 +444,7 @@ impl DefPathData {
} }
} }
pub fn get_name(&self) -> DefPathDataName { pub fn name(&self) -> DefPathDataName {
use self::DefPathData::*; use self::DefPathData::*;
match *self { match *self {
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => { TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => {
@ -454,7 +464,7 @@ impl DefPathData {
impl fmt::Display for DefPathData { impl fmt::Display for DefPathData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 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::Named(name) => f.write_str(&name.as_str()),
DefPathDataName::Anon { namespace } => write!(f, "{{{{{}}}}}", namespace), DefPathDataName::Anon { namespace } => write!(f, "{{{{{}}}}}", namespace),
} }

View file

@ -1496,27 +1496,16 @@ impl<F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
return Ok(self); 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 // FIXME(eddyb) `name` should never be empty, but it
// currently is for `extern { ... }` "foreign modules". // 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 { if !self.empty_path {
write!(self, "::")?; write!(self, "::")?;
} }
if Ident::with_dummy_span(name).is_raw_guess() {
write!(self, "r#")?;
}
match disambiguated_data.data.get_name() { let verbose = self.tcx.sess.verbose();
DefPathDataName::Named(name) => self.write_str(&name.as_str())?, disambiguated_data.fmt_maybe_verbose(&mut self, verbose)?;
DefPathDataName::Anon { namespace } => {
write!(self, "{{{}#{}}}", namespace, disambiguated_data.disambiguator)?
}
}
self.empty_path = false; self.empty_path = false;
} }