Skip to content

Commit fab45bf

Browse files
authored
Rollup merge of #87035 - GuillaumeGomez:fix-implementors-display, r=notriddle
Fix implementors display Part of #86632. This PR does a few things: * It fixes of the JS rendered implementors. * It generates anchors for JS rendered implementors to make it coherent with the others. * It adds a test to ensure that we won't have the same issue again. * It changes the way we render the rustdoc-gui crates to simplify it a bit and also to allow to have dependencies without going through compiletest. Before: ![Screenshot from 2021-07-10 13-30-13](https://user-images.githubusercontent.com/3050060/125174172-b4048700-e1c3-11eb-8f0e-c46081371d4f.png) After: ![Screenshot from 2021-07-10 21-11-15](https://user-images.githubusercontent.com/3050060/125174173-b49d1d80-e1c3-11eb-8740-1dbbff70c2eb.png) I plan to add the `[src]` links in another PR because this one is already big enough. cc `@Mark-Simulacrum` (for the bootstrap changes) r? `@Nemo157`
2 parents 2d9a038 + bd81949 commit fab45bf

File tree

13 files changed

+109
-18
lines changed

13 files changed

+109
-18
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,7 @@ __pycache__/
7272
**node_modules
7373
**package-lock.json
7474

75+
## Rustdoc GUI tests
76+
src/test/rustdoc-gui/src/**.lock
77+
7578
# Before adding new lines, see the comment at the top.

src/bootstrap/test.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -907,27 +907,25 @@ impl Step for RustdocGUI {
907907
// We remove existing folder to be sure there won't be artifacts remaining.
908908
let _ = fs::remove_dir_all(&out_dir);
909909

910-
let mut nb_generated = 0;
910+
let src_path = "src/test/rustdoc-gui/src";
911911
// We generate docs for the libraries present in the rustdoc-gui's src folder.
912-
let libs_dir = builder.build.src.join("src/test/rustdoc-gui/src");
913-
for entry in libs_dir.read_dir().expect("read_dir call failed") {
914-
let entry = entry.expect("invalid entry");
915-
let path = entry.path();
916-
if path.extension().map(|e| e == "rs").unwrap_or(false) {
917-
let mut command = builder.rustdoc_cmd(self.compiler);
918-
command.arg(path).arg("-o").arg(&out_dir);
919-
builder.run(&mut command);
920-
nb_generated += 1;
921-
}
922-
}
923-
assert!(nb_generated > 0, "no documentation was generated...");
912+
let mut cargo = Command::new(&builder.initial_cargo);
913+
cargo
914+
.arg("doc")
915+
.arg("--workspace")
916+
.arg("--target-dir")
917+
.arg(&out_dir)
918+
.env("RUSTDOC", builder.rustdoc(self.compiler))
919+
.env("RUSTC", builder.rustc(self.compiler))
920+
.current_dir(&builder.build.src.join(src_path));
921+
builder.run(&mut cargo);
924922

925923
// We now run GUI tests.
926924
let mut command = Command::new(&nodejs);
927925
command
928926
.arg(builder.build.src.join("src/tools/rustdoc-gui/tester.js"))
929927
.arg("--doc-folder")
930-
.arg(out_dir)
928+
.arg(out_dir.join("doc"))
931929
.arg("--tests-folder")
932930
.arg(builder.build.src.join("src/test/rustdoc-gui"));
933931
for path in &builder.paths {

src/librustdoc/html/static/js/main.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,9 @@ function hideThemeButtonState() {
683683
});
684684
}
685685

686+
var currentNbImpls = implementors.getElementsByClassName("impl").length;
687+
var traitName = document.querySelector("h1.fqn > .in-band > .trait").textContent;
688+
var baseIdName = "impl-" + traitName + "-";
686689
var libs = Object.getOwnPropertyNames(imp);
687690
for (var i = 0, llength = libs.length; i < llength; ++i) {
688691
if (libs[i] === window.currentCrate) { continue; }
@@ -705,6 +708,7 @@ function hideThemeButtonState() {
705708

706709
var code = document.createElement("code");
707710
code.innerHTML = struct.text;
711+
addClass(code, "in-band");
708712

709713
onEachLazy(code.getElementsByTagName("a"), function(elem) {
710714
var href = elem.getAttribute("href");
@@ -714,12 +718,18 @@ function hideThemeButtonState() {
714718
}
715719
});
716720

717-
var display = document.createElement("h3");
721+
var currentId = baseIdName + currentNbImpls;
722+
var anchor = document.createElement("a");
723+
anchor.href = "#" + currentId;
724+
addClass(anchor, "anchor");
725+
726+
var display = document.createElement("div");
727+
display.id = currentId;
718728
addClass(display, "impl");
719-
display.innerHTML = "<span class=\"in-band\"><table class=\"table-display\">" +
720-
"<tbody><tr><td><code>" + code.outerHTML + "</code></td><td></td></tr>" +
721-
"</tbody></table></span>";
729+
display.appendChild(anchor);
730+
display.appendChild(code);
722731
list.appendChild(display);
732+
currentNbImpls += 1;
723733
}
724734
}
725735
};
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// The goal of this test is to check that the external trait implementors, generated with JS,
2+
// have the same display than the "local" ones.
3+
goto: file://|DOC_PATH|/implementors/trait.Whatever.html
4+
assert: "#implementors-list"
5+
// There are supposed to be two implementors listed.
6+
assert-count: ("#implementors-list > .impl", 2)
7+
// Now we check that both implementors have an anchor, an ID and a similar DOM.
8+
assert: ("#implementors-list > .impl:nth-child(1) > a.anchor")
9+
assert-attribute: ("#implementors-list > .impl:nth-child(1)", {"id": "impl-Whatever"})
10+
assert-attribute: ("#implementors-list > .impl:nth-child(1) > a.anchor", {"href": "#impl-Whatever"})
11+
assert: "#implementors-list > .impl:nth-child(1) > code.in-band"
12+
13+
assert: ("#implementors-list > .impl:nth-child(2) > a.anchor")
14+
assert-attribute: ("#implementors-list > .impl:nth-child(2)", {"id": "impl-Whatever-1"})
15+
assert-attribute: ("#implementors-list > .impl:nth-child(2) > a.anchor", {"href": "#impl-Whatever-1"})
16+
assert: "#implementors-list > .impl:nth-child(2) > code.in-band"

src/test/rustdoc-gui/src/Cargo.lock

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# This file is automatically @generated by Cargo.
2+
# It is not intended for manual editing.
3+
version = 3
4+
5+
[[package]]
6+
name = "implementors"
7+
version = "0.1.0"
8+
9+
[[package]]
10+
name = "lib2"
11+
version = "0.1.0"
12+
dependencies = [
13+
"implementors",
14+
]
15+
16+
[[package]]
17+
name = "test_docs"
18+
version = "0.1.0"

src/test/rustdoc-gui/src/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[workspace]
2+
members = [
3+
"test_docs",
4+
"lib2",
5+
"implementors",
6+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "implementors"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
[lib]
7+
path = "lib.rs"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pub trait Whatever {
2+
fn method() {}
3+
}
4+
5+
pub struct Struct;
6+
7+
impl Whatever for Struct {}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "lib2"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
[lib]
7+
path = "lib.rs"
8+
9+
[dependencies]
10+
implementors = { path = "../implementors" }

src/test/rustdoc-gui/src/lib2.rs src/test/rustdoc-gui/src/lib2/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ impl Trait for Foo {
3131
type X = u32;
3232
const Y: u32 = 0;
3333
}
34+
35+
impl implementors::Whatever for Foo {}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[cfg(test)]
2+
mod tests {
3+
#[test]
4+
fn it_works() {
5+
assert_eq!(2 + 2, 4);
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "test_docs"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
[lib]
7+
path = "lib.rs"
File renamed without changes.

0 commit comments

Comments
 (0)