Skip to content

Commit 2e1f25b

Browse files
Improve code for FileName retrieval in rustdoc
1 parent caa8172 commit 2e1f25b

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

Diff for: src/librustdoc/html/sources.rs

+21-28
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
99
use rustc_hir::def_id::LOCAL_CRATE;
1010
use rustc_middle::ty::TyCtxt;
1111
use rustc_session::Session;
12-
use rustc_span::{FileName, sym};
12+
use rustc_span::{FileName, FileNameDisplayPreference, RealFileName, sym};
1313
use tracing::info;
1414

1515
use crate::clean;
@@ -50,8 +50,14 @@ struct LocalSourcesCollector<'a, 'tcx> {
5050
src_root: &'a Path,
5151
}
5252

53-
fn is_real_and_local(span: clean::Span, sess: &Session) -> bool {
54-
span.cnum(sess) == LOCAL_CRATE && span.filename(sess).is_real()
53+
fn is_real_and_local(span: clean::Span, sess: &Session) -> Option<RealFileName> {
54+
if span.cnum(sess) == LOCAL_CRATE
55+
&& let FileName::Real(file) = span.filename(sess)
56+
{
57+
Some(file)
58+
} else {
59+
None
60+
}
5561
}
5662

5763
impl LocalSourcesCollector<'_, '_> {
@@ -60,16 +66,7 @@ impl LocalSourcesCollector<'_, '_> {
6066
let span = item.span(self.tcx);
6167
let Some(span) = span else { return };
6268
// skip all synthetic "files"
63-
if !is_real_and_local(span, sess) {
64-
return;
65-
}
66-
let filename = span.filename(sess);
67-
let p = if let FileName::Real(file) = filename {
68-
match file.into_local_path() {
69-
Some(p) => p,
70-
None => return,
71-
}
72-
} else {
69+
let Some(p) = is_real_and_local(span, sess).and_then(|file| file.into_local_path()) else {
7370
return;
7471
};
7572
if self.local_sources.contains_key(&*p) {
@@ -135,8 +132,7 @@ impl DocVisitor<'_> for SourceCollector<'_, '_> {
135132
// If we're not rendering sources, there's nothing to do.
136133
// If we're including source files, and we haven't seen this file yet,
137134
// then we need to render it out to the filesystem.
138-
if is_real_and_local(span, sess) {
139-
let filename = span.filename(sess);
135+
if let Some(filename) = is_real_and_local(span, sess) {
140136
let span = span.inner();
141137
let pos = sess.source_map().lookup_source_file(span.lo());
142138
let file_span = span.with_lo(pos.start_pos).with_hi(pos.end_position());
@@ -152,7 +148,7 @@ impl DocVisitor<'_> for SourceCollector<'_, '_> {
152148
span,
153149
format!(
154150
"failed to render source code for `{filename}`: {e}",
155-
filename = filename.prefer_local(),
151+
filename = filename.to_string_lossy(FileNameDisplayPreference::Local),
156152
),
157153
);
158154
false
@@ -168,18 +164,13 @@ impl SourceCollector<'_, '_> {
168164
/// Renders the given filename into its corresponding HTML source file.
169165
fn emit_source(
170166
&mut self,
171-
filename: &FileName,
167+
file: &RealFileName,
172168
file_span: rustc_span::Span,
173169
) -> Result<(), Error> {
174-
let p = match *filename {
175-
FileName::Real(ref file) => {
176-
if let Some(local_path) = file.local_path() {
177-
local_path.to_path_buf()
178-
} else {
179-
unreachable!("only the current crate should have sources emitted");
180-
}
181-
}
182-
_ => return Ok(()),
170+
let p = if let Some(local_path) = file.local_path() {
171+
local_path.to_path_buf()
172+
} else {
173+
unreachable!("only the current crate should have sources emitted");
183174
};
184175
if self.emitted_local_sources.contains(&*p) {
185176
// We've already emitted this source
@@ -233,8 +224,10 @@ impl SourceCollector<'_, '_> {
233224
cur.push(&fname);
234225

235226
let title = format!("{} - source", src_fname.to_string_lossy());
236-
let desc =
237-
format!("Source of the Rust file `{}`.", filename.prefer_remapped_unconditionaly());
227+
let desc = format!(
228+
"Source of the Rust file `{}`.",
229+
file.to_string_lossy(FileNameDisplayPreference::Remapped)
230+
);
238231
let page = layout::Page {
239232
title: &title,
240233
css_class: "src",

0 commit comments

Comments
 (0)