Skip to content

Commit fa10e4d

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 4dfd827 commit fa10e4d

23 files changed

+823
-48
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4688,6 +4688,7 @@ dependencies = [
46884688
"arrayvec",
46894689
"askama",
46904690
"expect-test",
4691+
"indexmap 2.0.0",
46914692
"itertools",
46924693
"minifier",
46934694
"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.3"
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
@@ -934,18 +934,27 @@ fn clean_ty_generics<'tcx>(
934934
fn clean_ty_alias_inner_type<'tcx>(
935935
ty: Ty<'tcx>,
936936
cx: &mut DocContext<'tcx>,
937+
ret: &mut Vec<Item>,
937938
) -> Option<TypeAliasInnerType> {
938939
let ty::Adt(adt_def, args) = ty.kind() else {
939940
return None;
940941
};
941942

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

954+
if !adt_def.did().is_local() {
955+
inline::record_extern_fqn(cx, adt_def.did(), ItemType::Enum);
956+
}
957+
949958
TypeAliasInnerType::Enum {
950959
variants,
951960
is_non_exhaustive: adt_def.is_variant_list_non_exhaustive(),
@@ -961,8 +970,14 @@ fn clean_ty_alias_inner_type<'tcx>(
961970
clean_variant_def_with_args(variant, args, cx).kind.inner_items().cloned().collect();
962971

963972
if adt_def.is_struct() {
973+
if !adt_def.did().is_local() {
974+
inline::record_extern_fqn(cx, adt_def.did(), ItemType::Struct);
975+
}
964976
TypeAliasInnerType::Struct { ctor_kind: variant.ctor_kind(), fields }
965977
} else {
978+
if !adt_def.did().is_local() {
979+
inline::record_extern_fqn(cx, adt_def.did(), ItemType::Union);
980+
}
966981
TypeAliasInnerType::Union { fields }
967982
}
968983
})
@@ -2744,14 +2759,24 @@ fn clean_maybe_renamed_item<'tcx>(
27442759
}
27452760

27462761
let ty = cx.tcx.type_of(def_id).instantiate_identity();
2747-
let inner_type = clean_ty_alias_inner_type(ty, cx);
27482762

2749-
TypeAliasItem(Box::new(TypeAlias {
2750-
generics,
2751-
inner_type,
2752-
type_: rustdoc_ty,
2753-
item_type: Some(type_),
2754-
}))
2763+
let mut ret = Vec::new();
2764+
let inner_type = clean_ty_alias_inner_type(ty, cx, &mut ret);
2765+
2766+
ret.push(generate_item_with_correct_attrs(
2767+
cx,
2768+
TypeAliasItem(Box::new(TypeAlias {
2769+
generics,
2770+
inner_type,
2771+
type_: rustdoc_ty,
2772+
item_type: Some(type_),
2773+
})),
2774+
item.owner_id.def_id.to_def_id(),
2775+
name,
2776+
import_id,
2777+
renamed,
2778+
));
2779+
return ret;
27552780
}
27562781
ItemKind::Enum(ref def, generics) => EnumItem(Enum {
27572782
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
@@ -1066,6 +1066,8 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
10661066
}
10671067
}
10681068

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

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

0 commit comments

Comments
 (0)