about summary refs log tree commit diff
path: root/tests/rustdoc
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-10-27 23:08:24 +0000
committerbors <bors@rust-lang.org>2023-10-27 23:08:24 +0000
commit6f349cdbfaf75e12ed3c307721da34a400d31b7c (patch)
tree045b85946c9310269e2b6c5c8633c49bf2fbb3de /tests/rustdoc
parent2f1bd0729b74787f55d4cbc7818cfd787cd43a99 (diff)
parent46fdeb24fd16156f73d95272b48604ab967c81db (diff)
downloadrust-6f349cdbfaf75e12ed3c307721da34a400d31b7c.tar.gz
rust-6f349cdbfaf75e12ed3c307721da34a400d31b7c.zip
Auto merge of #116471 - notriddle:notriddle/js-trait-alias, r=GuillaumeGomez
rustdoc: use JS to inline target type impl docs into alias

Preview docs:

- https://notriddle.com/rustdoc-html-demo-5/js-trait-alias/std/io/type.Result.html

- https://notriddle.com/rustdoc-html-demo-5/js-trait-alias-compiler/rustc_middle/ty/type.PolyTraitRef.html

This pull request also includes a bug fix for trait alias inlining across crates. This means more documentation is generated, and is why ripgrep runs slower (it's a thin wrapper on top of the `grep` crate, so 5% of its docs are now the Result type).

- Before, built with rustdoc 1.75.0-nightly (aa1a71e9e 2023-10-26), Result type alias method docs are missing: http://notriddle.com/rustdoc-html-demo-5/ripgrep-js-nightly/rg/type.Result.html
- After, built with this branch, all the methods on Result are shown: http://notriddle.com/rustdoc-html-demo-5/ripgrep-js-trait-alias/rg/type.Result.html

*Review note: This is mostly just reverting https://github.com/rust-lang/rust/pull/115201. The last commit has the new work in it.*

Fixes #115718

This is an attempt to balance three problems, each of which would
be violated by a simpler implementation:

- A type alias should show all the `impl` blocks for the target
  type, and vice versa, if they're applicable. If nothing was
  done, and rustdoc continues to match them up in HIR, this
  would not work.

- Copying the target type's docs into its aliases' HTML pages
  directly causes far too much redundant HTML text to be generated
  when a crate has large numbers of methods and large numbers
  of type aliases.

- Using JavaScript exclusively for type alias impl docs would
  be a functional regression, and could make some docs very hard
  to find for non-JS readers.

- Making sure that only applicable docs are show in the
  resulting page requires a type checkers. Do not reimplement
  the type checker in JavaScript.

So, to make it work, rustdoc stashes these type-alias-inlined docs
in a JSONP "database-lite". The file is generated in `write_shared.rs`,
included in a `<script>` tag added in `print_item.rs`, and `main.js`
takes care of patching the additional docs into the DOM.

The format of `trait.impl` and `type.impl` JS files are superficially
similar. Each line, except the JSONP wrapper itself, belongs to a crate,
and they are otherwise separate (rustdoc should be idempotent). The
"meat" of the file is HTML strings, so the frontend code is very simple.
Links are relative to the doc root, though, so the frontend needs to fix
that up, and inlined docs can reuse these files.

However, there are a few differences, caused by the sophisticated
features that type aliases have. Consider this crate graph:

```text
 ---------------------------------
 | crate A: struct Foo<T>        |
 |          type Bar = Foo<i32>  |
 |          impl X for Foo<i8>   |
 |          impl Y for Foo<i32>  |
 ---------------------------------
     |
 ----------------------------------
 | crate B: type Baz = A::Foo<i8> |
 |          type Xyy = A::Foo<i8> |
 |          impl Z for Xyy        |
 ----------------------------------
```

The type.impl/A/struct.Foo.js JS file has a structure kinda like this:

```js
JSONP({
"A": [["impl Y for Foo<i32>", "Y", "A::Bar"]],
"B": [["impl X for Foo<i8>", "X", "B::Baz", "B::Xyy"], ["impl Z for Xyy", "Z", "B::Baz"]],
});
```

When the type.impl file is loaded, only the current crate's docs are
actually used. The main reason to bundle them together is that there's
enough duplication in them for DEFLATE to remove the redundancy.

The contents of a crate are a list of impl blocks, themselves
represented as lists. The first item in the sublist is the HTML block,
the second item is the name of the trait (which goes in the sidebar),
and all others are the names of type aliases that successfully match.

This way:

- There's no need to generate these files for types that have no aliases
  in the current crate. If a dependent crate makes a type alias, it'll
  take care of generating its own docs.
- There's no need to reimplement parts of the type checker in
  JavaScript. The Rust backend does the checking, and includes its
  results in the file.
- Docs defined directly on the type alias are dropped directly in the
  HTML by `render_assoc_items`, and are accessible without JavaScript.
  The JSONP file will not list impl items that are known to be part
  of the main HTML file already.

[JSONP]: https://en.wikipedia.org/wiki/JSONP
Diffstat (limited to 'tests/rustdoc')
-rw-r--r--tests/rustdoc/deref/deref-mut-methods.rs2
-rw-r--r--tests/rustdoc/deref/deref-recursive-pathbuf.rs4
-rw-r--r--tests/rustdoc/hidden-impls.rs2
-rw-r--r--tests/rustdoc/impl-parts-crosscrate.rs2
-rw-r--r--tests/rustdoc/inline_cross/implementors-js.rs10
-rw-r--r--tests/rustdoc/issue-43701.rs2
-rw-r--r--tests/rustdoc/strip-enum-variant.no-not-shown.html2
-rw-r--r--tests/rustdoc/strip-enum-variant.rs2
-rw-r--r--tests/rustdoc/type-alias/auxiliary/parent-crate-115718.rs9
-rw-r--r--tests/rustdoc/type-alias/cross-crate-115718.rs34
-rw-r--r--tests/rustdoc/type-alias/deeply-nested-112515.rs (renamed from tests/rustdoc/issue-112515-impl-ty-alias.rs)0
-rw-r--r--tests/rustdoc/type-alias/deref-32077.rs (renamed from tests/rustdoc/type-alias-impls-32077.rs)19
-rw-r--r--tests/rustdoc/type-alias/same-crate-115718.rs34
13 files changed, 101 insertions, 21 deletions
diff --git a/tests/rustdoc/deref/deref-mut-methods.rs b/tests/rustdoc/deref/deref-mut-methods.rs
index fdf8434224f..65681f81245 100644
--- a/tests/rustdoc/deref/deref-mut-methods.rs
+++ b/tests/rustdoc/deref/deref-mut-methods.rs
@@ -9,7 +9,7 @@ impl Foo {
 }
 
 // @has foo/struct.Bar.html
-// @has - '//*[@class="sidebar-elems"]//*[@class="block"]//a[@href="#method.foo"]' 'foo'
+// @has - '//*[@class="sidebar-elems"]//*[@class="block deref-methods"]//a[@href="#method.foo"]' 'foo'
 pub struct Bar {
     foo: Foo,
 }
diff --git a/tests/rustdoc/deref/deref-recursive-pathbuf.rs b/tests/rustdoc/deref/deref-recursive-pathbuf.rs
index be2b42b5ac6..7aee3147ba8 100644
--- a/tests/rustdoc/deref/deref-recursive-pathbuf.rs
+++ b/tests/rustdoc/deref/deref-recursive-pathbuf.rs
@@ -8,9 +8,9 @@
 // @has '-' '//*[@id="deref-methods-Path"]' 'Methods from Deref<Target = Path>'
 // @has '-' '//*[@class="impl-items"]//*[@id="method.exists"]' 'pub fn exists(&self)'
 // @has '-' '//div[@class="sidebar-elems"]//h3/a[@href="#deref-methods-PathBuf"]' 'Methods from Deref<Target=PathBuf>'
-// @has '-' '//*[@class="sidebar-elems"]//*[@class="block"]//a[@href="#method.as_path"]' 'as_path'
+// @has '-' '//*[@class="sidebar-elems"]//*[@class="block deref-methods"]//a[@href="#method.as_path"]' 'as_path'
 // @has '-' '//div[@class="sidebar-elems"]//h3/a[@href="#deref-methods-Path"]' 'Methods from Deref<Target=Path>'
-// @has '-' '//*[@class="sidebar-elems"]//*[@class="block"]//a[@href="#method.exists"]' 'exists'
+// @has '-' '//*[@class="sidebar-elems"]//*[@class="block deref-methods"]//a[@href="#method.exists"]' 'exists'
 
 #![crate_name = "foo"]
 
diff --git a/tests/rustdoc/hidden-impls.rs b/tests/rustdoc/hidden-impls.rs
index 26e2e0e0660..3283fbfecce 100644
--- a/tests/rustdoc/hidden-impls.rs
+++ b/tests/rustdoc/hidden-impls.rs
@@ -12,6 +12,6 @@ pub mod __hidden {
 
 // @has foo/trait.Clone.html
 // @!hasraw - 'Foo'
-// @has implementors/core/clone/trait.Clone.js
+// @has trait.impl/core/clone/trait.Clone.js
 // @!hasraw - 'Foo'
 pub use std::clone::Clone;
diff --git a/tests/rustdoc/impl-parts-crosscrate.rs b/tests/rustdoc/impl-parts-crosscrate.rs
index 34733f1f8cc..da109ea7090 100644
--- a/tests/rustdoc/impl-parts-crosscrate.rs
+++ b/tests/rustdoc/impl-parts-crosscrate.rs
@@ -12,7 +12,7 @@ pub struct Bar<T> { t: T }
 // full impl string.  Instead, just make sure something from each part
 // is mentioned.
 
-// @hasraw implementors/rustdoc_impl_parts_crosscrate/trait.AnAutoTrait.js Bar
+// @hasraw trait.impl/rustdoc_impl_parts_crosscrate/trait.AnAutoTrait.js Bar
 // @hasraw - Send
 // @hasraw - !AnAutoTrait
 // @hasraw - Copy
diff --git a/tests/rustdoc/inline_cross/implementors-js.rs b/tests/rustdoc/inline_cross/implementors-js.rs
index c79f05d8d3c..c17d52d0f41 100644
--- a/tests/rustdoc/inline_cross/implementors-js.rs
+++ b/tests/rustdoc/inline_cross/implementors-js.rs
@@ -4,13 +4,13 @@
 
 extern crate implementors_inline;
 
-// @!has implementors/implementors_js/trait.MyTrait.js
-// @has implementors/implementors_inline/my_trait/trait.MyTrait.js
-// @!has implementors/implementors_inline/prelude/trait.MyTrait.js
+// @!has trait.impl/implementors_js/trait.MyTrait.js
+// @has trait.impl/implementors_inline/my_trait/trait.MyTrait.js
+// @!has trait.impl/implementors_inline/prelude/trait.MyTrait.js
 // @has implementors_inline/my_trait/trait.MyTrait.html
-// @has - '//script/@src' '../../implementors/implementors_inline/my_trait/trait.MyTrait.js'
+// @has - '//script/@src' '../../trait.impl/implementors_inline/my_trait/trait.MyTrait.js'
 // @has implementors_js/trait.MyTrait.html
-// @has - '//script/@src' '../implementors/implementors_inline/my_trait/trait.MyTrait.js'
+// @has - '//script/@src' '../trait.impl/implementors_inline/my_trait/trait.MyTrait.js'
 /// When re-exporting this trait, the HTML will be inlined,
 /// but, vitally, the JavaScript will be located only at the
 /// one canonical path.
diff --git a/tests/rustdoc/issue-43701.rs b/tests/rustdoc/issue-43701.rs
index 44335e961f9..de772881e73 100644
--- a/tests/rustdoc/issue-43701.rs
+++ b/tests/rustdoc/issue-43701.rs
@@ -2,4 +2,4 @@
 
 pub use std::vec::Vec;
 
-// @!has implementors/core/clone/trait.Clone.js
+// @!has trait.impl/core/clone/trait.Clone.js
diff --git a/tests/rustdoc/strip-enum-variant.no-not-shown.html b/tests/rustdoc/strip-enum-variant.no-not-shown.html
index 782198956a0..e072335297d 100644
--- a/tests/rustdoc/strip-enum-variant.no-not-shown.html
+++ b/tests/rustdoc/strip-enum-variant.no-not-shown.html
@@ -1 +1 @@
-<ul class="block"><li><a href="#variant.Shown">Shown</a></li></ul>
\ No newline at end of file
+<ul class="block variant"><li><a href="#variant.Shown">Shown</a></li></ul>
\ No newline at end of file
diff --git a/tests/rustdoc/strip-enum-variant.rs b/tests/rustdoc/strip-enum-variant.rs
index 8753a7dc613..2512fa34b39 100644
--- a/tests/rustdoc/strip-enum-variant.rs
+++ b/tests/rustdoc/strip-enum-variant.rs
@@ -3,7 +3,7 @@
 // @!has - '//code' 'NotShown'
 // @has - '//code' '// some variants omitted'
 // Also check that `NotShown` isn't displayed in the sidebar.
-// @snapshot no-not-shown - '//*[@class="sidebar-elems"]/section/*[@class="block"][1]'
+// @snapshot no-not-shown - '//*[@class="sidebar-elems"]/section/*[@class="block variant"]'
 pub enum MyThing {
     Shown,
     #[doc(hidden)]
diff --git a/tests/rustdoc/type-alias/auxiliary/parent-crate-115718.rs b/tests/rustdoc/type-alias/auxiliary/parent-crate-115718.rs
new file mode 100644
index 00000000000..3607612c27a
--- /dev/null
+++ b/tests/rustdoc/type-alias/auxiliary/parent-crate-115718.rs
@@ -0,0 +1,9 @@
+pub struct MyStruct<T>(T);
+
+pub trait MyTrait1 {
+    fn method_trait_1();
+}
+
+impl MyTrait1 for MyStruct<u16> {
+    fn method_trait_1() {}
+}
diff --git a/tests/rustdoc/type-alias/cross-crate-115718.rs b/tests/rustdoc/type-alias/cross-crate-115718.rs
new file mode 100644
index 00000000000..372e62e4213
--- /dev/null
+++ b/tests/rustdoc/type-alias/cross-crate-115718.rs
@@ -0,0 +1,34 @@
+// aux-build: parent-crate-115718.rs
+
+// https://github.com/rust-lang/rust/issues/115718
+#![crate_name = "foo"]
+
+extern crate parent_crate_115718;
+
+use parent_crate_115718::MyStruct;
+
+pub trait MyTrait2 {
+    fn method_trait_2();
+}
+
+impl MyTrait2 for MyStruct<u16> {
+    fn method_trait_2() {}
+}
+
+pub trait MyTrait3 {
+    fn method_trait_3();
+}
+
+impl MyTrait3 for MyType {
+    fn method_trait_3() {}
+}
+
+// @hasraw 'type.impl/parent_crate_115718/struct.MyStruct.js' 'method_trait_1'
+// @hasraw 'type.impl/parent_crate_115718/struct.MyStruct.js' 'method_trait_2'
+// Avoid duplicating these docs.
+// @!hasraw 'foo/type.MyType.html' 'method_trait_1'
+// @!hasraw 'foo/type.MyType.html' 'method_trait_2'
+// The one made directly on the type alias should be attached to the HTML instead.
+// @!hasraw 'type.impl/parent_crate_115718/struct.MyStruct.js' 'method_trait_3'
+// @hasraw 'foo/type.MyType.html' 'method_trait_3'
+pub type MyType = MyStruct<u16>;
diff --git a/tests/rustdoc/issue-112515-impl-ty-alias.rs b/tests/rustdoc/type-alias/deeply-nested-112515.rs
index 161188ee576..161188ee576 100644
--- a/tests/rustdoc/issue-112515-impl-ty-alias.rs
+++ b/tests/rustdoc/type-alias/deeply-nested-112515.rs
diff --git a/tests/rustdoc/type-alias-impls-32077.rs b/tests/rustdoc/type-alias/deref-32077.rs
index 7bb763f86af..186ebb1a632 100644
--- a/tests/rustdoc/type-alias-impls-32077.rs
+++ b/tests/rustdoc/type-alias/deref-32077.rs
@@ -1,6 +1,5 @@
 // Regression test for <https://github.com/rust-lang/rust/issues/32077>.
 
-// https://github.com/rust-lang/rust/issues/32077
 #![crate_name = "foo"]
 
 pub struct GenericStruct<T>(T);
@@ -25,18 +24,19 @@ impl Bar for GenericStruct<u32> {}
 // We check that we have the implementation of the type alias itself.
 // @has - '//*[@id="impl-GenericStruct%3Cu8%3E"]/h3' 'impl TypedefStruct'
 // @has - '//*[@id="method.on_alias"]/h4' 'pub fn on_alias()'
-// @has - '//*[@id="impl-GenericStruct%3CT%3E"]/h3' 'impl<T> GenericStruct<T>'
-// @has - '//*[@id="method.on_gen"]/h4' 'pub fn on_gen(arg: T)'
-// @has - '//*[@id="impl-Foo-for-GenericStruct%3CT%3E"]/h3' 'impl<T> Foo for GenericStruct<T>'
 // This trait implementation doesn't match the type alias parameters so shouldn't appear in docs.
 // @!has - '//h3' 'impl Bar for GenericStruct<u32> {}'
 // Same goes for the `Deref` impl.
 // @!has - '//h2' 'Methods from Deref<Target = u32>'
 // @count - '//nav[@class="sidebar"]//a' 'on_alias' 1
-// @count - '//nav[@class="sidebar"]//a' 'on_gen' 1
-// @count - '//nav[@class="sidebar"]//a' 'Foo' 1
+// @!has - '//nav[@class="sidebar"]//a' 'on_gen'
+// @!has - '//nav[@class="sidebar"]//a' 'Foo'
 // @!has - '//nav[@class="sidebar"]//a' 'Bar'
 // @!has - '//nav[@class="sidebar"]//a' 'on_u32'
+// TypedefStruct inlined to GenericStruct
+// @hasraw 'type.impl/foo/struct.GenericStruct.js' 'TypedefStruct'
+// @hasraw 'type.impl/foo/struct.GenericStruct.js' 'method.on_gen'
+// @hasraw 'type.impl/foo/struct.GenericStruct.js' 'Foo'
 pub type TypedefStruct = GenericStruct<u8>;
 
 impl TypedefStruct {
@@ -54,8 +54,11 @@ impl std::ops::Deref for GenericStruct<u32> {
 pub struct Wrap<T>(GenericStruct<T>);
 
 // @has 'foo/type.Alias.html'
-// @has - '//h2' 'Methods from Deref<Target = u32>'
-// @has - '//*[@id="impl-Deref-for-Wrap%3CT%3E"]/h3' 'impl<T> Deref for Wrap<T>'
+// @!has - '//h2' 'Methods from Deref<Target = u32>'
+// @!has - '//*[@id="impl-Deref-for-Wrap%3CT%3E"]/h3' 'impl<T> Deref for Wrap<T>'
+// @hasraw 'type.impl/foo/struct.Wrap.js' 'impl-Deref-for-Wrap%3CT%3E'
+// Deref Methods aren't gathered for type aliases, though the actual impl is.
+// @!hasraw 'type.impl/foo/struct.Wrap.js' 'BITS'
 pub type Alias = Wrap<u32>;
 
 impl<T> std::ops::Deref for Wrap<T> {
diff --git a/tests/rustdoc/type-alias/same-crate-115718.rs b/tests/rustdoc/type-alias/same-crate-115718.rs
new file mode 100644
index 00000000000..26e5db85cd6
--- /dev/null
+++ b/tests/rustdoc/type-alias/same-crate-115718.rs
@@ -0,0 +1,34 @@
+// https://github.com/rust-lang/rust/issues/115718
+#![crate_name = "foo"]
+
+pub trait MyTrait1 {
+    fn method_trait_1();
+}
+
+pub trait MyTrait2 {
+    fn method_trait_2();
+}
+
+pub struct MyStruct<T>(T);
+
+impl MyStruct<u32> {
+    pub fn method_u32() {}
+}
+
+impl MyStruct<u16> {
+    pub fn method_u16() {}
+}
+
+impl MyTrait1 for MyStruct<u32> {
+    fn method_trait_1() {}
+}
+
+impl MyTrait2 for MyStruct<u16> {
+    fn method_trait_2() {}
+}
+
+// @hasraw 'type.impl/foo/struct.MyStruct.js' 'method_u16'
+// @!hasraw 'type.impl/foo/struct.MyStruct.js' 'method_u32'
+// @!hasraw 'type.impl/foo/struct.MyStruct.js' 'method_trait_1'
+// @hasraw 'type.impl/foo/struct.MyStruct.js' 'method_trait_2'
+pub type MyType = MyStruct<u16>;