Skip to content

Commit 00c04f9

Browse files
authored
Unrolled build for #151589
Rollup merge of #151589 - Urgau:documentation-scope, r=GuillaumeGomez Add a `documentation` remapping path scope for rustdoc usage This PR adds a new remapping path scope for rustdoc usage: `documentation`, instead of rustdoc abusing the other scopes for it's usage. Like remapping paths in rustdoc, this scope is unstable. (rustdoc doesn't even have yet an equivalent to [rustc `--remap-path-scope`](https://doc.rust-lang.org/nightly/rustc/remap-source-paths.html#--remap-path-scope)). I also took the opportunity to add a bit of documentation in rustdoc book.
2 parents fb292b7 + dd84a4f commit 00c04f9

23 files changed

Lines changed: 184 additions & 22 deletions

compiler/rustc_session/src/config.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,18 +1322,26 @@ impl OutputFilenames {
13221322
pub(crate) fn parse_remap_path_scope(
13231323
early_dcx: &EarlyDiagCtxt,
13241324
matches: &getopts::Matches,
1325+
unstable_opts: &UnstableOptions,
13251326
) -> RemapPathScopeComponents {
13261327
if let Some(v) = matches.opt_str("remap-path-scope") {
13271328
let mut slot = RemapPathScopeComponents::empty();
13281329
for s in v.split(',') {
13291330
slot |= match s {
13301331
"macro" => RemapPathScopeComponents::MACRO,
13311332
"diagnostics" => RemapPathScopeComponents::DIAGNOSTICS,
1333+
"documentation" => {
1334+
if !unstable_opts.unstable_options {
1335+
early_dcx.early_fatal("remapping `documentation` path scope requested but `-Zunstable-options` not specified");
1336+
}
1337+
1338+
RemapPathScopeComponents::DOCUMENTATION
1339+
},
13321340
"debuginfo" => RemapPathScopeComponents::DEBUGINFO,
13331341
"coverage" => RemapPathScopeComponents::COVERAGE,
13341342
"object" => RemapPathScopeComponents::OBJECT,
13351343
"all" => RemapPathScopeComponents::all(),
1336-
_ => early_dcx.early_fatal("argument for `--remap-path-scope` must be a comma separated list of scopes: `macro`, `diagnostics`, `debuginfo`, `coverage`, `object`, `all`"),
1344+
_ => early_dcx.early_fatal("argument for `--remap-path-scope` must be a comma separated list of scopes: `macro`, `diagnostics`, `documentation`, `debuginfo`, `coverage`, `object`, `all`"),
13371345
}
13381346
}
13391347
slot
@@ -2677,7 +2685,7 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26772685
let externs = parse_externs(early_dcx, matches, &unstable_opts);
26782686

26792687
let remap_path_prefix = parse_remap_path_prefix(early_dcx, matches, &unstable_opts);
2680-
let remap_path_scope = parse_remap_path_scope(early_dcx, matches);
2688+
let remap_path_scope = parse_remap_path_scope(early_dcx, matches, &unstable_opts);
26812689

26822690
let pretty = parse_pretty(early_dcx, &unstable_opts);
26832691

compiler/rustc_span/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ bitflags::bitflags! {
233233
const DEBUGINFO = 1 << 3;
234234
/// Apply remappings to coverage information
235235
const COVERAGE = 1 << 4;
236+
/// Apply remappings to documentation information
237+
const DOCUMENTATION = 1 << 5;
236238

237239
/// An alias for `macro`, `debuginfo` and `coverage`. This ensures all paths in compiled
238240
/// executables, libraries and objects are remapped but not elsewhere.

src/doc/rustc/src/remap-source-paths.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ The valid scopes are:
5252
- `debuginfo` - apply remappings to debug information
5353
- `coverage` - apply remappings to coverage information
5454
- `object` - apply remappings to all paths in compiled executables or libraries, but not elsewhere. Currently an alias for `macro,coverage,debuginfo`.
55-
- `all` (default) - an alias for all of the above, also equivalent to supplying only `--remap-path-prefix` without `--remap-path-scope`.
55+
- `all` (default) - an alias for all of the above (and unstable scopes), also equivalent to supplying only `--remap-path-prefix` without `--remap-path-scope`.
5656

5757
The scopes accepted by `--remap-path-scope` are not exhaustive - new scopes may be added in future releases for eventual stabilisation.
5858
This implies that the `all` scope can correspond to different scopes between releases.

src/doc/rustdoc/src/unstable-features.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,22 @@ pass `--doctest-build-arg ARG` for each argument `ARG`.
751751

752752
This flag enables the generation of toggles to expand macros in the HTML source code pages.
753753

754+
## `--remap-path-prefix`: Remap source code paths in output
755+
756+
This flag is the equivalent flag from `rustc` `--remap-path-prefix`.
757+
758+
it permits remapping source path prefixes in all output, including compiler diagnostics,
759+
debug information, macro expansions, etc. It takes a value of the form `FROM=TO`
760+
where a path prefix equal to `FROM` is rewritten to the value `TO`.
761+
762+
### `documentation` scope
763+
764+
`rustdoc` (and by extension `rustc`) have a special `documentation` remapping scope, it
765+
permits remapping source paths that ends up in the generated documentation.
766+
767+
Currently the scope can only be specified from `rustc`, due to the lack of an equivalent
768+
`--remap-path-scope` flag in `rustc`.
769+
754770
## `#[doc(cfg)]` and `#[doc(auto_cfg)]`
755771

756772
This feature aims at providing rustdoc users the possibility to add visual markers to the rendered documentation to know under which conditions an item is available (currently possible through the following unstable feature: `doc_cfg`).

src/librustdoc/clean/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl ExternalCrate {
152152
FileName::Real(ref p) => {
153153
match p
154154
.local_path()
155-
.or(Some(p.path(RemapPathScopeComponents::MACRO)))
155+
.or(Some(p.path(RemapPathScopeComponents::DOCUMENTATION)))
156156
.unwrap()
157157
.parent()
158158
{

src/librustdoc/doctest.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -957,8 +957,10 @@ impl ScrapedDocTest {
957957
if !item_path.is_empty() {
958958
item_path.push(' ');
959959
}
960-
let name =
961-
format!("{} - {item_path}(line {line})", filename.prefer_remapped_unconditionally());
960+
let name = format!(
961+
"{} - {item_path}(line {line})",
962+
filename.display(RemapPathScopeComponents::DOCUMENTATION)
963+
);
962964

963965
Self { filename, line, langstr, text, name, span, global_crate_attrs }
964966
}
@@ -969,9 +971,12 @@ impl ScrapedDocTest {
969971
fn no_run(&self, opts: &RustdocOptions) -> bool {
970972
self.langstr.no_run || opts.no_run
971973
}
974+
972975
fn path(&self) -> PathBuf {
973976
match &self.filename {
974-
FileName::Real(name) => name.path(RemapPathScopeComponents::DIAGNOSTICS).to_path_buf(),
977+
FileName::Real(name) => {
978+
name.path(RemapPathScopeComponents::DOCUMENTATION).to_path_buf()
979+
}
975980
_ => PathBuf::from(r"doctest.rs"),
976981
}
977982
}
@@ -1016,9 +1021,12 @@ impl CreateRunnableDocTests {
10161021

10171022
fn add_test(&mut self, scraped_test: ScrapedDocTest, dcx: Option<DiagCtxtHandle<'_>>) {
10181023
// For example `module/file.rs` would become `module_file_rs`
1024+
//
1025+
// Note that we are kind-of extending the definition of the MACRO scope here, but
1026+
// after all `#[doc]` is kind-of a macro.
10191027
let file = scraped_test
10201028
.filename
1021-
.prefer_local_unconditionally()
1029+
.display(RemapPathScopeComponents::MACRO)
10221030
.to_string_lossy()
10231031
.chars()
10241032
.map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })

src/librustdoc/doctest/extracted.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! This module contains the logic to extract doctests and output a JSON containing this
44
//! information.
55
6+
use rustc_span::RemapPathScopeComponents;
67
use rustc_span::edition::Edition;
78
use serde::Serialize;
89

@@ -62,7 +63,7 @@ impl ExtractedDocTests {
6263
Some(&opts.crate_name),
6364
);
6465
self.doctests.push(ExtractedDocTest {
65-
file: filename.prefer_remapped_unconditionally().to_string(),
66+
file: filename.display(RemapPathScopeComponents::DOCUMENTATION).to_string(),
6667
line,
6768
doctest_attributes: langstr.into(),
6869
doctest_code: match wrapped {

src/librustdoc/html/render/context.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ impl<'tcx> Context<'tcx> {
367367
let file = match span.filename(self.sess()) {
368368
FileName::Real(ref path) => path
369369
.local_path()
370-
.unwrap_or(path.path(RemapPathScopeComponents::MACRO))
370+
.unwrap_or(path.path(RemapPathScopeComponents::DOCUMENTATION))
371371
.to_path_buf(),
372372
_ => return None,
373373
};
@@ -503,7 +503,11 @@ impl<'tcx> Context<'tcx> {
503503

504504
let src_root = match krate.src(tcx) {
505505
FileName::Real(ref p) => {
506-
match p.local_path().unwrap_or(p.path(RemapPathScopeComponents::MACRO)).parent() {
506+
match p
507+
.local_path()
508+
.unwrap_or(p.path(RemapPathScopeComponents::DOCUMENTATION))
509+
.parent()
510+
{
507511
Some(p) => p.to_path_buf(),
508512
None => PathBuf::new(),
509513
}

src/librustdoc/passes/calculate_doc_coverage.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl CoverageCalculator<'_, '_> {
124124
&self
125125
.items
126126
.iter()
127-
.map(|(k, v)| (k.prefer_local_unconditionally().to_string(), v))
127+
.map(|(k, v)| (k.display(RemapPathScopeComponents::COVERAGE).to_string(), v))
128128
.collect::<BTreeMap<String, &ItemCount>>(),
129129
)
130130
.expect("failed to convert JSON data to string")
@@ -168,9 +168,7 @@ impl CoverageCalculator<'_, '_> {
168168
if let Some(percentage) = count.percentage() {
169169
print_table_record(
170170
&limit_filename_len(
171-
file.display(RemapPathScopeComponents::DIAGNOSTICS)
172-
.to_string_lossy()
173-
.into(),
171+
file.display(RemapPathScopeComponents::COVERAGE).to_string(),
174172
),
175173
count,
176174
percentage,

tests/run-make/remap-path-prefix/rmake.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ fn main() {
1212
let mut out_simple = rustc();
1313
let mut out_object = rustc();
1414
let mut out_macro = rustc();
15+
let mut out_doc = rustc();
1516
let mut out_diagobj = rustc();
17+
let mut out_diagdocobj = rustc();
1618
out_simple
1719
.remap_path_prefix("auxiliary", "/the/aux")
1820
.crate_type("lib")
@@ -28,23 +30,39 @@ fn main() {
2830
.crate_type("lib")
2931
.emit("metadata")
3032
.input("auxiliary/lib.rs");
33+
out_doc
34+
.remap_path_prefix("auxiliary", "/the/aux")
35+
.crate_type("lib")
36+
.emit("metadata")
37+
.input("auxiliary/lib.rs");
3138
out_diagobj
3239
.remap_path_prefix("auxiliary", "/the/aux")
3340
.crate_type("lib")
3441
.emit("metadata")
3542
.input("auxiliary/lib.rs");
43+
out_diagdocobj
44+
.remap_path_prefix("auxiliary", "/the/aux")
45+
.crate_type("lib")
46+
.emit("metadata")
47+
.input("auxiliary/lib.rs");
3648

3749
out_simple.run();
3850
rmeta_contains("/the/aux/lib.rs");
3951
rmeta_not_contains("auxiliary");
4052

4153
out_object.arg("--remap-path-scope=object");
4254
out_macro.arg("--remap-path-scope=macro");
55+
out_doc.arg("--remap-path-scope=documentation").arg("-Zunstable-options");
4356
out_diagobj.arg("--remap-path-scope=diagnostics,object");
57+
out_diagdocobj
58+
.arg("--remap-path-scope=diagnostics,documentation,object")
59+
.arg("-Zunstable-options");
4460
if is_darwin() {
4561
out_object.arg("-Csplit-debuginfo=off");
4662
out_macro.arg("-Csplit-debuginfo=off");
63+
out_doc.arg("-Csplit-debuginfo=off");
4764
out_diagobj.arg("-Csplit-debuginfo=off");
65+
out_diagdocobj.arg("-Csplit-debuginfo=off");
4866
}
4967

5068
out_object.run();
@@ -53,8 +71,14 @@ fn main() {
5371
out_macro.run();
5472
rmeta_contains("/the/aux/lib.rs");
5573
rmeta_contains("auxiliary");
74+
out_doc.run();
75+
rmeta_contains("/the/aux/lib.rs");
76+
rmeta_contains("auxiliary");
5677
out_diagobj.run();
5778
rmeta_contains("/the/aux/lib.rs");
79+
rmeta_contains("auxiliary");
80+
out_diagdocobj.run();
81+
rmeta_contains("/the/aux/lib.rs");
5882
rmeta_not_contains("auxiliary");
5983
}
6084

0 commit comments

Comments
 (0)