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