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_attr_data_structures::{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 mut url_parts = make_href(root_path, shortty, url_parts, &fqp, is_remote);
486    if def_id != original_def_id {
487        let kind = ItemType::from_def_kind(original_def_kind, Some(def_kind));
488        url_parts = format!("{url_parts}#{kind}.{}", tcx.item_name(original_def_id))
489    };
490    Ok((url_parts, 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)),
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) -> String {
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    url_parts.finish()
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)
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    let url_parts = make_href(root_path, shortty, url_parts, &fqp, is_remote);
609    Ok((url_parts, shortty, fqp.clone()))
610}
611
612pub(crate) fn href(
613    did: DefId,
614    cx: &Context<'_>,
615) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
616    href_with_root_path(did, cx, None)
617}
618
619/// Both paths should only be modules.
620/// This is because modules get their own directories; that is, `std::vec` and `std::vec::Vec` will
621/// both need `../iter/trait.Iterator.html` to get at the iterator trait.
622pub(crate) fn href_relative_parts(fqp: &[Symbol], relative_to_fqp: &[Symbol]) -> UrlPartsBuilder {
623    for (i, (f, r)) in fqp.iter().zip(relative_to_fqp.iter()).enumerate() {
624        // e.g. linking to std::iter from std::vec (`dissimilar_part_count` will be 1)
625        if f != r {
626            let dissimilar_part_count = relative_to_fqp.len() - i;
627            let fqp_module = &fqp[i..];
628            return iter::repeat_n(sym::dotdot, dissimilar_part_count)
629                .chain(fqp_module.iter().copied())
630                .collect();
631        }
632    }
633    match relative_to_fqp.len().cmp(&fqp.len()) {
634        Ordering::Less => {
635            // e.g. linking to std::sync::atomic from std::sync
636            fqp[relative_to_fqp.len()..fqp.len()].iter().copied().collect()
637        }
638        Ordering::Greater => {
639            // e.g. linking to std::sync from std::sync::atomic
640            let dissimilar_part_count = relative_to_fqp.len() - fqp.len();
641            iter::repeat_n(sym::dotdot, dissimilar_part_count).collect()
642        }
643        Ordering::Equal => {
644            // linking to the same module
645            UrlPartsBuilder::new()
646        }
647    }
648}
649
650pub(crate) fn link_tooltip(
651    did: DefId,
652    fragment: &Option<UrlFragment>,
653    cx: &Context<'_>,
654) -> impl fmt::Display {
655    fmt::from_fn(move |f| {
656        let cache = cx.cache();
657        let Some((fqp, shortty)) = cache.paths.get(&did).or_else(|| cache.external_paths.get(&did))
658        else {
659            return Ok(());
660        };
661        let fqp = if *shortty == ItemType::Primitive {
662            // primitives are documented in a crate, but not actually part of it
663            slice::from_ref(fqp.last().unwrap())
664        } else {
665            fqp
666        };
667        if let &Some(UrlFragment::Item(id)) = fragment {
668            write!(f, "{} ", cx.tcx().def_descr(id))?;
669            for component in fqp {
670                write!(f, "{component}::")?;
671            }
672            write!(f, "{}", cx.tcx().item_name(id))?;
673        } else if !fqp.is_empty() {
674            write!(f, "{shortty} ")?;
675            fqp.iter().joined("::", f)?;
676        }
677        Ok(())
678    })
679}
680
681/// Used to render a [`clean::Path`].
682fn resolved_path(
683    w: &mut fmt::Formatter<'_>,
684    did: DefId,
685    path: &clean::Path,
686    print_all: bool,
687    use_absolute: bool,
688    cx: &Context<'_>,
689) -> fmt::Result {
690    let last = path.segments.last().unwrap();
691
692    if print_all {
693        for seg in &path.segments[..path.segments.len() - 1] {
694            write!(w, "{}::", if seg.name == kw::PathRoot { "" } else { seg.name.as_str() })?;
695        }
696    }
697    if w.alternate() {
698        write!(w, "{}{:#}", last.name, last.args.print(cx))?;
699    } else {
700        let path = fmt::from_fn(|f| {
701            if use_absolute {
702                if let Ok((_, _, fqp)) = href(did, cx) {
703                    write!(
704                        f,
705                        "{path}::{anchor}",
706                        path = join_with_double_colon(&fqp[..fqp.len() - 1]),
707                        anchor = print_anchor(did, *fqp.last().unwrap(), cx)
708                    )
709                } else {
710                    write!(f, "{}", last.name)
711                }
712            } else {
713                write!(f, "{}", print_anchor(did, last.name, cx))
714            }
715        });
716        write!(w, "{path}{args}", args = last.args.print(cx))?;
717    }
718    Ok(())
719}
720
721fn primitive_link(
722    f: &mut fmt::Formatter<'_>,
723    prim: clean::PrimitiveType,
724    name: fmt::Arguments<'_>,
725    cx: &Context<'_>,
726) -> fmt::Result {
727    primitive_link_fragment(f, prim, name, "", cx)
728}
729
730fn primitive_link_fragment(
731    f: &mut fmt::Formatter<'_>,
732    prim: clean::PrimitiveType,
733    name: fmt::Arguments<'_>,
734    fragment: &str,
735    cx: &Context<'_>,
736) -> fmt::Result {
737    let m = &cx.cache();
738    let mut needs_termination = false;
739    if !f.alternate() {
740        match m.primitive_locations.get(&prim) {
741            Some(&def_id) if def_id.is_local() => {
742                let len = cx.current.len();
743                let path = fmt::from_fn(|f| {
744                    if len == 0 {
745                        let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
746                        write!(f, "{cname_sym}/")?;
747                    } else {
748                        for _ in 0..(len - 1) {
749                            f.write_str("../")?;
750                        }
751                    }
752                    Ok(())
753                });
754                write!(
755                    f,
756                    "<a class=\"primitive\" href=\"{path}primitive.{}.html{fragment}\">",
757                    prim.as_sym()
758                )?;
759                needs_termination = true;
760            }
761            Some(&def_id) => {
762                let loc = match m.extern_locations[&def_id.krate] {
763                    ExternalLocation::Remote(ref s) => {
764                        let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
765                        let builder: UrlPartsBuilder =
766                            [s.as_str().trim_end_matches('/'), cname_sym.as_str()]
767                                .into_iter()
768                                .collect();
769                        Some(builder)
770                    }
771                    ExternalLocation::Local => {
772                        let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
773                        Some(if cx.current.first() == Some(&cname_sym) {
774                            iter::repeat_n(sym::dotdot, cx.current.len() - 1).collect()
775                        } else {
776                            iter::repeat_n(sym::dotdot, cx.current.len())
777                                .chain(iter::once(cname_sym))
778                                .collect()
779                        })
780                    }
781                    ExternalLocation::Unknown => None,
782                };
783                if let Some(mut loc) = loc {
784                    loc.push_fmt(format_args!("primitive.{}.html", prim.as_sym()));
785                    write!(f, "<a class=\"primitive\" href=\"{}{fragment}\">", loc.finish())?;
786                    needs_termination = true;
787                }
788            }
789            None => {}
790        }
791    }
792    Display::fmt(&name, f)?;
793    if needs_termination {
794        write!(f, "</a>")?;
795    }
796    Ok(())
797}
798
799fn print_tybounds(
800    bounds: &[clean::PolyTrait],
801    lt: &Option<clean::Lifetime>,
802    cx: &Context<'_>,
803) -> impl Display {
804    fmt::from_fn(move |f| {
805        bounds.iter().map(|bound| bound.print(cx)).joined(" + ", f)?;
806        if let Some(lt) = lt {
807            // We don't need to check `alternate` since we can be certain that
808            // the lifetime doesn't contain any characters which need escaping.
809            write!(f, " + {}", lt.print())?;
810        }
811        Ok(())
812    })
813}
814
815fn print_higher_ranked_params_with_space(
816    params: &[clean::GenericParamDef],
817    cx: &Context<'_>,
818    keyword: &'static str,
819) -> impl Display {
820    fmt::from_fn(move |f| {
821        if !params.is_empty() {
822            f.write_str(keyword)?;
823            f.write_str(if f.alternate() { "<" } else { "&lt;" })?;
824            params.iter().map(|lt| lt.print(cx)).joined(", ", f)?;
825            f.write_str(if f.alternate() { "> " } else { "&gt; " })?;
826        }
827        Ok(())
828    })
829}
830
831pub(crate) fn print_anchor(did: DefId, text: Symbol, cx: &Context<'_>) -> impl Display {
832    fmt::from_fn(move |f| {
833        let parts = href(did, cx);
834        if let Ok((url, short_ty, fqp)) = parts {
835            write!(
836                f,
837                r#"<a class="{short_ty}" href="{url}" title="{short_ty} {path}">{text}</a>"#,
838                path = join_with_double_colon(&fqp),
839                text = EscapeBodyText(text.as_str()),
840            )
841        } else {
842            f.write_str(text.as_str())
843        }
844    })
845}
846
847fn fmt_type(
848    t: &clean::Type,
849    f: &mut fmt::Formatter<'_>,
850    use_absolute: bool,
851    cx: &Context<'_>,
852) -> fmt::Result {
853    trace!("fmt_type(t = {t:?})");
854
855    match t {
856        clean::Generic(name) => f.write_str(name.as_str()),
857        clean::SelfTy => f.write_str("Self"),
858        clean::Type::Path { path } => {
859            // Paths like `T::Output` and `Self::Output` should be rendered with all segments.
860            let did = path.def_id();
861            resolved_path(f, did, path, path.is_assoc_ty(), use_absolute, cx)
862        }
863        clean::DynTrait(bounds, lt) => {
864            f.write_str("dyn ")?;
865            print_tybounds(bounds, lt, cx).fmt(f)
866        }
867        clean::Infer => write!(f, "_"),
868        clean::Primitive(clean::PrimitiveType::Never) => {
869            primitive_link(f, PrimitiveType::Never, format_args!("!"), cx)
870        }
871        &clean::Primitive(prim) => primitive_link(f, prim, format_args!("{}", prim.as_sym()), cx),
872        clean::BareFunction(decl) => {
873            print_higher_ranked_params_with_space(&decl.generic_params, cx, "for").fmt(f)?;
874            decl.safety.print_with_space().fmt(f)?;
875            print_abi_with_space(decl.abi).fmt(f)?;
876            if f.alternate() {
877                f.write_str("fn")?;
878            } else {
879                primitive_link(f, PrimitiveType::Fn, format_args!("fn"), cx)?;
880            }
881            decl.decl.print(cx).fmt(f)
882        }
883        clean::UnsafeBinder(binder) => {
884            print_higher_ranked_params_with_space(&binder.generic_params, cx, "unsafe").fmt(f)?;
885            binder.ty.print(cx).fmt(f)
886        }
887        clean::Tuple(typs) => match &typs[..] {
888            &[] => primitive_link(f, PrimitiveType::Unit, format_args!("()"), cx),
889            [one] => {
890                if let clean::Generic(name) = one {
891                    primitive_link(f, PrimitiveType::Tuple, format_args!("({name},)"), cx)
892                } else {
893                    write!(f, "(")?;
894                    one.print(cx).fmt(f)?;
895                    write!(f, ",)")
896                }
897            }
898            many => {
899                let generic_names: Vec<Symbol> = many
900                    .iter()
901                    .filter_map(|t| match t {
902                        clean::Generic(name) => Some(*name),
903                        _ => None,
904                    })
905                    .collect();
906                let is_generic = generic_names.len() == many.len();
907                if is_generic {
908                    primitive_link(
909                        f,
910                        PrimitiveType::Tuple,
911                        format_args!(
912                            "({})",
913                            fmt::from_fn(|f| generic_names.iter().joined(", ", f))
914                        ),
915                        cx,
916                    )
917                } else {
918                    f.write_str("(")?;
919                    many.iter().map(|item| item.print(cx)).joined(", ", f)?;
920                    f.write_str(")")
921                }
922            }
923        },
924        clean::Slice(box clean::Generic(name)) => {
925            primitive_link(f, PrimitiveType::Slice, format_args!("[{name}]"), cx)
926        }
927        clean::Slice(t) => {
928            write!(f, "[")?;
929            t.print(cx).fmt(f)?;
930            write!(f, "]")
931        }
932        clean::Type::Pat(t, pat) => {
933            fmt::Display::fmt(&t.print(cx), f)?;
934            write!(f, " is {pat}")
935        }
936        clean::Array(box clean::Generic(name), n) if !f.alternate() => primitive_link(
937            f,
938            PrimitiveType::Array,
939            format_args!("[{name}; {n}]", n = Escape(n)),
940            cx,
941        ),
942        clean::Array(t, n) => {
943            write!(f, "[")?;
944            t.print(cx).fmt(f)?;
945            if f.alternate() {
946                write!(f, "; {n}")?;
947            } else {
948                write!(f, "; ")?;
949                primitive_link(f, PrimitiveType::Array, format_args!("{n}", n = Escape(n)), cx)?;
950            }
951            write!(f, "]")
952        }
953        clean::RawPointer(m, t) => {
954            let m = match m {
955                hir::Mutability::Mut => "mut",
956                hir::Mutability::Not => "const",
957            };
958
959            if matches!(**t, clean::Generic(_)) || t.is_assoc_ty() {
960                let ty = t.print(cx);
961                if f.alternate() {
962                    primitive_link(
963                        f,
964                        clean::PrimitiveType::RawPointer,
965                        format_args!("*{m} {ty:#}"),
966                        cx,
967                    )
968                } else {
969                    primitive_link(
970                        f,
971                        clean::PrimitiveType::RawPointer,
972                        format_args!("*{m} {ty}"),
973                        cx,
974                    )
975                }
976            } else {
977                primitive_link(f, clean::PrimitiveType::RawPointer, format_args!("*{m} "), cx)?;
978                t.print(cx).fmt(f)
979            }
980        }
981        clean::BorrowedRef { lifetime: l, mutability, type_: ty } => {
982            let lt = fmt::from_fn(|f| match l {
983                Some(l) => write!(f, "{} ", l.print()),
984                _ => Ok(()),
985            });
986            let m = mutability.print_with_space();
987            let amp = if f.alternate() { "&" } else { "&amp;" };
988
989            if let clean::Generic(name) = **ty {
990                return primitive_link(
991                    f,
992                    PrimitiveType::Reference,
993                    format_args!("{amp}{lt}{m}{name}"),
994                    cx,
995                );
996            }
997
998            write!(f, "{amp}{lt}{m}")?;
999
1000            let needs_parens = match **ty {
1001                clean::DynTrait(ref bounds, ref trait_lt)
1002                    if bounds.len() > 1 || trait_lt.is_some() =>
1003                {
1004                    true
1005                }
1006                clean::ImplTrait(ref bounds) if bounds.len() > 1 => true,
1007                _ => false,
1008            };
1009            if needs_parens {
1010                f.write_str("(")?;
1011            }
1012            fmt_type(ty, f, use_absolute, cx)?;
1013            if needs_parens {
1014                f.write_str(")")?;
1015            }
1016            Ok(())
1017        }
1018        clean::ImplTrait(bounds) => {
1019            f.write_str("impl ")?;
1020            print_generic_bounds(bounds, cx).fmt(f)
1021        }
1022        &clean::QPath(box clean::QPathData {
1023            ref assoc,
1024            ref self_type,
1025            ref trait_,
1026            should_show_cast,
1027        }) => {
1028            // FIXME(inherent_associated_types): Once we support non-ADT self-types (#106719),
1029            // we need to surround them with angle brackets in some cases (e.g. `<dyn …>::P`).
1030
1031            if f.alternate() {
1032                if let Some(trait_) = trait_
1033                    && should_show_cast
1034                {
1035                    write!(f, "<{:#} as {:#}>::", self_type.print(cx), trait_.print(cx))?
1036                } else {
1037                    write!(f, "{:#}::", self_type.print(cx))?
1038                }
1039            } else {
1040                if let Some(trait_) = trait_
1041                    && should_show_cast
1042                {
1043                    write!(f, "&lt;{} as {}&gt;::", self_type.print(cx), trait_.print(cx))?
1044                } else {
1045                    write!(f, "{}::", self_type.print(cx))?
1046                }
1047            };
1048            // It's pretty unsightly to look at `<A as B>::C` in output, and
1049            // we've got hyperlinking on our side, so try to avoid longer
1050            // notation as much as possible by making `C` a hyperlink to trait
1051            // `B` to disambiguate.
1052            //
1053            // FIXME: this is still a lossy conversion and there should probably
1054            //        be a better way of representing this in general? Most of
1055            //        the ugliness comes from inlining across crates where
1056            //        everything comes in as a fully resolved QPath (hard to
1057            //        look at).
1058            if !f.alternate() {
1059                // FIXME(inherent_associated_types): We always link to the very first associated
1060                // type (in respect to source order) that bears the given name (`assoc.name`) and that is
1061                // affiliated with the computed `DefId`. This is obviously incorrect when we have
1062                // multiple impl blocks. Ideally, we would thread the `DefId` of the assoc ty itself
1063                // through here and map it to the corresponding HTML ID that was generated by
1064                // `render::Context::derive_id` when the impl blocks were rendered.
1065                // There is no such mapping unfortunately.
1066                // As a hack, we could badly imitate `derive_id` here by keeping *count* when looking
1067                // for the assoc ty `DefId` in `tcx.associated_items(self_ty_did).in_definition_order()`
1068                // considering privacy, `doc(hidden)`, etc.
1069                // I don't feel like that right now :cold_sweat:.
1070
1071                let parent_href = match trait_ {
1072                    Some(trait_) => href(trait_.def_id(), cx).ok(),
1073                    None => self_type.def_id(cx.cache()).and_then(|did| href(did, cx).ok()),
1074                };
1075
1076                if let Some((url, _, path)) = parent_href {
1077                    write!(
1078                        f,
1079                        "<a class=\"associatedtype\" href=\"{url}#{shortty}.{name}\" \
1080                                    title=\"type {path}::{name}\">{name}</a>",
1081                        shortty = ItemType::AssocType,
1082                        name = assoc.name,
1083                        path = join_with_double_colon(&path),
1084                    )
1085                } else {
1086                    write!(f, "{}", assoc.name)
1087                }
1088            } else {
1089                write!(f, "{}", assoc.name)
1090            }?;
1091
1092            assoc.args.print(cx).fmt(f)
1093        }
1094    }
1095}
1096
1097impl clean::Type {
1098    pub(crate) fn print(&self, cx: &Context<'_>) -> impl Display {
1099        fmt::from_fn(move |f| fmt_type(self, f, false, cx))
1100    }
1101}
1102
1103impl clean::Path {
1104    pub(crate) fn print(&self, cx: &Context<'_>) -> impl Display {
1105        fmt::from_fn(move |f| resolved_path(f, self.def_id(), self, false, false, cx))
1106    }
1107}
1108
1109impl clean::Impl {
1110    pub(crate) fn print(&self, use_absolute: bool, cx: &Context<'_>) -> impl Display {
1111        fmt::from_fn(move |f| {
1112            f.write_str("impl")?;
1113            self.generics.print(cx).fmt(f)?;
1114            f.write_str(" ")?;
1115
1116            if let Some(ref ty) = self.trait_ {
1117                if self.is_negative_trait_impl() {
1118                    write!(f, "!")?;
1119                }
1120                if self.kind.is_fake_variadic()
1121                    && let Some(generics) = ty.generics()
1122                    && let Ok(inner_type) = generics.exactly_one()
1123                {
1124                    let last = ty.last();
1125                    if f.alternate() {
1126                        write!(f, "{}<", last)?;
1127                        self.print_type(inner_type, f, use_absolute, cx)?;
1128                        write!(f, ">")?;
1129                    } else {
1130                        write!(f, "{}&lt;", print_anchor(ty.def_id(), last, cx))?;
1131                        self.print_type(inner_type, f, use_absolute, cx)?;
1132                        write!(f, "&gt;")?;
1133                    }
1134                } else {
1135                    ty.print(cx).fmt(f)?;
1136                }
1137                write!(f, " for ")?;
1138            }
1139
1140            if let Some(ty) = self.kind.as_blanket_ty() {
1141                fmt_type(ty, f, use_absolute, cx)?;
1142            } else {
1143                self.print_type(&self.for_, f, use_absolute, cx)?;
1144            }
1145
1146            print_where_clause(&self.generics, cx, 0, Ending::Newline).maybe_display().fmt(f)
1147        })
1148    }
1149    fn print_type(
1150        &self,
1151        type_: &clean::Type,
1152        f: &mut fmt::Formatter<'_>,
1153        use_absolute: bool,
1154        cx: &Context<'_>,
1155    ) -> Result<(), fmt::Error> {
1156        if let clean::Type::Tuple(types) = type_
1157            && let [clean::Type::Generic(name)] = &types[..]
1158            && (self.kind.is_fake_variadic() || self.kind.is_auto())
1159        {
1160            // Hardcoded anchor library/core/src/primitive_docs.rs
1161            // Link should match `# Trait implementations`
1162            primitive_link_fragment(
1163                f,
1164                PrimitiveType::Tuple,
1165                format_args!("({name}₁, {name}₂, …, {name}ₙ)"),
1166                "#trait-implementations-1",
1167                cx,
1168            )?;
1169        } else if let clean::Type::Array(ty, len) = type_
1170            && let clean::Type::Generic(name) = &**ty
1171            && &len[..] == "1"
1172            && (self.kind.is_fake_variadic() || self.kind.is_auto())
1173        {
1174            primitive_link(f, PrimitiveType::Array, format_args!("[{name}; N]"), cx)?;
1175        } else if let clean::BareFunction(bare_fn) = &type_
1176            && let [clean::Parameter { type_: clean::Type::Generic(name), .. }] =
1177                &bare_fn.decl.inputs[..]
1178            && (self.kind.is_fake_variadic() || self.kind.is_auto())
1179        {
1180            // Hardcoded anchor library/core/src/primitive_docs.rs
1181            // Link should match `# Trait implementations`
1182
1183            print_higher_ranked_params_with_space(&bare_fn.generic_params, cx, "for").fmt(f)?;
1184            bare_fn.safety.print_with_space().fmt(f)?;
1185            print_abi_with_space(bare_fn.abi).fmt(f)?;
1186            let ellipsis = if bare_fn.decl.c_variadic { ", ..." } else { "" };
1187            primitive_link_fragment(
1188                f,
1189                PrimitiveType::Tuple,
1190                format_args!("fn({name}₁, {name}₂, …, {name}ₙ{ellipsis})"),
1191                "#trait-implementations-1",
1192                cx,
1193            )?;
1194            // Write output.
1195            if !bare_fn.decl.output.is_unit() {
1196                write!(f, " -> ")?;
1197                fmt_type(&bare_fn.decl.output, f, use_absolute, cx)?;
1198            }
1199        } else if let clean::Type::Path { path } = type_
1200            && let Some(generics) = path.generics()
1201            && let Ok(ty) = generics.exactly_one()
1202            && self.kind.is_fake_variadic()
1203        {
1204            let wrapper = print_anchor(path.def_id(), path.last(), cx);
1205            if f.alternate() {
1206                write!(f, "{wrapper:#}&lt;")?;
1207            } else {
1208                write!(f, "{wrapper}<")?;
1209            }
1210            self.print_type(ty, f, use_absolute, cx)?;
1211            if f.alternate() {
1212                write!(f, "&gt;")?;
1213            } else {
1214                write!(f, ">")?;
1215            }
1216        } else {
1217            fmt_type(type_, f, use_absolute, cx)?;
1218        }
1219        Ok(())
1220    }
1221}
1222
1223pub(crate) fn print_params(params: &[clean::Parameter], cx: &Context<'_>) -> impl Display {
1224    fmt::from_fn(move |f| {
1225        params
1226            .iter()
1227            .map(|param| {
1228                fmt::from_fn(|f| {
1229                    if let Some(name) = param.name {
1230                        write!(f, "{}: ", name)?;
1231                    }
1232                    param.type_.print(cx).fmt(f)
1233                })
1234            })
1235            .joined(", ", f)
1236    })
1237}
1238
1239// Implements Write but only counts the bytes "written".
1240struct WriteCounter(usize);
1241
1242impl std::fmt::Write for WriteCounter {
1243    fn write_str(&mut self, s: &str) -> fmt::Result {
1244        self.0 += s.len();
1245        Ok(())
1246    }
1247}
1248
1249// Implements Display by emitting the given number of spaces.
1250struct Indent(usize);
1251
1252impl Display for Indent {
1253    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1254        (0..self.0).for_each(|_| {
1255            f.write_char(' ').unwrap();
1256        });
1257        Ok(())
1258    }
1259}
1260
1261impl clean::FnDecl {
1262    pub(crate) fn print(&self, cx: &Context<'_>) -> impl Display {
1263        fmt::from_fn(move |f| {
1264            let ellipsis = if self.c_variadic { ", ..." } else { "" };
1265            if f.alternate() {
1266                write!(
1267                    f,
1268                    "({params:#}{ellipsis}){arrow:#}",
1269                    params = print_params(&self.inputs, cx),
1270                    ellipsis = ellipsis,
1271                    arrow = self.print_output(cx)
1272                )
1273            } else {
1274                write!(
1275                    f,
1276                    "({params}{ellipsis}){arrow}",
1277                    params = print_params(&self.inputs, cx),
1278                    ellipsis = ellipsis,
1279                    arrow = self.print_output(cx)
1280                )
1281            }
1282        })
1283    }
1284
1285    /// * `header_len`: The length of the function header and name. In other words, the number of
1286    ///   characters in the function declaration up to but not including the parentheses.
1287    ///   This is expected to go into a `<pre>`/`code-header` block, so indentation and newlines
1288    ///   are preserved.
1289    /// * `indent`: The number of spaces to indent each successive line with, if line-wrapping is
1290    ///   necessary.
1291    pub(crate) fn full_print(
1292        &self,
1293        header_len: usize,
1294        indent: usize,
1295        cx: &Context<'_>,
1296    ) -> impl Display {
1297        fmt::from_fn(move |f| {
1298            // First, generate the text form of the declaration, with no line wrapping, and count the bytes.
1299            let mut counter = WriteCounter(0);
1300            write!(&mut counter, "{:#}", fmt::from_fn(|f| { self.inner_full_print(None, f, cx) }))
1301                .unwrap();
1302            // If the text form was over 80 characters wide, we will line-wrap our output.
1303            let line_wrapping_indent =
1304                if header_len + counter.0 > 80 { Some(indent) } else { None };
1305            // Generate the final output. This happens to accept `{:#}` formatting to get textual
1306            // output but in practice it is only formatted with `{}` to get HTML output.
1307            self.inner_full_print(line_wrapping_indent, f, cx)
1308        })
1309    }
1310
1311    fn inner_full_print(
1312        &self,
1313        // For None, the declaration will not be line-wrapped. For Some(n),
1314        // the declaration will be line-wrapped, with an indent of n spaces.
1315        line_wrapping_indent: Option<usize>,
1316        f: &mut fmt::Formatter<'_>,
1317        cx: &Context<'_>,
1318    ) -> fmt::Result {
1319        let amp = if f.alternate() { "&" } else { "&amp;" };
1320
1321        write!(f, "(")?;
1322        if let Some(n) = line_wrapping_indent
1323            && !self.inputs.is_empty()
1324        {
1325            write!(f, "\n{}", Indent(n + 4))?;
1326        }
1327
1328        let last_input_index = self.inputs.len().checked_sub(1);
1329        for (i, param) in self.inputs.iter().enumerate() {
1330            if let Some(selfty) = param.to_receiver() {
1331                match selfty {
1332                    clean::SelfTy => {
1333                        write!(f, "self")?;
1334                    }
1335                    clean::BorrowedRef { lifetime, mutability, type_: box clean::SelfTy } => {
1336                        write!(f, "{amp}")?;
1337                        if let Some(lt) = lifetime {
1338                            write!(f, "{lt} ", lt = lt.print())?;
1339                        }
1340                        write!(f, "{mutability}self", mutability = mutability.print_with_space())?;
1341                    }
1342                    _ => {
1343                        write!(f, "self: ")?;
1344                        selfty.print(cx).fmt(f)?;
1345                    }
1346                }
1347            } else {
1348                if param.is_const {
1349                    write!(f, "const ")?;
1350                }
1351                if let Some(name) = param.name {
1352                    write!(f, "{}: ", name)?;
1353                }
1354                param.type_.print(cx).fmt(f)?;
1355            }
1356            match (line_wrapping_indent, last_input_index) {
1357                (_, None) => (),
1358                (None, Some(last_i)) if i != last_i => write!(f, ", ")?,
1359                (None, Some(_)) => (),
1360                (Some(n), Some(last_i)) if i != last_i => write!(f, ",\n{}", Indent(n + 4))?,
1361                (Some(_), Some(_)) => writeln!(f, ",")?,
1362            }
1363        }
1364
1365        if self.c_variadic {
1366            match line_wrapping_indent {
1367                None => write!(f, ", ...")?,
1368                Some(n) => writeln!(f, "{}...", Indent(n + 4))?,
1369            };
1370        }
1371
1372        match line_wrapping_indent {
1373            None => write!(f, ")")?,
1374            Some(n) => write!(f, "{})", Indent(n))?,
1375        };
1376
1377        self.print_output(cx).fmt(f)
1378    }
1379
1380    fn print_output(&self, cx: &Context<'_>) -> impl Display {
1381        fmt::from_fn(move |f| match &self.output {
1382            clean::Tuple(tys) if tys.is_empty() => Ok(()),
1383            ty if f.alternate() => {
1384                write!(f, " -> {:#}", ty.print(cx))
1385            }
1386            ty => write!(f, " -&gt; {}", ty.print(cx)),
1387        })
1388    }
1389}
1390
1391pub(crate) fn visibility_print_with_space(item: &clean::Item, cx: &Context<'_>) -> impl Display {
1392    fmt::from_fn(move |f| {
1393        if item.is_doc_hidden() {
1394            f.write_str("#[doc(hidden)] ")?;
1395        }
1396
1397        match item.visibility(cx.tcx()) {
1398            None => {}
1399            Some(ty::Visibility::Public) => f.write_str("pub ")?,
1400            Some(ty::Visibility::Restricted(vis_did)) => {
1401                // FIXME(camelid): This may not work correctly if `item_did` is a module.
1402                //                 However, rustdoc currently never displays a module's
1403                //                 visibility, so it shouldn't matter.
1404                let parent_module =
1405                    find_nearest_parent_module(cx.tcx(), item.item_id.expect_def_id());
1406
1407                if vis_did.is_crate_root() {
1408                    f.write_str("pub(crate) ")?;
1409                } else if parent_module == Some(vis_did) {
1410                    // `pub(in foo)` where `foo` is the parent module
1411                    // is the same as no visibility modifier; do nothing
1412                } else if parent_module
1413                    .and_then(|parent| find_nearest_parent_module(cx.tcx(), parent))
1414                    == Some(vis_did)
1415                {
1416                    f.write_str("pub(super) ")?;
1417                } else {
1418                    let path = cx.tcx().def_path(vis_did);
1419                    debug!("path={path:?}");
1420                    // modified from `resolved_path()` to work with `DefPathData`
1421                    let last_name = path.data.last().unwrap().data.get_opt_name().unwrap();
1422                    let anchor = print_anchor(vis_did, last_name, cx);
1423
1424                    f.write_str("pub(in ")?;
1425                    for seg in &path.data[..path.data.len() - 1] {
1426                        write!(f, "{}::", seg.data.get_opt_name().unwrap())?;
1427                    }
1428                    write!(f, "{anchor}) ")?;
1429                }
1430            }
1431        }
1432        Ok(())
1433    })
1434}
1435
1436pub(crate) trait PrintWithSpace {
1437    fn print_with_space(&self) -> &str;
1438}
1439
1440impl PrintWithSpace for hir::Safety {
1441    fn print_with_space(&self) -> &str {
1442        self.prefix_str()
1443    }
1444}
1445
1446impl PrintWithSpace for hir::HeaderSafety {
1447    fn print_with_space(&self) -> &str {
1448        match self {
1449            hir::HeaderSafety::SafeTargetFeatures => "",
1450            hir::HeaderSafety::Normal(safety) => safety.print_with_space(),
1451        }
1452    }
1453}
1454
1455impl PrintWithSpace for hir::IsAsync {
1456    fn print_with_space(&self) -> &str {
1457        match self {
1458            hir::IsAsync::Async(_) => "async ",
1459            hir::IsAsync::NotAsync => "",
1460        }
1461    }
1462}
1463
1464impl PrintWithSpace for hir::Mutability {
1465    fn print_with_space(&self) -> &str {
1466        match self {
1467            hir::Mutability::Not => "",
1468            hir::Mutability::Mut => "mut ",
1469        }
1470    }
1471}
1472
1473pub(crate) fn print_constness_with_space(
1474    c: &hir::Constness,
1475    overall_stab: Option<StableSince>,
1476    const_stab: Option<ConstStability>,
1477) -> &'static str {
1478    match c {
1479        hir::Constness::Const => match (overall_stab, const_stab) {
1480            // const stable...
1481            (_, Some(ConstStability { level: StabilityLevel::Stable { .. }, .. }))
1482            // ...or when feature(staged_api) is not set...
1483            | (_, None)
1484            // ...or when const unstable, but overall unstable too
1485            | (None, Some(ConstStability { level: StabilityLevel::Unstable { .. }, .. })) => {
1486                "const "
1487            }
1488            // const unstable (and overall stable)
1489            (Some(_), Some(ConstStability { level: StabilityLevel::Unstable { .. }, .. })) => "",
1490        },
1491        // not const
1492        hir::Constness::NotConst => "",
1493    }
1494}
1495
1496impl clean::Import {
1497    pub(crate) fn print(&self, cx: &Context<'_>) -> impl Display {
1498        fmt::from_fn(move |f| match self.kind {
1499            clean::ImportKind::Simple(name) => {
1500                if name == self.source.path.last() {
1501                    write!(f, "use {};", self.source.print(cx))
1502                } else {
1503                    write!(f, "use {source} as {name};", source = self.source.print(cx))
1504                }
1505            }
1506            clean::ImportKind::Glob => {
1507                if self.source.path.segments.is_empty() {
1508                    write!(f, "use *;")
1509                } else {
1510                    write!(f, "use {}::*;", self.source.print(cx))
1511                }
1512            }
1513        })
1514    }
1515}
1516
1517impl clean::ImportSource {
1518    pub(crate) fn print(&self, cx: &Context<'_>) -> impl Display {
1519        fmt::from_fn(move |f| match self.did {
1520            Some(did) => resolved_path(f, did, &self.path, true, false, cx),
1521            _ => {
1522                for seg in &self.path.segments[..self.path.segments.len() - 1] {
1523                    write!(f, "{}::", seg.name)?;
1524                }
1525                let name = self.path.last();
1526                if let hir::def::Res::PrimTy(p) = self.path.res {
1527                    primitive_link(f, PrimitiveType::from(p), format_args!("{name}"), cx)?;
1528                } else {
1529                    f.write_str(name.as_str())?;
1530                }
1531                Ok(())
1532            }
1533        })
1534    }
1535}
1536
1537impl clean::AssocItemConstraint {
1538    pub(crate) fn print(&self, cx: &Context<'_>) -> impl Display {
1539        fmt::from_fn(move |f| {
1540            f.write_str(self.assoc.name.as_str())?;
1541            self.assoc.args.print(cx).fmt(f)?;
1542            match self.kind {
1543                clean::AssocItemConstraintKind::Equality { ref term } => {
1544                    f.write_str(" = ")?;
1545                    term.print(cx).fmt(f)?;
1546                }
1547                clean::AssocItemConstraintKind::Bound { ref bounds } => {
1548                    if !bounds.is_empty() {
1549                        f.write_str(": ")?;
1550                        print_generic_bounds(bounds, cx).fmt(f)?;
1551                    }
1552                }
1553            }
1554            Ok(())
1555        })
1556    }
1557}
1558
1559pub(crate) fn print_abi_with_space(abi: ExternAbi) -> impl Display {
1560    fmt::from_fn(move |f| {
1561        let quot = if f.alternate() { "\"" } else { "&quot;" };
1562        match abi {
1563            ExternAbi::Rust => Ok(()),
1564            abi => write!(f, "extern {0}{1}{0} ", quot, abi.name()),
1565        }
1566    })
1567}
1568
1569pub(crate) fn print_default_space(v: bool) -> &'static str {
1570    if v { "default " } else { "" }
1571}
1572
1573impl clean::GenericArg {
1574    pub(crate) fn print(&self, cx: &Context<'_>) -> impl Display {
1575        fmt::from_fn(move |f| match self {
1576            clean::GenericArg::Lifetime(lt) => lt.print().fmt(f),
1577            clean::GenericArg::Type(ty) => ty.print(cx).fmt(f),
1578            clean::GenericArg::Const(ct) => ct.print(cx.tcx()).fmt(f),
1579            clean::GenericArg::Infer => Display::fmt("_", f),
1580        })
1581    }
1582}
1583
1584impl clean::Term {
1585    pub(crate) fn print(&self, cx: &Context<'_>) -> impl Display {
1586        fmt::from_fn(move |f| match self {
1587            clean::Term::Type(ty) => ty.print(cx).fmt(f),
1588            clean::Term::Constant(ct) => ct.print(cx.tcx()).fmt(f),
1589        })
1590    }
1591}