Skip to content

Commit 0243d95

Browse files
committed
rustdoc: use JS to inline target type impl docs into alias
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
1 parent 287e783 commit 0243d95

23 files changed

+821
-48
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4588,6 +4588,7 @@ dependencies = [
45884588
"arrayvec",
45894589
"askama",
45904590
"expect-test",
4591+
"indexmap 2.0.0",
45914592
"itertools",
45924593
"minifier",
45934594
"once_cell",

src/librustdoc/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ path = "lib.rs"
1010
arrayvec = { version = "0.7", default-features = false }
1111
askama = { version = "0.12", default-features = false, features = ["config"] }
1212
itertools = "0.10.1"
13+
indexmap = "2"
1314
minifier = "0.2.2"
1415
once_cell = "1.10.0"
1516
regex = "1"

src/librustdoc/clean/inline.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ use crate::clean::{
2626
use crate::core::DocContext;
2727
use crate::formats::item_type::ItemType;
2828

29+
use super::Item;
30+
2931
/// Attempt to inline a definition into this AST.
3032
///
3133
/// This function will fetch the definition specified, and if it is
@@ -83,7 +85,7 @@ pub(crate) fn try_inline(
8385
Res::Def(DefKind::TyAlias, did) => {
8486
record_extern_fqn(cx, did, ItemType::TypeAlias);
8587
build_impls(cx, did, attrs_without_docs, &mut ret);
86-
clean::TypeAliasItem(build_type_alias(cx, did))
88+
clean::TypeAliasItem(build_type_alias(cx, did, &mut ret))
8789
}
8890
Res::Def(DefKind::Enum, did) => {
8991
record_extern_fqn(cx, did, ItemType::Enum);
@@ -281,11 +283,15 @@ fn build_union(cx: &mut DocContext<'_>, did: DefId) -> clean::Union {
281283
clean::Union { generics, fields }
282284
}
283285

284-
fn build_type_alias(cx: &mut DocContext<'_>, did: DefId) -> Box<clean::TypeAlias> {
286+
fn build_type_alias(
287+
cx: &mut DocContext<'_>,
288+
did: DefId,
289+
ret: &mut Vec<Item>,
290+
) -> Box<clean::TypeAlias> {
285291
let predicates = cx.tcx.explicit_predicates_of(did);
286292
let ty = cx.tcx.type_of(did).instantiate_identity();
287293
let type_ = clean_middle_ty(ty::Binder::dummy(ty), cx, Some(did), None);
288-
let inner_type = clean_ty_alias_inner_type(ty, cx);
294+
let inner_type = clean_ty_alias_inner_type(ty, cx, ret);
289295

290296
Box::new(clean::TypeAlias {
291297
type_,

src/librustdoc/clean/mod.rs

+32-7
Original file line numberDiff line numberDiff line change
@@ -932,18 +932,27 @@ fn clean_ty_generics<'tcx>(
932932
fn clean_ty_alias_inner_type<'tcx>(
933933
ty: Ty<'tcx>,
934934
cx: &mut DocContext<'tcx>,
935+
ret: &mut Vec<Item>,
935936
) -> Option<TypeAliasInnerType> {
936937
let ty::Adt(adt_def, args) = ty.kind() else {
937938
return None;
938939
};
939940

941+
if !adt_def.did().is_local() {
942+
inline::build_impls(cx, adt_def.did(), None, ret);
943+
}
944+
940945
Some(if adt_def.is_enum() {
941946
let variants: rustc_index::IndexVec<_, _> = adt_def
942947
.variants()
943948
.iter()
944949
.map(|variant| clean_variant_def_with_args(variant, args, cx))
945950
.collect();
946951

952+
if !adt_def.did().is_local() {
953+
inline::record_extern_fqn(cx, adt_def.did(), ItemType::Enum);
954+
}
955+
947956
TypeAliasInnerType::Enum {
948957
variants,
949958
is_non_exhaustive: adt_def.is_variant_list_non_exhaustive(),
@@ -959,8 +968,14 @@ fn clean_ty_alias_inner_type<'tcx>(
959968
clean_variant_def_with_args(variant, args, cx).kind.inner_items().cloned().collect();
960969

961970
if adt_def.is_struct() {
971+
if !adt_def.did().is_local() {
972+
inline::record_extern_fqn(cx, adt_def.did(), ItemType::Struct);
973+
}
962974
TypeAliasInnerType::Struct { ctor_kind: variant.ctor_kind(), fields }
963975
} else {
976+
if !adt_def.did().is_local() {
977+
inline::record_extern_fqn(cx, adt_def.did(), ItemType::Union);
978+
}
964979
TypeAliasInnerType::Union { fields }
965980
}
966981
})
@@ -2734,14 +2749,24 @@ fn clean_maybe_renamed_item<'tcx>(
27342749
}
27352750

27362751
let ty = cx.tcx.type_of(def_id).instantiate_identity();
2737-
let inner_type = clean_ty_alias_inner_type(ty, cx);
27382752

2739-
TypeAliasItem(Box::new(TypeAlias {
2740-
generics,
2741-
inner_type,
2742-
type_: rustdoc_ty,
2743-
item_type: Some(type_),
2744-
}))
2753+
let mut ret = Vec::new();
2754+
let inner_type = clean_ty_alias_inner_type(ty, cx, &mut ret);
2755+
2756+
ret.push(generate_item_with_correct_attrs(
2757+
cx,
2758+
TypeAliasItem(Box::new(TypeAlias {
2759+
generics,
2760+
inner_type,
2761+
type_: rustdoc_ty,
2762+
item_type: Some(type_),
2763+
})),
2764+
item.owner_id.def_id.to_def_id(),
2765+
name,
2766+
import_id,
2767+
renamed,
2768+
));
2769+
return ret;
27452770
}
27462771
ItemKind::Enum(ref def, generics) => EnumItem(Enum {
27472772
variants: def.variants.iter().map(|v| clean_variant(v, cx)).collect(),

src/librustdoc/formats/cache.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ pub(crate) struct Cache {
5050
/// Unlike 'paths', this mapping ignores any renames that occur
5151
/// due to 'use' statements.
5252
///
53-
/// This map is used when writing out the special 'implementors'
54-
/// javascript file. By using the exact path that the type
53+
/// This map is used when writing out the `impl.trait` and `impl.type`
54+
/// javascript files. By using the exact path that the type
5555
/// is declared with, we ensure that each path will be identical
5656
/// to the path used if the corresponding type is inlined. By
5757
/// doing this, we can detect duplicate impls on a trait page, and only display

src/librustdoc/formats/item_type.rs

+3
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ impl ItemType {
180180
pub(crate) fn is_method(&self) -> bool {
181181
matches!(*self, ItemType::Method | ItemType::TyMethod)
182182
}
183+
pub(crate) fn is_adt(&self) -> bool {
184+
matches!(*self, ItemType::Struct | ItemType::Union | ItemType::Enum)
185+
}
183186
}
184187

185188
impl fmt::Display for ItemType {

src/librustdoc/html/render/print_item.rs

+98
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,8 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
10641064
}
10651065
}
10661066

1067+
// [RUSTDOCIMPL] trait.impl
1068+
//
10671069
// Include implementors in crates that depend on the current crate.
10681070
//
10691071
// This is complicated by the way rustdoc is invoked, which is basically
@@ -1313,6 +1315,102 @@ fn item_type_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &c
13131315
// we need #14072 to make sense of the generics.
13141316
write!(w, "{}", render_assoc_items(cx, it, def_id, AssocItemRender::All));
13151317
write!(w, "{}", document_type_layout(cx, def_id));
1318+
1319+
// [RUSTDOCIMPL] type.impl
1320+
//
1321+
// Include type definitions from the alias target type.
1322+
//
1323+
// Earlier versions of this code worked by having `render_assoc_items`
1324+
// include this data directly. That generates *O*`(types*impls)` of HTML
1325+
// text, and some real crates have a lot of types and impls.
1326+
//
1327+
// To create the same UX without generating half a gigabyte of HTML for a
1328+
// crate that only contains 20 megabytes of actual documentation[^115718],
1329+
// rustdoc stashes these type-alias-inlined docs in a [JSONP]
1330+
// "database-lite". The file itself is generated in `write_shared.rs`,
1331+
// and hooks into functions provided by `main.js`.
1332+
//
1333+
// The format of `trait.impl` and `type.impl` JS files are superficially
1334+
// similar. Each line, except the JSONP wrapper itself, belongs to a crate,
1335+
// and they are otherwise separate (rustdoc should be idempotent). The
1336+
// "meat" of the file is HTML strings, so the frontend code is very simple.
1337+
// Links are relative to the doc root, though, so the frontend needs to fix
1338+
// that up, and inlined docs can reuse these files.
1339+
//
1340+
// However, there are a few differences, caused by the sophisticated
1341+
// features that type aliases have. Consider this crate graph:
1342+
//
1343+
// ```text
1344+
// ---------------------------------
1345+
// | crate A: struct Foo<T> |
1346+
// | type Bar = Foo<i32> |
1347+
// | impl X for Foo<i8> |
1348+
// | impl Y for Foo<i32> |
1349+
// ---------------------------------
1350+
// |
1351+
// ----------------------------------
1352+
// | crate B: type Baz = A::Foo<i8> |
1353+
// | type Xyy = A::Foo<i8> |
1354+
// | impl Z for Xyy |
1355+
// ----------------------------------
1356+
// ```
1357+
//
1358+
// The type.impl/A/struct.Foo.js JS file has a structure kinda like this:
1359+
//
1360+
// ```js
1361+
// JSONP({
1362+
// "A": [["impl Y for Foo<i32>", "Y", "A::Bar"]],
1363+
// "B": [["impl X for Foo<i8>", "X", "B::Baz", "B::Xyy"], ["impl Z for Xyy", "Z", "B::Baz"]],
1364+
// });
1365+
// ```
1366+
//
1367+
// When the type.impl file is loaded, only the current crate's docs are
1368+
// actually used. The main reason to bundle them together is that there's
1369+
// enough duplication in them for DEFLATE to remove the redundancy.
1370+
//
1371+
// The contents of a crate are a list of impl blocks, themselves
1372+
// represented as lists. The first item in the sublist is the HTML block,
1373+
// the second item is the name of the trait (which goes in the sidebar),
1374+
// and all others are the names of type aliases that successfully match.
1375+
//
1376+
// This way:
1377+
//
1378+
// - There's no need to generate these files for types that have no aliases
1379+
// in the current crate. If a dependent crate makes a type alias, it'll
1380+
// take care of generating its own docs.
1381+
// - There's no need to reimplement parts of the type checker in
1382+
// JavaScript. The Rust backend does the checking, and includes its
1383+
// results in the file.
1384+
// - Docs defined directly on the type alias are dropped directly in the
1385+
// HTML by `render_assoc_items`, and are accessible without JavaScript.
1386+
// The JSONP file will not list impl items that are known to be part
1387+
// of the main HTML file already.
1388+
//
1389+
// [JSONP]: https://en.wikipedia.org/wiki/JSONP
1390+
// [^115718]: https://github.com/rust-lang/rust/issues/115718
1391+
let cloned_shared = Rc::clone(&cx.shared);
1392+
let cache = &cloned_shared.cache;
1393+
if let Some(target_did) = t.type_.def_id(cache) &&
1394+
let get_extern = { || cache.external_paths.get(&target_did) } &&
1395+
let Some(&(ref target_fqp, target_type)) = cache.paths.get(&target_did).or_else(get_extern) &&
1396+
target_type.is_adt() && // primitives cannot be inlined
1397+
let Some(self_did) = it.item_id.as_def_id() &&
1398+
let get_local = { || cache.paths.get(&self_did).map(|(p, _)| p) } &&
1399+
let Some(self_fqp) = cache.exact_paths.get(&self_did).or_else(get_local)
1400+
{
1401+
let mut js_src_path: UrlPartsBuilder = std::iter::repeat("..")
1402+
.take(cx.current.len())
1403+
.chain(std::iter::once("type.impl"))
1404+
.collect();
1405+
js_src_path.extend(target_fqp[..target_fqp.len() - 1].iter().copied());
1406+
js_src_path.push_fmt(format_args!("{target_type}.{}.js", target_fqp.last().unwrap()));
1407+
let self_path = self_fqp.iter().map(Symbol::as_str).collect::<Vec<&str>>().join("::");
1408+
write!(
1409+
w,
1410+
"<script src=\"{src}\" data-self-path=\"{self_path}\" async></script>",
1411+
src = js_src_path.finish(),
1412+
);
1413+
}
13161414
}
13171415

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

0 commit comments

Comments
 (0)