Make rustdoc use the documentation remapping scope and cleanups

This commit is contained in:
Urgau 2026-01-22 22:43:27 +01:00
parent 91e3e2a37f
commit fbab2c0e92
5 changed files with 23 additions and 12 deletions

View file

@ -152,7 +152,7 @@ impl ExternalCrate {
FileName::Real(ref p) => {
match p
.local_path()
.or(Some(p.path(RemapPathScopeComponents::MACRO)))
.or(Some(p.path(RemapPathScopeComponents::DOCUMENTATION)))
.unwrap()
.parent()
{

View file

@ -957,8 +957,10 @@ impl ScrapedDocTest {
if !item_path.is_empty() {
item_path.push(' ');
}
let name =
format!("{} - {item_path}(line {line})", filename.prefer_remapped_unconditionally());
let name = format!(
"{} - {item_path}(line {line})",
filename.display(RemapPathScopeComponents::DOCUMENTATION)
);
Self { filename, line, langstr, text, name, span, global_crate_attrs }
}
@ -969,9 +971,12 @@ impl ScrapedDocTest {
fn no_run(&self, opts: &RustdocOptions) -> bool {
self.langstr.no_run || opts.no_run
}
fn path(&self) -> PathBuf {
match &self.filename {
FileName::Real(name) => name.path(RemapPathScopeComponents::DIAGNOSTICS).to_path_buf(),
FileName::Real(name) => {
name.path(RemapPathScopeComponents::DOCUMENTATION).to_path_buf()
}
_ => PathBuf::from(r"doctest.rs"),
}
}
@ -1016,9 +1021,12 @@ impl CreateRunnableDocTests {
fn add_test(&mut self, scraped_test: ScrapedDocTest, dcx: Option<DiagCtxtHandle<'_>>) {
// For example `module/file.rs` would become `module_file_rs`
//
// Note that we are kind-of extending the definition of the MACRO scope here, but
// after all `#[doc]` is kind-of a macro.
let file = scraped_test
.filename
.prefer_local_unconditionally()
.display(RemapPathScopeComponents::MACRO)
.to_string_lossy()
.chars()
.map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })

View file

@ -3,6 +3,7 @@
//! This module contains the logic to extract doctests and output a JSON containing this
//! information.
use rustc_span::RemapPathScopeComponents;
use rustc_span::edition::Edition;
use serde::Serialize;
@ -62,7 +63,7 @@ impl ExtractedDocTests {
Some(&opts.crate_name),
);
self.doctests.push(ExtractedDocTest {
file: filename.prefer_remapped_unconditionally().to_string(),
file: filename.display(RemapPathScopeComponents::DOCUMENTATION).to_string(),
line,
doctest_attributes: langstr.into(),
doctest_code: match wrapped {

View file

@ -367,7 +367,7 @@ impl<'tcx> Context<'tcx> {
let file = match span.filename(self.sess()) {
FileName::Real(ref path) => path
.local_path()
.unwrap_or(path.path(RemapPathScopeComponents::MACRO))
.unwrap_or(path.path(RemapPathScopeComponents::DOCUMENTATION))
.to_path_buf(),
_ => return None,
};
@ -503,7 +503,11 @@ impl<'tcx> Context<'tcx> {
let src_root = match krate.src(tcx) {
FileName::Real(ref p) => {
match p.local_path().unwrap_or(p.path(RemapPathScopeComponents::MACRO)).parent() {
match p
.local_path()
.unwrap_or(p.path(RemapPathScopeComponents::DOCUMENTATION))
.parent()
{
Some(p) => p.to_path_buf(),
None => PathBuf::new(),
}

View file

@ -124,7 +124,7 @@ impl CoverageCalculator<'_, '_> {
&self
.items
.iter()
.map(|(k, v)| (k.prefer_local_unconditionally().to_string(), v))
.map(|(k, v)| (k.display(RemapPathScopeComponents::COVERAGE).to_string(), v))
.collect::<BTreeMap<String, &ItemCount>>(),
)
.expect("failed to convert JSON data to string")
@ -168,9 +168,7 @@ impl CoverageCalculator<'_, '_> {
if let Some(percentage) = count.percentage() {
print_table_record(
&limit_filename_len(
file.display(RemapPathScopeComponents::DIAGNOSTICS)
.to_string_lossy()
.into(),
file.display(RemapPathScopeComponents::COVERAGE).to_string(),
),
count,
percentage,