Skip to content

Commit 270151b

Browse files
Add generate-old-style-files option to rustdoc
1 parent 1ac7277 commit 270151b

File tree

10 files changed

+77
-25
lines changed

10 files changed

+77
-25
lines changed

src/bootstrap/doc.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,8 @@ fn invoke_rustdoc(builder: &Builder, compiler: Compiler, target: Interned<String
317317
.arg("-o").arg(&out)
318318
.arg(&path)
319319
.arg("--markdown-css")
320-
.arg("../rust.css");
320+
.arg("../rust.css")
321+
.arg("--generate-redirect-pages");
321322

322323
builder.run(&mut cmd);
323324
}
@@ -491,6 +492,7 @@ impl Step for Std {
491492
cargo.arg("--")
492493
.arg("--markdown-css").arg("rust.css")
493494
.arg("--markdown-no-toc")
495+
.arg("--generate-redirect-pages")
494496
.arg("--index-page").arg(&builder.src.join("src/doc/index.md"));
495497

496498
builder.run(&mut cargo);

src/librustdoc/config.rs

+4
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ pub struct RenderOptions {
192192
/// If false, the `select` element to have search filtering by crates on rendered docs
193193
/// won't be generated.
194194
pub generate_search_filter: bool,
195+
/// Option (disabled by default) to generate files used by RLS and some other tools.
196+
pub generate_redirect_pages: bool,
195197
}
196198

197199
impl Options {
@@ -436,6 +438,7 @@ impl Options {
436438
let static_root_path = matches.opt_str("static-root-path");
437439
let generate_search_filter = !matches.opt_present("disable-per-crate-search");
438440
let persist_doctests = matches.opt_str("persist-doctests").map(PathBuf::from);
441+
let generate_redirect_pages = matches.opt_present("generate-redirect-pages");
439442

440443
let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format);
441444

@@ -480,6 +483,7 @@ impl Options {
480483
markdown_css,
481484
markdown_playground_url,
482485
generate_search_filter,
486+
generate_redirect_pages,
483487
}
484488
})
485489
}

src/librustdoc/html/render.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use std::default::Default;
3333
use std::error;
3434
use std::fmt::{self, Display, Formatter, Write as FmtWrite};
3535
use std::ffi::OsStr;
36-
use std::fs::{self, File};
36+
use std::fs::{self, File, OpenOptions};
3737
use std::io::prelude::*;
3838
use std::io::{self, BufWriter, BufReader};
3939
use std::mem;
@@ -136,6 +136,8 @@ struct SharedContext {
136136
/// If false, the `select` element to have search filtering by crates on rendered docs
137137
/// won't be generated.
138138
pub generate_search_filter: bool,
139+
/// Option disabled by default to generate files used by RLS and some other tools.
140+
pub generate_redirect_pages: bool,
139141
}
140142

141143
impl SharedContext {
@@ -504,6 +506,7 @@ pub fn run(mut krate: clean::Crate,
504506
resource_suffix,
505507
static_root_path,
506508
generate_search_filter,
509+
generate_redirect_pages,
507510
..
508511
} = options;
509512

@@ -533,6 +536,7 @@ pub fn run(mut krate: clean::Crate,
533536
resource_suffix,
534537
static_root_path,
535538
generate_search_filter,
539+
generate_redirect_pages,
536540
};
537541

538542
// If user passed in `--playground-url` arg, we fill in crate name here
@@ -2229,6 +2233,27 @@ impl Context {
22292233
if !self.render_redirect_pages {
22302234
all.append(full_path(self, &item), &item_type);
22312235
}
2236+
if self.shared.generate_redirect_pages {
2237+
// Redirect from a sane URL using the namespace to Rustdoc's
2238+
// URL for the page.
2239+
let redir_name = format!("{}.{}.html", name, item_type.name_space());
2240+
let redir_dst = self.dst.join(redir_name);
2241+
if let Ok(redirect_out) = OpenOptions::new().create_new(true)
2242+
.write(true)
2243+
.open(&redir_dst) {
2244+
let mut redirect_out = BufWriter::new(redirect_out);
2245+
try_err!(layout::redirect(&mut redirect_out, file_name), &redir_dst);
2246+
}
2247+
// If the item is a macro, redirect from the old macro URL (with !)
2248+
// to the new one (without).
2249+
if item_type == ItemType::Macro {
2250+
let redir_name = format!("{}.{}!.html", item_type, name);
2251+
let redir_dst = self.dst.join(redir_name);
2252+
let redirect_out = try_err!(File::create(&redir_dst), &redir_dst);
2253+
let mut redirect_out = BufWriter::new(redirect_out);
2254+
try_err!(layout::redirect(&mut redirect_out, file_name), &redir_dst);
2255+
}
2256+
}
22322257
}
22332258
}
22342259
Ok(())

src/librustdoc/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,11 @@ fn opts() -> Vec<RustcOptGroup> {
348348
"Directory to persist doctest executables into",
349349
"PATH")
350350
}),
351+
stable("generate-redirect-pages", |o| {
352+
o.optflag("",
353+
"generate-redirect-pages",
354+
"Generate extra pages to support legacy URLs and tool links")
355+
}),
351356
]
352357
}
353358

src/test/rustdoc/issue-19190.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// compile-flags:--generate-redirect-pages
2+
13
use std::ops::Deref;
24

35
pub struct Foo;
@@ -13,6 +15,9 @@ impl Deref for Bar {
1315
fn deref(&self) -> &Foo { loop {} }
1416
}
1517

18+
// @has issue_19190/Bar.t.html
1619
// @has issue_19190/struct.Bar.html
20+
// @has - '//*[@id="foo.v"]' 'fn foo(&self)'
1721
// @has - '//*[@id="method.foo"]' 'fn foo(&self)'
22+
// @!has - '//*[@id="static_foo.v"]' 'fn static_foo()'
1823
// @!has - '//*[@id="method.static_foo"]' 'fn static_foo()'

src/test/rustdoc/issue-35169-2.rs

+7
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@ impl DerefMut for Bar {
2424
}
2525

2626
// @has issue_35169_2/struct.Bar.html
27+
// @has - '//*[@id="by_ref.v"]' 'fn by_ref(&self)'
2728
// @has - '//*[@id="method.by_ref"]' 'fn by_ref(&self)'
29+
// @has - '//*[@id="by_explicit_ref.v"]' 'fn by_explicit_ref(self: &Foo)'
2830
// @has - '//*[@id="method.by_explicit_ref"]' 'fn by_explicit_ref(self: &Foo)'
31+
// @has - '//*[@id="by_mut_ref.v"]' 'fn by_mut_ref(&mut self)'
2932
// @has - '//*[@id="method.by_mut_ref"]' 'fn by_mut_ref(&mut self)'
33+
// @has - '//*[@id="by_explicit_mut_ref.v"]' 'fn by_explicit_mut_ref(self: &mut Foo)'
3034
// @has - '//*[@id="method.by_explicit_mut_ref"]' 'fn by_explicit_mut_ref(self: &mut Foo)'
35+
// @!has - '//*[@id="by_explicit_box.v"]' 'fn by_explicit_box(self: Box<Foo>)'
3136
// @!has - '//*[@id="method.by_explicit_box"]' 'fn by_explicit_box(self: Box<Foo>)'
37+
// @!has - '//*[@id="by_explicit_self_box.v"]' 'fn by_explicit_self_box(self: Box<Self>)'
3238
// @!has - '//*[@id="method.by_explicit_self_box"]' 'fn by_explicit_self_box(self: Box<Self>)'
39+
// @!has - '//*[@id="static_foo.v"]' 'fn static_foo()'
3340
// @!has - '//*[@id="method.static_foo"]' 'fn static_foo()'

src/test/rustdoc/issue-35169.rs

+7
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,17 @@ impl Deref for Bar {
1919
}
2020

2121
// @has issue_35169/struct.Bar.html
22+
// @has - '//*[@id="by_ref.v"]' 'fn by_ref(&self)'
2223
// @has - '//*[@id="method.by_ref"]' 'fn by_ref(&self)'
24+
// @has - '//*[@id="by_explicit_ref.v"]' 'fn by_explicit_ref(self: &Foo)'
2325
// @has - '//*[@id="method.by_explicit_ref"]' 'fn by_explicit_ref(self: &Foo)'
26+
// @!has - '//*[@id="by_mut_ref.v"]' 'fn by_mut_ref(&mut self)'
2427
// @!has - '//*[@id="method.by_mut_ref"]' 'fn by_mut_ref(&mut self)'
28+
// @!has - '//*[@id="by_explicit_mut_ref.v"]' 'fn by_explicit_mut_ref(self: &mut Foo)'
2529
// @!has - '//*[@id="method.by_explicit_mut_ref"]' 'fn by_explicit_mut_ref(self: &mut Foo)'
30+
// @!has - '//*[@id="by_explicit_box.v"]' 'fn by_explicit_box(self: Box<Foo>)'
2631
// @!has - '//*[@id="method.by_explicit_box"]' 'fn by_explicit_box(self: Box<Foo>)'
32+
// @!has - '//*[@id="by_explicit_self_box.v"]' 'fn by_explicit_self_box(self: Box<Self>)'
2733
// @!has - '//*[@id="method.by_explicit_self_box"]' 'fn by_explicit_self_box(self: Box<Self>)'
34+
// @!has - '//*[@id="static_foo.v"]' 'fn static_foo()'
2835
// @!has - '//*[@id="method.static_foo"]' 'fn static_foo()'

src/test/rustdoc/old-style-files.rs

-23
This file was deleted.

src/test/rustdoc/structfields.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
// compile-flags:--generate-redirect-pages
2+
13
// @has structfields/Foo.t.html
24
// @has - struct.Foo.html
5+
// @has structfields/struct.Foo.html
36
pub struct Foo {
47
// @has - //pre "pub a: ()"
58
pub a: (),
@@ -13,13 +16,17 @@ pub struct Foo {
1316
pub d: usize,
1417
}
1518

19+
// @has structfields/Bar.t.html
20+
// @has - struct.Bar.html
1621
// @has structfields/struct.Bar.html
1722
pub struct Bar {
1823
// @has - //pre "pub a: ()"
1924
pub a: (),
2025
// @!has - //pre "// some fields omitted"
2126
}
2227

28+
// @has structfields/Qux.t.html
29+
// @has - enum.Qux.html
2330
// @has structfields/enum.Qux.html
2431
pub enum Qux {
2532
Quz {

src/test/rustdoc/without-redirect.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![crate_name = "foo"]
2+
3+
// @has foo/macro.bar.html
4+
// @!has foo/macro.bar!.html
5+
// @!has foo/bar.m.html
6+
#[macro_export]
7+
macro_rules! bar {
8+
() => {}
9+
}
10+
11+
// @has foo/struct.Bar.html
12+
// @!has foo/Bar.t.html
13+
pub struct Bar;

0 commit comments

Comments
 (0)