Skip to main content

rustdoc/html/render/
print_item.rs

1use std::borrow::Cow;
2use std::cmp::Ordering;
3use std::fmt::{self, Display, Write as _};
4use std::iter;
5
6use askama::Template;
7use rustc_abi::VariantIdx;
8use rustc_ast::join_path_syms;
9use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
10use rustc_hir as hir;
11use rustc_hir::def::{CtorKind, MacroKinds};
12use rustc_hir::def_id::DefId;
13use rustc_index::IndexVec;
14use rustc_middle::ty::{self, TyCtxt};
15use rustc_span::hygiene::MacroKind;
16use rustc_span::symbol::{Symbol, sym};
17use tracing::{debug, info};
18
19use super::type_layout::document_type_layout;
20use super::{
21    AssocItemLink, AssocItemRender, Context, ImplRenderingParameters, RenderMode,
22    collect_paths_for_type, document, ensure_trailing_slash, get_filtered_impls_for_reference,
23    item_ty_to_section, notable_traits_button, notable_traits_json, render_all_impls,
24    render_assoc_item, render_assoc_items, render_attributes_in_code, render_impl,
25    render_repr_attribute_in_code, render_rightside, render_stability_since_raw,
26    render_stability_since_raw_with_extra, write_section_heading,
27};
28use crate::clean;
29use crate::config::ModuleSorting;
30use crate::display::{Joined as _, MaybeDisplay as _};
31use crate::formats::Impl;
32use crate::formats::item_type::ItemType;
33use crate::html::escape::{Escape, EscapeBodyTextWithWbr};
34use crate::html::format::{
35    Ending, PrintWithSpace, full_print_fn_decl, print_abi_with_space, print_constness_with_space,
36    print_generic_bound, print_generics, print_impl, print_import, print_path, print_type,
37    print_where_clause, visibility_print_with_space,
38};
39use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};
40use crate::html::render::sidebar::filters;
41use crate::html::render::{document_full, document_item_info};
42use crate::html::url_parts_builder::UrlPartsBuilder;
43
44const ITEM_TABLE_OPEN: &str = "<dl class=\"item-table\">";
45const REEXPORTS_TABLE_OPEN: &str = "<dl class=\"item-table reexports\">";
46const ITEM_TABLE_CLOSE: &str = "</dl>";
47
48// A component in a `use` path, like `string` in std::string::ToString
49struct PathComponent {
50    path: String,
51    name: Symbol,
52}
53
54#[derive(Template)]
55#[template(path = "print_item.html")]
56struct ItemVars<'a> {
57    typ: &'a str,
58    name: &'a str,
59    item_type: &'a str,
60    path_components: Vec<PathComponent>,
61    stability_since_raw: &'a str,
62    src_href: Option<&'a str>,
63}
64
65pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item) -> impl fmt::Display {
66    debug_assert!(!item.is_stripped());
67
68    fmt::from_fn(|buf| {
69        let typ = match item.kind {
70            clean::ModuleItem(_) => {
71                if item.is_crate() {
72                    "Crate "
73                } else {
74                    "Module "
75                }
76            }
77            clean::FunctionItem(..) | clean::ForeignFunctionItem(..) => "Function ",
78            clean::TraitItem(..) => "Trait ",
79            clean::StructItem(..) => "Struct ",
80            clean::UnionItem(..) => "Union ",
81            clean::EnumItem(..) => "Enum ",
82            clean::TypeAliasItem(..) => "Type Alias ",
83            clean::MacroItem(..) => "Macro ",
84            clean::ProcMacroItem(ref mac) => match mac.kind {
85                MacroKind::Bang => "Macro ",
86                MacroKind::Attr => "Attribute Macro ",
87                MacroKind::Derive => "Derive Macro ",
88            },
89            clean::PrimitiveItem(..) => "Primitive Type ",
90            clean::StaticItem(..) | clean::ForeignStaticItem(..) => "Static ",
91            clean::ConstantItem(..) => "Constant ",
92            clean::ForeignTypeItem => "Foreign Type ",
93            clean::KeywordItem => "Keyword ",
94            clean::AttributeItem => "Attribute ",
95            clean::TraitAliasItem(..) => "Trait Alias ",
96            _ => {
97                // We don't generate pages for any other type.
98                unreachable!();
99            }
100        };
101        let stability_since_raw =
102            render_stability_since_raw(item.stable_since(cx.tcx()), item.const_stability(cx.tcx()))
103                .maybe_display()
104                .to_string();
105
106        // Write source tag
107        //
108        // When this item is part of a `crate use` in a downstream crate, the
109        // source link in the downstream documentation will actually come back to
110        // this page, and this link will be auto-clicked. The `id` attribute is
111        // used to find the link to auto-click.
112        let src_href =
113            if cx.info.include_sources && !item.is_primitive() { cx.src_href(item) } else { None };
114
115        let path_components = if item.is_fake_item() {
116            vec![]
117        } else {
118            let cur = &cx.current;
119            let amt = if item.is_mod() { cur.len() - 1 } else { cur.len() };
120            cur.iter()
121                .enumerate()
122                .take(amt)
123                .map(|(i, component)| PathComponent {
124                    path: "../".repeat(cur.len() - i - 1),
125                    name: *component,
126                })
127                .collect()
128        };
129
130        let item_vars = ItemVars {
131            typ,
132            name: item.name.as_ref().unwrap().as_str(),
133            // It's fine to use `type_` here because, even if it's a decl macro with multiple kinds,
134            // since we're generating its documentation page, we can default to the macro type.
135            item_type: &item.type_().to_string(),
136            path_components,
137            stability_since_raw: &stability_since_raw,
138            src_href: src_href.as_deref(),
139        };
140
141        item_vars.render_into(buf).unwrap();
142
143        match &item.kind {
144            clean::ModuleItem(m) => {
145                write!(buf, "{}", item_module(cx, item, &m.items))
146            }
147            clean::FunctionItem(f) | clean::ForeignFunctionItem(f, _) => {
148                write!(buf, "{}", item_function(cx, item, f))
149            }
150            clean::TraitItem(t) => write!(buf, "{}", item_trait(cx, item, t)),
151            clean::StructItem(s) => {
152                write!(buf, "{}", item_struct(cx, item, s))
153            }
154            clean::UnionItem(s) => write!(buf, "{}", item_union(cx, item, s)),
155            clean::EnumItem(e) => write!(buf, "{}", item_enum(cx, item, e)),
156            clean::TypeAliasItem(t) => {
157                write!(buf, "{}", item_type_alias(cx, item, t))
158            }
159            clean::MacroItem(m, kinds) => write!(buf, "{}", item_macro(cx, item, m, *kinds)),
160            clean::ProcMacroItem(m) => {
161                write!(buf, "{}", item_proc_macro(cx, item, m))
162            }
163            clean::PrimitiveItem(_) => write!(buf, "{}", item_primitive(cx, item)),
164            clean::StaticItem(i) => {
165                write!(buf, "{}", item_static(cx, item, i, None))
166            }
167            clean::ForeignStaticItem(i, safety) => {
168                write!(buf, "{}", item_static(cx, item, i, Some(*safety)))
169            }
170            clean::ConstantItem(ci) => {
171                write!(buf, "{}", item_constant(cx, item, &ci.generics, &ci.type_, &ci.kind))
172            }
173            clean::ForeignTypeItem => {
174                write!(buf, "{}", item_foreign_type(cx, item))
175            }
176            clean::KeywordItem | clean::AttributeItem => {
177                write!(buf, "{}", item_keyword_or_attribute(cx, item))
178            }
179            clean::TraitAliasItem(ta) => {
180                write!(buf, "{}", item_trait_alias(cx, item, ta))
181            }
182            _ => {
183                // We don't generate pages for any other type.
184                unreachable!();
185            }
186        }?;
187
188        // Render notable-traits.js used for all methods in this module.
189        let mut types_with_notable_traits = cx.types_with_notable_traits.borrow_mut();
190        if !types_with_notable_traits.is_empty() {
191            write!(
192                buf,
193                r#"<script type="text/json" id="notable-traits-data">{}</script>"#,
194                notable_traits_json(types_with_notable_traits.iter(), cx),
195            )?;
196            types_with_notable_traits.clear();
197        }
198        Ok(())
199    })
200}
201
202/// For large structs, enums, unions, etc, determine whether to hide their fields
203fn should_hide_fields(n_fields: usize) -> bool {
204    n_fields > 12
205}
206
207fn toggle_open(mut w: impl fmt::Write, text: impl Display) {
208    write!(
209        w,
210        "<details class=\"toggle type-contents-toggle\">\
211            <summary class=\"hideme\">\
212                <span>Show {text}</span>\
213            </summary>",
214    )
215    .unwrap();
216}
217
218fn toggle_close(mut w: impl fmt::Write) {
219    w.write_str("</details>").unwrap();
220}
221
222fn item_module(cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) -> impl fmt::Display {
223    fn deprecation_class_attr(is_deprecated: bool) -> &'static str {
224        if is_deprecated { " class=\"deprecated\"" } else { "" }
225    }
226
227    fmt::from_fn(|w| {
228        write!(w, "{}", document(cx, item, None, HeadingOffset::H2))?;
229
230        let mut not_stripped_items: FxIndexMap<ItemType, Vec<(usize, &clean::Item)>> =
231            FxIndexMap::default();
232
233        for (index, item) in items.iter().filter(|i| !i.is_stripped()).enumerate() {
234            // To prevent having new "decl macro attribute/derive" sections in the module,
235            // we cheat by turning them into their "proc-macro equivalent".
236            for type_ in item.types() {
237                let type_ = match type_ {
238                    ItemType::DeclMacroAttribute => ItemType::ProcAttribute,
239                    ItemType::DeclMacroDerive => ItemType::ProcDerive,
240                    type_ => type_,
241                };
242                not_stripped_items.entry(type_).or_default().push((index, item));
243            }
244        }
245
246        // the order of item types in the listing
247        fn reorder(ty: ItemType) -> u8 {
248            match ty {
249                ItemType::ExternCrate => 0,
250                ItemType::Import => 1,
251                ItemType::Primitive => 2,
252                ItemType::Module => 3,
253                ItemType::Macro => 4,
254                ItemType::Struct => 5,
255                ItemType::Enum => 6,
256                ItemType::Constant => 7,
257                ItemType::Static => 8,
258                ItemType::Trait => 9,
259                ItemType::Function => 10,
260                ItemType::TypeAlias => 12,
261                ItemType::Union => 13,
262                _ => 14 + ty as u8,
263            }
264        }
265
266        fn cmp(i1: &clean::Item, i2: &clean::Item, tcx: TyCtxt<'_>) -> Ordering {
267            let is_stable1 =
268                i1.stability(tcx).as_ref().map(|s| s.level.is_stable()).unwrap_or(true);
269            let is_stable2 =
270                i2.stability(tcx).as_ref().map(|s| s.level.is_stable()).unwrap_or(true);
271            if is_stable1 != is_stable2 {
272                // true is bigger than false in the standard bool ordering,
273                // but we actually want stable items to come first
274                return is_stable2.cmp(&is_stable1);
275            }
276            match (i1.name, i2.name) {
277                (Some(name1), Some(name2)) => compare_names(name1.as_str(), name2.as_str()),
278                (Some(_), None) => Ordering::Greater,
279                (None, Some(_)) => Ordering::Less,
280                (None, None) => Ordering::Equal,
281            }
282        }
283
284        let tcx = cx.tcx();
285
286        match cx.shared.module_sorting {
287            ModuleSorting::Alphabetical => {
288                for items in not_stripped_items.values_mut() {
289                    items.sort_by(|(_, i1), (_, i2)| cmp(i1, i2, tcx));
290                }
291            }
292            ModuleSorting::DeclarationOrder => {}
293        }
294        // This call is to remove re-export duplicates in cases such as:
295        //
296        // ```
297        // pub(crate) mod foo {
298        //     pub(crate) mod bar {
299        //         pub(crate) trait Double { fn foo(); }
300        //     }
301        // }
302        //
303        // pub(crate) use foo::bar::*;
304        // pub(crate) use foo::*;
305        // ```
306        //
307        // `Double` will appear twice in the generated docs.
308        //
309        // FIXME: This code is quite ugly and could be improved. Small issue: DefId
310        // can be identical even if the elements are different (mostly in imports).
311        // So in case this is an import, we keep everything by adding a "unique id"
312        // (which is the position in the vector).
313        for items in not_stripped_items.values_mut() {
314            items.dedup_by_key(|(idx, i)| {
315                (
316                    i.item_id,
317                    if i.name.is_some() { Some(full_path(cx, i)) } else { None },
318                    i.type_(),
319                    if i.is_import() { *idx } else { 0 },
320                )
321            });
322        }
323
324        debug!("{not_stripped_items:?}");
325
326        let mut types = not_stripped_items.keys().copied().collect::<Vec<_>>();
327        types.sort_unstable_by(|a, b| reorder(*a).cmp(&reorder(*b)));
328
329        for type_ in types {
330            let my_section = item_ty_to_section(type_);
331            let tag = if my_section == super::ItemSection::Reexports {
332                REEXPORTS_TABLE_OPEN
333            } else {
334                ITEM_TABLE_OPEN
335            };
336            write!(
337                w,
338                "{}",
339                write_section_heading(my_section.name(), &cx.derive_id(my_section.id()), None, tag)
340            )?;
341
342            for (_, myitem) in &not_stripped_items[&type_] {
343                let visibility_and_hidden = |item: &clean::Item| match item.visibility(tcx) {
344                    Some(ty::Visibility::Restricted(_)) => {
345                        if item.is_doc_hidden() {
346                            // Don't separate with a space when there are two of them
347                            "<span title=\"Restricted Visibility\">&nbsp;🔒</span><span title=\"Hidden item\">👻</span> "
348                        } else {
349                            "<span title=\"Restricted Visibility\">&nbsp;🔒</span> "
350                        }
351                    }
352                    _ if item.is_doc_hidden() => "<span title=\"Hidden item\">&nbsp;👻</span> ",
353                    _ => "",
354                };
355
356                match myitem.kind {
357                    clean::ExternCrateItem { ref src } => {
358                        use crate::html::format::print_anchor;
359
360                        let visibility_and_hidden = visibility_and_hidden(myitem);
361                        // Module listings use the hidden marker, so skip doc(hidden) here.
362                        super::render_attributes_in_code_with_options(
363                            w,
364                            myitem,
365                            "",
366                            cx,
367                            false,
368                            "<dt><code>",
369                        )?;
370                        match *src {
371                            Some(src) => {
372                                write!(
373                                    w,
374                                    "{}extern crate {} as {};",
375                                    visibility_print_with_space(myitem, cx),
376                                    print_anchor(myitem.item_id.expect_def_id(), src, cx),
377                                    EscapeBodyTextWithWbr(myitem.name.unwrap().as_str())
378                                )?;
379                            }
380                            None => {
381                                write!(
382                                    w,
383                                    "{}extern crate {};",
384                                    visibility_print_with_space(myitem, cx),
385                                    print_anchor(
386                                        myitem.item_id.expect_def_id(),
387                                        myitem.name.unwrap(),
388                                        cx
389                                    )
390                                )?;
391                            }
392                        }
393                        write!(w, "</code>{visibility_and_hidden}</dt>")?
394                    }
395                    clean::ImportItem(ref import) => {
396                        let (stab_tags, deprecation) = match import.source.did {
397                            Some(import_def_id) => {
398                                let stab_tags =
399                                    print_extra_info_tags(tcx, myitem, item, Some(import_def_id));
400                                let deprecation = tcx
401                                    .lookup_deprecation(import_def_id)
402                                    .is_some_and(|deprecation| deprecation.is_in_effect());
403                                (Some(stab_tags), deprecation)
404                            }
405                            None => (None, item.is_deprecated(tcx)),
406                        };
407                        let visibility_and_hidden = visibility_and_hidden(myitem);
408                        let id = match import.kind {
409                            clean::ImportKind::Simple(s) => Some(format_args!(
410                                " id=\"{}\"",
411                                cx.derive_id(format!("reexport.{s}"))
412                            )),
413                            clean::ImportKind::Glob => None,
414                        };
415                        write!(
416                            w,
417                            "<dt{id}{deprecation_attr}><code>",
418                            id = id.maybe_display(),
419                            deprecation_attr = deprecation_class_attr(deprecation)
420                        )?;
421                        write!(
422                            w,
423                            "{vis}{imp}</code>{visibility_and_hidden}{stab_tags}\
424                            </dt>",
425                            vis = visibility_print_with_space(myitem, cx),
426                            imp = print_import(import, cx),
427                            visibility_and_hidden = visibility_and_hidden,
428                            stab_tags = stab_tags.maybe_display(),
429                        )?;
430                    }
431                    _ => {
432                        let Some(item_name) = myitem.name else { continue };
433
434                        let unsafety_flag = match myitem.kind {
435                            clean::FunctionItem(_) | clean::ForeignFunctionItem(..)
436                                if myitem.fn_header(tcx).unwrap().safety
437                                    == hir::HeaderSafety::Normal(hir::Safety::Unsafe) =>
438                            {
439                                "<sup title=\"unsafe function\">âš </sup>"
440                            }
441                            clean::ForeignStaticItem(_, hir::Safety::Unsafe) => {
442                                "<sup title=\"unsafe static\">âš </sup>"
443                            }
444                            _ => "",
445                        };
446                        let visibility_and_hidden = visibility_and_hidden(myitem);
447
448                        let docs = MarkdownSummaryLine(&myitem.doc_value(), &myitem.links(cx))
449                            .into_string();
450                        let (docs_before, docs_after) =
451                            if docs.is_empty() { ("", "") } else { ("<dd>", "</dd>") };
452                        let deprecation_attr = deprecation_class_attr(myitem.is_deprecated(tcx));
453                        write!(
454                            w,
455                            "<dt{deprecation_attr}>\
456                                <a class=\"{class}\" href=\"{href}\" title=\"{title1} {title2}\">\
457                                {name}\
458                                </a>\
459                                {visibility_and_hidden}\
460                                {unsafety_flag}\
461                                {stab_tags}\
462                            </dt>\
463                            {docs_before}{docs}{docs_after}",
464                            name = EscapeBodyTextWithWbr(item_name.as_str()),
465                            visibility_and_hidden = visibility_and_hidden,
466                            stab_tags = print_extra_info_tags(tcx, myitem, item, None),
467                            class = type_,
468                            unsafety_flag = unsafety_flag,
469                            href = print_item_path(myitem),
470                            title1 = myitem.type_(),
471                            title2 = full_path(cx, myitem),
472                        )?;
473                    }
474                }
475            }
476            w.write_str(ITEM_TABLE_CLOSE)?;
477        }
478
479        Ok(())
480    })
481}
482
483/// Render the stability, deprecation and portability tags that are displayed in the item's summary
484/// at the module level.
485fn print_extra_info_tags(
486    tcx: TyCtxt<'_>,
487    item: &clean::Item,
488    parent: &clean::Item,
489    import_def_id: Option<DefId>,
490) -> impl Display {
491    fmt::from_fn(move |f| {
492        fn tag_html(class: &str, title: &str, contents: &str) -> impl Display {
493            fmt::from_fn(move |f| {
494                write!(
495                    f,
496                    r#"<wbr><span class="stab {class}" title="{title}">{contents}</span>"#,
497                    title = Escape(title),
498                )
499            })
500        }
501
502        // The trailing space after each tag is to space it properly against the rest of the docs.
503        let deprecation = import_def_id
504            .map_or_else(|| item.deprecation(tcx), |import_did| tcx.lookup_deprecation(import_did));
505        if let Some(depr) = deprecation {
506            let message = if depr.is_in_effect() { "Deprecated" } else { "Deprecation planned" };
507            write!(f, "{}", tag_html("deprecated", "", message))?;
508        }
509
510        // The "rustc_private" crates are permanently unstable so it makes no sense
511        // to render "unstable" everywhere.
512        let stability = import_def_id
513            .map_or_else(|| item.stability(tcx), |import_did| tcx.lookup_stability(import_did));
514        if stability.is_some_and(|s| s.is_unstable() && s.feature != sym::rustc_private) {
515            write!(f, "{}", tag_html("unstable", "", "Experimental"))?;
516        }
517
518        debug!(name = ?item.name, cfg = ?item.cfg, parent_cfg = ?parent.cfg, "Portability");
519
520        let cfg = match (&item.cfg, parent.cfg.as_ref()) {
521            (Some(cfg), Some(parent_cfg)) => cfg.simplify_with(parent_cfg).map(Cow::Owned),
522            (cfg, _) => cfg.as_deref().map(Cow::Borrowed),
523        };
524
525        if let Some(cfg) = cfg {
526            write!(
527                f,
528                "{}",
529                tag_html("portability", &cfg.render_long_plain(), &cfg.render_short_html())
530            )
531        } else {
532            Ok(())
533        }
534    })
535}
536
537fn item_function(cx: &Context<'_>, it: &clean::Item, f: &clean::Function) -> impl fmt::Display {
538    fmt::from_fn(|w| {
539        let tcx = cx.tcx();
540        let header = it.fn_header(tcx).expect("printing a function which isn't a function");
541        debug!(
542            "item_function/const: {:?} {:?} {:?} {:?}",
543            it.name,
544            &header.constness,
545            it.stable_since(tcx),
546            it.const_stability(tcx),
547        );
548        let constness = print_constness_with_space(
549            &header.constness,
550            it.stable_since(tcx),
551            it.const_stability(tcx),
552        );
553        let safety = header.safety.print_with_space();
554        let abi = print_abi_with_space(header.abi).to_string();
555        let asyncness = header.asyncness.print_with_space();
556        let visibility = visibility_print_with_space(it, cx).to_string();
557        let name = it.name.unwrap();
558
559        let generics_len = format!("{:#}", print_generics(&f.generics, cx)).len();
560        let header_len = "fn ".len()
561            + visibility.len()
562            + constness.len()
563            + asyncness.len()
564            + safety.len()
565            + abi.len()
566            + name.as_str().len()
567            + generics_len;
568
569        let notable_traits = notable_traits_button(&f.decl.output, cx).maybe_display();
570
571        wrap_item(w, |w| {
572            render_attributes_in_code(w, it, "", cx)?;
573            write!(
574                w,
575                "{vis}{constness}{asyncness}{safety}{abi}fn \
576                {name}{generics}{decl}{notable_traits}{where_clause}",
577                vis = visibility,
578                constness = constness,
579                asyncness = asyncness,
580                safety = safety,
581                abi = abi,
582                name = name,
583                generics = print_generics(&f.generics, cx),
584                where_clause =
585                    print_where_clause(&f.generics, cx, 0, Ending::Newline).maybe_display(),
586                decl = full_print_fn_decl(&f.decl, header_len, 0, cx),
587            )
588        })?;
589        write!(w, "{}", document(cx, it, None, HeadingOffset::H2))
590    })
591}
592
593/// Struct used to handle insertion of "negative impl" marker in the generated DOM.
594///
595/// This marker appears once in all trait impl lists to divide negative impls from positive impls.
596struct NegativeMarker {
597    inserted: bool,
598}
599
600impl NegativeMarker {
601    fn new() -> Self {
602        Self { inserted: false }
603    }
604
605    fn insert_if_needed(&mut self, w: &mut fmt::Formatter<'_>, implementor: &Impl) -> fmt::Result {
606        if !self.inserted && !implementor.is_negative_trait_impl() {
607            w.write_str("<div class=\"negative-marker\"></div>")?;
608            self.inserted = true;
609        }
610        Ok(())
611    }
612}
613
614fn item_trait(cx: &Context<'_>, it: &clean::Item, t: &clean::Trait) -> impl fmt::Display {
615    fmt::from_fn(|w| {
616        let tcx = cx.tcx();
617        let bounds = print_bounds(&t.bounds, false, cx);
618        let required_types =
619            t.items.iter().filter(|m| m.is_required_associated_type()).collect::<Vec<_>>();
620        let provided_types = t.items.iter().filter(|m| m.is_associated_type()).collect::<Vec<_>>();
621        let required_consts =
622            t.items.iter().filter(|m| m.is_required_associated_const()).collect::<Vec<_>>();
623        let provided_consts =
624            t.items.iter().filter(|m| m.is_associated_const()).collect::<Vec<_>>();
625        let required_methods = t.items.iter().filter(|m| m.is_ty_method()).collect::<Vec<_>>();
626        let provided_methods = t.items.iter().filter(|m| m.is_method()).collect::<Vec<_>>();
627        let count_types = required_types.len() + provided_types.len();
628        let count_consts = required_consts.len() + provided_consts.len();
629        let count_methods = required_methods.len() + provided_methods.len();
630        let &rustc_middle::ty::TraitDef {
631            must_implement_one_of: ref must_implement_one_of_functions,
632            impl_restriction,
633            ..
634        } = tcx.trait_def(t.def_id);
635
636        // Output the trait definition
637        wrap_item(w, |mut w| {
638            render_attributes_in_code(&mut w, it, "", cx)?;
639            write!(
640                w,
641                "{vis}{safety}{is_auto}trait {name}{generics}{bounds}",
642                vis = visibility_print_with_space(it, cx),
643                safety = t.safety(tcx).print_with_space(),
644                is_auto = if t.is_auto(tcx) { "auto " } else { "" },
645                name = it.name.unwrap(),
646                generics = print_generics(&t.generics, cx),
647            )?;
648
649            if !t.generics.where_predicates.is_empty() {
650                write!(
651                    w,
652                    "{}",
653                    print_where_clause(&t.generics, cx, 0, Ending::Newline).maybe_display()
654                )?;
655            } else {
656                w.write_char(' ')?;
657            }
658
659            if t.items.is_empty() {
660                w.write_str("{ }")
661            } else {
662                // FIXME: we should be using a derived_id for the Anchors here
663                w.write_str("{\n")?;
664                let mut toggle = false;
665
666                // If there are too many associated types, hide _everything_
667                if should_hide_fields(count_types) {
668                    toggle = true;
669                    toggle_open(
670                        &mut w,
671                        format_args!(
672                            "{} associated items",
673                            count_types + count_consts + count_methods
674                        ),
675                    );
676                }
677                for types in [&required_types, &provided_types] {
678                    for t in types {
679                        writeln!(
680                            w,
681                            "{};",
682                            render_assoc_item(
683                                t,
684                                AssocItemLink::Anchor(None),
685                                ItemType::Trait,
686                                cx,
687                                RenderMode::Normal,
688                            )
689                        )?;
690                    }
691                }
692                // If there are too many associated constants, hide everything after them
693                // We also do this if the types + consts is large because otherwise we could
694                // render a bunch of types and _then_ a bunch of consts just because both were
695                // _just_ under the limit
696                if !toggle && should_hide_fields(count_types + count_consts) {
697                    toggle = true;
698                    toggle_open(
699                        &mut w,
700                        format_args!(
701                            "{count_consts} associated constant{plural_const} and \
702                         {count_methods} method{plural_method}",
703                            plural_const = pluralize(count_consts),
704                            plural_method = pluralize(count_methods),
705                        ),
706                    );
707                }
708                if count_types != 0 && (count_consts != 0 || count_methods != 0) {
709                    w.write_str("\n")?;
710                }
711                for consts in [&required_consts, &provided_consts] {
712                    for c in consts {
713                        writeln!(
714                            w,
715                            "{};",
716                            render_assoc_item(
717                                c,
718                                AssocItemLink::Anchor(None),
719                                ItemType::Trait,
720                                cx,
721                                RenderMode::Normal,
722                            )
723                        )?;
724                    }
725                }
726                if !toggle && should_hide_fields(count_methods) {
727                    toggle = true;
728                    toggle_open(&mut w, format_args!("{count_methods} methods"));
729                }
730                if count_consts != 0 && count_methods != 0 {
731                    w.write_str("\n")?;
732                }
733
734                if !required_methods.is_empty() {
735                    writeln!(w, "    // Required method{}", pluralize(required_methods.len()))?;
736                }
737                for (pos, m) in required_methods.iter().enumerate() {
738                    writeln!(
739                        w,
740                        "{};",
741                        render_assoc_item(
742                            m,
743                            AssocItemLink::Anchor(None),
744                            ItemType::Trait,
745                            cx,
746                            RenderMode::Normal,
747                        )
748                    )?;
749
750                    if pos < required_methods.len() - 1 {
751                        w.write_str("<span class=\"item-spacer\"></span>")?;
752                    }
753                }
754                if !required_methods.is_empty() && !provided_methods.is_empty() {
755                    w.write_str("\n")?;
756                }
757
758                if !provided_methods.is_empty() {
759                    writeln!(w, "    // Provided method{}", pluralize(provided_methods.len()))?;
760                }
761                for (pos, m) in provided_methods.iter().enumerate() {
762                    writeln!(
763                        w,
764                        "{} {{ ... }}",
765                        render_assoc_item(
766                            m,
767                            AssocItemLink::Anchor(None),
768                            ItemType::Trait,
769                            cx,
770                            RenderMode::Normal,
771                        )
772                    )?;
773
774                    if pos < provided_methods.len() - 1 {
775                        w.write_str("<span class=\"item-spacer\"></span>")?;
776                    }
777                }
778                if toggle {
779                    toggle_close(&mut w);
780                }
781                w.write_str("}")
782            }
783        })?;
784
785        if let rustc_middle::ty::trait_def::ImplRestrictionKind::Restricted(def_id, _) =
786            impl_restriction
787        {
788            let v1;
789            let v2;
790            write!(
791                w,
792                "<div class=\"impl-restriction\">ⓘ <i>This trait cannot be implemented outside <code>{}</code>.</i></div>",
793                if cx.cache().document_private {
794                    v1 =
795                        rustc_middle::ty::print::with_resolve_crate_name!(tcx.def_path_str(def_id));
796                    v1.as_str()
797                } else {
798                    v2 = tcx.crate_name(def_id.krate);
799                    v2.as_str()
800                },
801            )?;
802        }
803
804        // Trait documentation
805        write!(w, "{}", document(cx, it, None, HeadingOffset::H2))?;
806
807        fn trait_item(cx: &Context<'_>, m: &clean::Item, t: &clean::Item) -> impl fmt::Display {
808            fmt::from_fn(|w| {
809                let name = m.name.unwrap();
810                info!("Documenting {name} on {ty_name:?}", ty_name = t.name);
811                let item_type = m.type_();
812                let id = cx.derive_id(format!("{item_type}.{name}"));
813
814                let content = document_full(m, cx, HeadingOffset::H5).to_string();
815
816                let mut deprecation_class =
817                    if m.is_deprecated(cx.tcx()) { " deprecated" } else { "" };
818
819                let toggled = !content.is_empty();
820                if toggled {
821                    let method_toggle_class =
822                        if item_type.is_method() { " method-toggle" } else { "" };
823                    write!(
824                        w,
825                        "<details \
826                            class=\"toggle{method_toggle_class}{deprecation_class}\" \
827                            open><summary>"
828                    )?;
829                    deprecation_class = "";
830                }
831                write!(
832                    w,
833                    "<section id=\"{id}\" class=\"method{deprecation_class}\">\
834                    {}\
835                    <h4 class=\"code-header\">{}</h4></section>",
836                    render_rightside(cx, m, RenderMode::Normal),
837                    render_assoc_item(
838                        m,
839                        AssocItemLink::Anchor(Some(&id)),
840                        ItemType::Impl,
841                        cx,
842                        RenderMode::Normal,
843                    )
844                )?;
845                document_item_info(cx, m, Some(t)).render_into(w).unwrap();
846                if toggled {
847                    write!(w, "</summary>{content}</details>")?;
848                }
849                Ok(())
850            })
851        }
852
853        if !required_consts.is_empty() {
854            write!(
855                w,
856                "{}",
857                write_section_heading(
858                    "Required Associated Constants",
859                    "required-associated-consts",
860                    None,
861                    "<div class=\"methods\">",
862                )
863            )?;
864            for t in required_consts {
865                write!(w, "{}", trait_item(cx, t, it))?;
866            }
867            w.write_str("</div>")?;
868        }
869        if !provided_consts.is_empty() {
870            write!(
871                w,
872                "{}",
873                write_section_heading(
874                    "Provided Associated Constants",
875                    "provided-associated-consts",
876                    None,
877                    "<div class=\"methods\">",
878                )
879            )?;
880            for t in provided_consts {
881                write!(w, "{}", trait_item(cx, t, it))?;
882            }
883            w.write_str("</div>")?;
884        }
885
886        if !required_types.is_empty() {
887            write!(
888                w,
889                "{}",
890                write_section_heading(
891                    "Required Associated Types",
892                    "required-associated-types",
893                    None,
894                    "<div class=\"methods\">",
895                )
896            )?;
897            for t in required_types {
898                write!(w, "{}", trait_item(cx, t, it))?;
899            }
900            w.write_str("</div>")?;
901        }
902        if !provided_types.is_empty() {
903            write!(
904                w,
905                "{}",
906                write_section_heading(
907                    "Provided Associated Types",
908                    "provided-associated-types",
909                    None,
910                    "<div class=\"methods\">",
911                )
912            )?;
913            for t in provided_types {
914                write!(w, "{}", trait_item(cx, t, it))?;
915            }
916            w.write_str("</div>")?;
917        }
918
919        // Output the documentation for each function individually
920        if !required_methods.is_empty() || must_implement_one_of_functions.is_some() {
921            write!(
922                w,
923                "{}",
924                write_section_heading(
925                    "Required Methods",
926                    "required-methods",
927                    None,
928                    "<div class=\"methods\">",
929                )
930            )?;
931
932            if let Some(list) = must_implement_one_of_functions.as_deref() {
933                write!(
934                    w,
935                    "<div class=\"stab must_implement\">At least one of the `{}` methods is required.</div>",
936                    fmt::from_fn(|f| list.iter().joined("`, `", f)),
937                )?;
938            }
939
940            for m in required_methods {
941                write!(w, "{}", trait_item(cx, m, it))?;
942            }
943            w.write_str("</div>")?;
944        }
945        if !provided_methods.is_empty() {
946            write!(
947                w,
948                "{}",
949                write_section_heading(
950                    "Provided Methods",
951                    "provided-methods",
952                    None,
953                    "<div class=\"methods\">",
954                )
955            )?;
956            for m in provided_methods {
957                write!(w, "{}", trait_item(cx, m, it))?;
958            }
959            w.write_str("</div>")?;
960        }
961
962        // If there are methods directly on this trait object, render them here.
963        write!(
964            w,
965            "{}",
966            render_assoc_items(cx, it, it.item_id.expect_def_id(), AssocItemRender::All)
967        )?;
968
969        let mut extern_crates = FxIndexSet::default();
970
971        write!(
972            w,
973            "{}",
974            write_section_heading(
975                "Dyn Compatibility",
976                "dyn-compatibility",
977                None,
978                format_args!(
979                    "<div class=\"dyn-compatibility-info\"><p>This trait {} \
980                    <a href=\"{base}/reference/items/traits.html#dyn-compatibility\">dyn compatible</a>.</p>\
981                    <p><i>In older versions of Rust, dyn compatibility was called \"object safety\".</i></p></div>",
982                    if t.is_dyn_compatible(cx.tcx()) { "<b>is</b>" } else { "is <b>not</b>" },
983                    base = crate::clean::utils::DOC_RUST_LANG_ORG_VERSION
984                ),
985            ),
986        )?;
987
988        if let Some(implementors) = cx.shared.cache.implementors.get(&it.item_id.expect_def_id()) {
989            // The DefId is for the first Type found with that name. The bool is
990            // if any Types with the same name but different DefId have been found.
991            let mut implementor_dups: FxHashMap<Symbol, (DefId, bool)> = FxHashMap::default();
992            for implementor in implementors {
993                if let Some(did) =
994                    implementor.inner_impl().for_.without_borrowed_ref().def_id(&cx.shared.cache)
995                    && !did.is_local()
996                {
997                    extern_crates.insert(did.krate);
998                }
999                match implementor.inner_impl().for_.without_borrowed_ref() {
1000                    clean::Type::Path { path } if !path.is_assoc_ty() => {
1001                        let did = path.def_id();
1002                        let &mut (prev_did, ref mut has_duplicates) =
1003                            implementor_dups.entry(path.last()).or_insert((did, false));
1004                        if prev_did != did {
1005                            *has_duplicates = true;
1006                        }
1007                    }
1008                    _ => {}
1009                }
1010            }
1011
1012            let (local, mut foreign) =
1013                implementors.iter().partition::<Vec<_>, _>(|i| i.is_on_local_type(cx));
1014
1015            let (mut synthetic, mut concrete): (Vec<&&Impl>, Vec<&&Impl>) =
1016                local.iter().partition(|i| i.inner_impl().kind.is_auto());
1017
1018            synthetic.sort_by_cached_key(|i| ImplString::new_impl(i, cx));
1019            concrete.sort_by_cached_key(|i| ImplString::new_impl(i, cx));
1020            foreign.sort_by_cached_key(|i| ImplString::new_impl(i, cx));
1021
1022            if !foreign.is_empty() {
1023                write!(
1024                    w,
1025                    "{}",
1026                    write_section_heading(
1027                        "Implementations on Foreign Types",
1028                        "foreign-impls",
1029                        None,
1030                        ""
1031                    )
1032                )?;
1033
1034                for implementor in foreign {
1035                    let provided_methods = implementor.inner_impl().provided_trait_methods(tcx);
1036                    let assoc_link =
1037                        AssocItemLink::GotoSource(implementor.impl_item.item_id, &provided_methods);
1038                    write!(
1039                        w,
1040                        "{}",
1041                        render_impl(
1042                            cx,
1043                            implementor,
1044                            it,
1045                            assoc_link,
1046                            RenderMode::Normal,
1047                            None,
1048                            &[],
1049                            ImplRenderingParameters {
1050                                show_def_docs: false,
1051                                show_default_items: false,
1052                                show_non_assoc_items: true,
1053                                toggle_open_by_default: false,
1054                            },
1055                        )
1056                    )?;
1057                }
1058            }
1059
1060            write!(
1061                w,
1062                "{}",
1063                write_section_heading(
1064                    "Implementors",
1065                    "implementors",
1066                    None,
1067                    "<div id=\"implementors-list\">",
1068                )
1069            )?;
1070            let mut negative_marker = NegativeMarker::new();
1071            for implementor in concrete {
1072                negative_marker.insert_if_needed(w, implementor)?;
1073                write!(w, "{}", render_implementor(cx, implementor, it, &implementor_dups, &[]))?;
1074            }
1075            w.write_str("</div>")?;
1076
1077            if t.is_auto(tcx) {
1078                write!(
1079                    w,
1080                    "{}",
1081                    write_section_heading(
1082                        "Auto implementors",
1083                        "synthetic-implementors",
1084                        None,
1085                        "<div id=\"synthetic-implementors-list\">",
1086                    )
1087                )?;
1088                let mut negative_marker = NegativeMarker::new();
1089                for implementor in synthetic {
1090                    negative_marker.insert_if_needed(w, implementor)?;
1091                    write!(
1092                        w,
1093                        "{}",
1094                        render_implementor(
1095                            cx,
1096                            implementor,
1097                            it,
1098                            &implementor_dups,
1099                            &collect_paths_for_type(
1100                                &implementor.inner_impl().for_,
1101                                &cx.shared.cache,
1102                            ),
1103                        )
1104                    )?;
1105                }
1106                w.write_str("</div>")?;
1107            }
1108        } else {
1109            // even without any implementations to write in, we still want the heading and list, so the
1110            // implementors javascript file pulled in below has somewhere to write the impls into
1111            write!(
1112                w,
1113                "{}",
1114                write_section_heading(
1115                    "Implementors",
1116                    "implementors",
1117                    None,
1118                    "<div id=\"implementors-list\"></div>",
1119                )
1120            )?;
1121
1122            if t.is_auto(tcx) {
1123                write!(
1124                    w,
1125                    "{}",
1126                    write_section_heading(
1127                        "Auto implementors",
1128                        "synthetic-implementors",
1129                        None,
1130                        "<div id=\"synthetic-implementors-list\"></div>",
1131                    )
1132                )?;
1133            }
1134        }
1135
1136        // [RUSTDOCIMPL] trait.impl
1137        //
1138        // Include implementors in crates that depend on the current crate.
1139        //
1140        // This is complicated by the way rustdoc is invoked, which is basically
1141        // the same way rustc is invoked: it gets called, one at a time, for each
1142        // crate. When building the rustdocs for the current crate, rustdoc can
1143        // see crate metadata for its dependencies, but cannot see metadata for its
1144        // dependents.
1145        //
1146        // To make this work, we generate a "hook" at this stage, and our
1147        // dependents can "plug in" to it when they build. For simplicity's sake,
1148        // it's [JSONP]: a JavaScript file with the data we need (and can parse),
1149        // surrounded by a tiny wrapper that the Rust side ignores, but allows the
1150        // JavaScript side to include without having to worry about Same Origin
1151        // Policy. The code for *that* is in `write_shared.rs`.
1152        //
1153        // This is further complicated by `#[doc(inline)]`. We want all copies
1154        // of an inlined trait to reference the same JS file, to address complex
1155        // dependency graphs like this one (lower crates depend on higher crates):
1156        //
1157        // ```text
1158        //  --------------------------------------------
1159        //  |            crate A: trait Foo            |
1160        //  --------------------------------------------
1161        //      |                               |
1162        //  --------------------------------    |
1163        //  | crate B: impl A::Foo for Bar |    |
1164        //  --------------------------------    |
1165        //      |                               |
1166        //  ---------------------------------------------
1167        //  | crate C: #[doc(inline)] use A::Foo as Baz |
1168        //  |          impl Baz for Quux                |
1169        //  ---------------------------------------------
1170        // ```
1171        //
1172        // Basically, we want `C::Baz` and `A::Foo` to show the same set of
1173        // impls, which is easier if they both treat `/trait.impl/A/trait.Foo.js`
1174        // as the Single Source of Truth.
1175        //
1176        // We also want the `impl Baz for Quux` to be written to
1177        // `trait.Foo.js`. However, when we generate plain HTML for `C::Baz`,
1178        // we're going to want to generate plain HTML for `impl Baz for Quux` too,
1179        // because that'll load faster, and it's better for SEO. And we don't want
1180        // the same impl to show up twice on the same page.
1181        //
1182        // To make this work, the trait.impl/A/trait.Foo.js JS file has a structure kinda
1183        // like this:
1184        //
1185        // ```js
1186        // JSONP({
1187        // "B": {"impl A::Foo for Bar"},
1188        // "C": {"impl Baz for Quux"},
1189        // });
1190        // ```
1191        //
1192        // First of all, this means we can rebuild a crate, and it'll replace its own
1193        // data if something changes. That is, `rustdoc` is idempotent. The other
1194        // advantage is that we can list the crates that get included in the HTML,
1195        // and ignore them when doing the JavaScript-based part of rendering.
1196        // So C's HTML will have something like this:
1197        //
1198        // ```html
1199        // <script src="/trait.impl/A/trait.Foo.js"
1200        //     data-ignore-extern-crates="A,B" async></script>
1201        // ```
1202        //
1203        // And, when the JS runs, anything in data-ignore-extern-crates is known
1204        // to already be in the HTML, and will be ignored.
1205        //
1206        // [JSONP]: https://en.wikipedia.org/wiki/JSONP
1207        let mut js_src_path: UrlPartsBuilder =
1208            iter::repeat_n("..", cx.current.len()).chain(iter::once("trait.impl")).collect();
1209        if let Some(did) = it.item_id.as_def_id()
1210            && let get_extern = { || cx.shared.cache.external_paths.get(&did).map(|s| &s.0) }
1211            && let Some(fqp) = cx.shared.cache.exact_paths.get(&did).or_else(get_extern)
1212        {
1213            js_src_path.extend(fqp[..fqp.len() - 1].iter().copied());
1214            js_src_path.push_fmt(format_args!("{}.{}.js", it.type_(), fqp.last().unwrap()));
1215        } else {
1216            js_src_path.extend(cx.current.iter().copied());
1217            js_src_path.push_fmt(format_args!("{}.{}.js", it.type_(), it.name.unwrap()));
1218        }
1219        let extern_crates = fmt::from_fn(|f| {
1220            if !extern_crates.is_empty() {
1221                f.write_str(" data-ignore-extern-crates=\"")?;
1222                extern_crates.iter().map(|&cnum| tcx.crate_name(cnum)).joined(",", f)?;
1223                f.write_str("\"")?;
1224            }
1225            Ok(())
1226        });
1227        write!(
1228            w,
1229            "<script src=\"{src}\"{extern_crates} async></script>",
1230            src = js_src_path.finish()
1231        )
1232    })
1233}
1234
1235fn item_trait_alias(
1236    cx: &Context<'_>,
1237    it: &clean::Item,
1238    t: &clean::TraitAlias,
1239) -> impl fmt::Display {
1240    fmt::from_fn(|w| {
1241        wrap_item(w, |w| {
1242            render_attributes_in_code(w, it, "", cx)?;
1243            write!(
1244                w,
1245                "trait {name}{generics} = {bounds}{where_clause};",
1246                name = it.name.unwrap(),
1247                generics = print_generics(&t.generics, cx),
1248                bounds = print_bounds(&t.bounds, true, cx),
1249                where_clause =
1250                    print_where_clause(&t.generics, cx, 0, Ending::NoNewline).maybe_display(),
1251            )
1252        })?;
1253
1254        write!(w, "{}", document(cx, it, None, HeadingOffset::H2))?;
1255        // Render any items associated directly to this alias, as otherwise they
1256        // won't be visible anywhere in the docs. It would be nice to also show
1257        // associated items from the aliased type (see discussion in #32077), but
1258        // we need #14072 to make sense of the generics.
1259        write!(
1260            w,
1261            "{}",
1262            render_assoc_items(cx, it, it.item_id.expect_def_id(), AssocItemRender::All)
1263        )
1264    })
1265}
1266
1267fn item_type_alias(cx: &Context<'_>, it: &clean::Item, t: &clean::TypeAlias) -> impl fmt::Display {
1268    fmt::from_fn(|w| {
1269        wrap_item(w, |w| {
1270            render_attributes_in_code(w, it, "", cx)?;
1271            write!(
1272                w,
1273                "{vis}type {name}{generics}{where_clause} = {type_};",
1274                vis = visibility_print_with_space(it, cx),
1275                name = it.name.unwrap(),
1276                generics = print_generics(&t.generics, cx),
1277                where_clause =
1278                    print_where_clause(&t.generics, cx, 0, Ending::Newline).maybe_display(),
1279                type_ = print_type(&t.type_, cx),
1280            )
1281        })?;
1282
1283        write!(w, "{}", document(cx, it, None, HeadingOffset::H2))?;
1284
1285        if let Some(inner_type) = &t.inner_type {
1286            write!(w, "{}", write_section_heading("Aliased Type", "aliased-type", None, ""),)?;
1287
1288            match inner_type {
1289                clean::TypeAliasInnerType::Enum { variants, is_non_exhaustive } => {
1290                    let ty = cx
1291                        .tcx()
1292                        .type_of(it.def_id().unwrap())
1293                        .instantiate_identity()
1294                        .skip_norm_wip();
1295                    let enum_def_id = ty.ty_adt_def().unwrap().did();
1296
1297                    DisplayEnum {
1298                        variants,
1299                        generics: &t.generics,
1300                        is_non_exhaustive: *is_non_exhaustive,
1301                        def_id: enum_def_id,
1302                    }
1303                    .render_into(cx, it, true, w)?;
1304                }
1305                clean::TypeAliasInnerType::Union { fields } => {
1306                    let ty = cx
1307                        .tcx()
1308                        .type_of(it.def_id().unwrap())
1309                        .instantiate_identity()
1310                        .skip_norm_wip();
1311                    let union_def_id = ty.ty_adt_def().unwrap().did();
1312
1313                    ItemUnion {
1314                        cx,
1315                        it,
1316                        fields,
1317                        generics: &t.generics,
1318                        is_type_alias: true,
1319                        def_id: union_def_id,
1320                    }
1321                    .render_into(w)?;
1322                }
1323                clean::TypeAliasInnerType::Struct { ctor_kind, fields } => {
1324                    let ty = cx
1325                        .tcx()
1326                        .type_of(it.def_id().unwrap())
1327                        .instantiate_identity()
1328                        .skip_norm_wip();
1329                    let struct_def_id = ty.ty_adt_def().unwrap().did();
1330
1331                    DisplayStruct {
1332                        ctor_kind: *ctor_kind,
1333                        generics: &t.generics,
1334                        fields,
1335                        def_id: struct_def_id,
1336                    }
1337                    .render_into(cx, it, true, w)?;
1338                }
1339            }
1340        } else {
1341            let def_id = it.item_id.expect_def_id();
1342            // Render any items associated directly to this alias, as otherwise they
1343            // won't be visible anywhere in the docs. It would be nice to also show
1344            // associated items from the aliased type (see discussion in #32077), but
1345            // we need #14072 to make sense of the generics.
1346            write!(
1347                w,
1348                "{}{}",
1349                render_assoc_items(cx, it, def_id, AssocItemRender::All),
1350                document_type_layout(cx, def_id)
1351            )?;
1352        }
1353
1354        // [RUSTDOCIMPL] type.impl
1355        //
1356        // Include type definitions from the alias target type.
1357        //
1358        // Earlier versions of this code worked by having `render_assoc_items`
1359        // include this data directly. That generates *O*`(types*impls)` of HTML
1360        // text, and some real crates have a lot of types and impls.
1361        //
1362        // To create the same UX without generating half a gigabyte of HTML for a
1363        // crate that only contains 20 megabytes of actual documentation[^115718],
1364        // rustdoc stashes these type-alias-inlined docs in a [JSONP]
1365        // "database-lite". The file itself is generated in `write_shared.rs`,
1366        // and hooks into functions provided by `main.js`.
1367        //
1368        // The format of `trait.impl` and `type.impl` JS files are superficially
1369        // similar. Each line, except the JSONP wrapper itself, belongs to a crate,
1370        // and they are otherwise separate (rustdoc should be idempotent). The
1371        // "meat" of the file is HTML strings, so the frontend code is very simple.
1372        // Links are relative to the doc root, though, so the frontend needs to fix
1373        // that up, and inlined docs can reuse these files.
1374        //
1375        // However, there are a few differences, caused by the sophisticated
1376        // features that type aliases have. Consider this crate graph:
1377        //
1378        // ```text
1379        //  ---------------------------------
1380        //  | crate A: struct Foo<T>        |
1381        //  |          type Bar = Foo<i32>  |
1382        //  |          impl X for Foo<i8>   |
1383        //  |          impl Y for Foo<i32>  |
1384        //  ---------------------------------
1385        //      |
1386        //  ----------------------------------
1387        //  | crate B: type Baz = A::Foo<i8> |
1388        //  |          type Xyy = A::Foo<i8> |
1389        //  |          impl Z for Xyy        |
1390        //  ----------------------------------
1391        // ```
1392        //
1393        // The type.impl/A/struct.Foo.js JS file has a structure kinda like this:
1394        //
1395        // ```js
1396        // JSONP({
1397        // "A": [["impl Y for Foo<i32>", "Y", "A::Bar"]],
1398        // "B": [["impl X for Foo<i8>", "X", "B::Baz", "B::Xyy"], ["impl Z for Xyy", "Z", "B::Baz"]],
1399        // });
1400        // ```
1401        //
1402        // When the type.impl file is loaded, only the current crate's docs are
1403        // actually used. The main reason to bundle them together is that there's
1404        // enough duplication in them for DEFLATE to remove the redundancy.
1405        //
1406        // The contents of a crate are a list of impl blocks, themselves
1407        // represented as lists. The first item in the sublist is the HTML block,
1408        // the second item is the name of the trait (which goes in the sidebar),
1409        // and all others are the names of type aliases that successfully match.
1410        //
1411        // This way:
1412        //
1413        // - There's no need to generate these files for types that have no aliases
1414        //   in the current crate. If a dependent crate makes a type alias, it'll
1415        //   take care of generating its own docs.
1416        // - There's no need to reimplement parts of the type checker in
1417        //   JavaScript. The Rust backend does the checking, and includes its
1418        //   results in the file.
1419        // - Docs defined directly on the type alias are dropped directly in the
1420        //   HTML by `render_assoc_items`, and are accessible without JavaScript.
1421        //   The JSONP file will not list impl items that are known to be part
1422        //   of the main HTML file already.
1423        //
1424        // [JSONP]: https://en.wikipedia.org/wiki/JSONP
1425        // [^115718]: https://github.com/rust-lang/rust/issues/115718
1426        let cache = &cx.shared.cache;
1427        if let Some(target_did) = t.type_.def_id(cache)
1428            && let get_extern = { || cache.external_paths.get(&target_did) }
1429            && let Some(&(ref target_fqp, target_type)) =
1430                cache.paths.get(&target_did).or_else(get_extern)
1431            && target_type.is_adt() // primitives cannot be inlined
1432            && let Some(self_did) = it.item_id.as_def_id()
1433            && let get_local = { || cache.paths.get(&self_did).map(|(p, _)| p) }
1434            && let Some(self_fqp) = cache.exact_paths.get(&self_did).or_else(get_local)
1435        {
1436            let mut js_src_path: UrlPartsBuilder =
1437                iter::repeat_n("..", cx.current.len()).chain(iter::once("type.impl")).collect();
1438            js_src_path.extend(target_fqp[..target_fqp.len() - 1].iter().copied());
1439            js_src_path.push_fmt(format_args!("{target_type}.{}.js", target_fqp.last().unwrap()));
1440            let self_path = join_path_syms(self_fqp);
1441            write!(
1442                w,
1443                "<script src=\"{src}\" data-self-path=\"{self_path}\" async></script>",
1444                src = js_src_path.finish(),
1445            )?;
1446        }
1447        Ok(())
1448    })
1449}
1450
1451#[derive(Template)]
1452#[template(path = "item_union.html")]
1453struct ItemUnion<'a, 'cx> {
1454    cx: &'a Context<'cx>,
1455    it: &'a clean::Item,
1456    fields: &'a [clean::Item],
1457    generics: &'a clean::Generics,
1458    is_type_alias: bool,
1459    def_id: DefId,
1460}
1461
1462impl<'a, 'cx: 'a> ItemUnion<'a, 'cx> {
1463    fn document(&self) -> impl fmt::Display {
1464        document(self.cx, self.it, None, HeadingOffset::H2)
1465    }
1466
1467    fn document_type_layout(&self) -> impl fmt::Display {
1468        let def_id = self.it.item_id.expect_def_id();
1469        document_type_layout(self.cx, def_id)
1470    }
1471
1472    fn render_assoc_items(&self) -> impl fmt::Display {
1473        let def_id = self.it.item_id.expect_def_id();
1474        render_assoc_items(self.cx, self.it, def_id, AssocItemRender::All)
1475    }
1476
1477    fn render_union(&self) -> impl Display {
1478        render_union(
1479            self.it,
1480            Some(self.generics),
1481            self.fields,
1482            self.def_id,
1483            self.is_type_alias,
1484            self.cx,
1485        )
1486    }
1487
1488    fn print_field_attrs(&self, field: &'a clean::Item) -> impl Display {
1489        fmt::from_fn(move |w| {
1490            render_attributes_in_code(w, field, "", self.cx)?;
1491            Ok(())
1492        })
1493    }
1494
1495    fn document_field(&self, field: &'a clean::Item) -> impl Display {
1496        document(self.cx, field, Some(self.it), HeadingOffset::H3)
1497    }
1498
1499    fn stability_field(&self, field: &clean::Item) -> Option<String> {
1500        field.stability_class(self.cx.tcx())
1501    }
1502
1503    fn print_ty(&self, ty: &'a clean::Type) -> impl Display {
1504        print_type(ty, self.cx)
1505    }
1506
1507    // FIXME (GuillaumeGomez): When <https://github.com/askama-rs/askama/issues/452> is implemented,
1508    // we can replace the returned value with:
1509    //
1510    // `iter::Peekable<impl Iterator<Item = (&'a clean::Item, &'a clean::Type)>>`
1511    //
1512    // And update `item_union.html`.
1513    fn fields_iter(&self) -> impl Iterator<Item = (&'a clean::Item, &'a clean::Type)> {
1514        self.fields.iter().filter_map(|f| match f.kind {
1515            clean::StructFieldItem(ref ty) => Some((f, ty)),
1516            _ => None,
1517        })
1518    }
1519}
1520
1521fn item_union(cx: &Context<'_>, it: &clean::Item, s: &clean::Union) -> impl fmt::Display {
1522    fmt::from_fn(|w| {
1523        ItemUnion {
1524            cx,
1525            it,
1526            fields: &s.fields,
1527            generics: &s.generics,
1528            is_type_alias: false,
1529            def_id: it.def_id().unwrap(),
1530        }
1531        .render_into(w)?;
1532        Ok(())
1533    })
1534}
1535
1536fn print_tuple_struct_fields(cx: &Context<'_>, s: &[clean::Item]) -> impl Display {
1537    fmt::from_fn(|f| {
1538        if !s.is_empty()
1539            && s.iter()
1540                .all(|field| matches!(field.kind, clean::StrippedItem(clean::StructFieldItem(..))))
1541        {
1542            return f.write_str("<span class=\"comment\">/* private fields */</span>");
1543        }
1544
1545        s.iter()
1546            .map(|ty| {
1547                fmt::from_fn(|f| match ty.kind {
1548                    clean::StrippedItem(clean::StructFieldItem(_)) => f.write_str("_"),
1549                    clean::StructFieldItem(ref ty) => write!(f, "{}", print_type(ty, cx)),
1550                    _ => unreachable!(),
1551                })
1552            })
1553            .joined(", ", f)
1554    })
1555}
1556
1557struct DisplayEnum<'clean> {
1558    variants: &'clean IndexVec<VariantIdx, clean::Item>,
1559    generics: &'clean clean::Generics,
1560    is_non_exhaustive: bool,
1561    def_id: DefId,
1562}
1563
1564impl<'clean> DisplayEnum<'clean> {
1565    fn render_into<W: fmt::Write>(
1566        self,
1567        cx: &Context<'_>,
1568        it: &clean::Item,
1569        is_type_alias: bool,
1570        w: &mut W,
1571    ) -> fmt::Result {
1572        let non_stripped_variant_count = self.variants.iter().filter(|i| !i.is_stripped()).count();
1573        let variants_len = self.variants.len();
1574        let has_stripped_entries = variants_len != non_stripped_variant_count;
1575
1576        wrap_item(w, |w| {
1577            if is_type_alias {
1578                // For now the only attributes we render for type aliases are `repr` attributes.
1579                render_repr_attribute_in_code(w, cx, self.def_id)?;
1580            } else {
1581                render_attributes_in_code(w, it, "", cx)?;
1582            }
1583            write!(
1584                w,
1585                "{}enum {}{}{}",
1586                visibility_print_with_space(it, cx),
1587                it.name.unwrap(),
1588                print_generics(&self.generics, cx),
1589                render_enum_fields(
1590                    cx,
1591                    Some(self.generics),
1592                    self.variants,
1593                    non_stripped_variant_count,
1594                    has_stripped_entries,
1595                    self.is_non_exhaustive,
1596                    self.def_id,
1597                ),
1598            )
1599        })?;
1600
1601        let def_id = it.item_id.expect_def_id();
1602        let layout_def_id = if is_type_alias {
1603            self.def_id
1604        } else {
1605            write!(w, "{}", document(cx, it, None, HeadingOffset::H2))?;
1606            // We don't return the same `DefId` since the layout size of the type alias might be
1607            // different since we might have more information on the generics.
1608            def_id
1609        };
1610
1611        if non_stripped_variant_count != 0 {
1612            write!(w, "{}", item_variants(cx, it, self.variants, self.def_id))?;
1613        }
1614        write!(
1615            w,
1616            "{}{}",
1617            render_assoc_items(cx, it, def_id, AssocItemRender::All),
1618            document_type_layout(cx, layout_def_id)
1619        )
1620    }
1621}
1622
1623fn item_enum(cx: &Context<'_>, it: &clean::Item, e: &clean::Enum) -> impl fmt::Display {
1624    fmt::from_fn(|w| {
1625        DisplayEnum {
1626            variants: &e.variants,
1627            generics: &e.generics,
1628            is_non_exhaustive: it.is_non_exhaustive(),
1629            def_id: it.def_id().unwrap(),
1630        }
1631        .render_into(cx, it, false, w)
1632    })
1633}
1634
1635/// It'll return false if any variant is not a C-like variant. Otherwise it'll return true if at
1636/// least one of them has an explicit discriminant or if the enum has `#[repr(C)]` or an integer
1637/// `repr`.
1638fn should_show_enum_discriminant(
1639    cx: &Context<'_>,
1640    enum_def_id: DefId,
1641    variants: &IndexVec<VariantIdx, clean::Item>,
1642) -> bool {
1643    let mut has_variants_with_value = false;
1644    for variant in variants {
1645        if let clean::VariantItem(ref var) = variant.kind
1646            && matches!(var.kind, clean::VariantKind::CLike)
1647        {
1648            has_variants_with_value |= var.discriminant.is_some();
1649        } else {
1650            return false;
1651        }
1652    }
1653    if has_variants_with_value {
1654        return true;
1655    }
1656    let repr = cx.tcx().adt_def(enum_def_id).repr();
1657    repr.c() || repr.int.is_some()
1658}
1659
1660fn display_c_like_variant(
1661    cx: &Context<'_>,
1662    item: &clean::Item,
1663    variant: &clean::Variant,
1664    index: VariantIdx,
1665    should_show_enum_discriminant: bool,
1666    enum_def_id: DefId,
1667) -> impl fmt::Display {
1668    fmt::from_fn(move |w| {
1669        let name = item.name.unwrap();
1670        if let Some(ref value) = variant.discriminant {
1671            write!(w, "{} = {}", name.as_str(), value.value(cx.tcx(), true))?;
1672        } else if should_show_enum_discriminant {
1673            let adt_def = cx.tcx().adt_def(enum_def_id);
1674            let discr = adt_def.discriminant_for_variant(cx.tcx(), index);
1675            // Use `discr`'s `Display` impl to render the value with the correct
1676            // signedness, including proper sign-extension for signed types.
1677            write!(w, "{} = {}", name.as_str(), discr)?;
1678        } else {
1679            write!(w, "{name}")?;
1680        }
1681        Ok(())
1682    })
1683}
1684
1685fn render_enum_fields(
1686    cx: &Context<'_>,
1687    g: Option<&clean::Generics>,
1688    variants: &IndexVec<VariantIdx, clean::Item>,
1689    count_variants: usize,
1690    has_stripped_entries: bool,
1691    is_non_exhaustive: bool,
1692    enum_def_id: DefId,
1693) -> impl fmt::Display {
1694    fmt::from_fn(move |w| {
1695        let should_show_enum_discriminant =
1696            should_show_enum_discriminant(cx, enum_def_id, variants);
1697        if let Some(generics) = g
1698            && let Some(where_clause) = print_where_clause(generics, cx, 0, Ending::Newline)
1699        {
1700            write!(w, "{where_clause}")?;
1701        } else {
1702            // If there wasn't a `where` clause, we add a whitespace.
1703            w.write_char(' ')?;
1704        }
1705
1706        let variants_stripped = has_stripped_entries;
1707        if count_variants == 0 && !variants_stripped {
1708            w.write_str("{}")
1709        } else {
1710            w.write_str("{\n")?;
1711            let toggle = should_hide_fields(count_variants);
1712            if toggle {
1713                toggle_open(&mut *w, format_args!("{count_variants} variants"));
1714            }
1715            const TAB: &str = "    ";
1716            for (index, v) in variants.iter_enumerated() {
1717                if v.is_stripped() {
1718                    continue;
1719                }
1720                render_attributes_in_code(w, v, TAB, cx)?;
1721                w.write_str(TAB)?;
1722                match v.kind {
1723                    clean::VariantItem(ref var) => match var.kind {
1724                        clean::VariantKind::CLike => {
1725                            write!(
1726                                w,
1727                                "{}",
1728                                display_c_like_variant(
1729                                    cx,
1730                                    v,
1731                                    var,
1732                                    index,
1733                                    should_show_enum_discriminant,
1734                                    enum_def_id,
1735                                )
1736                            )?;
1737                        }
1738                        clean::VariantKind::Tuple(ref s) => {
1739                            write!(w, "{}({})", v.name.unwrap(), print_tuple_struct_fields(cx, s))?;
1740                        }
1741                        clean::VariantKind::Struct(ref s) => {
1742                            write!(
1743                                w,
1744                                "{}",
1745                                render_struct(v, None, None, &s.fields, TAB, false, cx)
1746                            )?;
1747                        }
1748                    },
1749                    _ => unreachable!(),
1750                }
1751                w.write_str(",\n")?;
1752            }
1753
1754            if variants_stripped && !is_non_exhaustive {
1755                w.write_str("    <span class=\"comment\">// some variants omitted</span>\n")?;
1756            }
1757            if toggle {
1758                toggle_close(&mut *w);
1759            }
1760            w.write_str("}")
1761        }
1762    })
1763}
1764
1765fn item_variants(
1766    cx: &Context<'_>,
1767    it: &clean::Item,
1768    variants: &IndexVec<VariantIdx, clean::Item>,
1769    enum_def_id: DefId,
1770) -> impl fmt::Display {
1771    fmt::from_fn(move |w| {
1772        let tcx = cx.tcx();
1773        write!(
1774            w,
1775            "{}",
1776            write_section_heading(
1777                format_args!("Variants{}", document_non_exhaustive_header(it)),
1778                "variants",
1779                Some("variants"),
1780                format_args!("{}<div class=\"variants\">", document_non_exhaustive(it)),
1781            ),
1782        )?;
1783
1784        let should_show_enum_discriminant =
1785            should_show_enum_discriminant(cx, enum_def_id, variants);
1786        for (index, variant) in variants.iter_enumerated() {
1787            if variant.is_stripped() {
1788                continue;
1789            }
1790            let id = cx.derive_id(format!("{}.{}", ItemType::Variant, variant.name.unwrap()));
1791            write!(
1792                w,
1793                "<section id=\"{id}\" class=\"variant\">\
1794                    <a href=\"#{id}\" class=\"anchor\">§</a>\
1795                    {}\
1796                    <h3 class=\"code-header\">",
1797                render_stability_since_raw_with_extra(
1798                    variant.stable_since(tcx),
1799                    variant.const_stability(tcx),
1800                    " rightside",
1801                )
1802                .maybe_display()
1803            )?;
1804            render_attributes_in_code(w, variant, "", cx)?;
1805            if let clean::VariantItem(ref var) = variant.kind
1806                && let clean::VariantKind::CLike = var.kind
1807            {
1808                write!(
1809                    w,
1810                    "{}",
1811                    display_c_like_variant(
1812                        cx,
1813                        variant,
1814                        var,
1815                        index,
1816                        should_show_enum_discriminant,
1817                        enum_def_id,
1818                    )
1819                )?;
1820            } else {
1821                w.write_str(variant.name.unwrap().as_str())?;
1822            }
1823
1824            let clean::VariantItem(variant_data) = &variant.kind else { unreachable!() };
1825
1826            if let clean::VariantKind::Tuple(ref s) = variant_data.kind {
1827                write!(w, "({})", print_tuple_struct_fields(cx, s))?;
1828            }
1829            w.write_str("</h3></section>")?;
1830
1831            write!(w, "{}", document(cx, variant, Some(it), HeadingOffset::H4))?;
1832
1833            let heading_and_fields = match &variant_data.kind {
1834                clean::VariantKind::Struct(s) => {
1835                    // If there is no field to display, no need to add the heading.
1836                    if s.fields.iter().any(|f| !f.is_doc_hidden()) {
1837                        Some(("Fields", &s.fields))
1838                    } else {
1839                        None
1840                    }
1841                }
1842                clean::VariantKind::Tuple(fields) => {
1843                    // Documentation on tuple variant fields is rare, so to reduce noise we only emit
1844                    // the section if at least one field is documented.
1845                    if fields.iter().any(|f| !f.doc_value().is_empty()) {
1846                        Some(("Tuple Fields", fields))
1847                    } else {
1848                        None
1849                    }
1850                }
1851                clean::VariantKind::CLike => None,
1852            };
1853
1854            if let Some((heading, fields)) = heading_and_fields {
1855                let variant_id =
1856                    cx.derive_id(format!("{}.{}.fields", ItemType::Variant, variant.name.unwrap()));
1857                write!(
1858                    w,
1859                    "<div class=\"sub-variant\" id=\"{variant_id}\">\
1860                        <h4>{heading}</h4>\
1861                        {}",
1862                    document_non_exhaustive(variant)
1863                )?;
1864                for field in fields {
1865                    match field.kind {
1866                        clean::StrippedItem(clean::StructFieldItem(_)) => {}
1867                        clean::StructFieldItem(ref ty) => {
1868                            let id = cx.derive_id(format!(
1869                                "variant.{}.field.{}",
1870                                variant.name.unwrap(),
1871                                field.name.unwrap()
1872                            ));
1873                            write!(
1874                                w,
1875                                "<div class=\"sub-variant-field\">\
1876                                    <span id=\"{id}\" class=\"section-header\">\
1877                                        <a href=\"#{id}\" class=\"anchor field\">§</a>\
1878                                        <code>"
1879                            )?;
1880                            render_attributes_in_code(w, field, "", cx)?;
1881                            write!(
1882                                w,
1883                                "{f}: {t}</code>\
1884                                    </span>\
1885                                    {doc}\
1886                                </div>",
1887                                f = field.name.unwrap(),
1888                                t = print_type(ty, cx),
1889                                doc = document(cx, field, Some(variant), HeadingOffset::H5),
1890                            )?;
1891                        }
1892                        _ => unreachable!(),
1893                    }
1894                }
1895                w.write_str("</div>")?;
1896            }
1897        }
1898        w.write_str("</div>")
1899    })
1900}
1901
1902fn item_macro(
1903    cx: &Context<'_>,
1904    it: &clean::Item,
1905    t: &clean::Macro,
1906    kinds: MacroKinds,
1907) -> impl fmt::Display {
1908    fmt::from_fn(move |w| {
1909        wrap_item(w, |w| {
1910            render_attributes_in_code(w, it, "", cx)?;
1911            if !t.macro_rules {
1912                write!(w, "{}", visibility_print_with_space(it, cx))?;
1913            }
1914            write!(w, "{}", Escape(&t.source))
1915        })?;
1916        if kinds != MacroKinds::BANG {
1917            write!(
1918                w,
1919                "<h3 class='macro-info'>ⓘ This is {} {}</h3>",
1920                kinds.article(),
1921                kinds.descr(),
1922            )?;
1923        }
1924        write!(w, "{}", document(cx, it, None, HeadingOffset::H2))
1925    })
1926}
1927
1928fn item_proc_macro(cx: &Context<'_>, it: &clean::Item, m: &clean::ProcMacro) -> impl fmt::Display {
1929    fmt::from_fn(|w| {
1930        wrap_item(w, |w| {
1931            let name = it.name.expect("proc-macros always have names");
1932            match m.kind {
1933                MacroKind::Bang => {
1934                    write!(w, "{name}!() {{ <span class=\"comment\">/* proc-macro */</span> }}")?;
1935                }
1936                MacroKind::Attr => {
1937                    write!(w, "#[{name}]")?;
1938                }
1939                MacroKind::Derive => {
1940                    write!(w, "#[derive({name})]")?;
1941                    if !m.helpers.is_empty() {
1942                        w.write_str(
1943                            "\n{\n    \
1944                            <span class=\"comment\">// Attributes available to this derive:</span>\n",
1945                        )?;
1946                        for attr in &m.helpers {
1947                            writeln!(w, "    #[{attr}]")?;
1948                        }
1949                        w.write_str("}\n")?;
1950                    }
1951                }
1952            }
1953            fmt::Result::Ok(())
1954        })?;
1955        write!(w, "{}", document(cx, it, None, HeadingOffset::H2))
1956    })
1957}
1958
1959fn item_primitive(cx: &Context<'_>, it: &clean::Item) -> impl fmt::Display {
1960    fmt::from_fn(|w| {
1961        let def_id = it.item_id.expect_def_id();
1962        write!(w, "{}", document(cx, it, None, HeadingOffset::H2))?;
1963        if it.name.map(|n| n.as_str() != "reference").unwrap_or(false) {
1964            write!(w, "{}", render_assoc_items(cx, it, def_id, AssocItemRender::All))
1965        } else {
1966            // We handle the "reference" primitive type on its own because we only want to list
1967            // implementations on generic types.
1968            let (concrete, synthetic, blanket_impl) =
1969                get_filtered_impls_for_reference(&cx.shared, it);
1970
1971            render_all_impls(w, cx, it, concrete, synthetic, blanket_impl)
1972        }
1973    })
1974}
1975
1976fn item_constant(
1977    cx: &Context<'_>,
1978    it: &clean::Item,
1979    generics: &clean::Generics,
1980    ty: &clean::Type,
1981    c: &clean::ConstantKind,
1982) -> impl fmt::Display {
1983    fmt::from_fn(|w| {
1984        wrap_item(w, |w| {
1985            let tcx = cx.tcx();
1986            render_attributes_in_code(w, it, "", cx)?;
1987
1988            write!(
1989                w,
1990                "{vis}const {name}{generics}: {typ}{where_clause}",
1991                vis = visibility_print_with_space(it, cx),
1992                name = it.name.unwrap(),
1993                generics = print_generics(generics, cx),
1994                typ = print_type(ty, cx),
1995                where_clause =
1996                    print_where_clause(generics, cx, 0, Ending::NoNewline).maybe_display(),
1997            )?;
1998
1999            // FIXME: The code below now prints
2000            //            ` = _; // 100i32`
2001            //        if the expression is
2002            //            `50 + 50`
2003            //        which looks just wrong.
2004            //        Should we print
2005            //            ` = 100i32;`
2006            //        instead?
2007
2008            let value = c.value(tcx);
2009            let is_literal = c.is_literal(tcx);
2010            let expr = c.expr(tcx);
2011            if value.is_some() || is_literal {
2012                write!(w, " = {expr};", expr = Escape(&expr))?;
2013            } else {
2014                w.write_str(";")?;
2015            }
2016
2017            if !is_literal && let Some(value) = &value {
2018                let value_lowercase = value.to_lowercase();
2019                let expr_lowercase = expr.to_lowercase();
2020
2021                if value_lowercase != expr_lowercase
2022                    && value_lowercase.trim_end_matches("i32") != expr_lowercase
2023                {
2024                    write!(w, " // {value}", value = Escape(value))?;
2025                }
2026            }
2027            Ok::<(), fmt::Error>(())
2028        })?;
2029
2030        write!(w, "{}", document(cx, it, None, HeadingOffset::H2))
2031    })
2032}
2033
2034struct DisplayStruct<'a> {
2035    ctor_kind: Option<CtorKind>,
2036    generics: &'a clean::Generics,
2037    fields: &'a [clean::Item],
2038    def_id: DefId,
2039}
2040
2041impl<'a> DisplayStruct<'a> {
2042    fn render_into<W: fmt::Write>(
2043        self,
2044        cx: &Context<'_>,
2045        it: &clean::Item,
2046        is_type_alias: bool,
2047        w: &mut W,
2048    ) -> fmt::Result {
2049        wrap_item(w, |w| {
2050            if is_type_alias {
2051                // For now the only attributes we render for type aliases are `repr` attributes.
2052                render_repr_attribute_in_code(w, cx, self.def_id)?;
2053            } else {
2054                render_attributes_in_code(w, it, "", cx)?;
2055            }
2056            write!(
2057                w,
2058                "{}",
2059                render_struct(it, Some(self.generics), self.ctor_kind, self.fields, "", true, cx)
2060            )
2061        })?;
2062
2063        if !is_type_alias {
2064            write!(w, "{}", document(cx, it, None, HeadingOffset::H2))?;
2065        }
2066
2067        let def_id = it.item_id.expect_def_id();
2068        write!(
2069            w,
2070            "{}{}{}",
2071            item_fields(cx, it, self.fields, self.ctor_kind),
2072            render_assoc_items(cx, it, def_id, AssocItemRender::All),
2073            document_type_layout(cx, def_id),
2074        )
2075    }
2076}
2077
2078fn item_struct(cx: &Context<'_>, it: &clean::Item, s: &clean::Struct) -> impl fmt::Display {
2079    fmt::from_fn(|w| {
2080        DisplayStruct {
2081            ctor_kind: s.ctor_kind,
2082            generics: &s.generics,
2083            fields: s.fields.as_slice(),
2084            def_id: it.def_id().unwrap(),
2085        }
2086        .render_into(cx, it, false, w)
2087    })
2088}
2089
2090fn item_fields(
2091    cx: &Context<'_>,
2092    it: &clean::Item,
2093    fields: &[clean::Item],
2094    ctor_kind: Option<CtorKind>,
2095) -> impl fmt::Display {
2096    fmt::from_fn(move |w| {
2097        let mut fields = fields
2098            .iter()
2099            .filter_map(|f| match f.kind {
2100                clean::StructFieldItem(ref ty) => Some((f, ty)),
2101                _ => None,
2102            })
2103            .peekable();
2104        if let None | Some(CtorKind::Fn) = ctor_kind
2105            && fields.peek().is_some()
2106        {
2107            let title = format_args!(
2108                "{}{}",
2109                if ctor_kind.is_none() { "Fields" } else { "Tuple Fields" },
2110                document_non_exhaustive_header(it),
2111            );
2112            write!(
2113                w,
2114                "{}",
2115                write_section_heading(title, "fields", Some("fields"), document_non_exhaustive(it))
2116            )?;
2117            for (index, (field, ty)) in fields.enumerate() {
2118                let field_name =
2119                    field.name.map_or_else(|| index.to_string(), |sym| sym.as_str().to_string());
2120                let id = cx.derive_id(format!("{typ}.{field_name}", typ = ItemType::StructField));
2121                write!(
2122                    w,
2123                    "<span id=\"{id}\" class=\"{item_type} section-header\">\
2124                        <a href=\"#{id}\" class=\"anchor field\">§</a>\
2125                        <code>",
2126                    item_type = ItemType::StructField,
2127                )?;
2128                render_attributes_in_code(w, field, "", cx)?;
2129                write!(
2130                    w,
2131                    "{field_name}: {ty}</code>\
2132                    </span>\
2133                    {doc}",
2134                    ty = print_type(ty, cx),
2135                    doc = document(cx, field, Some(it), HeadingOffset::H3),
2136                )?;
2137            }
2138        }
2139        Ok(())
2140    })
2141}
2142
2143fn item_static(
2144    cx: &Context<'_>,
2145    it: &clean::Item,
2146    s: &clean::Static,
2147    safety: Option<hir::Safety>,
2148) -> impl fmt::Display {
2149    fmt::from_fn(move |w| {
2150        wrap_item(w, |w| {
2151            render_attributes_in_code(w, it, "", cx)?;
2152            write!(
2153                w,
2154                "{vis}{safe}static {mutability}{name}: {typ}",
2155                vis = visibility_print_with_space(it, cx),
2156                safe = safety.map(|safe| safe.prefix_str()).unwrap_or(""),
2157                mutability = s.mutability.print_with_space(),
2158                name = it.name.unwrap(),
2159                typ = print_type(&s.type_, cx)
2160            )
2161        })?;
2162
2163        write!(w, "{}", document(cx, it, None, HeadingOffset::H2))
2164    })
2165}
2166
2167fn item_foreign_type(cx: &Context<'_>, it: &clean::Item) -> impl fmt::Display {
2168    fmt::from_fn(|w| {
2169        wrap_item(w, |w| {
2170            w.write_str("extern {\n")?;
2171            render_attributes_in_code(w, it, "", cx)?;
2172            write!(w, "    {}type {};\n}}", visibility_print_with_space(it, cx), it.name.unwrap())
2173        })?;
2174
2175        write!(
2176            w,
2177            "{}{}",
2178            document(cx, it, None, HeadingOffset::H2),
2179            render_assoc_items(cx, it, it.item_id.expect_def_id(), AssocItemRender::All)
2180        )
2181    })
2182}
2183
2184fn item_keyword_or_attribute(cx: &Context<'_>, it: &clean::Item) -> impl fmt::Display {
2185    document(cx, it, None, HeadingOffset::H2)
2186}
2187
2188/// Compare two strings treating multi-digit numbers as single units (i.e. natural sort order).
2189///
2190/// This code is copied from [`rustfmt`], and should probably be released as a crate at some point.
2191///
2192/// [`rustfmt`]:https://github.com/rust-lang/rustfmt/blob/rustfmt-2.0.0-rc.2/src/formatting/reorder.rs#L32
2193pub(crate) fn compare_names(left: &str, right: &str) -> Ordering {
2194    let mut left = left.chars().peekable();
2195    let mut right = right.chars().peekable();
2196
2197    loop {
2198        // The strings are equal so far and not inside a number in both sides
2199        let (l, r) = match (left.next(), right.next()) {
2200            // Is this the end of both strings?
2201            (None, None) => return Ordering::Equal,
2202            // If for one, the shorter one is considered smaller
2203            (None, Some(_)) => return Ordering::Less,
2204            (Some(_), None) => return Ordering::Greater,
2205            (Some(l), Some(r)) => (l, r),
2206        };
2207        let next_ordering = match (l.to_digit(10), r.to_digit(10)) {
2208            // If neither is a digit, just compare them
2209            (None, None) => Ord::cmp(&l, &r),
2210            // The one with shorter non-digit run is smaller
2211            // For `strverscmp` it's smaller iff next char in longer is greater than digits
2212            (None, Some(_)) => Ordering::Greater,
2213            (Some(_), None) => Ordering::Less,
2214            // If both start numbers, we have to compare the numbers
2215            (Some(l), Some(r)) => {
2216                if l == 0 || r == 0 {
2217                    // Fraction mode: compare as if there was leading `0.`
2218                    let ordering = Ord::cmp(&l, &r);
2219                    if ordering != Ordering::Equal {
2220                        return ordering;
2221                    }
2222                    loop {
2223                        // Get next pair
2224                        let (l, r) = match (left.peek(), right.peek()) {
2225                            // Is this the end of both strings?
2226                            (None, None) => return Ordering::Equal,
2227                            // If for one, the shorter one is considered smaller
2228                            (None, Some(_)) => return Ordering::Less,
2229                            (Some(_), None) => return Ordering::Greater,
2230                            (Some(l), Some(r)) => (l, r),
2231                        };
2232                        // Are they digits?
2233                        match (l.to_digit(10), r.to_digit(10)) {
2234                            // If out of digits, use the stored ordering due to equal length
2235                            (None, None) => break Ordering::Equal,
2236                            // If one is shorter, it's smaller
2237                            (None, Some(_)) => return Ordering::Less,
2238                            (Some(_), None) => return Ordering::Greater,
2239                            // If both are digits, consume them and take into account
2240                            (Some(l), Some(r)) => {
2241                                left.next();
2242                                right.next();
2243                                let ordering = Ord::cmp(&l, &r);
2244                                if ordering != Ordering::Equal {
2245                                    return ordering;
2246                                }
2247                            }
2248                        }
2249                    }
2250                } else {
2251                    // Integer mode
2252                    let mut same_length_ordering = Ord::cmp(&l, &r);
2253                    loop {
2254                        // Get next pair
2255                        let (l, r) = match (left.peek(), right.peek()) {
2256                            // Is this the end of both strings?
2257                            (None, None) => return same_length_ordering,
2258                            // If for one, the shorter one is considered smaller
2259                            (None, Some(_)) => return Ordering::Less,
2260                            (Some(_), None) => return Ordering::Greater,
2261                            (Some(l), Some(r)) => (l, r),
2262                        };
2263                        // Are they digits?
2264                        match (l.to_digit(10), r.to_digit(10)) {
2265                            // If out of digits, use the stored ordering due to equal length
2266                            (None, None) => break same_length_ordering,
2267                            // If one is shorter, it's smaller
2268                            (None, Some(_)) => return Ordering::Less,
2269                            (Some(_), None) => return Ordering::Greater,
2270                            // If both are digits, consume them and take into account
2271                            (Some(l), Some(r)) => {
2272                                left.next();
2273                                right.next();
2274                                same_length_ordering = same_length_ordering.then(Ord::cmp(&l, &r));
2275                            }
2276                        }
2277                    }
2278                }
2279            }
2280        };
2281        if next_ordering != Ordering::Equal {
2282            return next_ordering;
2283        }
2284    }
2285}
2286
2287pub(super) fn full_path(cx: &Context<'_>, item: &clean::Item) -> String {
2288    let mut s = join_path_syms(&cx.current);
2289    s.push_str("::");
2290    s.push_str(item.name.unwrap().as_str());
2291    s
2292}
2293
2294pub(super) fn print_item_path(item: &clean::Item) -> impl Display {
2295    fmt::from_fn(move |f| match item.kind {
2296        clean::ItemKind::ModuleItem(..) => {
2297            write!(f, "{}index.html", ensure_trailing_slash(item.name.unwrap().as_str()))
2298        }
2299        _ => f.write_str(&item.html_filename()),
2300    })
2301}
2302
2303pub(super) fn print_ty_path(ty: ItemType, name: &str) -> impl Display {
2304    fmt::from_fn(move |f| match ty {
2305        ItemType::Module => write!(f, "{}index.html", ensure_trailing_slash(name)),
2306        _ => write!(f, "{ty}.{name}.html"),
2307    })
2308}
2309
2310fn print_bounds(
2311    bounds: &[clean::GenericBound],
2312    trait_alias: bool,
2313    cx: &Context<'_>,
2314) -> impl Display {
2315    (!bounds.is_empty())
2316        .then_some(fmt::from_fn(move |f| {
2317            let has_lots_of_bounds = bounds.len() > 2;
2318            let inter_str = if has_lots_of_bounds { "\n    + " } else { " + " };
2319            if !trait_alias {
2320                if has_lots_of_bounds {
2321                    f.write_str(":\n    ")?;
2322                } else {
2323                    f.write_str(": ")?;
2324                }
2325            }
2326
2327            bounds.iter().map(|p| print_generic_bound(p, cx)).joined(inter_str, f)
2328        }))
2329        .maybe_display()
2330}
2331
2332fn wrap_item<W, F>(w: &mut W, f: F) -> fmt::Result
2333where
2334    W: fmt::Write,
2335    F: FnOnce(&mut W) -> fmt::Result,
2336{
2337    w.write_str(r#"<pre class="rust item-decl"><code>"#)?;
2338    f(w)?;
2339    w.write_str("</code></pre>")
2340}
2341
2342#[derive(PartialEq, Eq)]
2343pub(super) struct ImplString {
2344    // Plain text (not HTML text) because this is only used for sorting purposes, and the plain
2345    // text is much shorter and thus faster to compare.
2346    cmp_text: String,
2347}
2348
2349impl ImplString {
2350    fn new_impl(i: &Impl, cx: &Context<'_>) -> Self {
2351        let impl_ = i.inner_impl();
2352        Self { cmp_text: format!("{:#}", print_impl(impl_, false, cx)) }
2353    }
2354
2355    pub(super) fn new_path(i: &Impl, cx: &Context<'_>) -> Option<Self> {
2356        let path = i.inner_impl().trait_.as_ref()?;
2357        Some(Self { cmp_text: format!("{:#}", print_path(path, cx)) })
2358    }
2359}
2360
2361impl PartialOrd for ImplString {
2362    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
2363        Some(Ord::cmp(self, other))
2364    }
2365}
2366
2367impl Ord for ImplString {
2368    fn cmp(&self, other: &Self) -> Ordering {
2369        // Negative impls are naturally sorted first, because `impl !A` is less than `impl B` for
2370        // any value of `B`, because `!` is less than any identifier-starting char.
2371        compare_names(&self.cmp_text, &other.cmp_text)
2372    }
2373}
2374
2375fn render_implementor(
2376    cx: &Context<'_>,
2377    implementor: &Impl,
2378    trait_: &clean::Item,
2379    implementor_dups: &FxHashMap<Symbol, (DefId, bool)>,
2380    aliases: &[String],
2381) -> impl fmt::Display {
2382    // If there's already another implementor that has the same abridged name, use the
2383    // full path, for example in `std::iter::ExactSizeIterator`
2384    let use_absolute = match implementor.inner_impl().for_ {
2385        clean::Type::Path { ref path, .. }
2386        | clean::BorrowedRef { type_: clean::Type::Path { ref path, .. }, .. }
2387            if !path.is_assoc_ty() =>
2388        {
2389            implementor_dups[&path.last()].1
2390        }
2391        _ => false,
2392    };
2393    render_impl(
2394        cx,
2395        implementor,
2396        trait_,
2397        AssocItemLink::Anchor(None),
2398        RenderMode::Normal,
2399        Some(use_absolute),
2400        aliases,
2401        ImplRenderingParameters {
2402            show_def_docs: false,
2403            show_default_items: false,
2404            show_non_assoc_items: false,
2405            toggle_open_by_default: false,
2406        },
2407    )
2408}
2409
2410fn render_union(
2411    it: &clean::Item,
2412    g: Option<&clean::Generics>,
2413    fields: &[clean::Item],
2414    def_id: DefId,
2415    is_type_alias: bool,
2416    cx: &Context<'_>,
2417) -> impl Display {
2418    fmt::from_fn(move |mut f| {
2419        if is_type_alias {
2420            // For now the only attributes we render for type aliases are `repr` attributes.
2421            render_repr_attribute_in_code(f, cx, def_id)?;
2422        } else {
2423            render_attributes_in_code(f, it, "", cx)?;
2424        }
2425        write!(f, "{}union {}", visibility_print_with_space(it, cx), it.name.unwrap(),)?;
2426
2427        let where_displayed = if let Some(generics) = g {
2428            write!(f, "{}", print_generics(generics, cx))?;
2429            if let Some(where_clause) = print_where_clause(generics, cx, 0, Ending::Newline) {
2430                write!(f, "{where_clause}")?;
2431                true
2432            } else {
2433                false
2434            }
2435        } else {
2436            false
2437        };
2438
2439        // If there wasn't a `where` clause, we add a whitespace.
2440        if !where_displayed {
2441            f.write_str(" ")?;
2442        }
2443
2444        writeln!(f, "{{")?;
2445        let count_fields =
2446            fields.iter().filter(|field| matches!(field.kind, clean::StructFieldItem(..))).count();
2447        let toggle = should_hide_fields(count_fields);
2448        if toggle {
2449            toggle_open(&mut f, format_args!("{count_fields} fields"));
2450        }
2451
2452        for field in fields {
2453            if let clean::StructFieldItem(ref ty) = field.kind {
2454                render_attributes_in_code(&mut f, field, "    ", cx)?;
2455                writeln!(
2456                    f,
2457                    "    {}{}: {},",
2458                    visibility_print_with_space(field, cx),
2459                    field.name.unwrap(),
2460                    print_type(ty, cx)
2461                )?;
2462            }
2463        }
2464
2465        if it.has_stripped_entries().unwrap() {
2466            writeln!(f, "    <span class=\"comment\">/* private fields */</span>")?;
2467        }
2468        if toggle {
2469            toggle_close(&mut f);
2470        }
2471        f.write_str("}").unwrap();
2472        Ok(())
2473    })
2474}
2475
2476fn render_struct(
2477    it: &clean::Item,
2478    g: Option<&clean::Generics>,
2479    ty: Option<CtorKind>,
2480    fields: &[clean::Item],
2481    tab: &str,
2482    structhead: bool,
2483    cx: &Context<'_>,
2484) -> impl fmt::Display {
2485    fmt::from_fn(move |w| {
2486        write!(
2487            w,
2488            "{}{}{}",
2489            visibility_print_with_space(it, cx),
2490            if structhead { "struct " } else { "" },
2491            it.name.unwrap()
2492        )?;
2493        if let Some(g) = g {
2494            write!(w, "{}", print_generics(g, cx))?;
2495        }
2496        write!(
2497            w,
2498            "{}",
2499            render_struct_fields(
2500                g,
2501                ty,
2502                fields,
2503                tab,
2504                structhead,
2505                it.has_stripped_entries().unwrap_or(false),
2506                cx,
2507            )
2508        )
2509    })
2510}
2511
2512fn render_struct_fields(
2513    g: Option<&clean::Generics>,
2514    ty: Option<CtorKind>,
2515    fields: &[clean::Item],
2516    tab: &str,
2517    structhead: bool,
2518    has_stripped_entries: bool,
2519    cx: &Context<'_>,
2520) -> impl fmt::Display {
2521    fmt::from_fn(move |w| {
2522        match ty {
2523            None => {
2524                let where_displayed = if let Some(generics) = g
2525                    && let Some(where_clause) = print_where_clause(generics, cx, 0, Ending::Newline)
2526                {
2527                    write!(w, "{where_clause}")?;
2528                    true
2529                } else {
2530                    false
2531                };
2532
2533                // If there wasn't a `where` clause, we add a whitespace.
2534                if !where_displayed {
2535                    w.write_str(" {")?;
2536                } else {
2537                    w.write_str("{")?;
2538                }
2539                let count_fields =
2540                    fields.iter().filter(|f| matches!(f.kind, clean::StructFieldItem(..))).count();
2541                let has_visible_fields = count_fields > 0;
2542                let toggle = should_hide_fields(count_fields);
2543                if toggle {
2544                    toggle_open(&mut *w, format_args!("{count_fields} fields"));
2545                }
2546                if has_visible_fields {
2547                    writeln!(w)?;
2548                }
2549                for field in fields {
2550                    if let clean::StructFieldItem(ref ty) = field.kind {
2551                        render_attributes_in_code(w, field, format_args!("{tab}    "), cx)?;
2552                        writeln!(
2553                            w,
2554                            "{tab}    {vis}{name}: {ty},",
2555                            vis = visibility_print_with_space(field, cx),
2556                            name = field.name.unwrap(),
2557                            ty = print_type(ty, cx)
2558                        )?;
2559                    }
2560                }
2561
2562                if has_visible_fields {
2563                    if has_stripped_entries {
2564                        writeln!(
2565                            w,
2566                            "{tab}    <span class=\"comment\">/* private fields */</span>"
2567                        )?;
2568                    }
2569                    write!(w, "{tab}")?;
2570                } else if has_stripped_entries {
2571                    write!(w, " <span class=\"comment\">/* private fields */</span> ")?;
2572                }
2573                if toggle {
2574                    toggle_close(&mut *w);
2575                }
2576                w.write_str("}")?;
2577            }
2578            Some(CtorKind::Fn) => {
2579                w.write_str("(")?;
2580                if !fields.is_empty()
2581                    && fields.iter().all(|field| {
2582                        matches!(field.kind, clean::StrippedItem(clean::StructFieldItem(..)))
2583                    })
2584                {
2585                    write!(w, "<span class=\"comment\">/* private fields */</span>")?;
2586                } else {
2587                    for (i, field) in fields.iter().enumerate() {
2588                        if i > 0 {
2589                            w.write_str(", ")?;
2590                        }
2591                        match field.kind {
2592                            clean::StrippedItem(clean::StructFieldItem(..)) => {
2593                                write!(w, "_")?;
2594                            }
2595                            clean::StructFieldItem(ref ty) => {
2596                                write!(
2597                                    w,
2598                                    "{}{}",
2599                                    visibility_print_with_space(field, cx),
2600                                    print_type(ty, cx),
2601                                )?;
2602                            }
2603                            _ => unreachable!(),
2604                        }
2605                    }
2606                }
2607                w.write_str(")")?;
2608                if let Some(g) = g {
2609                    write!(
2610                        w,
2611                        "{}",
2612                        print_where_clause(g, cx, 0, Ending::NoNewline).maybe_display()
2613                    )?;
2614                }
2615                // We only want a ";" when we are displaying a tuple struct, not a variant tuple struct.
2616                if structhead {
2617                    w.write_str(";")?;
2618                }
2619            }
2620            Some(CtorKind::Const) => {
2621                // Needed for PhantomData.
2622                if let Some(g) = g {
2623                    write!(
2624                        w,
2625                        "{}",
2626                        print_where_clause(g, cx, 0, Ending::NoNewline).maybe_display()
2627                    )?;
2628                }
2629                w.write_str(";")?;
2630            }
2631        }
2632        Ok(())
2633    })
2634}
2635
2636fn document_non_exhaustive_header(item: &clean::Item) -> &str {
2637    if item.is_non_exhaustive() { " (Non-exhaustive)" } else { "" }
2638}
2639
2640fn document_non_exhaustive(item: &clean::Item) -> impl Display {
2641    fmt::from_fn(|f| {
2642        if item.is_non_exhaustive() {
2643            write!(
2644                f,
2645                "<details class=\"toggle non-exhaustive\">\
2646                    <summary class=\"hideme\"><span>{}</span></summary>\
2647                    <div class=\"docblock\">",
2648                {
2649                    if item.is_struct() {
2650                        "This struct is marked as non-exhaustive"
2651                    } else if item.is_enum() {
2652                        "This enum is marked as non-exhaustive"
2653                    } else if item.is_variant() {
2654                        "This variant is marked as non-exhaustive"
2655                    } else {
2656                        "This type is marked as non-exhaustive"
2657                    }
2658                }
2659            )?;
2660
2661            if item.is_struct() {
2662                f.write_str(
2663                    "Non-exhaustive structs could have additional fields added in future. \
2664                    Therefore, non-exhaustive structs cannot be constructed in external crates \
2665                    using the traditional <code>Struct { .. }</code> syntax; cannot be \
2666                    matched against without a wildcard <code>..</code>; and \
2667                    struct update syntax will not work.",
2668                )?;
2669            } else if item.is_enum() {
2670                f.write_str(
2671                    "Non-exhaustive enums could have additional variants added in future. \
2672                    Therefore, when matching against variants of non-exhaustive enums, an \
2673                    extra wildcard arm must be added to account for any future variants.",
2674                )?;
2675            } else if item.is_variant() {
2676                f.write_str(
2677                    "Non-exhaustive enum variants could have additional fields added in future. \
2678                    Therefore, non-exhaustive enum variants cannot be constructed in external \
2679                    crates and cannot be matched against.",
2680                )?;
2681            } else {
2682                f.write_str(
2683                    "This type will require a wildcard arm in any match statements or constructors.",
2684                )?;
2685            }
2686
2687            f.write_str("</div></details>")?;
2688        }
2689        Ok(())
2690    })
2691}
2692
2693fn pluralize(count: usize) -> &'static str {
2694    if count > 1 { "s" } else { "" }
2695}