Rollup merge of #155357 - Urgau:rustdoc-remap-path-scope, r=GuillaumeGomez

Add `--remap-path-scope` as unstable in rustdoc

This PR adds support for `rustc` `--remap-path-scope` flag in rustdoc as unstable.

`rustc` documentation for the flag is [here](https://doc.rust-lang.org/nightly/rustc/remap-source-paths.html#--remap-path-scope).

I added some complementary tests for `rustdoc`, no need I think to duplicate `rustc` UI tests.
This commit is contained in:
Jacob Pratt
2026-04-16 01:54:07 -04:00
committed by GitHub
26 changed files with 270 additions and 2 deletions
+2 -1
View File
@@ -1320,7 +1320,8 @@ pub fn split_dwarf_path(
}
}
pub(crate) fn parse_remap_path_scope(
// pub for rustdoc
pub fn parse_remap_path_scope(
early_dcx: &EarlyDiagCtxt,
matches: &getopts::Matches,
unstable_opts: &UnstableOptions,
+6
View File
@@ -759,6 +759,12 @@ it permits remapping source path prefixes in all output, including compiler diag
debug information, macro expansions, etc. It takes a value of the form `FROM=TO`
where a path prefix equal to `FROM` is rewritten to the value `TO`.
## `--remap-path-scope`: Scopes to which the source remapping should be done
This flag is the equivalent flag from `rustc` `--remap-path-scope`.
Defines which scopes of paths should be remapped by --remap-path-prefix.
### `documentation` scope
`rustdoc` (and by extension `rustc`) have a special `documentation` remapping scope, it
+7 -1
View File
@@ -15,8 +15,8 @@
use rustc_session::lint::Level;
use rustc_session::search_paths::SearchPath;
use rustc_session::{EarlyDiagCtxt, getopts};
use rustc_span::FileName;
use rustc_span::edition::Edition;
use rustc_span::{FileName, RemapPathScopeComponents};
use rustc_target::spec::TargetTuple;
use crate::core::new_dcx;
@@ -140,6 +140,8 @@ pub(crate) struct Options {
pub(crate) no_run: bool,
/// What sources are being mapped.
pub(crate) remap_path_prefix: Vec<(PathBuf, PathBuf)>,
/// Which scope(s) to use with `--remap-path-prefix`
pub(crate) remap_path_scope: RemapPathScopeComponents,
/// The path to a rustc-like binary to build tests with. If not set, we
/// default to loading from `$sysroot/bin/rustc`.
@@ -222,6 +224,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
.field("no_run", &self.no_run)
.field("test_builder_wrappers", &self.test_builder_wrappers)
.field("remap-file-prefix", &self.remap_path_prefix)
.field("remap-file-scope", &self.remap_path_scope)
.field("no_capture", &self.no_capture)
.field("scrape_examples_options", &self.scrape_examples_options)
.field("unstable_features", &self.unstable_features)
@@ -423,6 +426,8 @@ pub(crate) fn from_matches(
early_dcx.early_fatal(err);
}
};
let remap_path_scope =
rustc_session::config::parse_remap_path_scope(early_dcx, matches, &unstable_opts);
let dcx = new_dcx(error_format, None, diagnostic_width, &unstable_opts);
let dcx = dcx.handle();
@@ -889,6 +894,7 @@ fn println_condition(condition: Condition) {
no_run,
test_builder_wrappers,
remap_path_prefix,
remap_path_scope,
no_capture,
crate_name,
output_format,
+2
View File
@@ -211,6 +211,7 @@ pub(crate) fn create_config(
lint_cap,
scrape_examples_options,
remap_path_prefix,
remap_path_scope,
target_modifiers,
..
}: RustdocOptions,
@@ -270,6 +271,7 @@ pub(crate) fn create_config(
crate_name,
test,
remap_path_prefix,
remap_path_scope,
output_types: if let Some(file) = render_options.dep_info() {
OutputTypes::new(&[(OutputType::DepInfo, file.cloned())])
} else {
+1
View File
@@ -171,6 +171,7 @@ pub(crate) fn run(dcx: DiagCtxtHandle<'_>, input: Input, options: RustdocOptions
target_triple: options.target.clone(),
crate_name: options.crate_name.clone(),
remap_path_prefix: options.remap_path_prefix.clone(),
remap_path_scope: options.remap_path_scope.clone(),
unstable_opts: options.unstable_opts.clone(),
error_format: options.error_format.clone(),
target_modifiers: options.target_modifiers.clone(),
+8
View File
@@ -556,6 +556,14 @@ fn opts() -> Vec<RustcOptGroup> {
"Remap source names in compiler messages",
"FROM=TO",
),
opt(
Unstable,
Opt,
"",
"remap-path-scope",
"Defines which scopes of paths should be remapped by `--remap-path-prefix`",
"[macro,diagnostics,debuginfo,coverage,object,all]",
),
opt(
Unstable,
FlagMulti,
@@ -160,6 +160,9 @@ Options:
rustdoc will emit a hard error.
--remap-path-prefix FROM=TO
Remap source names in compiler messages
--remap-path-scope [macro,diagnostics,debuginfo,coverage,object,all]
Defines which scopes of paths should be remapped by
`--remap-path-prefix`
--show-type-layout
Include the memory layout of types in the docs
--no-capture Don't capture stdout and stderr of tests
@@ -0,0 +1,22 @@
// This test checks the output of remapping with `--remap-path-prefix` and
// `--remap-path-scope` with a doctest.
//@ failure-status: 101
//@ rustc-env:RUST_BACKTRACE=0
//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME"
//@ revisions: with-diag-scope with-macro-scope with-object-scope with-doc-scope
//@ revisions: without-scope
//@ compile-flags:--test --test-args --test-threads=1
//@ compile-flags:-Z unstable-options --remap-path-prefix={{src-base}}=remapped_path
//@[with-diag-scope] compile-flags: -Zunstable-options --remap-path-scope=diagnostics
//@[with-macro-scope] compile-flags: -Zunstable-options --remap-path-scope=macro
//@[with-object-scope] compile-flags: -Zunstable-options --remap-path-scope=debuginfo
//@[with-doc-scope] compile-flags: -Zunstable-options --remap-path-scope=documentation
/// ```
/// fn invalid(
/// ```
pub struct SomeStruct;
@@ -0,0 +1,24 @@
running 1 test
test $DIR/remap-path-prefix-doctest.rs - SomeStruct (line 19) ... FAILED
failures:
---- $DIR/remap-path-prefix-doctest.rs - SomeStruct (line 19) stdout ----
error: this file contains an unclosed delimiter
--> $DIR/remap-path-prefix-doctest.rs:20:12
|
LL | fn invalid(
| -^
| |
| unclosed delimiter
error: aborting due to 1 previous error
Couldn't compile the test.
failures:
$DIR/remap-path-prefix-doctest.rs - SomeStruct (line 19)
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
@@ -0,0 +1,24 @@
running 1 test
test remapped_path/remap-path-prefix-doctest.rs - SomeStruct (line 19) ... FAILED
failures:
---- remapped_path/remap-path-prefix-doctest.rs - SomeStruct (line 19) stdout ----
error: this file contains an unclosed delimiter
--> remapped_path/remap-path-prefix-doctest.rs:20:12
|
LL | fn invalid(
| -^
| |
| unclosed delimiter
error: aborting due to 1 previous error
Couldn't compile the test.
failures:
remapped_path/remap-path-prefix-doctest.rs - SomeStruct (line 19)
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
@@ -0,0 +1,24 @@
running 1 test
test $DIR/remap-path-prefix-doctest.rs - SomeStruct (line 19) ... FAILED
failures:
---- $DIR/remap-path-prefix-doctest.rs - SomeStruct (line 19) stdout ----
error: this file contains an unclosed delimiter
--> $DIR/remap-path-prefix-doctest.rs:20:12
|
LL | fn invalid(
| -^
| |
| unclosed delimiter
error: aborting due to 1 previous error
Couldn't compile the test.
failures:
$DIR/remap-path-prefix-doctest.rs - SomeStruct (line 19)
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
@@ -0,0 +1,24 @@
running 1 test
test $DIR/remap-path-prefix-doctest.rs - SomeStruct (line 19) ... FAILED
failures:
---- $DIR/remap-path-prefix-doctest.rs - SomeStruct (line 19) stdout ----
error: this file contains an unclosed delimiter
--> $DIR/remap-path-prefix-doctest.rs:20:12
|
LL | fn invalid(
| -^
| |
| unclosed delimiter
error: aborting due to 1 previous error
Couldn't compile the test.
failures:
$DIR/remap-path-prefix-doctest.rs - SomeStruct (line 19)
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
@@ -0,0 +1,24 @@
running 1 test
test remapped_path/remap-path-prefix-doctest.rs - SomeStruct (line 19) ... FAILED
failures:
---- remapped_path/remap-path-prefix-doctest.rs - SomeStruct (line 19) stdout ----
error: this file contains an unclosed delimiter
--> remapped_path/remap-path-prefix-doctest.rs:20:12
|
LL | fn invalid(
| -^
| |
| unclosed delimiter
error: aborting due to 1 previous error
Couldn't compile the test.
failures:
remapped_path/remap-path-prefix-doctest.rs - SomeStruct (line 19)
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
+28
View File
@@ -0,0 +1,28 @@
// This test exercises `--remap-path-prefix` and `--remap-path-scope` with macros,
// like file!() and a diagnostic with compile_error!().
//
// See the compiler test suite for a more advanced tests, we just want to
// make sure here that rustdoc passes the right scopes to the underline rustc APIs.
//@ revisions: with-diag-scope with-macro-scope with-debuginfo-scope with-doc-scope
//@ revisions: without-scopes without-remap
//@[with-diag-scope] compile-flags: -Zunstable-options --remap-path-prefix={{src-base}}=remapped
//@[with-macro-scope] compile-flags: -Zunstable-options --remap-path-prefix={{src-base}}=remapped
//@[with-debuginfo-scope] compile-flags: -Zunstable-options --remap-path-prefix={{src-base}}=remapped
//@[with-doc-scope] compile-flags: -Zunstable-options --remap-path-prefix={{src-base}}=remapped
//@[without-scopes] compile-flags: -Zunstable-options --remap-path-prefix={{src-base}}=remapped
//@[with-diag-scope] compile-flags: -Zunstable-options --remap-path-scope=diagnostics
//@[with-macro-scope] compile-flags: -Zunstable-options --remap-path-scope=macro
//@[with-debuginfo-scope] compile-flags: -Zunstable-options --remap-path-scope=debuginfo
//@[with-doc-scope] compile-flags: -Zunstable-options --remap-path-scope=documentation
compile_error!(concat!("file!() = ", file!()));
//[with-macro-scope]~^ ERROR file!()
//[with-debuginfo-scope]~^^ ERROR file!()
//[with-doc-scope]~^^^ ERROR file!()
//[without-remap]~^^^^ ERROR file!()
//[with-diag-scope]~? ERROR file!()
//[without-scopes]~? ERROR file!()
@@ -0,0 +1,8 @@
error: file!() = $DIR/remap-path-prefix.rs
--> $DIR/remap-path-prefix.rs:21:1
|
LL | compile_error!(concat!("file!() = ", file!()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error
@@ -0,0 +1,8 @@
error: file!() = $DIR/remap-path-prefix.rs
--> remapped/remap-path-prefix.rs:21:1
|
LL | compile_error!(concat!("file!() = ", file!()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error
@@ -0,0 +1,8 @@
error: file!() = $DIR/remap-path-prefix.rs
--> $DIR/remap-path-prefix.rs:21:1
|
LL | compile_error!(concat!("file!() = ", file!()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error
@@ -0,0 +1,8 @@
error: file!() = remapped/remap-path-prefix.rs
--> $DIR/remap-path-prefix.rs:21:1
|
LL | compile_error!(concat!("file!() = ", file!()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error
@@ -0,0 +1,8 @@
error: file!() = $DIR/remap-path-prefix.rs
--> $DIR/remap-path-prefix.rs:21:1
|
LL | compile_error!(concat!("file!() = ", file!()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error
@@ -0,0 +1,8 @@
error: file!() = remapped/remap-path-prefix.rs
--> remapped/remap-path-prefix.rs:21:1
|
LL | compile_error!(concat!("file!() = ", file!()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error
@@ -0,0 +1,2 @@
error: argument for `--remap-path-scope` must be a comma separated list of scopes: `macro`, `diagnostics`, `documentation`, `debuginfo`, `coverage`, `object`, `all`
@@ -0,0 +1,10 @@
// Error on invalid --remap-path-scope arguments
//@ revisions: foo underscore
//@ compile-flags: -Zunstable-options
//@ [foo]compile-flags: --remap-path-scope=foo
//@ [underscore]compile-flags: --remap-path-scope=macro_object
//~? RAW argument for `--remap-path-scope
fn main() {}
@@ -0,0 +1,2 @@
error: argument for `--remap-path-scope` must be a comma separated list of scopes: `macro`, `diagnostics`, `documentation`, `debuginfo`, `coverage`, `object`, `all`
@@ -0,0 +1,7 @@
// Regression test to make sure `--remap-path-scope` is unstable in rustdoc
//@ compile-flags:--remap-path-scope macro
//~? RAW the `-Z unstable-options` flag must also be passed to enable the flag `remap-path-scope`
fn main() {}
@@ -0,0 +1,2 @@
error: the `-Z unstable-options` flag must also be passed to enable the flag `remap-path-scope`