Skip to content

Commit f242e29

Browse files
authored
Rollup merge of rust-lang#98973 - GuillaumeGomez:inherent-impl-anchors, r=notriddle
Remove (unused) inherent impl anchors This is something `@notriddle` realized a few days ago: we have unused anchors in the DOM. This PR removes them. You can test it [here](https://rustdoc.crud.net/imperio/inherent-impl-anchors/foo/index.html). r? `@notriddle`
2 parents 71b3fbd + 7967152 commit f242e29

9 files changed

+72
-4
lines changed

src/librustdoc/html/render/mod.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,10 @@ fn render_impl(
14121412
id, item_type, in_trait_class,
14131413
);
14141414
render_rightside(w, cx, item, containing_item, render_mode);
1415-
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
1415+
if trait_.is_some() {
1416+
// Anchors are only used on trait impls.
1417+
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
1418+
}
14161419
w.write_str("<h4 class=\"code-header\">");
14171420
render_assoc_item(
14181421
w,
@@ -1435,7 +1438,10 @@ fn render_impl(
14351438
id, item_type, in_trait_class
14361439
);
14371440
render_rightside(w, cx, item, containing_item, render_mode);
1438-
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
1441+
if trait_.is_some() {
1442+
// Anchors are only used on trait impls.
1443+
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
1444+
}
14391445
w.write_str("<h4 class=\"code-header\">");
14401446
assoc_const(
14411447
w,
@@ -1457,7 +1463,10 @@ fn render_impl(
14571463
let source_id = format!("{}.{}", item_type, name);
14581464
let id = cx.derive_id(source_id.clone());
14591465
write!(w, "<section id=\"{}\" class=\"{}{}\">", id, item_type, in_trait_class);
1460-
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
1466+
if trait_.is_some() {
1467+
// Anchors are only used on trait impls.
1468+
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
1469+
}
14611470
w.write_str("<h4 class=\"code-header\">");
14621471
assoc_type(
14631472
w,
@@ -1480,7 +1489,10 @@ fn render_impl(
14801489
"<section id=\"{}\" class=\"{}{} has-srclink\">",
14811490
id, item_type, in_trait_class
14821491
);
1483-
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
1492+
if trait_.is_some() {
1493+
// Anchors are only used on trait impls.
1494+
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
1495+
}
14841496
w.write_str("<h4 class=\"code-header\">");
14851497
assoc_type(
14861498
w,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div id="associatedconstant.YOLO" class="method has-srclink"><div class="rightside"><a class="srclink" href="../src/foo/anchors.rs.html#16">source</a></div><h4 class="code-header">const <a href="#associatedconstant.YOLO" class="constant">YOLO</a>: <a class="primitive" href="{{channel}}/std/primitive.u32.html">u32</a></h4></div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<section id="associatedconstant.X" class="associatedconstant has-srclink"><span class="rightside"><a class="srclink" href="../src/foo/anchors.rs.html#42">source</a></span><h4 class="code-header">pub const <a href="#associatedconstant.X" class="constant">X</a>: <a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a> = 0i32</h4></section>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<section id="method.new" class="method has-srclink"><span class="rightside"><a class="srclink" href="../src/foo/anchors.rs.html#48">source</a></span><h4 class="code-header">pub fn <a href="#method.new" class="fnname">new</a>() -&gt; Self</h4></section>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div id="method.bar" class="method has-srclink"><div class="rightside"><a class="srclink" href="../src/foo/anchors.rs.html#23">source</a></div><h4 class="code-header">fn <a href="#method.bar" class="fnname">bar</a>()</h4></div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div id="tymethod.foo" class="method has-srclink"><div class="rightside"><a class="srclink" href="../src/foo/anchors.rs.html#20">source</a></div><h4 class="code-header">fn <a href="#tymethod.foo" class="fnname">foo</a>()</h4></div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div id="associatedtype.T" class="method has-srclink"><div class="rightside"><a class="srclink" href="../src/foo/anchors.rs.html#13">source</a></div><h4 class="code-header">type <a href="#associatedtype.T" class="associatedtype">T</a></h4></div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<section id="associatedtype.Y" class="associatedtype has-srclink"><h4 class="code-header">type <a href="#associatedtype.Y" class="associatedtype">Y</a> = <a class="primitive" href="{{channel}}/std/primitive.u32.html">u32</a></h4></section>

src/test/rustdoc/anchors.rs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// This test ensures that anchors are generated in the right places.
2+
3+
#![feature(inherent_associated_types)]
4+
#![allow(incomplete_features)]
5+
#![crate_name = "foo"]
6+
7+
pub struct Foo;
8+
9+
// @has 'foo/trait.Bar.html'
10+
pub trait Bar {
11+
// There should be no anchors here.
12+
// @snapshot no_type_anchor - '//*[@id="associatedtype.T"]'
13+
type T;
14+
// There should be no anchors here.
15+
// @snapshot no_const_anchor - '//*[@id="associatedconstant.YOLO"]'
16+
const YOLO: u32;
17+
18+
// There should be no anchors here.
19+
// @snapshot no_tymethod_anchor - '//*[@id="tymethod.foo"]'
20+
fn foo();
21+
// There should be no anchors here.
22+
// @snapshot no_trait_method_anchor - '//*[@id="method.bar"]'
23+
fn bar() {}
24+
}
25+
26+
// @has 'foo/struct.Foo.html'
27+
impl Bar for Foo {
28+
// @has - '//*[@id="associatedtype.T"]/a[@class="anchor"]' ''
29+
type T = u32;
30+
// @has - '//*[@id="associatedconstant.YOLO"]/a[@class="anchor"]' ''
31+
const YOLO: u32 = 0;
32+
33+
// @has - '//*[@id="method.foo"]/a[@class="anchor"]' ''
34+
fn foo() {}
35+
// Same check for provided "bar" method.
36+
// @has - '//*[@id="method.bar"]/a[@class="anchor"]' ''
37+
}
38+
39+
impl Foo {
40+
// @snapshot no_const_anchor2 - '//*[@id="associatedconstant.X"]'
41+
// There should be no anchors here.
42+
pub const X: i32 = 0;
43+
// @snapshot no_type_anchor2 - '//*[@id="associatedtype.Y"]'
44+
// There should be no anchors here.
45+
pub type Y = u32;
46+
// @snapshot no_method_anchor - '//*[@id="method.new"]'
47+
// There should be no anchors here.
48+
pub fn new() -> Self { Self }
49+
}

0 commit comments

Comments
 (0)