rustdoc/html/
format.rs

1//! HTML formatting module
2//!
3//! This module contains a large number of `Display` implementations for
4//! various types in `rustdoc::clean`.
5//!
6//! These implementations all emit HTML. As an internal implementation detail,
7//! some of them support an alternate format that emits text, but that should
8//! not be used external to this module.
9
10use std::cmp::Ordering;
11use std::fmt::{self, Display, Write};
12use std::iter::{self, once};
13use std::slice;
14
15use itertools::{Either, Itertools};
16use rustc_abi::ExternAbi;
17use rustc_ast::join_path_syms;
18use rustc_data_structures::fx::FxHashSet;
19use rustc_hir as hir;
20use rustc_hir::def::DefKind;
21use rustc_hir::def_id::{DefId, LOCAL_CRATE};
22use rustc_hir::{ConstStability, StabilityLevel, StableSince};
23use rustc_metadata::creader::{CStore, LoadedMacro};
24use rustc_middle::ty::{self, TyCtxt, TypingMode};
25use rustc_span::symbol::kw;
26use rustc_span::{Symbol, sym};
27use tracing::{debug, trace};
28
29use super::url_parts_builder::UrlPartsBuilder;
30use crate::clean::types::ExternalLocation;
31use crate::clean::utils::find_nearest_parent_module;
32use crate::clean::{self, ExternalCrate, PrimitiveType};
33use crate::display::{Joined as _, MaybeDisplay as _};
34use crate::formats::cache::Cache;
35use crate::formats::item_type::ItemType;
36use crate::html::escape::{Escape, EscapeBodyText};
37use crate::html::render::Context;
38use crate::passes::collect_intra_doc_links::UrlFragment;
39
40pub(crate) fn write_str(s: &mut String, f: fmt::Arguments<'_>) {
41    s.write_fmt(f).unwrap();
42}
43
44pub(crate) fn print_generic_bounds(
45    bounds: &[clean::GenericBound],
46    cx: &Context<'_>,
47) -> impl Display {
48    fmt::from_fn(move |f| {
49        let mut bounds_dup = FxHashSet::default();
50
51        bounds
52            .iter()
53            .filter(move |b| bounds_dup.insert(*b))
54            .map(|bound| bound.print(cx))
55            .joined(" + ", f)
56    })
57}
58
59impl clean::GenericParamDef {
60    pub(crate) fn print(&self, cx: &Context<'_>) -> impl Display {
61        fmt::from_fn(move |f| match &self.kind {
62            clean::GenericParamDefKind::Lifetime { outlives } => {
63                write!(f, "{}", self.name)?;
64
65                if !outlives.is_empty() {
66                    f.write_str(": ")?;
67                    outlives.iter().map(|lt| lt.print()).joined(" + ", f)?;
68                }
69
70                Ok(())
71            }
72            clean::GenericParamDefKind::Type { bounds, default, .. } => {
73                f.write_str(self.name.as_str())?;
74
75                if !bounds.is_empty() {
76                    f.write_str(": ")?;
77                    print_generic_bounds(bounds, cx).fmt(f)?;
78                }
79
80                if let Some(ty) = default {
81                    f.write_str(" = ")?;
82                    ty.print(cx).fmt(f)?;
83                }
84
85                Ok(())
86            }
87            clean::GenericParamDefKind::Const { ty, default, .. } => {
88                write!(f, "const {}: ", self.name)?;
89                ty.print(cx).fmt(f)?;
90
91                if let Some(default) = default {
92                    f.write_str(" = ")?;
93                    if f.alternate() {
94                        write!(f, "{default}")?;
95                    } else {
96                        write!(f, "{}", Escape(default))?;
97                    }
98                }
99
100                Ok(())
101            }
102        })
103    }
104}
105
106impl clean::Generics {
107    pub(crate) fn print(&self, cx: &Context<'_>) -> impl Display {
108        fmt::from_fn(move |f| {
109            let mut real_params = self.params.iter().filter(|p| !p.is_synthetic_param()).peekable();
110            if real_params.peek().is_none() {
111                return Ok(());
112            }
113
114            let real_params =
115                fmt::from_fn(|f| real_params.clone().map(|g| g.print(cx)).joined(", ", f));
116            if f.alternate() {
117                write!(f, "<{real_params:#}>")
118            } else {
119                write!(f, "&lt;{real_params}&gt;")
120            }
121        })
122    }
123}
124
125#[derive(Clone, Copy, PartialEq, Eq)]
126pub(crate) enum Ending {
127    Newline,
128    NoNewline,
129}
130
131fn print_where_predicate(predicate: &clean::WherePredicate, cx: &Context<'_>) -> impl Display {
132    fmt::from_fn(move |f| {
133        match predicate {
134            clean::WherePredicate::BoundPredicate { ty, bounds, bound_params } => {
135                print_higher_ranked_params_with_space(bound_params, cx, "for").fmt(f)?;
136                ty.print(cx).fmt(f)?;
137                f.write_str(":")?;
138                if !bounds.is_empty() {
139                    f.write_str(" ")?;
140                    print_generic_bounds(bounds, cx).fmt(f)?;
141                }
142                Ok(())
143            }
144            clean::WherePredicate::RegionPredicate { lifetime, bounds } => {
145                // We don't need to check `alternate` since we can be certain that neither
146                // the lifetime nor the bounds contain any characters which need escaping.
147                write!(f, "{}:", lifetime.print())?;
148                if !bounds.is_empty() {
149                    write!(f, " {}", print_generic_bounds(bounds, cx))?;
150                }
151                Ok(())
152            }
153            clean::WherePredicate::EqPredicate { lhs, rhs } => {
154                if f.alternate() {
155                    write!(f, "{:#} == {:#}", lhs.print(cx), rhs.print(cx))
156                } else {
157                    write!(f, "{} == {}", lhs.print(cx), rhs.print(cx))
158                }
159            }
160        }
161    })
162}
163
164/// * The Generics from which to emit a where-clause.
165/// * The number of spaces to indent each line with.
166/// * Whether the where-clause needs to add a comma and newline after the last bound.
167pub(crate) fn print_where_clause(
168    gens: &clean::Generics,
169    cx: &Context<'_>,
170    indent: usize,
171    ending: Ending,
172) -> Option<impl Display> {
173    if gens.where_predicates.is_empty() {
174        return None;
175    }
176
177    Some(fmt::from_fn(move |f| {
178        let where_preds = fmt::from_fn(|f| {
179            gens.where_predicates
180                .iter()
181                .map(|predicate| {
182                    fmt::from_fn(|f| {
183                        if f.alternate() {
184                            f.write_str(" ")?;
185                        } else {
186                            f.write_str("\n")?;
187                        }
188                        print_where_predicate(predicate, cx).fmt(f)
189                    })
190                })
191                .joined(",", f)
192        });
193
194        let clause = if f.alternate() {
195            if ending == Ending::Newline {
196                format!(" where{where_preds},")
197            } else {
198                format!(" where{where_preds}")
199            }
200        } else {
201            let mut br_with_padding = String::with_capacity(6 * indent + 28);
202            br_with_padding.push('\n');
203
204            let where_indent = 3;
205            let padding_amount = if ending == Ending::Newline {
206                indent + 4
207            } else if indent == 0 {
208                4
209            } else {
210                indent + where_indent + "where ".len()
211            };
212
213            for _ in 0..padding_amount {
214                br_with_padding.push(' ');
215            }
216            let where_preds = where_preds.to_string().replace('\n', &br_with_padding);
217
218            if ending == Ending::Newline {
219                let mut clause = " ".repeat(indent.saturating_sub(1));
220                write!(clause, "<div class=\"where\">where{where_preds},</div>")?;
221                clause
222            } else {
223                // insert a newline after a single space but before multiple spaces at the start
224                if indent == 0 {
225                    format!("\n<span class=\"where\">where{where_preds}</span>")
226                } else {
227                    // put the first one on the same line as the 'where' keyword
228                    let where_preds = where_preds.replacen(&br_with_padding, " ", 1);
229
230                    let mut clause = br_with_padding;
231                    // +1 is for `\n`.
232                    clause.truncate(indent + 1 + where_indent);
233
234                    write!(clause, "<span class=\"where\">where{where_preds}</span>")?;
235                    clause
236                }
237            }
238        };
239        write!(f, "{clause}")
240    }))
241}
242
243impl clean::Lifetime {
244    pub(crate) fn print(&self) -> impl Display {
245        self.0.as_str()
246    }
247}
248
249impl clean::ConstantKind {
250    pub(crate) fn print(&self, tcx: TyCtxt<'_>) -> impl Display {
251        let expr = self.expr(tcx);
252        fmt::from_fn(move |f| {
253            if f.alternate() { f.write_str(&expr) } else { write!(f, "{}", Escape(&expr)) }
254        })
255    }
256}
257
258impl clean::PolyTrait {
259    fn print(&self, cx: &Context<'_>) -> impl Display {
260        fmt::from_fn(move |f| {
261            print_higher_ranked_params_with_space(&self.generic_params, cx, "for").fmt(f)?;
262            self.trait_.print(cx).fmt(f)
263        })
264    }
265}
266
267impl clean::GenericBound {
268    pub(crate) fn print(&self, cx: &Context<'_>) -> impl Display {
269        fmt::from_fn(move |f| match self {
270            clean::GenericBound::Outlives(lt) => write!(f, "{}", lt.print()),
271            clean::GenericBound::TraitBound(ty, modifiers) => {
272                // `const` and `[const]` trait bounds are experimental; don't render them.
273                let hir::TraitBoundModifiers { polarity, constness: _ } = modifiers;
274                f.write_str(match polarity {
275                    hir::BoundPolarity::Positive => "",
276                    hir::BoundPolarity::Maybe(_) => "?",
277                    hir::BoundPolarity::Negative(_) => "!",
278                })?;
279                ty.print(cx).fmt(f)
280            }
281            clean::GenericBound::Use(args) => {
282                if f.alternate() {
283                    f.write_str("use<")?;
284                } else {
285                    f.write_str("use&lt;")?;
286                }
287                args.iter().map(|arg| arg.name()).joined(", ", f)?;
288                if f.alternate() { f.write_str(">") } else { f.write_str("&gt;") }
289            }
290        })
291    }
292}
293
294impl clean::GenericArgs {
295    fn print(&self, cx: &Context<'_>) -> impl Display {
296        fmt::from_fn(move |f| {
297            match self {
298                clean::GenericArgs::AngleBracketed { args, constraints } => {
299                    if !args.is_empty() || !constraints.is_empty() {
300                        if f.alternate() {
301                            f.write_str("<")?;
302                        } else {
303                            f.write_str("&lt;")?;
304                        }
305
306                        [Either::Left(args), Either::Right(constraints)]
307                            .into_iter()
308                            .flat_map(Either::factor_into_iter)
309                            .map(|either| {
310                                either.map_either(
311                                    |arg| arg.print(cx),
312                                    |constraint| constraint.print(cx),
313                                )
314                            })
315                            .joined(", ", f)?;
316
317                        if f.alternate() {
318                            f.write_str(">")?;
319                        } else {
320                            f.write_str("&gt;")?;
321                        }
322                    }
323                }
324                clean::GenericArgs::Parenthesized { inputs, output } => {
325                    f.write_str("(")?;
326                    inputs.iter().map(|ty| ty.print(cx)).joined(", ", f)?;
327                    f.write_str(")")?;
328                    if let Some(ref ty) = *output {
329                        if f.alternate() {
330                            write!(f, " -> {:#}", ty.print(cx))?;
331                        } else {
332                            write!(f, " -&gt; {}", ty.print(cx))?;
333                        }
334                    }
335                }
336                clean::GenericArgs::ReturnTypeNotation => {
337                    f.write_str("(..)")?;
338                }
339            }
340            Ok(())
341        })
342    }
343}
344
345// Possible errors when computing href link source for a `DefId`
346#[derive(PartialEq, Eq)]
347pub(crate) enum HrefError {
348    /// This item is known to rustdoc, but from a crate that does not have documentation generated.
349    ///
350    /// This can only happen for non-local items.
351    ///
352    /// # Example
353    ///
354    /// Crate `a` defines a public trait and crate `b` – the target crate that depends on `a` –
355    /// implements it for a local type.
356    /// We document `b` but **not** `a` (we only _build_ the latter – with `rustc`):
357    ///
358    /// ```sh
359    /// rustc a.rs --crate-type=lib
360    /// rustdoc b.rs --crate-type=lib --extern=a=liba.rlib
361    /// ```
362    ///
363    /// Now, the associated items in the trait impl want to link to the corresponding item in the
364    /// trait declaration (see `html::render::assoc_href_attr`) but it's not available since their
365    /// *documentation (was) not built*.
366    DocumentationNotBuilt,
367    /// This can only happen for non-local items when `--document-private-items` is not passed.
368    Private,
369    // Not in external cache, href link should be in same page
370    NotInExternalCache,
371    /// Refers to an unnamable item, such as one defined within a function or const block.
372    UnnamableItem,
373}
374
375/// This function is to get the external macro path because they are not in the cache used in
376/// `href_with_root_path`.
377fn generate_macro_def_id_path(
378    def_id: DefId,
379    cx: &Context<'_>,
380    root_path: Option<&str>,
381) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
382    let tcx = cx.tcx();
383    let crate_name = tcx.crate_name(def_id.krate);
384    let cache = cx.cache();
385
386    let fqp = clean::inline::item_relative_path(tcx, def_id);
387    let mut relative = fqp.iter().copied();
388    let cstore = CStore::from_tcx(tcx);
389    // We need this to prevent a `panic` when this function is used from intra doc links...
390    if !cstore.has_crate_data(def_id.krate) {
391        debug!("No data for crate {crate_name}");
392        return Err(HrefError::NotInExternalCache);
393    }
394    // Check to see if it is a macro 2.0 or built-in macro.
395    // More information in <https://rust-lang.github.io/rfcs/1584-macros.html>.
396    let is_macro_2 = match cstore.load_macro_untracked(def_id, tcx) {
397        // If `def.macro_rules` is `true`, then it's not a macro 2.0.
398        LoadedMacro::MacroDef { def, .. } => !def.macro_rules,
399        _ => false,
400    };
401
402    let mut path = if is_macro_2 {
403        once(crate_name).chain(relative).collect()
404    } else {
405        vec![crate_name, relative.next_back().unwrap()]
406    };
407    if path.len() < 2 {
408        // The minimum we can have is the crate name followed by the macro name. If shorter, then
409        // it means that `relative` was empty, which is an error.
410        debug!("macro path cannot be empty!");
411        return Err(HrefError::NotInExternalCache);
412    }
413
414    if let Some(last) = path.last_mut() {
415        *last = Symbol::intern(&format!("macro.{last}.html"));
416    }
417
418    let url = match cache.extern_locations[&def_id.krate] {
419        ExternalLocation::Remote(ref s) => {
420            // `ExternalLocation::Remote` always end with a `/`.
421            format!("{s}{path}", path = fmt::from_fn(|f| path.iter().joined("/", f)))
422        }
423        ExternalLocation::Local => {
424            // `root_path` always end with a `/`.
425            format!(
426                "{root_path}{path}",
427                root_path = root_path.unwrap_or(""),
428                path = fmt::from_fn(|f| path.iter().joined("/", f))
429            )
430        }
431        ExternalLocation::Unknown => {
432            debug!("crate {crate_name} not in cache when linkifying macros");
433            return Err(HrefError::NotInExternalCache);
434        }
435    };
436    Ok((url, ItemType::Macro, fqp))
437}
438
439fn generate_item_def_id_path(
440    mut def_id: DefId,
441    original_def_id: DefId,
442    cx: &Context<'_>,
443    root_path: Option<&str>,
444    original_def_kind: DefKind,
445) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
446    use rustc_middle::traits::ObligationCause;
447    use rustc_trait_selection::infer::TyCtxtInferExt;
448    use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt;
449
450    let tcx = cx.tcx();
451    let crate_name = tcx.crate_name(def_id.krate);
452
453    // No need to try to infer the actual parent item if it's not an associated item from the `impl`
454    // block.
455    if def_id != original_def_id && matches!(tcx.def_kind(def_id), DefKind::Impl { .. }) {
456        let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
457        def_id = infcx
458            .at(&ObligationCause::dummy(), tcx.param_env(def_id))
459            .query_normalize(ty::Binder::dummy(tcx.type_of(def_id).instantiate_identity()))
460            .map(|resolved| infcx.resolve_vars_if_possible(resolved.value))
461            .ok()
462            .and_then(|normalized| normalized.skip_binder().ty_adt_def())
463            .map(|adt| adt.did())
464            .unwrap_or(def_id);
465    }
466
467    let relative = clean::inline::item_relative_path(tcx, def_id);
468    let fqp: Vec<Symbol> = once(crate_name).chain(relative).collect();
469
470    let def_kind = tcx.def_kind(def_id);
471    let shortty = def_kind.into();
472    let module_fqp = to_module_fqp(shortty, &fqp);
473    let mut is_remote = false;
474
475    let url_parts = url_parts(cx.cache(), def_id, module_fqp, &cx.current, &mut is_remote)?;
476    let mut url_parts = make_href(root_path, shortty, url_parts, &fqp, is_remote);
477    if def_id != original_def_id {
478        let kind = ItemType::from_def_kind(original_def_kind, Some(def_kind));
479        url_parts = format!("{url_parts}#{kind}.{}", tcx.item_name(original_def_id))
480    };
481    Ok((url_parts, shortty, fqp))
482}
483
484/// Checks if the given defid refers to an item that is unnamable, such as one defined in a const block.
485fn is_unnamable(tcx: TyCtxt<'_>, did: DefId) -> bool {
486    let mut cur_did = did;
487    while let Some(parent) = tcx.opt_parent(cur_did) {
488        match tcx.def_kind(parent) {
489            // items defined in these can be linked to, as long as they are visible
490            DefKind::Mod | DefKind::ForeignMod => cur_did = parent,
491            // items in impls can be linked to,
492            // as long as we can link to the item the impl is on.
493            // since associated traits are not a thing,
494            // it should not be possible to refer to an impl item if
495            // the base type is not namable.
496            DefKind::Impl { .. } => return false,
497            // everything else does not have docs generated for it
498            _ => return true,
499        }
500    }
501    return false;
502}
503
504fn to_module_fqp(shortty: ItemType, fqp: &[Symbol]) -> &[Symbol] {
505    if shortty == ItemType::Module { fqp } else { &fqp[..fqp.len() - 1] }
506}
507
508fn url_parts(
509    cache: &Cache,
510    def_id: DefId,
511    module_fqp: &[Symbol],
512    relative_to: &[Symbol],
513    is_remote: &mut bool,
514) -> Result<UrlPartsBuilder, HrefError> {
515    match cache.extern_locations[&def_id.krate] {
516        ExternalLocation::Remote(ref s) => {
517            *is_remote = true;
518            let s = s.trim_end_matches('/');
519            let mut builder = UrlPartsBuilder::singleton(s);
520            builder.extend(module_fqp.iter().copied());
521            Ok(builder)
522        }
523        ExternalLocation::Local => Ok(href_relative_parts(module_fqp, relative_to)),
524        ExternalLocation::Unknown => Err(HrefError::DocumentationNotBuilt),
525    }
526}
527
528fn make_href(
529    root_path: Option<&str>,
530    shortty: ItemType,
531    mut url_parts: UrlPartsBuilder,
532    fqp: &[Symbol],
533    is_remote: bool,
534) -> String {
535    if !is_remote && let Some(root_path) = root_path {
536        let root = root_path.trim_end_matches('/');
537        url_parts.push_front(root);
538    }
539    debug!(?url_parts);
540    match shortty {
541        ItemType::Module => {
542            url_parts.push("index.html");
543        }
544        _ => {
545            let last = fqp.last().unwrap();
546            url_parts.push_fmt(format_args!("{shortty}.{last}.html"));
547        }
548    }
549    url_parts.finish()
550}
551
552pub(crate) fn href_with_root_path(
553    original_did: DefId,
554    cx: &Context<'_>,
555    root_path: Option<&str>,
556) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
557    let tcx = cx.tcx();
558    let def_kind = tcx.def_kind(original_did);
559    let did = match def_kind {
560        DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant => {
561            // documented on their parent's page
562            tcx.parent(original_did)
563        }
564        // If this a constructor, we get the parent (either a struct or a variant) and then
565        // generate the link for this item.
566        DefKind::Ctor(..) => return href_with_root_path(tcx.parent(original_did), cx, root_path),
567        DefKind::ExternCrate => {
568            // Link to the crate itself, not the `extern crate` item.
569            if let Some(local_did) = original_did.as_local() {
570                tcx.extern_mod_stmt_cnum(local_did).unwrap_or(LOCAL_CRATE).as_def_id()
571            } else {
572                original_did
573            }
574        }
575        _ => original_did,
576    };
577    if is_unnamable(cx.tcx(), did) {
578        return Err(HrefError::UnnamableItem);
579    }
580    let cache = cx.cache();
581    let relative_to = &cx.current;
582
583    if !original_did.is_local() {
584        // If we are generating an href for the "jump to def" feature, then the only case we want
585        // to ignore is if the item is `doc(hidden)` because we can't link to it.
586        if root_path.is_some() {
587            if tcx.is_doc_hidden(original_did) {
588                return Err(HrefError::Private);
589            }
590        } else if !cache.effective_visibilities.is_directly_public(tcx, did)
591            && !cache.document_private
592            && !cache.primitive_locations.values().any(|&id| id == did)
593        {
594            return Err(HrefError::Private);
595        }
596    }
597
598    let mut is_remote = false;
599    let (fqp, shortty, url_parts) = match cache.paths.get(&did) {
600        Some(&(ref fqp, shortty)) => (fqp, shortty, {
601            let module_fqp = to_module_fqp(shortty, fqp.as_slice());
602            debug!(?fqp, ?shortty, ?module_fqp);
603            href_relative_parts(module_fqp, relative_to)
604        }),
605        None => {
606            // Associated items are handled differently with "jump to def". The anchor is generated
607            // directly here whereas for intra-doc links, we have some extra computation being
608            // performed there.
609            let def_id_to_get = if root_path.is_some() { original_did } else { did };
610            if let Some(&(ref fqp, shortty)) = cache.external_paths.get(&def_id_to_get) {
611                let module_fqp = to_module_fqp(shortty, fqp);
612                (fqp, shortty, url_parts(cache, did, module_fqp, relative_to, &mut is_remote)?)
613            } else if matches!(def_kind, DefKind::Macro(_)) {
614                return generate_macro_def_id_path(did, cx, root_path);
615            } else if did.is_local() {
616                return Err(HrefError::Private);
617            } else {
618                return generate_item_def_id_path(did, original_did, cx, root_path, def_kind);
619            }
620        }
621    };
622    let url_parts = make_href(root_path, shortty, url_parts, fqp, is_remote);
623    Ok((url_parts, shortty, fqp.clone()))
624}
625
626pub(crate) fn href(
627    did: DefId,
628    cx: &Context<'_>,
629) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
630    href_with_root_path(did, cx, None)
631}
632
633/// Both paths should only be modules.
634/// This is because modules get their own directories; that is, `std::vec` and `std::vec::Vec` will
635/// both need `../iter/trait.Iterator.html` to get at the iterator trait.
636pub(crate) fn href_relative_parts(fqp: &[Symbol], relative_to_fqp: &[Symbol]) -> UrlPartsBuilder {
637    for (i, (f, r)) in fqp.iter().zip(relative_to_fqp.iter()).enumerate() {
638        // e.g. linking to std::iter from std::vec (`dissimilar_part_count` will be 1)
639        if f != r {
640            let dissimilar_part_count = relative_to_fqp.len() - i;
641            let fqp_module = &fqp[i..];
642            return iter::repeat_n(sym::dotdot, dissimilar_part_count)
643                .chain(fqp_module.iter().copied())
644                .collect();
645        }
646    }
647    match relative_to_fqp.len().cmp(&fqp.len()) {
648        Ordering::Less => {
649            // e.g. linking to std::sync::atomic from std::sync
650            fqp[relative_to_fqp.len()..fqp.len()].iter().copied().collect()
651        }
652        Ordering::Greater => {
653            // e.g. linking to std::sync from std::sync::atomic
654            let dissimilar_part_count = relative_to_fqp.len() - fqp.len();
655            iter::repeat_n(sym::dotdot, dissimilar_part_count).collect()
656        }
657        Ordering::Equal => {
658            // linking to the same module
659            UrlPartsBuilder::new()
660        }
661    }
662}
663
664pub(crate) fn link_tooltip(
665    did: DefId,
666    fragment: &Option<UrlFragment>,
667    cx: &Context<'_>,
668) -> impl fmt::Display {
669    fmt::from_fn(move |f| {
670        let cache = cx.cache();
671        let Some((fqp, shortty)) = cache.paths.get(&did).or_else(|| cache.external_paths.get(&did))
672        else {
673            return Ok(());
674        };
675        let fqp = if *shortty == ItemType::Primitive {
676            // primitives are documented in a crate, but not actually part of it
677            slice::from_ref(fqp.last().unwrap())
678        } else {
679            fqp
680        };
681        if let &Some(UrlFragment::Item(id)) = fragment {
682            write!(f, "{} ", cx.tcx().def_descr(id))?;
683            for component in fqp {
684                write!(f, "{component}::")?;
685            }
686            write!(f, "{}", cx.tcx().item_name(id))?;
687        } else if !fqp.is_empty() {
688            write!(f, "{shortty} ")?;
689            write!(f, "{}", join_path_syms(fqp))?;
690        }
691        Ok(())
692    })
693}
694
695/// Used to render a [`clean::Path`].
696fn resolved_path(
697    w: &mut fmt::Formatter<'_>,
698    did: DefId,
699    path: &clean::Path,
700    print_all: bool,
701    use_absolute: bool,
702    cx: &Context<'_>,
703) -> fmt::Result {
704    let last = path.segments.last().unwrap();
705
706    if print_all {
707        for seg in &path.segments[..path.segments.len() - 1] {
708            write!(w, "{}::", if seg.name == kw::PathRoot { "" } else { seg.name.as_str() })?;
709        }
710    }
711    if w.alternate() {
712        write!(w, "{}{:#}", last.name, last.args.print(cx))?;
713    } else {
714        let path = fmt::from_fn(|f| {
715            if use_absolute {
716                if let Ok((_, _, fqp)) = href(did, cx) {
717                    write!(
718                        f,
719                        "{path}::{anchor}",
720                        path = join_path_syms(&fqp[..fqp.len() - 1]),
721                        anchor = print_anchor(did, *fqp.last().unwrap(), cx)
722                    )
723                } else {
724                    write!(f, "{}", last.name)
725                }
726            } else {
727                write!(f, "{}", print_anchor(did, last.name, cx))
728            }
729        });
730        write!(w, "{path}{args}", args = last.args.print(cx))?;
731    }
732    Ok(())
733}
734
735fn primitive_link(
736    f: &mut fmt::Formatter<'_>,
737    prim: clean::PrimitiveType,
738    name: fmt::Arguments<'_>,
739    cx: &Context<'_>,
740) -> fmt::Result {
741    primitive_link_fragment(f, prim, name, "", cx)
742}
743
744fn primitive_link_fragment(
745    f: &mut fmt::Formatter<'_>,
746    prim: clean::PrimitiveType,
747    name: fmt::Arguments<'_>,
748    fragment: &str,
749    cx: &Context<'_>,
750) -> fmt::Result {
751    let m = &cx.cache();
752    let mut needs_termination = false;
753    if !f.alternate() {
754        match m.primitive_locations.get(&prim) {
755            Some(&def_id) if def_id.is_local() => {
756                let len = cx.current.len();
757                let path = fmt::from_fn(|f| {
758                    if len == 0 {
759                        let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
760                        write!(f, "{cname_sym}/")?;
761                    } else {
762                        for _ in 0..(len - 1) {
763                            f.write_str("../")?;
764                        }
765                    }
766                    Ok(())
767                });
768                write!(
769                    f,
770                    "<a class=\"primitive\" href=\"{path}primitive.{}.html{fragment}\">",
771                    prim.as_sym()
772                )?;
773                needs_termination = true;
774            }
775            Some(&def_id) => {
776                let loc = match m.extern_locations[&def_id.krate] {
777                    ExternalLocation::Remote(ref s) => {
778                        let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
779                        let builder: UrlPartsBuilder =
780                            [s.as_str().trim_end_matches('/'), cname_sym.as_str()]
781                                .into_iter()
782                                .collect();
783                        Some(builder)
784                    }
785                    ExternalLocation::Local => {
786                        let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
787                        Some(if cx.current.first() == Some(&cname_sym) {
788                            iter::repeat_n(sym::dotdot, cx.current.len() - 1).collect()
789                        } else {
790                            iter::repeat_n(sym::dotdot, cx.current.len())
791                                .chain(iter::once(cname_sym))
792                                .collect()
793                        })
794                    }
795                    ExternalLocation::Unknown => None,
796                };
797                if let Some(mut loc) = loc {
798                    loc.push_fmt(format_args!("primitive.{}.html", prim.as_sym()));
799                    write!(f, "<a class=\"primitive\" href=\"{}{fragment}\">", loc.finish())?;
800                    needs_termination = true;
801                }
802            }
803            None => {}
804        }
805    }
806    Display::fmt(&name, f)?;
807    if needs_termination {
808        write!(f, "</a>")?;
809    }
810    Ok(())
811}
812
813fn print_tybounds(
814    bounds: &[clean::PolyTrait],
815    lt: &Option<clean::Lifetime>,
816    cx: &Context<'_>,
817) -> impl Display {
818    fmt::from_fn(move |f| {
819        bounds.iter().map(|bound| bound.print(cx)).joined(" + ", f)?;
820        if let Some(lt) = lt {
821            // We don't need to check `alternate` since we can be certain that
822            // the lifetime doesn't contain any characters which need escaping.
823            write!(f, " + {}", lt.print())?;
824        }
825        Ok(())
826    })
827}
828
829fn print_higher_ranked_params_with_space(
830    params: &[clean::GenericParamDef],
831    cx: &Context<'_>,
832    keyword: &'static str,
833) -> impl Display {
834    fmt::from_fn(move |f| {
835        if !params.is_empty() {
836            f.write_str(keyword)?;
837            f.write_str(if f.alternate() { "<" } else { "&lt;" })?;
838            params.iter().map(|lt| lt.print(cx)).joined(", ", f)?;
839            f.write_str(if f.alternate() { "> " } else { "&gt; " })?;
840        }
841        Ok(())
842    })
843}
844
845pub(crate) fn print_anchor(did: DefId, text: Symbol, cx: &Context<'_>) -> impl Display {
846    fmt::from_fn(move |f| {
847        let parts = href(did, cx);
848        if let Ok((url, short_ty, fqp)) = parts {
849            write!(
850                f,
851                r#"<a class="{short_ty}" href="{url}" title="{short_ty} {path}">{text}</a>"#,
852                path = join_path_syms(fqp),
853                text = EscapeBodyText(text.as_str()),
854            )
855        } else {
856            f.write_str(text.as_str())
857        }
858    })
859}
860
861fn fmt_type(
862    t: &clean::Type,
863    f: &mut fmt::Formatter<'_>,
864    use_absolute: bool,
865    cx: &Context<'_>,
866) -> fmt::Result {
867    trace!("fmt_type(t = {t:?})");
868
869    match t {
870        clean::Generic(name) => f.write_str(name.as_str()),
871        clean::SelfTy => f.write_str("Self"),
872        clean::Type::Path { path } => {
873            // Paths like `T::Output` and `Self::Output` should be rendered with all segments.
874            let did = path.def_id();
875            resolved_path(f, did, path, path.is_assoc_ty(), use_absolute, cx)
876        }
877        clean::DynTrait(bounds, lt) => {
878            f.write_str("dyn ")?;
879            print_tybounds(bounds, lt, cx).fmt(f)
880        }
881        clean::Infer => write!(f, "_"),
882        clean::Primitive(clean::PrimitiveType::Never) => {
883            primitive_link(f, PrimitiveType::Never, format_args!("!"), cx)
884        }
885        &clean::Primitive(prim) => primitive_link(f, prim, format_args!("{}", prim.as_sym()), cx),
886        clean::BareFunction(decl) => {
887            print_higher_ranked_params_with_space(&decl.generic_params, cx, "for").fmt(f)?;
888            decl.safety.print_with_space().fmt(f)?;
889            print_abi_with_space(decl.abi).fmt(f)?;
890            if f.alternate() {
891                f.write_str("fn")?;
892            } else {
893                primitive_link(f, PrimitiveType::Fn, format_args!("fn"), cx)?;
894            }
895            decl.decl.print(cx).fmt(f)
896        }
897        clean::UnsafeBinder(binder) => {
898            print_higher_ranked_params_with_space(&binder.generic_params, cx, "unsafe").fmt(f)?;
899            binder.ty.print(cx).fmt(f)
900        }
901        clean::Tuple(typs) => match &typs[..] {
902            &[] => primitive_link(f, PrimitiveType::Unit, format_args!("()"), cx),
903            [one] => {
904                if let clean::Generic(name) = one {
905                    primitive_link(f, PrimitiveType::Tuple, format_args!("({name},)"), cx)
906                } else {
907                    write!(f, "(")?;
908                    one.print(cx).fmt(f)?;
909                    write!(f, ",)")
910                }
911            }
912            many => {
913                let generic_names: Vec<Symbol> = many
914                    .iter()
915                    .filter_map(|t| match t {
916                        clean::Generic(name) => Some(*name),
917                        _ => None,
918                    })
919                    .collect();
920                let is_generic = generic_names.len() == many.len();
921                if is_generic {
922                    primitive_link(
923                        f,
924                        PrimitiveType::Tuple,
925                        format_args!(
926                            "({})",
927                            fmt::from_fn(|f| generic_names.iter().joined(", ", f))
928                        ),
929                        cx,
930                    )
931                } else {
932                    f.write_str("(")?;
933                    many.iter().map(|item| item.print(cx)).joined(", ", f)?;
934                    f.write_str(")")
935                }
936            }
937        },
938        clean::Slice(box clean::Generic(name)) => {
939            primitive_link(f, PrimitiveType::Slice, format_args!("[{name}]"), cx)
940        }
941        clean::Slice(t) => {
942            write!(f, "[")?;
943            t.print(cx).fmt(f)?;
944            write!(f, "]")
945        }
946        clean::Type::Pat(t, pat) => {
947            fmt::Display::fmt(&t.print(cx), f)?;
948            write!(f, " is {pat}")
949        }
950        clean::Array(box clean::Generic(name), n) if !f.alternate() => primitive_link(
951            f,
952            PrimitiveType::Array,
953            format_args!("[{name}; {n}]", n = Escape(n)),
954            cx,
955        ),
956        clean::Array(t, n) => {
957            write!(f, "[")?;
958            t.print(cx).fmt(f)?;
959            if f.alternate() {
960                write!(f, "; {n}")?;
961            } else {
962                write!(f, "; ")?;
963                primitive_link(f, PrimitiveType::Array, format_args!("{n}", n = Escape(n)), cx)?;
964            }
965            write!(f, "]")
966        }
967        clean::RawPointer(m, t) => {
968            let m = match m {
969                hir::Mutability::Mut => "mut",
970                hir::Mutability::Not => "const",
971            };
972
973            if matches!(**t, clean::Generic(_)) || t.is_assoc_ty() {
974                let ty = t.print(cx);
975                if f.alternate() {
976                    primitive_link(
977                        f,
978                        clean::PrimitiveType::RawPointer,
979                        format_args!("*{m} {ty:#}"),
980                        cx,
981                    )
982                } else {
983                    primitive_link(
984                        f,
985                        clean::PrimitiveType::RawPointer,
986                        format_args!("*{m} {ty}"),
987                        cx,
988                    )
989                }
990            } else {
991                primitive_link(f, clean::PrimitiveType::RawPointer, format_args!("*{m} "), cx)?;
992                t.print(cx).fmt(f)
993            }
994        }
995        clean::BorrowedRef { lifetime: l, mutability, type_: ty } => {
996            let lt = fmt::from_fn(|f| match l {
997                Some(l) => write!(f, "{} ", l.print()),
998                _ => Ok(()),
999            });
1000            let m = mutability.print_with_space();
1001            let amp = if f.alternate() { "&" } else { "&amp;" };
1002
1003            if let clean::Generic(name) = **ty {
1004                return primitive_link(
1005                    f,
1006                    PrimitiveType::Reference,
1007                    format_args!("{amp}{lt}{m}{name}"),
1008                    cx,
1009                );
1010            }
1011
1012            write!(f, "{amp}{lt}{m}")?;
1013
1014            let needs_parens = match **ty {
1015                clean::DynTrait(ref bounds, ref trait_lt)
1016                    if bounds.len() > 1 || trait_lt.is_some() =>
1017                {
1018                    true
1019                }
1020                clean::ImplTrait(ref bounds) if bounds.len() > 1 => true,
1021                _ => false,
1022            };
1023            if needs_parens {
1024                f.write_str("(")?;
1025            }
1026            fmt_type(ty, f, use_absolute, cx)?;
1027            if needs_parens {
1028                f.write_str(")")?;
1029            }
1030            Ok(())
1031        }
1032        clean::ImplTrait(bounds) => {
1033            f.write_str("impl ")?;
1034            print_generic_bounds(bounds, cx).fmt(f)
1035        }
1036        clean::QPath(qpath) => qpath.print(cx).fmt(f),
1037    }
1038}
1039
1040impl clean::Type {
1041    pub(crate) fn print(&self, cx: &Context<'_>) -> impl Display {
1042        fmt::from_fn(move |f| fmt_type(self, f, false, cx))
1043    }
1044}
1045
1046impl clean::Path {
1047    pub(crate) fn print(&self, cx: &Context<'_>) -> impl Display {
1048        fmt::from_fn(move |f| resolved_path(f, self.def_id(), self, false, false, cx))
1049    }
1050}
1051
1052impl clean::QPathData {
1053    fn print(&self, cx: &Context<'_>) -> impl Display {
1054        let Self { ref assoc, ref self_type, should_fully_qualify, ref trait_ } = *self;
1055
1056        fmt::from_fn(move |f| {
1057            // FIXME(inherent_associated_types): Once we support non-ADT self-types (#106719),
1058            // we need to surround them with angle brackets in some cases (e.g. `<dyn …>::P`).
1059
1060            if f.alternate() {
1061                if let Some(trait_) = trait_
1062                    && should_fully_qualify
1063                {
1064                    write!(f, "<{:#} as {:#}>::", self_type.print(cx), trait_.print(cx))?
1065                } else {
1066                    write!(f, "{:#}::", self_type.print(cx))?
1067                }
1068            } else {
1069                if let Some(trait_) = trait_
1070                    && should_fully_qualify
1071                {
1072                    write!(f, "&lt;{} as {}&gt;::", self_type.print(cx), trait_.print(cx))?
1073                } else {
1074                    write!(f, "{}::", self_type.print(cx))?
1075                }
1076            };
1077            // It's pretty unsightly to look at `<A as B>::C` in output, and
1078            // we've got hyperlinking on our side, so try to avoid longer
1079            // notation as much as possible by making `C` a hyperlink to trait
1080            // `B` to disambiguate.
1081            //
1082            // FIXME: this is still a lossy conversion and there should probably
1083            //        be a better way of representing this in general? Most of
1084            //        the ugliness comes from inlining across crates where
1085            //        everything comes in as a fully resolved QPath (hard to
1086            //        look at).
1087            if !f.alternate() {
1088                // FIXME(inherent_associated_types): We always link to the very first associated
1089                // type (in respect to source order) that bears the given name (`assoc.name`) and that is
1090                // affiliated with the computed `DefId`. This is obviously incorrect when we have
1091                // multiple impl blocks. Ideally, we would thread the `DefId` of the assoc ty itself
1092                // through here and map it to the corresponding HTML ID that was generated by
1093                // `render::Context::derive_id` when the impl blocks were rendered.
1094                // There is no such mapping unfortunately.
1095                // As a hack, we could badly imitate `derive_id` here by keeping *count* when looking
1096                // for the assoc ty `DefId` in `tcx.associated_items(self_ty_did).in_definition_order()`
1097                // considering privacy, `doc(hidden)`, etc.
1098                // I don't feel like that right now :cold_sweat:.
1099
1100                let parent_href = match trait_ {
1101                    Some(trait_) => href(trait_.def_id(), cx).ok(),
1102                    None => self_type.def_id(cx.cache()).and_then(|did| href(did, cx).ok()),
1103                };
1104
1105                if let Some((url, _, path)) = parent_href {
1106                    write!(
1107                        f,
1108                        "<a class=\"associatedtype\" href=\"{url}#{shortty}.{name}\" \
1109                                    title=\"type {path}::{name}\">{name}</a>",
1110                        shortty = ItemType::AssocType,
1111                        name = assoc.name,
1112                        path = join_path_syms(path),
1113                    )
1114                } else {
1115                    write!(f, "{}", assoc.name)
1116                }
1117            } else {
1118                write!(f, "{}", assoc.name)
1119            }?;
1120
1121            assoc.args.print(cx).fmt(f)
1122        })
1123    }
1124}
1125
1126impl clean::Impl {
1127    pub(crate) fn print(&self, use_absolute: bool, cx: &Context<'_>) -> impl Display {
1128        fmt::from_fn(move |f| {
1129            f.write_str("impl")?;
1130            self.generics.print(cx).fmt(f)?;
1131            f.write_str(" ")?;
1132
1133            if let Some(ref ty) = self.trait_ {
1134                if self.is_negative_trait_impl() {
1135                    write!(f, "!")?;
1136                }
1137                if self.kind.is_fake_variadic()
1138                    && let Some(generics) = ty.generics()
1139                    && let Ok(inner_type) = generics.exactly_one()
1140                {
1141                    let last = ty.last();
1142                    if f.alternate() {
1143                        write!(f, "{last}<")?;
1144                        self.print_type(inner_type, f, use_absolute, cx)?;
1145                        write!(f, ">")?;
1146                    } else {
1147                        write!(f, "{}&lt;", print_anchor(ty.def_id(), last, cx))?;
1148                        self.print_type(inner_type, f, use_absolute, cx)?;
1149                        write!(f, "&gt;")?;
1150                    }
1151                } else {
1152                    ty.print(cx).fmt(f)?;
1153                }
1154                write!(f, " for ")?;
1155            }
1156
1157            if let Some(ty) = self.kind.as_blanket_ty() {
1158                fmt_type(ty, f, use_absolute, cx)?;
1159            } else {
1160                self.print_type(&self.for_, f, use_absolute, cx)?;
1161            }
1162
1163            print_where_clause(&self.generics, cx, 0, Ending::Newline).maybe_display().fmt(f)
1164        })
1165    }
1166    fn print_type(
1167        &self,
1168        type_: &clean::Type,
1169        f: &mut fmt::Formatter<'_>,
1170        use_absolute: bool,
1171        cx: &Context<'_>,
1172    ) -> Result<(), fmt::Error> {
1173        if let clean::Type::Tuple(types) = type_
1174            && let [clean::Type::Generic(name)] = &types[..]
1175            && (self.kind.is_fake_variadic() || self.kind.is_auto())
1176        {
1177            // Hardcoded anchor library/core/src/primitive_docs.rs
1178            // Link should match `# Trait implementations`
1179            primitive_link_fragment(
1180                f,
1181                PrimitiveType::Tuple,
1182                format_args!("({name}₁, {name}₂, …, {name}ₙ)"),
1183                "#trait-implementations-1",
1184                cx,
1185            )?;
1186        } else if let clean::Type::Array(ty, len) = type_
1187            && let clean::Type::Generic(name) = &**ty
1188            && &len[..] == "1"
1189            && (self.kind.is_fake_variadic() || self.kind.is_auto())
1190        {
1191            primitive_link(f, PrimitiveType::Array, format_args!("[{name}; N]"), cx)?;
1192        } else if let clean::BareFunction(bare_fn) = &type_
1193            && let [clean::Parameter { type_: clean::Type::Generic(name), .. }] =
1194                &bare_fn.decl.inputs[..]
1195            && (self.kind.is_fake_variadic() || self.kind.is_auto())
1196        {
1197            // Hardcoded anchor library/core/src/primitive_docs.rs
1198            // Link should match `# Trait implementations`
1199
1200            print_higher_ranked_params_with_space(&bare_fn.generic_params, cx, "for").fmt(f)?;
1201            bare_fn.safety.print_with_space().fmt(f)?;
1202            print_abi_with_space(bare_fn.abi).fmt(f)?;
1203            let ellipsis = if bare_fn.decl.c_variadic { ", ..." } else { "" };
1204            primitive_link_fragment(
1205                f,
1206                PrimitiveType::Tuple,
1207                format_args!("fn({name}₁, {name}₂, …, {name}ₙ{ellipsis})"),
1208                "#trait-implementations-1",
1209                cx,
1210            )?;
1211            // Write output.
1212            if !bare_fn.decl.output.is_unit() {
1213                write!(f, " -> ")?;
1214                fmt_type(&bare_fn.decl.output, f, use_absolute, cx)?;
1215            }
1216        } else if let clean::Type::Path { path } = type_
1217            && let Some(generics) = path.generics()
1218            && let Ok(ty) = generics.exactly_one()
1219            && self.kind.is_fake_variadic()
1220        {
1221            let wrapper = print_anchor(path.def_id(), path.last(), cx);
1222            if f.alternate() {
1223                write!(f, "{wrapper:#}&lt;")?;
1224            } else {
1225                write!(f, "{wrapper}<")?;
1226            }
1227            self.print_type(ty, f, use_absolute, cx)?;
1228            if f.alternate() {
1229                write!(f, "&gt;")?;
1230            } else {
1231                write!(f, ">")?;
1232            }
1233        } else {
1234            fmt_type(type_, f, use_absolute, cx)?;
1235        }
1236        Ok(())
1237    }
1238}
1239
1240pub(crate) fn print_params(params: &[clean::Parameter], cx: &Context<'_>) -> impl Display {
1241    fmt::from_fn(move |f| {
1242        params
1243            .iter()
1244            .map(|param| {
1245                fmt::from_fn(|f| {
1246                    if let Some(name) = param.name {
1247                        write!(f, "{name}: ")?;
1248                    }
1249                    param.type_.print(cx).fmt(f)
1250                })
1251            })
1252            .joined(", ", f)
1253    })
1254}
1255
1256// Implements Write but only counts the bytes "written".
1257struct WriteCounter(usize);
1258
1259impl std::fmt::Write for WriteCounter {
1260    fn write_str(&mut self, s: &str) -> fmt::Result {
1261        self.0 += s.len();
1262        Ok(())
1263    }
1264}
1265
1266// Implements Display by emitting the given number of spaces.
1267struct Indent(usize);
1268
1269impl Display for Indent {
1270    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1271        (0..self.0).for_each(|_| {
1272            f.write_char(' ').unwrap();
1273        });
1274        Ok(())
1275    }
1276}
1277
1278impl clean::FnDecl {
1279    pub(crate) fn print(&self, cx: &Context<'_>) -> impl Display {
1280        fmt::from_fn(move |f| {
1281            let ellipsis = if self.c_variadic { ", ..." } else { "" };
1282            if f.alternate() {
1283                write!(
1284                    f,
1285                    "({params:#}{ellipsis}){arrow:#}",
1286                    params = print_params(&self.inputs, cx),
1287                    ellipsis = ellipsis,
1288                    arrow = self.print_output(cx)
1289                )
1290            } else {
1291                write!(
1292                    f,
1293                    "({params}{ellipsis}){arrow}",
1294                    params = print_params(&self.inputs, cx),
1295                    ellipsis = ellipsis,
1296                    arrow = self.print_output(cx)
1297                )
1298            }
1299        })
1300    }
1301
1302    /// * `header_len`: The length of the function header and name. In other words, the number of
1303    ///   characters in the function declaration up to but not including the parentheses.
1304    ///   This is expected to go into a `<pre>`/`code-header` block, so indentation and newlines
1305    ///   are preserved.
1306    /// * `indent`: The number of spaces to indent each successive line with, if line-wrapping is
1307    ///   necessary.
1308    pub(crate) fn full_print(
1309        &self,
1310        header_len: usize,
1311        indent: usize,
1312        cx: &Context<'_>,
1313    ) -> impl Display {
1314        fmt::from_fn(move |f| {
1315            // First, generate the text form of the declaration, with no line wrapping, and count the bytes.
1316            let mut counter = WriteCounter(0);
1317            write!(&mut counter, "{:#}", fmt::from_fn(|f| { self.inner_full_print(None, f, cx) }))
1318                .unwrap();
1319            // If the text form was over 80 characters wide, we will line-wrap our output.
1320            let line_wrapping_indent =
1321                if header_len + counter.0 > 80 { Some(indent) } else { None };
1322            // Generate the final output. This happens to accept `{:#}` formatting to get textual
1323            // output but in practice it is only formatted with `{}` to get HTML output.
1324            self.inner_full_print(line_wrapping_indent, f, cx)
1325        })
1326    }
1327
1328    fn inner_full_print(
1329        &self,
1330        // For None, the declaration will not be line-wrapped. For Some(n),
1331        // the declaration will be line-wrapped, with an indent of n spaces.
1332        line_wrapping_indent: Option<usize>,
1333        f: &mut fmt::Formatter<'_>,
1334        cx: &Context<'_>,
1335    ) -> fmt::Result {
1336        let amp = if f.alternate() { "&" } else { "&amp;" };
1337
1338        write!(f, "(")?;
1339        if let Some(n) = line_wrapping_indent
1340            && !self.inputs.is_empty()
1341        {
1342            write!(f, "\n{}", Indent(n + 4))?;
1343        }
1344
1345        let last_input_index = self.inputs.len().checked_sub(1);
1346        for (i, param) in self.inputs.iter().enumerate() {
1347            if let Some(selfty) = param.to_receiver() {
1348                match selfty {
1349                    clean::SelfTy => {
1350                        write!(f, "self")?;
1351                    }
1352                    clean::BorrowedRef { lifetime, mutability, type_: box clean::SelfTy } => {
1353                        write!(f, "{amp}")?;
1354                        if let Some(lt) = lifetime {
1355                            write!(f, "{lt} ", lt = lt.print())?;
1356                        }
1357                        write!(f, "{mutability}self", mutability = mutability.print_with_space())?;
1358                    }
1359                    _ => {
1360                        write!(f, "self: ")?;
1361                        selfty.print(cx).fmt(f)?;
1362                    }
1363                }
1364            } else {
1365                if param.is_const {
1366                    write!(f, "const ")?;
1367                }
1368                if let Some(name) = param.name {
1369                    write!(f, "{name}: ")?;
1370                }
1371                param.type_.print(cx).fmt(f)?;
1372            }
1373            match (line_wrapping_indent, last_input_index) {
1374                (_, None) => (),
1375                (None, Some(last_i)) if i != last_i => write!(f, ", ")?,
1376                (None, Some(_)) => (),
1377                (Some(n), Some(last_i)) if i != last_i => write!(f, ",\n{}", Indent(n + 4))?,
1378                (Some(_), Some(_)) => writeln!(f, ",")?,
1379            }
1380        }
1381
1382        if self.c_variadic {
1383            match line_wrapping_indent {
1384                None => write!(f, ", ...")?,
1385                Some(n) => writeln!(f, "{}...", Indent(n + 4))?,
1386            };
1387        }
1388
1389        match line_wrapping_indent {
1390            None => write!(f, ")")?,
1391            Some(n) => write!(f, "{})", Indent(n))?,
1392        };
1393
1394        self.print_output(cx).fmt(f)
1395    }
1396
1397    fn print_output(&self, cx: &Context<'_>) -> impl Display {
1398        fmt::from_fn(move |f| match &self.output {
1399            clean::Tuple(tys) if tys.is_empty() => Ok(()),
1400            ty if f.alternate() => {
1401                write!(f, " -> {:#}", ty.print(cx))
1402            }
1403            ty => write!(f, " -&gt; {}", ty.print(cx)),
1404        })
1405    }
1406}
1407
1408pub(crate) fn visibility_print_with_space(item: &clean::Item, cx: &Context<'_>) -> impl Display {
1409    fmt::from_fn(move |f| {
1410        if item.is_doc_hidden() {
1411            f.write_str("#[doc(hidden)] ")?;
1412        }
1413
1414        match item.visibility(cx.tcx()) {
1415            None => {}
1416            Some(ty::Visibility::Public) => f.write_str("pub ")?,
1417            Some(ty::Visibility::Restricted(vis_did)) => {
1418                // FIXME(camelid): This may not work correctly if `item_did` is a module.
1419                //                 However, rustdoc currently never displays a module's
1420                //                 visibility, so it shouldn't matter.
1421                let parent_module =
1422                    find_nearest_parent_module(cx.tcx(), item.item_id.expect_def_id());
1423
1424                if vis_did.is_crate_root() {
1425                    f.write_str("pub(crate) ")?;
1426                } else if parent_module == Some(vis_did) {
1427                    // `pub(in foo)` where `foo` is the parent module
1428                    // is the same as no visibility modifier; do nothing
1429                } else if parent_module
1430                    .and_then(|parent| find_nearest_parent_module(cx.tcx(), parent))
1431                    == Some(vis_did)
1432                {
1433                    f.write_str("pub(super) ")?;
1434                } else {
1435                    let path = cx.tcx().def_path(vis_did);
1436                    debug!("path={path:?}");
1437                    // modified from `resolved_path()` to work with `DefPathData`
1438                    let last_name = path.data.last().unwrap().data.get_opt_name().unwrap();
1439                    let anchor = print_anchor(vis_did, last_name, cx);
1440
1441                    f.write_str("pub(in ")?;
1442                    for seg in &path.data[..path.data.len() - 1] {
1443                        write!(f, "{}::", seg.data.get_opt_name().unwrap())?;
1444                    }
1445                    write!(f, "{anchor}) ")?;
1446                }
1447            }
1448        }
1449        Ok(())
1450    })
1451}
1452
1453pub(crate) trait PrintWithSpace {
1454    fn print_with_space(&self) -> &str;
1455}
1456
1457impl PrintWithSpace for hir::Safety {
1458    fn print_with_space(&self) -> &str {
1459        self.prefix_str()
1460    }
1461}
1462
1463impl PrintWithSpace for hir::HeaderSafety {
1464    fn print_with_space(&self) -> &str {
1465        match self {
1466            hir::HeaderSafety::SafeTargetFeatures => "",
1467            hir::HeaderSafety::Normal(safety) => safety.print_with_space(),
1468        }
1469    }
1470}
1471
1472impl PrintWithSpace for hir::IsAsync {
1473    fn print_with_space(&self) -> &str {
1474        match self {
1475            hir::IsAsync::Async(_) => "async ",
1476            hir::IsAsync::NotAsync => "",
1477        }
1478    }
1479}
1480
1481impl PrintWithSpace for hir::Mutability {
1482    fn print_with_space(&self) -> &str {
1483        match self {
1484            hir::Mutability::Not => "",
1485            hir::Mutability::Mut => "mut ",
1486        }
1487    }
1488}
1489
1490pub(crate) fn print_constness_with_space(
1491    c: &hir::Constness,
1492    overall_stab: Option<StableSince>,
1493    const_stab: Option<ConstStability>,
1494) -> &'static str {
1495    match c {
1496        hir::Constness::Const => match (overall_stab, const_stab) {
1497            // const stable...
1498            (_, Some(ConstStability { level: StabilityLevel::Stable { .. }, .. }))
1499            // ...or when feature(staged_api) is not set...
1500            | (_, None)
1501            // ...or when const unstable, but overall unstable too
1502            | (None, Some(ConstStability { level: StabilityLevel::Unstable { .. }, .. })) => {
1503                "const "
1504            }
1505            // const unstable (and overall stable)
1506            (Some(_), Some(ConstStability { level: StabilityLevel::Unstable { .. }, .. })) => "",
1507        },
1508        // not const
1509        hir::Constness::NotConst => "",
1510    }
1511}
1512
1513impl clean::Import {
1514    pub(crate) fn print(&self, cx: &Context<'_>) -> impl Display {
1515        fmt::from_fn(move |f| match self.kind {
1516            clean::ImportKind::Simple(name) => {
1517                if name == self.source.path.last() {
1518                    write!(f, "use {};", self.source.print(cx))
1519                } else {
1520                    write!(f, "use {source} as {name};", source = self.source.print(cx))
1521                }
1522            }
1523            clean::ImportKind::Glob => {
1524                if self.source.path.segments.is_empty() {
1525                    write!(f, "use *;")
1526                } else {
1527                    write!(f, "use {}::*;", self.source.print(cx))
1528                }
1529            }
1530        })
1531    }
1532}
1533
1534impl clean::ImportSource {
1535    pub(crate) fn print(&self, cx: &Context<'_>) -> impl Display {
1536        fmt::from_fn(move |f| match self.did {
1537            Some(did) => resolved_path(f, did, &self.path, true, false, cx),
1538            _ => {
1539                for seg in &self.path.segments[..self.path.segments.len() - 1] {
1540                    write!(f, "{}::", seg.name)?;
1541                }
1542                let name = self.path.last();
1543                if let hir::def::Res::PrimTy(p) = self.path.res {
1544                    primitive_link(f, PrimitiveType::from(p), format_args!("{name}"), cx)?;
1545                } else {
1546                    f.write_str(name.as_str())?;
1547                }
1548                Ok(())
1549            }
1550        })
1551    }
1552}
1553
1554impl clean::AssocItemConstraint {
1555    pub(crate) fn print(&self, cx: &Context<'_>) -> impl Display {
1556        fmt::from_fn(move |f| {
1557            f.write_str(self.assoc.name.as_str())?;
1558            self.assoc.args.print(cx).fmt(f)?;
1559            match self.kind {
1560                clean::AssocItemConstraintKind::Equality { ref term } => {
1561                    f.write_str(" = ")?;
1562                    term.print(cx).fmt(f)?;
1563                }
1564                clean::AssocItemConstraintKind::Bound { ref bounds } => {
1565                    if !bounds.is_empty() {
1566                        f.write_str(": ")?;
1567                        print_generic_bounds(bounds, cx).fmt(f)?;
1568                    }
1569                }
1570            }
1571            Ok(())
1572        })
1573    }
1574}
1575
1576pub(crate) fn print_abi_with_space(abi: ExternAbi) -> impl Display {
1577    fmt::from_fn(move |f| {
1578        let quot = if f.alternate() { "\"" } else { "&quot;" };
1579        match abi {
1580            ExternAbi::Rust => Ok(()),
1581            abi => write!(f, "extern {0}{1}{0} ", quot, abi.name()),
1582        }
1583    })
1584}
1585
1586pub(crate) fn print_default_space(v: bool) -> &'static str {
1587    if v { "default " } else { "" }
1588}
1589
1590impl clean::GenericArg {
1591    pub(crate) fn print(&self, cx: &Context<'_>) -> impl Display {
1592        fmt::from_fn(move |f| match self {
1593            clean::GenericArg::Lifetime(lt) => lt.print().fmt(f),
1594            clean::GenericArg::Type(ty) => ty.print(cx).fmt(f),
1595            clean::GenericArg::Const(ct) => ct.print(cx.tcx()).fmt(f),
1596            clean::GenericArg::Infer => Display::fmt("_", f),
1597        })
1598    }
1599}
1600
1601impl clean::Term {
1602    pub(crate) fn print(&self, cx: &Context<'_>) -> impl Display {
1603        fmt::from_fn(move |f| match self {
1604            clean::Term::Type(ty) => ty.print(cx).fmt(f),
1605            clean::Term::Constant(ct) => ct.print(cx.tcx()).fmt(f),
1606        })
1607    }
1608}