Skip to content

Commit

Permalink
rustdoc: use JS to inline target type impl docs into alias
Browse files Browse the repository at this point in the history
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
  • Loading branch information
notriddle committed Oct 6, 2023
1 parent 287e783 commit 0243d95
Show file tree
Hide file tree
Showing 23 changed files with 821 additions and 48 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4588,6 +4588,7 @@ dependencies = [
"arrayvec",
"askama",
"expect-test",
"indexmap 2.0.0",
"itertools",
"minifier",
"once_cell",
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ path = "lib.rs"
arrayvec = { version = "0.7", default-features = false }
askama = { version = "0.12", default-features = false, features = ["config"] }
itertools = "0.10.1"
indexmap = "2"
minifier = "0.2.2"
once_cell = "1.10.0"
regex = "1"
Expand Down
12 changes: 9 additions & 3 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use crate::clean::{
use crate::core::DocContext;
use crate::formats::item_type::ItemType;

use super::Item;

/// Attempt to inline a definition into this AST.
///
/// This function will fetch the definition specified, and if it is
Expand Down Expand Up @@ -83,7 +85,7 @@ pub(crate) fn try_inline(
Res::Def(DefKind::TyAlias, did) => {
record_extern_fqn(cx, did, ItemType::TypeAlias);
build_impls(cx, did, attrs_without_docs, &mut ret);
clean::TypeAliasItem(build_type_alias(cx, did))
clean::TypeAliasItem(build_type_alias(cx, did, &mut ret))
}
Res::Def(DefKind::Enum, did) => {
record_extern_fqn(cx, did, ItemType::Enum);
Expand Down Expand Up @@ -281,11 +283,15 @@ fn build_union(cx: &mut DocContext<'_>, did: DefId) -> clean::Union {
clean::Union { generics, fields }
}

fn build_type_alias(cx: &mut DocContext<'_>, did: DefId) -> Box<clean::TypeAlias> {
fn build_type_alias(
cx: &mut DocContext<'_>,
did: DefId,
ret: &mut Vec<Item>,
) -> Box<clean::TypeAlias> {
let predicates = cx.tcx.explicit_predicates_of(did);
let ty = cx.tcx.type_of(did).instantiate_identity();
let type_ = clean_middle_ty(ty::Binder::dummy(ty), cx, Some(did), None);
let inner_type = clean_ty_alias_inner_type(ty, cx);
let inner_type = clean_ty_alias_inner_type(ty, cx, ret);

Box::new(clean::TypeAlias {
type_,
Expand Down
39 changes: 32 additions & 7 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -932,18 +932,27 @@ fn clean_ty_generics<'tcx>(
fn clean_ty_alias_inner_type<'tcx>(
ty: Ty<'tcx>,
cx: &mut DocContext<'tcx>,
ret: &mut Vec<Item>,
) -> Option<TypeAliasInnerType> {
let ty::Adt(adt_def, args) = ty.kind() else {
return None;
};

if !adt_def.did().is_local() {
inline::build_impls(cx, adt_def.did(), None, ret);
}

Some(if adt_def.is_enum() {
let variants: rustc_index::IndexVec<_, _> = adt_def
.variants()
.iter()
.map(|variant| clean_variant_def_with_args(variant, args, cx))
.collect();

if !adt_def.did().is_local() {
inline::record_extern_fqn(cx, adt_def.did(), ItemType::Enum);
}

TypeAliasInnerType::Enum {
variants,
is_non_exhaustive: adt_def.is_variant_list_non_exhaustive(),
Expand All @@ -959,8 +968,14 @@ fn clean_ty_alias_inner_type<'tcx>(
clean_variant_def_with_args(variant, args, cx).kind.inner_items().cloned().collect();

if adt_def.is_struct() {
if !adt_def.did().is_local() {
inline::record_extern_fqn(cx, adt_def.did(), ItemType::Struct);
}
TypeAliasInnerType::Struct { ctor_kind: variant.ctor_kind(), fields }
} else {
if !adt_def.did().is_local() {
inline::record_extern_fqn(cx, adt_def.did(), ItemType::Union);
}
TypeAliasInnerType::Union { fields }
}
})
Expand Down Expand Up @@ -2734,14 +2749,24 @@ fn clean_maybe_renamed_item<'tcx>(
}

let ty = cx.tcx.type_of(def_id).instantiate_identity();
let inner_type = clean_ty_alias_inner_type(ty, cx);

TypeAliasItem(Box::new(TypeAlias {
generics,
inner_type,
type_: rustdoc_ty,
item_type: Some(type_),
}))
let mut ret = Vec::new();
let inner_type = clean_ty_alias_inner_type(ty, cx, &mut ret);

ret.push(generate_item_with_correct_attrs(
cx,
TypeAliasItem(Box::new(TypeAlias {
generics,
inner_type,
type_: rustdoc_ty,
item_type: Some(type_),
})),
item.owner_id.def_id.to_def_id(),
name,
import_id,
renamed,
));
return ret;
}
ItemKind::Enum(ref def, generics) => EnumItem(Enum {
variants: def.variants.iter().map(|v| clean_variant(v, cx)).collect(),
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ pub(crate) struct Cache {
/// Unlike 'paths', this mapping ignores any renames that occur
/// due to 'use' statements.
///
/// This map is used when writing out the special 'implementors'
/// javascript file. By using the exact path that the type
/// This map is used when writing out the `impl.trait` and `impl.type`
/// javascript files. By using the exact path that the type
/// is declared with, we ensure that each path will be identical
/// to the path used if the corresponding type is inlined. By
/// doing this, we can detect duplicate impls on a trait page, and only display
Expand Down
3 changes: 3 additions & 0 deletions src/librustdoc/formats/item_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ impl ItemType {
pub(crate) fn is_method(&self) -> bool {
matches!(*self, ItemType::Method | ItemType::TyMethod)
}
pub(crate) fn is_adt(&self) -> bool {
matches!(*self, ItemType::Struct | ItemType::Union | ItemType::Enum)
}
}

impl fmt::Display for ItemType {
Expand Down
98 changes: 98 additions & 0 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,8 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
}
}

// [RUSTDOCIMPL] trait.impl
//
// Include implementors in crates that depend on the current crate.
//
// This is complicated by the way rustdoc is invoked, which is basically
Expand Down Expand Up @@ -1313,6 +1315,102 @@ fn item_type_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &c
// we need #14072 to make sense of the generics.
write!(w, "{}", render_assoc_items(cx, it, def_id, AssocItemRender::All));
write!(w, "{}", document_type_layout(cx, def_id));

// [RUSTDOCIMPL] type.impl
//
// Include type definitions from the alias target type.
//
// Earlier versions of this code worked by having `render_assoc_items`
// include this data directly. That generates *O*`(types*impls)` of HTML
// text, and some real crates have a lot of types and impls.
//
// To create the same UX without generating half a gigabyte of HTML for a
// crate that only contains 20 megabytes of actual documentation[^115718],
// rustdoc stashes these type-alias-inlined docs in a [JSONP]
// "database-lite". The file itself is generated in `write_shared.rs`,
// and hooks into functions provided by `main.js`.
//
// 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
// [^115718]: https://github.com/rust-lang/rust/issues/115718
let cloned_shared = Rc::clone(&cx.shared);
let cache = &cloned_shared.cache;
if let Some(target_did) = t.type_.def_id(cache) &&
let get_extern = { || cache.external_paths.get(&target_did) } &&
let Some(&(ref target_fqp, target_type)) = cache.paths.get(&target_did).or_else(get_extern) &&
target_type.is_adt() && // primitives cannot be inlined
let Some(self_did) = it.item_id.as_def_id() &&
let get_local = { || cache.paths.get(&self_did).map(|(p, _)| p) } &&
let Some(self_fqp) = cache.exact_paths.get(&self_did).or_else(get_local)
{
let mut js_src_path: UrlPartsBuilder = std::iter::repeat("..")
.take(cx.current.len())
.chain(std::iter::once("type.impl"))
.collect();
js_src_path.extend(target_fqp[..target_fqp.len() - 1].iter().copied());
js_src_path.push_fmt(format_args!("{target_type}.{}.js", target_fqp.last().unwrap()));
let self_path = self_fqp.iter().map(Symbol::as_str).collect::<Vec<&str>>().join("::");
write!(
w,
"<script src=\"{src}\" data-self-path=\"{self_path}\" async></script>",
src = js_src_path.finish(),
);
}
}

fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Union) {
Expand Down
Loading

0 comments on commit 0243d95

Please sign in to comment.