Skip to content

Commit 218d96e

Browse files
authored
Rollup merge of #70563 - GuillaumeGomez:page-hash-handling, r=ollie27,kinnison
[rustdoc] Page hash handling Fixes #70476 A good example to see the change is to use this URL: https://doc.rust-lang.org/nightly/std/string/struct.String.html#from_iter.v-3 After the change, it actually goes to the target element (and change the page hash to something more clear for the users). r? @kinnison cc @ollie27
2 parents 0c03aee + e955beb commit 218d96e

21 files changed

+48
-164
lines changed

src/bootstrap/bin/rustdoc.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ fn main() {
1313
let libdir = env::var_os("RUSTDOC_LIBDIR").expect("RUSTDOC_LIBDIR was not set");
1414
let stage = env::var("RUSTC_STAGE").expect("RUSTC_STAGE was not set");
1515
let sysroot = env::var_os("RUSTC_SYSROOT").expect("RUSTC_SYSROOT was not set");
16-
let mut has_unstable = false;
1716

1817
use std::str::FromStr;
1918

@@ -55,22 +54,10 @@ fn main() {
5554
cmd.arg("--crate-version").arg(version);
5655
}
5756

58-
// Needed to be able to run all rustdoc tests.
59-
if env::var_os("RUSTDOC_GENERATE_REDIRECT_PAGES").is_some() {
60-
// This "unstable-options" can be removed when `--generate-redirect-pages` is stabilized
61-
if !has_unstable {
62-
cmd.arg("-Z").arg("unstable-options");
63-
}
64-
cmd.arg("--generate-redirect-pages");
65-
has_unstable = true;
66-
}
67-
6857
// Needed to be able to run all rustdoc tests.
6958
if let Some(ref x) = env::var_os("RUSTDOC_RESOURCE_SUFFIX") {
7059
// This "unstable-options" can be removed when `--resource-suffix` is stabilized
71-
if !has_unstable {
72-
cmd.arg("-Z").arg("unstable-options");
73-
}
60+
cmd.arg("-Z").arg("unstable-options");
7461
cmd.arg("--resource-suffix").arg(x);
7562
}
7663

src/bootstrap/doc.rs

-1
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,6 @@ impl Step for Std {
451451
.arg("--markdown-css")
452452
.arg("rust.css")
453453
.arg("--markdown-no-toc")
454-
.arg("--generate-redirect-pages")
455454
.arg("-Z")
456455
.arg("unstable-options")
457456
.arg("--resource-suffix")

src/librustdoc/config.rs

-4
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,6 @@ pub struct RenderOptions {
242242
/// If false, the `select` element to have search filtering by crates on rendered docs
243243
/// won't be generated.
244244
pub generate_search_filter: bool,
245-
/// Option (disabled by default) to generate files used by RLS and some other tools.
246-
pub generate_redirect_pages: bool,
247245
/// Document items that have lower than `pub` visibility.
248246
pub document_private: bool,
249247
/// Document items that have `doc(hidden)`.
@@ -528,7 +526,6 @@ impl Options {
528526
let static_root_path = matches.opt_str("static-root-path");
529527
let generate_search_filter = !matches.opt_present("disable-per-crate-search");
530528
let persist_doctests = matches.opt_str("persist-doctests").map(PathBuf::from);
531-
let generate_redirect_pages = matches.opt_present("generate-redirect-pages");
532529
let test_builder = matches.opt_str("test-builder").map(PathBuf::from);
533530
let codegen_options_strs = matches.opt_strs("C");
534531
let debugging_options_strs = matches.opt_strs("Z");
@@ -592,7 +589,6 @@ impl Options {
592589
markdown_css,
593590
markdown_playground_url,
594591
generate_search_filter,
595-
generate_redirect_pages,
596592
document_private,
597593
document_hidden,
598594
},

src/librustdoc/html/item_type.rs

-37
Original file line numberDiff line numberDiff line change
@@ -154,47 +154,10 @@ impl ItemType {
154154
ItemType::TraitAlias => "traitalias",
155155
}
156156
}
157-
158-
pub fn name_space(&self) -> &'static str {
159-
match *self {
160-
ItemType::Struct
161-
| ItemType::Union
162-
| ItemType::Enum
163-
| ItemType::Module
164-
| ItemType::Typedef
165-
| ItemType::Trait
166-
| ItemType::Primitive
167-
| ItemType::AssocType
168-
| ItemType::OpaqueTy
169-
| ItemType::TraitAlias
170-
| ItemType::ForeignType => NAMESPACE_TYPE,
171-
172-
ItemType::ExternCrate
173-
| ItemType::Import
174-
| ItemType::Function
175-
| ItemType::Static
176-
| ItemType::Impl
177-
| ItemType::TyMethod
178-
| ItemType::Method
179-
| ItemType::StructField
180-
| ItemType::Variant
181-
| ItemType::Constant
182-
| ItemType::AssocConst => NAMESPACE_VALUE,
183-
184-
ItemType::Macro | ItemType::ProcAttribute | ItemType::ProcDerive => NAMESPACE_MACRO,
185-
186-
ItemType::Keyword => NAMESPACE_KEYWORD,
187-
}
188-
}
189157
}
190158

191159
impl fmt::Display for ItemType {
192160
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
193161
write!(f, "{}", self.as_str())
194162
}
195163
}
196-
197-
pub const NAMESPACE_TYPE: &str = "t";
198-
pub const NAMESPACE_VALUE: &str = "v";
199-
pub const NAMESPACE_MACRO: &str = "m";
200-
pub const NAMESPACE_KEYWORD: &str = "k";

src/librustdoc/html/render.rs

+9-50
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,6 @@ crate struct SharedContext {
195195
/// Optional path string to be used to load static files on output pages. If not set, uses
196196
/// combinations of `../` to reach the documentation root.
197197
pub static_root_path: Option<String>,
198-
/// Option disabled by default to generate files used by RLS and some other tools.
199-
pub generate_redirect_pages: bool,
200198
/// The fs handle we are working with.
201199
pub fs: DocFS,
202200
/// The default edition used to parse doctests.
@@ -468,7 +466,6 @@ pub fn run(
468466
resource_suffix,
469467
static_root_path,
470468
generate_search_filter,
471-
generate_redirect_pages,
472469
document_private,
473470
..
474471
} = options;
@@ -536,7 +533,6 @@ pub fn run(
536533
themes,
537534
resource_suffix,
538535
static_root_path,
539-
generate_redirect_pages,
540536
fs: DocFS::new(&errors),
541537
edition,
542538
codes: ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build()),
@@ -1556,14 +1552,6 @@ impl Context {
15561552
if !self.render_redirect_pages {
15571553
all.append(full_path(self, &item), &item_type);
15581554
}
1559-
if self.shared.generate_redirect_pages {
1560-
// Redirect from a sane URL using the namespace to Rustdoc's
1561-
// URL for the page.
1562-
let redir_name = format!("{}.{}.html", name, item_type.name_space());
1563-
let redir_dst = self.dst.join(redir_name);
1564-
let v = layout::redirect(file_name);
1565-
self.shared.fs.write(&redir_dst, v.as_bytes())?;
1566-
}
15671555
// If the item is a macro, redirect from the old macro URL (with !)
15681556
// to the new one (without).
15691557
if item_type == ItemType::Macro {
@@ -2586,8 +2574,7 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait)
25862574
let name = m.name.as_ref().unwrap();
25872575
let item_type = m.type_();
25882576
let id = cx.derive_id(format!("{}.{}", item_type, name));
2589-
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
2590-
write!(w, "<h3 id='{id}' class='method'><code id='{ns_id}'>", id = id, ns_id = ns_id);
2577+
write!(w, "<h3 id='{id}' class='method'><code>", id = id);
25912578
render_assoc_item(w, m, AssocItemLink::Anchor(Some(&id)), ItemType::Impl);
25922579
write!(w, "</code>");
25932580
render_stability_since(w, m, t);
@@ -2972,20 +2959,14 @@ fn item_struct(w: &mut Buffer, cx: &Context, it: &clean::Item, s: &clean::Struct
29722959
ItemType::StructField,
29732960
field.name.as_ref().unwrap()
29742961
));
2975-
let ns_id = cx.derive_id(format!(
2976-
"{}.{}",
2977-
field.name.as_ref().unwrap(),
2978-
ItemType::StructField.name_space()
2979-
));
29802962
write!(
29812963
w,
29822964
"<span id=\"{id}\" class=\"{item_type} small-section-header\">\
29832965
<a href=\"#{id}\" class=\"anchor field\"></a>\
2984-
<code id=\"{ns_id}\">{name}: {ty}</code>\
2966+
<code>{name}: {ty}</code>\
29852967
</span>",
29862968
item_type = ItemType::StructField,
29872969
id = id,
2988-
ns_id = ns_id,
29892970
name = field.name.as_ref().unwrap(),
29902971
ty = ty.print()
29912972
);
@@ -3103,18 +3084,12 @@ fn item_enum(w: &mut Buffer, cx: &Context, it: &clean::Item, e: &clean::Enum) {
31033084
for variant in &e.variants {
31043085
let id =
31053086
cx.derive_id(format!("{}.{}", ItemType::Variant, variant.name.as_ref().unwrap()));
3106-
let ns_id = cx.derive_id(format!(
3107-
"{}.{}",
3108-
variant.name.as_ref().unwrap(),
3109-
ItemType::Variant.name_space()
3110-
));
31113087
write!(
31123088
w,
31133089
"<div id=\"{id}\" class=\"variant small-section-header\">\
3114-
<a href=\"#{id}\" class=\"anchor field\"></a>\
3115-
<code id='{ns_id}'>{name}",
3090+
<a href=\"#{id}\" class=\"anchor field\"></a>\
3091+
<code>{name}",
31163092
id = id,
3117-
ns_id = ns_id,
31183093
name = variant.name.as_ref().unwrap()
31193094
);
31203095
if let clean::VariantItem(ref var) = variant.inner {
@@ -3155,21 +3130,13 @@ fn item_enum(w: &mut Buffer, cx: &Context, it: &clean::Item, e: &clean::Enum) {
31553130
variant.name.as_ref().unwrap(),
31563131
field.name.as_ref().unwrap()
31573132
));
3158-
let ns_id = cx.derive_id(format!(
3159-
"{}.{}.{}.{}",
3160-
variant.name.as_ref().unwrap(),
3161-
ItemType::Variant.name_space(),
3162-
field.name.as_ref().unwrap(),
3163-
ItemType::StructField.name_space()
3164-
));
31653133
write!(
31663134
w,
31673135
"<span id=\"{id}\" class=\"variant small-section-header\">\
31683136
<a href=\"#{id}\" class=\"anchor field\"></a>\
3169-
<code id='{ns_id}'>{f}:&nbsp;{t}\
3137+
<code>{f}:&nbsp;{t}\
31703138
</code></span>",
31713139
id = id,
3172-
ns_id = ns_id,
31733140
f = field.name.as_ref().unwrap(),
31743141
t = ty.print()
31753142
);
@@ -3661,9 +3628,7 @@ fn render_impl(
36613628
// Only render when the method is not static or we allow static methods
36623629
if render_method_item {
36633630
let id = cx.derive_id(format!("{}.{}", item_type, name));
3664-
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
3665-
write!(w, "<h4 id='{}' class=\"{}{}\">", id, item_type, extra_class);
3666-
write!(w, "<code id='{}'>", ns_id);
3631+
write!(w, "<h4 id='{}' class=\"{}{}\"><code>", id, item_type, extra_class);
36673632
render_assoc_item(w, item, link.anchor(&id), ItemType::Impl);
36683633
write!(w, "</code>");
36693634
render_stability_since_raw(w, item.stable_since(), outer_version);
@@ -3679,17 +3644,13 @@ fn render_impl(
36793644
}
36803645
clean::TypedefItem(ref tydef, _) => {
36813646
let id = cx.derive_id(format!("{}.{}", ItemType::AssocType, name));
3682-
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
3683-
write!(w, "<h4 id='{}' class=\"{}{}\">", id, item_type, extra_class);
3684-
write!(w, "<code id='{}'>", ns_id);
3647+
write!(w, "<h4 id='{}' class=\"{}{}\"><code>", id, item_type, extra_class);
36853648
assoc_type(w, item, &Vec::new(), Some(&tydef.type_), link.anchor(&id), "");
36863649
write!(w, "</code></h4>");
36873650
}
36883651
clean::AssocConstItem(ref ty, ref default) => {
36893652
let id = cx.derive_id(format!("{}.{}", item_type, name));
3690-
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
3691-
write!(w, "<h4 id='{}' class=\"{}{}\">", id, item_type, extra_class);
3692-
write!(w, "<code id='{}'>", ns_id);
3653+
write!(w, "<h4 id='{}' class=\"{}{}\"><code>", id, item_type, extra_class);
36933654
assoc_const(w, item, ty, default.as_ref(), link.anchor(&id), "");
36943655
write!(w, "</code>");
36953656
render_stability_since_raw(w, item.stable_since(), outer_version);
@@ -3704,9 +3665,7 @@ fn render_impl(
37043665
}
37053666
clean::AssocTypeItem(ref bounds, ref default) => {
37063667
let id = cx.derive_id(format!("{}.{}", item_type, name));
3707-
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
3708-
write!(w, "<h4 id='{}' class=\"{}{}\">", id, item_type, extra_class);
3709-
write!(w, "<code id='{}'>", ns_id);
3668+
write!(w, "<h4 id='{}' class=\"{}{}\"><code>", id, item_type, extra_class);
37103669
assoc_type(w, item, bounds, default.as_ref(), link.anchor(&id), "");
37113670
write!(w, "</code></h4>");
37123671
}

src/librustdoc/html/static/main.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,11 @@ function defocusSearchBar() {
112112
}
113113

114114
function getPageId() {
115-
var id = document.location.href.split("#")[1];
116-
if (id) {
117-
return id.split("?")[0].split("&")[0];
115+
if (window.location.hash) {
116+
var tmp = window.location.hash.replace(/^#/, "");
117+
if (tmp.length > 0) {
118+
return tmp;
119+
}
118120
}
119121
return null;
120122
}
@@ -2551,6 +2553,13 @@ function defocusSearchBar() {
25512553

25522554
onEachLazy(document.getElementsByClassName("docblock"), buildToggleWrapper);
25532555
onEachLazy(document.getElementsByClassName("sub-variant"), buildToggleWrapper);
2556+
var pageId = getPageId();
2557+
2558+
autoCollapse(pageId, getCurrentValue("rustdoc-collapse") === "true");
2559+
2560+
if (pageId !== null) {
2561+
expandSection(pageId);
2562+
}
25542563
}());
25552564

25562565
function createToggleWrapper(tog) {
@@ -2686,12 +2695,6 @@ function defocusSearchBar() {
26862695
hideSidebar();
26872696
};
26882697

2689-
autoCollapse(getPageId(), getCurrentValue("rustdoc-collapse") === "true");
2690-
2691-
if (window.location.hash && window.location.hash.length > 0) {
2692-
expandSection(window.location.hash.replace(/^#/, ""));
2693-
}
2694-
26952698
if (main) {
26962699
onEachLazy(main.getElementsByClassName("loading-content"), function(e) {
26972700
e.remove();

src/librustdoc/lib.rs

-7
Original file line numberDiff line numberDiff line change
@@ -374,13 +374,6 @@ fn opts() -> Vec<RustcOptGroup> {
374374
"PATH",
375375
)
376376
}),
377-
unstable("generate-redirect-pages", |o| {
378-
o.optflag(
379-
"",
380-
"generate-redirect-pages",
381-
"Generate extra pages to support legacy URLs and tool links",
382-
)
383-
}),
384377
unstable("show-coverage", |o| {
385378
o.optflag(
386379
"",

src/librustdoc/passes/collect_intra_doc_links.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,7 @@ fn handle_variant(
10691069
};
10701070
let parent_def = Res::Def(DefKind::Enum, parent);
10711071
let variant = cx.tcx.expect_variant_res(res);
1072-
Ok((parent_def, Some(format!("{}.v", variant.ident.name))))
1072+
Ok((parent_def, Some(format!("variant.{}", variant.ident.name))))
10731073
}
10741074

10751075
const PRIMITIVES: &[(&str, Res)] = &[

src/libstd/sys/unix/ext/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub trait FileExt {
133133
/// Note that similar to [`File::write`], it is not an error to return a
134134
/// short write.
135135
///
136-
/// [`File::write`]: ../../../../std/fs/struct.File.html#write.v
136+
/// [`File::write`]: ../../../../std/fs/struct.File.html#method.write
137137
///
138138
/// # Examples
139139
///

src/libstd/sys/vxworks/ext/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ pub trait FileExt {
132132
/// Note that similar to [`File::write`], it is not an error to return a
133133
/// short write.
134134
///
135-
/// [`File::write`]: ../../../../std/fs/struct.File.html#write.v
135+
/// [`File::write`]: ../../../../std/fs/struct.File.html#method.write
136136
///
137137
/// # Examples
138138
///

src/test/rustdoc/assoc-types.rs

-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
// @has assoc_types/trait.Index.html
66
pub trait Index<I: ?Sized> {
77
// @has - '//*[@id="associatedtype.Output"]//code' 'type Output: ?Sized'
8-
// @has - '//code[@id="Output.t"]' 'type Output: ?Sized'
98
type Output: ?Sized;
10-
// @has - '//code[@id="index.v"]' 'fn index'
119
// @has - '//*[@id="tymethod.index"]//code' \
1210
// "fn index<'a>(&'a self, index: I) -> &'a Self::Output"
1311
// @has - '//*[@id="tymethod.index"]//code//a[@href="../assoc_types/trait.Index.html#associatedtype.Output"]' \

src/test/rustdoc/const.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
pub struct Foo;
44

55
impl Foo {
6-
// @has const/struct.Foo.html '//code[@id="new.v"]' 'const unsafe fn new'
6+
// @has const/struct.Foo.html '//*[@id="method.new"]//code' 'const unsafe fn new'
77
pub const unsafe fn new() -> Foo {
88
Foo
99
}

src/test/rustdoc/inline_cross/impl_trait.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ pub use impl_trait_aux::func4;
3131
pub use impl_trait_aux::async_fn;
3232

3333
// @has impl_trait/struct.Foo.html
34-
// @has - '//code[@id="method.v"]' "pub fn method<'a>(_x: impl Clone + Into<Vec<u8>> + 'a)"
35-
// @!has - '//code[@id="method.v"]' 'where'
34+
// @has - '//*[@id="method.method"]//code' "pub fn method<'a>(_x: impl Clone + Into<Vec<u8>> + 'a)"
35+
// @!has - '//*[@id="method.method"]//code' 'where'
3636
pub use impl_trait_aux::Foo;
3737

3838
// @has impl_trait/struct.Bar.html

src/test/rustdoc/intra-link-self.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub struct MyStruct {
4040
}
4141

4242
pub enum MyEnum {
43-
// @has foo/enum.MyEnum.html '//a/@href' '../foo/enum.MyEnum.html#EnumVariant.v'
43+
// @has foo/enum.MyEnum.html '//a/@href' '../foo/enum.MyEnum.html#variant.EnumVariant'
4444

4545
/// [`EnumVariant`]
4646
///

0 commit comments

Comments
 (0)