Skip to content

Commit 19070a4

Browse files
committed
Adapt rustdoc to the overhauled filename handling
1 parent 77f5542 commit 19070a4

File tree

8 files changed

+40
-41
lines changed

8 files changed

+40
-41
lines changed

src/librustdoc/clean/types.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_resolve::rustdoc::{
2525
use rustc_session::Session;
2626
use rustc_span::hygiene::MacroKind;
2727
use rustc_span::symbol::{Symbol, kw, sym};
28-
use rustc_span::{DUMMY_SP, FileName, Loc};
28+
use rustc_span::{DUMMY_SP, FileName, Loc, RemapPathScopeComponents};
2929
use tracing::{debug, trace};
3030
use {rustc_ast as ast, rustc_hir as hir};
3131

@@ -148,10 +148,17 @@ impl ExternalCrate {
148148

149149
pub(crate) fn src_root(&self, tcx: TyCtxt<'_>) -> PathBuf {
150150
match self.src(tcx) {
151-
FileName::Real(ref p) => match p.local_path_if_available().parent() {
152-
Some(p) => p.to_path_buf(),
153-
None => PathBuf::new(),
154-
},
151+
FileName::Real(ref p) => {
152+
match p
153+
.local_path()
154+
.or(Some(p.path(RemapPathScopeComponents::MACRO)))
155+
.unwrap()
156+
.parent()
157+
{
158+
Some(p) => p.to_path_buf(),
159+
None => PathBuf::new(),
160+
}
161+
}
155162
_ => PathBuf::new(),
156163
}
157164
}

src/librustdoc/doctest.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use rustc_session::config::{self, CrateType, ErrorOutputType, Input};
2727
use rustc_session::lint;
2828
use rustc_span::edition::Edition;
2929
use rustc_span::symbol::sym;
30-
use rustc_span::{FileName, Span};
30+
use rustc_span::{FileName, RemapPathScopeComponents, Span};
3131
use rustc_target::spec::{Target, TargetTuple};
3232
use tempfile::{Builder as TempFileBuilder, TempDir};
3333
use tracing::debug;
@@ -944,14 +944,7 @@ impl ScrapedDocTest {
944944
}
945945
fn path(&self) -> PathBuf {
946946
match &self.filename {
947-
FileName::Real(path) => {
948-
if let Some(local_path) = path.local_path() {
949-
local_path.to_path_buf()
950-
} else {
951-
// Somehow we got the filename from the metadata of another crate, should never happen
952-
unreachable!("doctest from a different crate");
953-
}
954-
}
947+
FileName::Real(name) => name.path(RemapPathScopeComponents::DIAGNOSTICS).to_path_buf(),
955948
_ => PathBuf::from(r"doctest.rs"),
956949
}
957950
}
@@ -999,7 +992,7 @@ impl CreateRunnableDocTests {
999992
// For example `module/file.rs` would become `module_file_rs`
1000993
let file = scraped_test
1001994
.filename
1002-
.prefer_local()
995+
.prefer_local_unconditionally()
1003996
.to_string_lossy()
1004997
.chars()
1005998
.map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })

src/librustdoc/doctest/markdown.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use std::fs::read_to_string;
44
use std::sync::{Arc, Mutex};
55

66
use rustc_session::config::Input;
7-
use rustc_span::{DUMMY_SP, FileName};
7+
use rustc_span::source_map::FilePathMapping;
8+
use rustc_span::{DUMMY_SP, FileName, RealFileName};
89
use tempfile::tempdir;
910

1011
use super::{
@@ -105,8 +106,12 @@ pub(crate) fn test(input: &Input, options: Options) -> Result<(), String> {
105106
cur_path: vec![],
106107
filename: input
107108
.opt_path()
108-
.map(ToOwned::to_owned)
109-
.map(FileName::from)
109+
.map(|f| {
110+
// We don't have access to a rustc Session so let's just use a dummy
111+
// filepath mapping to create a real filename.
112+
let file_mapping = FilePathMapping::empty();
113+
FileName::Real(file_mapping.to_real_filename(&RealFileName::empty(), f))
114+
})
110115
.unwrap_or(FileName::Custom("input".to_owned())),
111116
};
112117
let codes = ErrorCodes::from(options.unstable_features.is_nightly_build());

src/librustdoc/doctest/rust.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Doctest functionality used only for doctests in `.rs` source files.
22
33
use std::cell::Cell;
4-
use std::env;
54
use std::sync::Arc;
65

76
use rustc_ast_pretty::pprust;
@@ -28,15 +27,6 @@ struct RustCollector {
2827
impl RustCollector {
2928
fn get_filename(&self) -> FileName {
3029
let filename = self.source_map.span_to_filename(self.position);
31-
if let FileName::Real(ref filename) = filename {
32-
let path = filename.remapped_path_if_available();
33-
// Strip the cwd prefix from the path. This will likely exist if
34-
// the path was not remapped.
35-
let path = env::current_dir()
36-
.map(|cur_dir| path.strip_prefix(&cur_dir).unwrap_or(path))
37-
.unwrap_or(path);
38-
return path.to_owned().into();
39-
}
4030
filename
4131
}
4232

src/librustdoc/html/render/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ impl<'tcx> Context<'tcx> {
361361

362362
// We can safely ignore synthetic `SourceFile`s.
363363
let file = match span.filename(self.sess()) {
364-
FileName::Real(ref path) => path.local_path_if_available().to_path_buf(),
364+
FileName::Real(ref path) => path.local_path()?.to_path_buf(),
365365
_ => return None,
366366
};
367367
let file = &file;
@@ -495,7 +495,7 @@ impl<'tcx> Context<'tcx> {
495495
} = options;
496496

497497
let src_root = match krate.src(tcx) {
498-
FileName::Real(ref p) => match p.local_path_if_available().parent() {
498+
FileName::Real(ref p) => match p.local_path().map(|p| p.parent()).flatten() {
499499
Some(p) => p.to_path_buf(),
500500
None => PathBuf::new(),
501501
},

src/librustdoc/html/render/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ use rustc_hir::{ConstStability, Mutability, RustcVersion, StabilityLevel, Stable
6060
use rustc_middle::ty::print::PrintTraitRefExt;
6161
use rustc_middle::ty::{self, TyCtxt};
6262
use rustc_span::symbol::{Symbol, sym};
63-
use rustc_span::{BytePos, DUMMY_SP, FileName, RealFileName};
63+
use rustc_span::{BytePos, DUMMY_SP, FileName};
6464
use tracing::{debug, info};
6565

6666
pub(crate) use self::context::*;
@@ -2772,7 +2772,7 @@ fn render_call_locations<W: fmt::Write>(
27722772
files
27732773
.iter()
27742774
.find(|file| match &file.name {
2775-
FileName::Real(RealFileName::LocalPath(other_path)) => rel_path == other_path,
2775+
FileName::Real(real) => real.local_path().map_or(false, |p| p == rel_path),
27762776
_ => false,
27772777
})
27782778
.map(|file| file.start_pos)

src/librustdoc/html/sources.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
88
use rustc_hir::def_id::LOCAL_CRATE;
99
use rustc_middle::ty::TyCtxt;
1010
use rustc_session::Session;
11-
use rustc_span::{FileName, FileNameDisplayPreference, RealFileName, sym};
11+
use rustc_span::{FileName, RealFileName, RemapPathScopeComponents, sym};
1212
use tracing::info;
1313

1414
use super::render::Context;
@@ -148,7 +148,10 @@ impl DocVisitor<'_> for SourceCollector<'_, '_> {
148148
span,
149149
format!(
150150
"failed to render source code for `{filename}`: {e}",
151-
filename = filename.to_string_lossy(FileNameDisplayPreference::Local),
151+
filename = filename
152+
.path(RemapPathScopeComponents::DIAGNOSTICS)
153+
.to_string_lossy()
154+
.into_owned(),
152155
),
153156
);
154157
false
@@ -224,10 +227,7 @@ impl SourceCollector<'_, '_> {
224227
cur.push(&fname);
225228

226229
let title = format!("{} - source", src_fname.to_string_lossy());
227-
let desc = format!(
228-
"Source of the Rust file `{}`.",
229-
file.to_string_lossy(FileNameDisplayPreference::Remapped)
230-
);
230+
let desc = format!("Source of the Rust file `{}`.", p.to_string_lossy());
231231
let page = layout::Page {
232232
title: &title,
233233
short_title: &src_fname.to_string_lossy(),

src/librustdoc/passes/calculate_doc_coverage.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hir as hir;
77
use rustc_lint::builtin::MISSING_DOCS;
88
use rustc_middle::lint::{LevelAndSource, LintLevelSource};
99
use rustc_session::lint;
10-
use rustc_span::FileName;
10+
use rustc_span::{FileName, RemapPathScopeComponents};
1111
use serde::Serialize;
1212
use tracing::debug;
1313

@@ -124,7 +124,7 @@ impl CoverageCalculator<'_, '_> {
124124
&self
125125
.items
126126
.iter()
127-
.map(|(k, v)| (k.prefer_local().to_string(), v))
127+
.map(|(k, v)| (k.prefer_local_unconditionally().to_string(), v))
128128
.collect::<BTreeMap<String, &ItemCount>>(),
129129
)
130130
.expect("failed to convert JSON data to string")
@@ -167,7 +167,11 @@ impl CoverageCalculator<'_, '_> {
167167
for (file, &count) in &self.items {
168168
if let Some(percentage) = count.percentage() {
169169
print_table_record(
170-
&limit_filename_len(file.prefer_local().to_string_lossy().into()),
170+
&limit_filename_len(
171+
file.display(RemapPathScopeComponents::DIAGNOSTICS)
172+
.to_string_lossy()
173+
.into(),
174+
),
171175
count,
172176
percentage,
173177
count.examples_percentage().unwrap_or(0.),

0 commit comments

Comments
 (0)