Skip to content

Commit 5530869

Browse files
authored
Rollup merge of rust-lang#133804 - GuillaumeGomez:improve-code, r=notriddle
Improve code for FileName retrieval in rustdoc Some calls were performed twice (first in `is_real_and_local` and then in the function calling it). Also the `FileName` was matched on a few times too. r? `@notriddle`
2 parents 9fd0972 + 2997ec5 commit 5530869

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

src/librustdoc/html/sources.rs

+22-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 filename_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,8 @@ 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) = filename_real_and_local(span, sess).and_then(|file| file.into_local_path())
70+
else {
7371
return;
7472
};
7573
if self.local_sources.contains_key(&*p) {
@@ -135,8 +133,7 @@ impl DocVisitor<'_> for SourceCollector<'_, '_> {
135133
// If we're not rendering sources, there's nothing to do.
136134
// If we're including source files, and we haven't seen this file yet,
137135
// then we need to render it out to the filesystem.
138-
if is_real_and_local(span, sess) {
139-
let filename = span.filename(sess);
136+
if let Some(filename) = filename_real_and_local(span, sess) {
140137
let span = span.inner();
141138
let pos = sess.source_map().lookup_source_file(span.lo());
142139
let file_span = span.with_lo(pos.start_pos).with_hi(pos.end_position());
@@ -152,7 +149,7 @@ impl DocVisitor<'_> for SourceCollector<'_, '_> {
152149
span,
153150
format!(
154151
"failed to render source code for `{filename}`: {e}",
155-
filename = filename.prefer_local(),
152+
filename = filename.to_string_lossy(FileNameDisplayPreference::Local),
156153
),
157154
);
158155
false
@@ -168,18 +165,13 @@ impl SourceCollector<'_, '_> {
168165
/// Renders the given filename into its corresponding HTML source file.
169166
fn emit_source(
170167
&mut self,
171-
filename: &FileName,
168+
file: &RealFileName,
172169
file_span: rustc_span::Span,
173170
) -> 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(()),
171+
let p = if let Some(local_path) = file.local_path() {
172+
local_path.to_path_buf()
173+
} else {
174+
unreachable!("only the current crate should have sources emitted");
183175
};
184176
if self.emitted_local_sources.contains(&*p) {
185177
// We've already emitted this source
@@ -233,8 +225,10 @@ impl SourceCollector<'_, '_> {
233225
cur.push(&fname);
234226

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

0 commit comments

Comments
 (0)