Remove impl Display for FileName and add FileNameDisplay wrapper type

This commit is contained in:
Andy Wang 2021-04-20 01:11:12 +01:00
parent ec34cd94dd
commit f8e55da6de
No known key found for this signature in database
GPG key ID: 181B49F9F38F3374
2 changed files with 40 additions and 13 deletions

View file

@ -202,6 +202,23 @@ impl RealFileName {
| RealFileName::Remapped { local_path: _, virtual_name: p } => &p, | RealFileName::Remapped { local_path: _, virtual_name: p } => &p,
} }
} }
fn to_string_lossy(&self, prefer_local: bool) -> Cow<'_, str> {
use RealFileName::*;
if prefer_local {
match self {
LocalPath(path)
| Remapped { local_path: None, virtual_name: path }
| Remapped { local_path: Some(path), virtual_name: _ } => path.to_string_lossy(),
}
} else {
match self {
LocalPath(path) | Remapped { local_path: _, virtual_name: path } => {
path.to_string_lossy()
}
}
}
}
} }
/// Differentiates between real files and common virtual files. /// Differentiates between real files and common virtual files.
@ -228,16 +245,24 @@ pub enum FileName {
InlineAsm(u64), InlineAsm(u64),
} }
impl std::fmt::Display for FileName { impl From<PathBuf> for FileName {
fn from(p: PathBuf) -> Self {
assert!(!p.to_string_lossy().ends_with('>'));
FileName::Real(RealFileName::LocalPath(p))
}
}
pub struct FileNameDisplay<'a> {
inner: &'a FileName,
prefer_local: bool,
}
impl fmt::Display for FileNameDisplay<'_> {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use FileName::*; use FileName::*;
match *self { match *self.inner {
Real(RealFileName::Named(ref path)) => write!(fmt, "{}", path.display()), Real(ref name) => {
// FIXME: might be nice to display both components of Devirtualized. write!(fmt, "{}", name.to_string_lossy(self.prefer_local))
// But for now (to backport fix for issue #70924), best to not
// perturb diagnostics so its obvious test suite still works.
Real(RealFileName::Devirtualized { ref local_path, virtual_name: _ }) => {
write!(fmt, "{}", local_path.display())
} }
QuoteExpansion(_) => write!(fmt, "<quote expansion>"), QuoteExpansion(_) => write!(fmt, "<quote expansion>"),
MacroExpansion(_) => write!(fmt, "<macro expansion>"), MacroExpansion(_) => write!(fmt, "<macro expansion>"),
@ -252,10 +277,12 @@ impl std::fmt::Display for FileName {
} }
} }
impl From<PathBuf> for FileName { impl FileNameDisplay<'_> {
fn from(p: PathBuf) -> Self { pub fn to_string_lossy(&self) -> Cow<'_, str> {
assert!(!p.to_string_lossy().ends_with('>')); match self.inner {
FileName::Real(RealFileName::LocalPath(p)) FileName::Real(ref inner) => inner.to_string_lossy(self.prefer_local),
_ => Cow::from(format!("{}", self)),
}
} }
} }

View file

@ -219,7 +219,7 @@ impl<'a, 'b> fold::DocFolder for CoverageCalculator<'a, 'b> {
// unless the user had an explicit `allow` // unless the user had an explicit `allow`
let should_have_docs = let should_have_docs =
level != lint::Level::Allow || matches!(source, LintLevelSource::Default); level != lint::Level::Allow || matches!(source, LintLevelSource::Default);
debug!("counting {:?} {:?} in {}", i.type_(), i.name, filename); debug!("counting {:?} {:?} in {:?}", i.type_(), i.name, filename);
self.items.entry(filename).or_default().count_item( self.items.entry(filename).or_default().count_item(
has_docs, has_docs,
has_doc_example, has_doc_example,