Skip to content

Commit f8e55da

Browse files
committed
Remove impl Display for FileName and add FileNameDisplay wrapper type
1 parent ec34cd9 commit f8e55da

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

compiler/rustc_span/src/lib.rs

+39-12
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,23 @@ impl RealFileName {
202202
| RealFileName::Remapped { local_path: _, virtual_name: p } => &p,
203203
}
204204
}
205+
206+
fn to_string_lossy(&self, prefer_local: bool) -> Cow<'_, str> {
207+
use RealFileName::*;
208+
if prefer_local {
209+
match self {
210+
LocalPath(path)
211+
| Remapped { local_path: None, virtual_name: path }
212+
| Remapped { local_path: Some(path), virtual_name: _ } => path.to_string_lossy(),
213+
}
214+
} else {
215+
match self {
216+
LocalPath(path) | Remapped { local_path: _, virtual_name: path } => {
217+
path.to_string_lossy()
218+
}
219+
}
220+
}
221+
}
205222
}
206223

207224
/// Differentiates between real files and common virtual files.
@@ -228,16 +245,24 @@ pub enum FileName {
228245
InlineAsm(u64),
229246
}
230247

231-
impl std::fmt::Display for FileName {
248+
impl From<PathBuf> for FileName {
249+
fn from(p: PathBuf) -> Self {
250+
assert!(!p.to_string_lossy().ends_with('>'));
251+
FileName::Real(RealFileName::LocalPath(p))
252+
}
253+
}
254+
255+
pub struct FileNameDisplay<'a> {
256+
inner: &'a FileName,
257+
prefer_local: bool,
258+
}
259+
260+
impl fmt::Display for FileNameDisplay<'_> {
232261
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
233262
use FileName::*;
234-
match *self {
235-
Real(RealFileName::Named(ref path)) => write!(fmt, "{}", path.display()),
236-
// FIXME: might be nice to display both components of Devirtualized.
237-
// But for now (to backport fix for issue #70924), best to not
238-
// perturb diagnostics so its obvious test suite still works.
239-
Real(RealFileName::Devirtualized { ref local_path, virtual_name: _ }) => {
240-
write!(fmt, "{}", local_path.display())
263+
match *self.inner {
264+
Real(ref name) => {
265+
write!(fmt, "{}", name.to_string_lossy(self.prefer_local))
241266
}
242267
QuoteExpansion(_) => write!(fmt, "<quote expansion>"),
243268
MacroExpansion(_) => write!(fmt, "<macro expansion>"),
@@ -252,10 +277,12 @@ impl std::fmt::Display for FileName {
252277
}
253278
}
254279

255-
impl From<PathBuf> for FileName {
256-
fn from(p: PathBuf) -> Self {
257-
assert!(!p.to_string_lossy().ends_with('>'));
258-
FileName::Real(RealFileName::LocalPath(p))
280+
impl FileNameDisplay<'_> {
281+
pub fn to_string_lossy(&self) -> Cow<'_, str> {
282+
match self.inner {
283+
FileName::Real(ref inner) => inner.to_string_lossy(self.prefer_local),
284+
_ => Cow::from(format!("{}", self)),
285+
}
259286
}
260287
}
261288

src/librustdoc/passes/calculate_doc_coverage.rs

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

0 commit comments

Comments
 (0)