| //! HTML formatting module | |
| //! | |
| //! This module contains a large number of `Display` implementations for | |
| //! various types in `rustdoc::clean`. | |
| //! | |
| //! These implementations all emit HTML. As an internal implementation detail, | |
| //! some of them support an alternate format that emits plain text. | |
| use std::cmp::Ordering; | |
| use std::fmt::{self, Display, Write}; | |
| use std::{iter, slice}; | |
| use itertools::{Either, Itertools}; | |
| use rustc_abi::ExternAbi; | |
| use rustc_ast::join_path_syms; | |
| use rustc_attr_ir::{ConstStability, StabilityLevel, StableSince}; | |
| use rustc_data_structures::fx::FxHashSet; | |
| use rustc_hir as hir; | |
| use rustc_hir::def::{DefKind, MacroKinds}; | |
| use rustc_hir::def_id::{DefId, LOCAL_CRATE, ModId}; | |
| use rustc_metadata::creader::CStore; | |
| use rustc_middle::ty::{self, Ty, TyCtxt, TypingMode}; | |
| use rustc_span::symbol::kw; | |
| use rustc_span::{Ident, Symbol, sym}; | |
| use tracing::{debug, trace}; | |
| use super::url_parts_builder::UrlPartsBuilder; | |
| use crate::clean::types::ExternalLocation; | |
| use crate::clean::utils::find_nearest_parent_module; | |
| use crate::clean::{self, ExternalCrate, PrimitiveType, WherePredicate}; | |
| use crate::display::{Joined as _, MaybeDisplay as _, WithOpts, Wrapped}; | |
| use crate::formats::cache::Cache; | |
| use crate::formats::item_type::ItemType; | |
| use crate::html::escape::{Escape, EscapeBodyText}; | |
| use crate::html::render::Context; | |
| use crate::passes::collect_intra_doc_links::UrlFragment; | |
| pub(crate) fn print_generic_bounds( | |
| bounds: &[clean::GenericBound], | |
| cx: &Context<'_>, | |
| ) -> impl Display { | |
| fmt::from_fn(move |f| { | |
| let mut bounds_dup = FxHashSet::default(); | |
| bounds | |
| .iter() | |
| .filter(move |b| bounds_dup.insert(*b)) | |
| .map(|bound| print_generic_bound(bound, cx)) | |
| .joined(" + ", f) | |
| }) | |
| } | |
| pub(crate) fn print_generic_param_def( | |
| generic_param: &clean::GenericParamDef, | |
| cx: &Context<'_>, | |
| ) -> impl Display { | |
| fmt::from_fn(move |f| match &generic_param.kind { | |
| clean::GenericParamDefKind::Lifetime { outlives } => { | |
| write!(f, "{}", generic_param.name)?; | |
| if !outlives.is_empty() { | |
| f.write_str(": ")?; | |
| outlives.iter().map(|lt| print_lifetime(lt)).joined(" + ", f)?; | |
| } | |
| Ok(()) | |
| } | |
| clean::GenericParamDefKind::Type { bounds, default, .. } => { | |
| f.write_str(generic_param.name.as_str())?; | |
| if !bounds.is_empty() { | |
| f.write_str(": ")?; | |
| print_generic_bounds(bounds, cx).fmt(f)?; | |
| } | |
| if let Some(ty) = default { | |
| f.write_str(" = ")?; | |
| print_type(ty, cx).fmt(f)?; | |
| } | |
| Ok(()) | |
| } | |
| clean::GenericParamDefKind::Const { ty, default, .. } => { | |
| write!(f, "const {}: ", generic_param.name)?; | |
| print_type(ty, cx).fmt(f)?; | |
| if let Some(default) = default { | |
| f.write_str(" = ")?; | |
| if f.alternate() { | |
| write!(f, "{default}")?; | |
| } else { | |
| write!(f, "{}", Escape(default))?; | |
| } | |
| } | |
| Ok(()) | |
| } | |
| }) | |
| } | |
| pub(crate) fn print_generics(generics: &clean::Generics, cx: &Context<'_>) -> impl Display { | |
| let mut real_params = generics.params.iter().filter(|p| !p.is_synthetic_param()).peekable(); | |
| if real_params.peek().is_none() { | |
| None | |
| } else { | |
| Some(Wrapped::with_angle_brackets().wrap_fn(move |f| { | |
| real_params.clone().map(|g| print_generic_param_def(g, cx)).joined(", ", f) | |
| })) | |
| } | |
| .maybe_display() | |
| } | |
| #[derive(Clone, Copy, PartialEq, Eq)] | |
| pub(crate) enum Ending { | |
| Newline, | |
| NoNewline, | |
| } | |
| fn print_where_predicate(predicate: &clean::WherePredicate, cx: &Context<'_>) -> impl Display { | |
| fmt::from_fn(move |f| { | |
| match predicate { | |
| clean::WherePredicate::BoundPredicate { ty, bounds, bound_params } => { | |
| print_higher_ranked_params_with_space(bound_params, cx, "for").fmt(f)?; | |
| print_type(ty, cx).fmt(f)?; | |
| f.write_str(":")?; | |
| if !bounds.is_empty() { | |
| f.write_str(" ")?; | |
| print_generic_bounds(bounds, cx).fmt(f)?; | |
| } | |
| Ok(()) | |
| } | |
| clean::WherePredicate::RegionPredicate { lifetime, bounds } => { | |
| // We don't need to check `alternate` since we can be certain that neither | |
| // the lifetime nor the bounds contain any characters which need escaping. | |
| write!(f, "{}:", print_lifetime(lifetime))?; | |
| if !bounds.is_empty() { | |
| write!(f, " {}", print_generic_bounds(bounds, cx))?; | |
| } | |
| Ok(()) | |
| } | |
| clean::WherePredicate::ProjectionPredicate { lhs, rhs } => { | |
| let opts = WithOpts::from(f); | |
| write!( | |
| f, | |
| "{} == {}", | |
| opts.display(print_qpath_data(lhs, cx)), | |
| opts.display(print_term(rhs, cx)), | |
| ) | |
| } | |
| } | |
| }) | |
| } | |
| /// * The Generics from which to emit a where-clause. | |
| /// * The number of spaces to indent each line with. | |
| /// * Whether the where-clause needs to add a comma and newline after the last bound. | |
| pub(crate) fn print_where_clause( | |
| gens: &clean::Generics, | |
| cx: &Context<'_>, | |
| indent: usize, | |
| ending: Ending, | |
| ) -> Option<impl Display> { | |
| if gens.where_predicates.is_empty() { | |
| return None; | |
| } | |
| fn where_preds( | |
| predicates: &[WherePredicate], | |
| cx: &Context<'_>, | |
| sep: impl Display, | |
| ) -> impl Display { | |
| fmt::from_fn(move |f| { | |
| predicates.iter().map(|predicate| print_where_predicate(predicate, cx)).joined(&sep, f) | |
| }) | |
| } | |
| let spaces = |n: usize| crate::display::repeat(' ', n); | |
| Some(fmt::from_fn(move |f| { | |
| if f.alternate() { | |
| write!(f, " where {:#}", where_preds(&gens.where_predicates, cx, ", "))?; | |
| if ending == Ending::Newline { | |
| f.write_char(',')?; | |
| } | |
| return Ok(()); | |
| } | |
| const WHERE_INDENT: usize = 3; | |
| let padding = { | |
| let padding_amount = if ending == Ending::Newline { | |
| indent + 4 | |
| } else if indent == 0 { | |
| 4 | |
| } else { | |
| indent + WHERE_INDENT + "where ".len() | |
| }; | |
| spaces(padding_amount) | |
| }; | |
| let br_with_padding = format_args!("\n{padding}"); | |
| let sep = format_args!(",{br_with_padding}"); | |
| let where_preds = where_preds(&gens.where_predicates, cx, sep); | |
| if ending == Ending::Newline { | |
| write!( | |
| f, | |
| "{indent}<div class=\"where\">where{br_with_padding}{where_preds},</div>", | |
| indent = spaces(indent.saturating_sub(1)), | |
| ) | |
| } else if indent == 0 { | |
| write!(f, "\n<span class=\"where\">where{br_with_padding}{where_preds}</span>") | |
| } else { | |
| write!( | |
| f, | |
| "\n{indent}<span class=\"where\">where {where_preds}</span>", | |
| indent = spaces(indent + WHERE_INDENT), | |
| ) | |
| } | |
| })) | |
| } | |
| #[inline] | |
| pub(crate) fn print_lifetime(lt: &clean::Lifetime) -> &str { | |
| lt.0.as_str() | |
| } | |
| pub(crate) fn print_constant_kind( | |
| constant_kind: &clean::ConstantKind, | |
| tcx: TyCtxt<'_>, | |
| ) -> impl Display { | |
| let expr = constant_kind.expr(tcx); | |
| fmt::from_fn( | |
| move |f| { | |
| if f.alternate() { f.write_str(&expr) } else { write!(f, "{}", Escape(&expr)) } | |
| }, | |
| ) | |
| } | |
| fn print_poly_trait(poly_trait: &clean::PolyTrait, cx: &Context<'_>) -> impl Display { | |
| fmt::from_fn(move |f| { | |
| print_higher_ranked_params_with_space(&poly_trait.generic_params, cx, "for").fmt(f)?; | |
| print_path(&poly_trait.trait_, cx).fmt(f) | |
| }) | |
| } | |
| pub(crate) fn print_generic_bound( | |
| generic_bound: &clean::GenericBound, | |
| cx: &Context<'_>, | |
| ) -> impl Display { | |
| fmt::from_fn(move |f| match generic_bound { | |
| clean::GenericBound::Outlives(lt) => f.write_str(print_lifetime(lt)), | |
| clean::GenericBound::TraitBound(ty, modifiers) => { | |
| // `const` and `[const]` trait bounds are experimental; don't render them. | |
| let hir::TraitBoundModifiers { polarity, constness: _ } = modifiers; | |
| f.write_str(match polarity { | |
| hir::BoundPolarity::Positive => "", | |
| hir::BoundPolarity::Maybe(_) => "?", | |
| hir::BoundPolarity::Negative(_) => "!", | |
| })?; | |
| print_poly_trait(ty, cx).fmt(f) | |
| } | |
| clean::GenericBound::Use(args) => { | |
| f.write_str("use")?; | |
| Wrapped::with_angle_brackets() | |
| .wrap_fn(|f| args.iter().map(|arg| arg.name()).joined(", ", f)) | |
| .fmt(f) | |
| } | |
| }) | |
| } | |
| fn print_generic_args(generic_args: &clean::GenericArgs, cx: &Context<'_>) -> impl Display { | |
| fmt::from_fn(move |f| { | |
| match generic_args { | |
| clean::GenericArgs::AngleBracketed { args, constraints } => { | |
| if !args.is_empty() || !constraints.is_empty() { | |
| Wrapped::with_angle_brackets() | |
| .wrap_fn(|f| { | |
| [Either::Left(args), Either::Right(constraints)] | |
| .into_iter() | |
| .flat_map(Either::factor_into_iter) | |
| .map(|either| { | |
| either.map_either( | |
| |arg| print_generic_arg(arg, cx), | |
| |constraint| print_assoc_item_constraint(constraint, cx), | |
| ) | |
| }) | |
| .joined(", ", f) | |
| }) | |
| .fmt(f)?; | |
| } | |
| } | |
| clean::GenericArgs::Parenthesized { inputs, output } => { | |
| Wrapped::with_parens() | |
| .wrap_fn(|f| inputs.iter().map(|ty| print_type(ty, cx)).joined(", ", f)) | |
| .fmt(f)?; | |
| if let Some(ref ty) = *output { | |
| f.write_str(if f.alternate() { " -> " } else { " -> " })?; | |
| print_type(ty, cx).fmt(f)?; | |
| } | |
| } | |
| clean::GenericArgs::ReturnTypeNotation => { | |
| f.write_str("(..)")?; | |
| } | |
| } | |
| Ok(()) | |
| }) | |
| } | |
| // Possible errors when computing href link source for a `DefId` | |
| #[derive(PartialEq, Eq, Debug)] | |
| pub(crate) enum HrefError { | |
| /// This item is known to rustdoc, but from a crate that does not have documentation generated. | |
| /// | |
| /// This can only happen for non-local items. | |
| /// | |
| /// # Example | |
| /// | |
| /// Crate `a` defines a public trait and crate `b` – the target crate that depends on `a` – | |
| /// implements it for a local type. | |
| /// We document `b` but **not** `a` (we only _build_ the latter – with `rustc`): | |
| /// | |
| /// ```sh | |
| /// rustc a.rs --crate-type=lib | |
| /// rustdoc b.rs --crate-type=lib --extern=a=liba.rlib | |
| /// ``` | |
| /// | |
| /// Now, the associated items in the trait impl want to link to the corresponding item in the | |
| /// trait declaration (see `html::render::assoc_href_attr`) but it's not available since their | |
| /// *documentation (was) not built*. | |
| DocumentationNotBuilt, | |
| /// This can only happen for non-local items when `--document-private-items` is not passed. | |
| Private, | |
| // Not in external cache, href link should be in same page | |
| NotInExternalCache, | |
| /// Refers to an unnamable item, such as one defined within a function or const block. | |
| UnnamableItem, | |
| } | |
| /// Type representing information of an `href` attribute. | |
| #[derive(Debug)] | |
| pub(crate) struct HrefInfo { | |
| /// URL to the item page. | |
| pub(crate) url: String, | |
| /// Kind of the item (used to generate the `title` attribute). | |
| pub(crate) kind: ItemType, | |
| /// Rust path to the item (used to generate the `title` attribute). | |
| pub(crate) rust_path: Vec<Symbol>, | |
| } | |
| /// This function is to get the external macro path because they are not in the cache used in | |
| /// `href_with_jump_to_def_path_depth`. | |
| fn generate_macro_def_id_path( | |
| def_id: DefId, | |
| cx: &Context<'_>, | |
| jump_to_def_path_depth: Option<usize>, | |
| ) -> Result<HrefInfo, HrefError> { | |
| let tcx = cx.tcx(); | |
| let crate_name = tcx.crate_name(def_id.krate); | |
| let cache = cx.cache(); | |
| let cstore = CStore::from_tcx(tcx); | |
| // We need this to prevent a `panic` when this function is used from intra doc links... | |
| if !cstore.has_crate_data(def_id.krate) { | |
| debug!("No data for crate {crate_name}"); | |
| return Err(HrefError::NotInExternalCache); | |
| } | |
| let DefKind::Macro(kinds) = tcx.def_kind(def_id) else { | |
| unreachable!(); | |
| }; | |
| let item_type = if kinds == MacroKinds::DERIVE { | |
| ItemType::ProcDerive | |
| } else if kinds == MacroKinds::ATTR { | |
| ItemType::ProcAttribute | |
| } else { | |
| ItemType::Macro | |
| }; | |
| let path = clean::inline::get_item_path(tcx, def_id, item_type); | |
| // The minimum we can have is the crate name followed by the macro name. If shorter, then | |
| // it means that `relative` was empty, which is an error. | |
| let [module_path @ .., last] = path.as_slice() else { | |
| debug!("macro path is empty!"); | |
| return Err(HrefError::NotInExternalCache); | |
| }; | |
| if module_path.is_empty() { | |
| debug!("macro path too short: missing crate prefix (got 1 element, need at least 2)"); | |
| return Err(HrefError::NotInExternalCache); | |
| } | |
| let url = match cache.extern_locations[&def_id.krate] { | |
| ExternalLocation::Remote { ref url, is_absolute } => { | |
| let depth = jump_to_def_path_depth.unwrap_or(cx.current.len()); | |
| let mut prefix = remote_url_prefix(url, is_absolute, depth); | |
| prefix.extend(module_path.iter().copied()); | |
| prefix.push_fmt(format_args!("{}.{last}.html", item_type.as_str())); | |
| prefix.finish() | |
| } | |
| ExternalLocation::Local => { | |
| format!( | |
| "{prefix}{path}/{item_type}.{last}.html", | |
| prefix = "../".repeat(jump_to_def_path_depth.unwrap_or(0)), | |
| path = fmt::from_fn(|f| module_path.iter().joined("/", f)), | |
| item_type = item_type.as_str(), | |
| ) | |
| } | |
| ExternalLocation::Unknown => { | |
| debug!("crate {crate_name} not in cache when linkifying macros"); | |
| return Err(HrefError::NotInExternalCache); | |
| } | |
| }; | |
| Ok(HrefInfo { url, kind: item_type, rust_path: path }) | |
| } | |
| /// Takes an impl `DefId` and return the self `Ty` of the impl. | |
| fn impl_self_ty(tcx: TyCtxt<'_>, impl_def_id: DefId) -> Ty<'_> { | |
| use rustc_middle::traits::ObligationCause; | |
| use rustc_middle::ty; | |
| use rustc_trait_selection::infer::TyCtxtInferExt; | |
| use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt; | |
| let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis()); | |
| let ty = tcx.type_of(impl_def_id); | |
| infcx | |
| .at(&ObligationCause::dummy(), tcx.param_env(impl_def_id)) | |
| .query_normalize(ty::Binder::dummy(ty.instantiate_identity().skip_norm_wip())) | |
| .map(|resolved| infcx.deeply_resolve_ignoring_regions(resolved.value).skip_binder()) | |
| .unwrap_or(ty.skip_binder()) | |
| } | |
| fn transitive_reexport_path(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Vec<Symbol>> { | |
| transitive_reexport_path_inner(tcx, def_id, &mut Vec::new()) | |
| } | |
| /// Simplified implementation of `rustc_middle::ty::print::pretty::try_print_visible_def_path_recur`. | |
| fn transitive_reexport_path_inner( | |
| tcx: TyCtxt<'_>, | |
| def_id: DefId, | |
| callers: &mut Vec<DefId>, | |
| ) -> Option<Vec<Symbol>> { | |
| use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData}; | |
| if let Some(cnum) = def_id.as_crate_root() { | |
| return Some(vec![tcx.crate_name(cnum)]); | |
| } | |
| let visible_parent_map = tcx.visible_parent_map(()); | |
| let mut cur_def_key = tcx.def_key(def_id); | |
| // For a constructor, we want the name of its parent rather than <unnamed>. | |
| if let DefPathData::Ctor = cur_def_key.disambiguated_data.data { | |
| let parent = DefId { | |
| krate: def_id.krate, | |
| index: cur_def_key | |
| .parent | |
| .expect("`DefPathData::Ctor` / `VariantData` missing a parent"), | |
| }; | |
| cur_def_key = tcx.def_key(parent); | |
| } | |
| let visible_parent = visible_parent_map.get(&def_id).cloned()?; | |
| // FIXME: Should we also check for private items? | |
| if tcx.is_doc_hidden(visible_parent) { | |
| return None; | |
| } | |
| let actual_parent = tcx.opt_parent(def_id); | |
| let mut data = cur_def_key.disambiguated_data.data; | |
| match data { | |
| DefPathData::TypeNs(ref mut name) if Some(visible_parent) != actual_parent => { | |
| // Item might be re-exported several times, but filter for the one | |
| // that's public and whose identifier isn't `_`. | |
| let reexport = tcx | |
| .module_children(ModId::new_unchecked(visible_parent)) | |
| .iter() | |
| .filter(|child| child.res.opt_def_id() == Some(def_id)) | |
| .find(|child| child.vis.is_public() && child.ident.name != kw::Underscore) | |
| .map(|child| child.ident.name); | |
| if let Some(new_name) = reexport { | |
| *name = new_name; | |
| } else { | |
| // There is no name that is public and isn't `_`, so bail. | |
| return None; | |
| } | |
| } | |
| // Re-exported `extern crate`. | |
| DefPathData::CrateRoot => { | |
| data = DefPathData::TypeNs(tcx.crate_name(def_id.krate)); | |
| } | |
| _ => {} | |
| } | |
| if callers.contains(&visible_parent) { | |
| return None; | |
| } | |
| callers.push(visible_parent); | |
| let mut path = transitive_reexport_path_inner(tcx, visible_parent, callers)?; | |
| callers.pop(); | |
| path.push(DisambiguatedDefPathData { data, disambiguator: 0 }.as_sym(false)); | |
| Some(path) | |
| } | |
| fn generate_item_def_id_path( | |
| mut def_id: DefId, | |
| original_def_id: DefId, | |
| cx: &Context<'_>, | |
| jump_to_def_path_depth: Option<usize>, | |
| ) -> Result<HrefInfo, HrefError> { | |
| let tcx = cx.tcx(); | |
| let crate_name = tcx.crate_name(def_id.krate); | |
| let mut prim = None; | |
| let mut maybe_have_impl_not_in_def_crate = false; | |
| // No need to try to infer the actual parent item if it's not an associated item from the `impl` | |
| // block. | |
| if def_id != original_def_id | |
| && let DefKind::Impl { of_trait } = tcx.def_kind(def_id) | |
| { | |
| let ty = impl_self_ty(tcx, def_id); | |
| // If this is a dyn trait, we want to get the actual trait from which the method comes from. | |
| // Since a `dyn trait` (as of 2026) can only be composed of a trait plus auto traits, we | |
| // look for the trait and ignore auto traits. | |
| if let ty::Dynamic(traits, _) = ty.kind() | |
| && let Some(trait_def_id) = | |
| traits.iter().find_map(|trait_| match trait_.skip_binder() { | |
| ty::ExistentialPredicate::Trait(t) => Some(t.def_id), | |
| ty::ExistentialPredicate::Projection(p) => Some(p.trait_ref(tcx).def_id), | |
| ty::ExistentialPredicate::AutoTrait(_) => None, | |
| }) | |
| { | |
| def_id = trait_def_id; | |
| } else if let Some(new_def_id) = ty.ty_adt_def().map(|adt| adt.did()) { | |
| // If the inferred item is available in the local path map, it means we're reexporting | |
| // from the current crate, so better use the local path directly. | |
| if cx.cache().paths.contains_key(&new_def_id) | |
| && let Ok(mut info) = | |
| href_with_jump_to_def_path_depth(new_def_id, cx, jump_to_def_path_depth, None) | |
| { | |
| let kind = ItemType::from_def_id(original_def_id, tcx); | |
| // We need to append the anchor to the URL otherwise we're just linking to the | |
| // parent item... | |
| info.url = format!("{}#{kind}.{}", info.url, tcx.item_name(original_def_id)); | |
| return Ok(info); | |
| } | |
| def_id = new_def_id; | |
| maybe_have_impl_not_in_def_crate = !of_trait | |
| && !original_def_id.is_local() | |
| && !def_id.is_local() | |
| && def_id.krate != original_def_id.krate; | |
| } else { | |
| // This hack is because primitive types are only documented in `core` and `std`. | |
| // However, with `#[rustc_allow_incoherent_impl]`, a lot of primitive methods are | |
| // implemented in `core`. Some of them are documented in `core` (like `[]::sort`) while | |
| // others aren't (like `[]::to_vec`). So in case we're not documenting `core`, we link | |
| // to `std`. | |
| if !of_trait && crate_name != sym::core { | |
| if ![sym::alloc, sym::std].contains(&crate_name) { | |
| // We cannot link to this primitive's associated item as it's not part of | |
| // `core`, `alloc` or `std` so returning early. | |
| return Err(HrefError::UnnamableItem); | |
| } | |
| maybe_have_impl_not_in_def_crate = true; | |
| } | |
| prim = PrimitiveType::from_ty(ty); | |
| } | |
| } | |
| let (shortty, fqp) = if let Some(prim) = prim { | |
| let crate_name = if maybe_have_impl_not_in_def_crate { sym::std } else { crate_name }; | |
| (ItemType::Primitive, vec![crate_name, prim.as_sym()]) | |
| } else { | |
| ( | |
| ItemType::from_def_id(def_id, tcx), | |
| if maybe_have_impl_not_in_def_crate | |
| // We have a method, not coming from a trait, implemented from a different crate | |
| // where the original item is defined. So in short, the item is using | |
| // `#[rustc_allow_incoherent_impl]` and we need to keep the non-final item path. | |
| // Sadly if we use `item_relative_path` which uses `def_path`, it renders the final | |
| // item path and not the intermediate one. | |
| && let Some(fqp) = transitive_reexport_path(tcx, def_id) | |
| { | |
| fqp | |
| } else { | |
| let mut fqp = vec![crate_name]; | |
| fqp.append(&mut clean::inline::item_relative_path(tcx, def_id)); | |
| fqp | |
| }, | |
| ) | |
| }; | |
| let module_fqp = to_module_fqp(shortty, &fqp); | |
| let (parts, is_remote) = | |
| url_parts(cx.cache(), def_id, module_fqp, &cx.current, jump_to_def_path_depth)?; | |
| let mut url = make_href(jump_to_def_path_depth, shortty, parts, &fqp, is_remote); | |
| if def_id != original_def_id { | |
| let kind = ItemType::from_def_id(original_def_id, tcx); | |
| url = format!("{url}#{kind}.{}", tcx.item_name(original_def_id)) | |
| }; | |
| Ok(HrefInfo { url, kind: shortty, rust_path: fqp }) | |
| } | |
| /// Checks if the given defid refers to an item that is unnamable, such as one defined in a const block. | |
| fn is_unnamable(tcx: TyCtxt<'_>, did: DefId) -> bool { | |
| let mut cur_did = did; | |
| while let Some(parent) = tcx.opt_parent(cur_did) { | |
| match tcx.def_kind(parent) { | |
| // items defined in these can be linked to, as long as they are visible | |
| DefKind::Mod | DefKind::ForeignMod => cur_did = parent, | |
| // items in impls can be linked to, | |
| // as long as we can link to the item the impl is on. | |
| // since associated traits are not a thing, | |
| // it should not be possible to refer to an impl item if | |
| // the base type is not namable. | |
| DefKind::Impl { .. } => return false, | |
| // everything else does not have docs generated for it | |
| _ => return true, | |
| } | |
| } | |
| return false; | |
| } | |
| fn to_module_fqp(shortty: ItemType, fqp: &[Symbol]) -> &[Symbol] { | |
| if shortty == ItemType::Module { fqp } else { &fqp[..fqp.len() - 1] } | |
| } | |
| fn remote_url_prefix(url: &str, is_absolute: bool, depth: usize) -> UrlPartsBuilder { | |
| let url = url.trim_end_matches('/'); | |
| if is_absolute { | |
| UrlPartsBuilder::singleton(url) | |
| } else { | |
| let extra = depth.saturating_sub(1); | |
| let mut b: UrlPartsBuilder = iter::repeat_n("..", extra).collect(); | |
| b.push(url); | |
| b | |
| } | |
| } | |
| /// Returns `(url_parts, is_remote)` where `is_remote` indicates the URL points to an | |
| /// external location and should not use relative URLs. | |
| /// | |
| /// This function tries to generate minimal, relative URLs. When generating an intra-doc | |
| /// link from one item page to another, it compares the paths of both items and generates | |
| /// exactly the right number of "../"'s to reach the deepest common parent, then fills in | |
| /// the rest of the path. This is done by comparing `module_fqp` with `relative_to`. | |
| /// | |
| /// When generating a link for jump-to-def across crates, that won't work, because sources are | |
| /// stored under `src/<crate>/<path>.rs`. When linking jump-to-def for another crate, we just | |
| /// write enough "../"'s to reach the doc root, then generate the entire path for `module_fqp`. | |
| /// The right number of "../"'s is stored in `jump_to_def_path_depth`. | |
| fn url_parts( | |
| cache: &Cache, | |
| def_id: DefId, | |
| module_fqp: &[Symbol], | |
| relative_to: &[Symbol], | |
| jump_to_def_path_depth: Option<usize>, | |
| ) -> Result<(UrlPartsBuilder, bool), HrefError> { | |
| let location = if def_id.krate == LOCAL_CRATE { | |
| &ExternalLocation::Local | |
| } else { | |
| &cache.extern_locations[&def_id.krate] | |
| }; | |
| match *location { | |
| ExternalLocation::Remote { ref url, is_absolute } => { | |
| let depth = jump_to_def_path_depth.unwrap_or(relative_to.len()); | |
| let mut builder = remote_url_prefix(url, is_absolute, depth); | |
| builder.extend(module_fqp.iter().copied()); | |
| // Remote URLs (both absolute and relative) already encode the correct path; | |
| // no additional depth prefix should be applied. | |
| Ok((builder, true)) | |
| } | |
| ExternalLocation::Local => Ok((href_relative_parts(module_fqp, relative_to), false)), | |
| ExternalLocation::Unknown => Err(HrefError::DocumentationNotBuilt), | |
| } | |
| } | |
| fn make_href( | |
| jump_to_def_path_depth: Option<usize>, | |
| shortty: ItemType, | |
| mut url_parts: UrlPartsBuilder, | |
| fqp: &[Symbol], | |
| is_remote: bool, | |
| ) -> String { | |
| // Remote URLs already have the correct depth via `remote_url_prefix`; | |
| // only local items need `jump_to_def_path_depth` to bridge from source pages to the doc tree. | |
| if !is_remote && let Some(depth) = jump_to_def_path_depth { | |
| url_parts.push_front(&iter::repeat_n("..", depth).join("/")); | |
| } | |
| debug!(?url_parts); | |
| match shortty { | |
| ItemType::Module => { | |
| url_parts.push("index.html"); | |
| } | |
| _ => { | |
| let last = fqp.last().unwrap(); | |
| url_parts.push_fmt(format_args!("{shortty}.{last}.html")); | |
| } | |
| } | |
| url_parts.finish() | |
| } | |
| fn ty_inherits_doc_hidden<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool { | |
| match ty.kind() { | |
| // If this is a dyn trait, we want to get the actual trait from which the method comes from. | |
| // Since a `dyn trait` (as of 2026) can only be composed of a trait plus auto traits, we | |
| // look for the trait and ignore auto traits. | |
| ty::Dynamic(traits, _) => traits | |
| .iter() | |
| .find_map(|trait_| match trait_.skip_binder() { | |
| ty::ExistentialPredicate::Trait(t) => Some(inherits_doc_hidden(tcx, t.def_id)), | |
| ty::ExistentialPredicate::Projection(p) => { | |
| Some(inherits_doc_hidden(tcx, p.trait_ref(tcx).def_id)) | |
| } | |
| ty::ExistentialPredicate::AutoTrait(_) => None, | |
| }) | |
| .unwrap_or(false), | |
| ty::Adt(adt, _) => inherits_doc_hidden(tcx, adt.did()), | |
| ty::Foreign(def_id) => inherits_doc_hidden(tcx, *def_id), | |
| ty::Ref(_, ty, _) => ty_inherits_doc_hidden(tcx, *ty), | |
| // For now we consider that everything doesn't inherit `#[doc(hidden)]`. To be confirmed | |
| // later. | |
| _ => false, | |
| } | |
| } | |
| fn inherits_doc_hidden(tcx: TyCtxt<'_>, mut def_id: DefId) -> bool { | |
| loop { | |
| if tcx.is_doc_hidden(def_id) { | |
| return true; | |
| } else if def_id.is_crate_root() { | |
| return false; | |
| } else if let DefKind::Impl { of_trait } = tcx.def_kind(def_id) { | |
| // `impl` blocks stand a bit on their own: unless they have `#[doc(hidden)]` directly | |
| // on them, they don't inherit it from the parent context. | |
| // Instead we check that the `Self` item and the trait aren't hidden. | |
| return ty_inherits_doc_hidden(tcx, impl_self_ty(tcx, def_id)) | |
| || (of_trait && inherits_doc_hidden(tcx, tcx.impl_trait_id(def_id))); | |
| } | |
| def_id = tcx.parent(def_id); | |
| } | |
| } | |
| /// If `jump_to_def_path_depth` is `Some`, the link comes from a source page and needs that many | |
| /// `..` segments to reach the doc root; `None` generates a link relative to the current page. | |
| pub(crate) fn href_with_jump_to_def_path_depth( | |
| original_did: DefId, | |
| cx: &Context<'_>, | |
| jump_to_def_path_depth: Option<usize>, | |
| preferred_name: Option<&str>, | |
| ) -> Result<HrefInfo, HrefError> { | |
| let tcx = cx.tcx(); | |
| let def_kind = tcx.def_kind(original_did); | |
| let did = match def_kind { | |
| DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant => { | |
| // documented on their parent's page | |
| tcx.parent(original_did) | |
| } | |
| // If this a constructor, we get the parent (either a struct or a variant) and then | |
| // generate the link for this item. | |
| DefKind::Ctor(..) => { | |
| return href_with_jump_to_def_path_depth( | |
| tcx.parent(original_did), | |
| cx, | |
| jump_to_def_path_depth, | |
| preferred_name, | |
| ); | |
| } | |
| DefKind::ExternCrate => { | |
| // Link to the crate itself, not the `extern crate` item. | |
| if let Some(local_did) = original_did.as_local() { | |
| tcx.extern_mod_stmt_cnum(local_did).unwrap_or(LOCAL_CRATE).as_def_id() | |
| } else { | |
| original_did | |
| } | |
| } | |
| _ => original_did, | |
| }; | |
| if is_unnamable(tcx, did) { | |
| return Err(HrefError::UnnamableItem); | |
| } | |
| let cache = cx.cache(); | |
| let relative_to = &cx.current; | |
| if !original_did.is_local() { | |
| // If we are generating an href for the "jump to def" feature, then the only case we want | |
| // to ignore is if the item is `doc(hidden)` because we can't link to it. | |
| if jump_to_def_path_depth.is_some() { | |
| if inherits_doc_hidden(tcx, original_did) { | |
| return Err(HrefError::Private); | |
| } | |
| } else if !cache.effective_visibilities.is_directly_public(tcx, did) | |
| && !cache.document_private | |
| && !cache.primitive_locations.values().any(|&id| id == did) | |
| { | |
| return Err(HrefError::Private); | |
| } | |
| } | |
| let (fqp, shortty, url_parts, is_remote) = match cache.paths.get(&did) { | |
| Some(info) => ( | |
| info.get_preferred_path(preferred_name), | |
| info.ty, | |
| { | |
| let module_fqp = to_module_fqp(info.ty, info.parts.as_slice()); | |
| debug!(?info.parts, ?info.ty, ?module_fqp); | |
| href_relative_parts(module_fqp, relative_to) | |
| }, | |
| false, | |
| ), | |
| None => { | |
| // Associated items are handled differently with "jump to def": the anchor is generated | |
| // directly here whereas for intra-doc links, we have some extra computation being | |
| // performed there. | |
| // | |
| // So if `jump_to_def_path_depth` is `Some()` and it's an associated item, we enter to | |
| // enter the `else` branch to have the anchor generated here directly. | |
| if (jump_to_def_path_depth.is_none() || original_did == did) | |
| && let Some(&(ref fqp, shortty)) = cache.external_paths.get(&did) | |
| { | |
| let module_fqp = to_module_fqp(shortty, fqp); | |
| let (parts, is_remote) = | |
| url_parts(cache, did, module_fqp, relative_to, jump_to_def_path_depth)?; | |
| (fqp.as_slice(), shortty, parts, is_remote) | |
| } else if matches!(def_kind, DefKind::Macro(_)) { | |
| return generate_macro_def_id_path(did, cx, jump_to_def_path_depth); | |
| } else if did.is_local() { | |
| return Err(HrefError::Private); | |
| } else { | |
| return generate_item_def_id_path(did, original_did, cx, jump_to_def_path_depth); | |
| } | |
| } | |
| }; | |
| Ok(HrefInfo { | |
| url: make_href(jump_to_def_path_depth, shortty, url_parts, fqp, is_remote), | |
| kind: shortty, | |
| rust_path: fqp.to_vec(), | |
| }) | |
| } | |
| pub(crate) fn href(did: DefId, cx: &Context<'_>) -> Result<HrefInfo, HrefError> { | |
| href_with_jump_to_def_path_depth(did, cx, None, None) | |
| } | |
| pub(crate) fn href_with_path_check( | |
| did: DefId, | |
| cx: &Context<'_>, | |
| text: &str, | |
| ) -> Result<HrefInfo, HrefError> { | |
| href_with_jump_to_def_path_depth(did, cx, None, Some(text)) | |
| } | |
| /// Both paths should only be modules. | |
| /// This is because modules get their own directories; that is, `std::vec` and `std::vec::Vec` will | |
| /// both need `../iter/trait.Iterator.html` to get at the iterator trait. | |
| pub(crate) fn href_relative_parts(fqp: &[Symbol], relative_to_fqp: &[Symbol]) -> UrlPartsBuilder { | |
| for (i, (f, r)) in fqp.iter().zip(relative_to_fqp.iter()).enumerate() { | |
| // e.g. linking to std::iter from std::vec (`dissimilar_part_count` will be 1) | |
| if f != r { | |
| let dissimilar_part_count = relative_to_fqp.len() - i; | |
| let fqp_module = &fqp[i..]; | |
| return iter::repeat_n("..", dissimilar_part_count) | |
| .chain(fqp_module.iter().map(|s| s.as_str())) | |
| .collect(); | |
| } | |
| } | |
| match relative_to_fqp.len().cmp(&fqp.len()) { | |
| Ordering::Less => { | |
| // e.g. linking to std::sync::atomic from std::sync | |
| fqp[relative_to_fqp.len()..fqp.len()].iter().copied().collect() | |
| } | |
| Ordering::Greater => { | |
| // e.g. linking to std::sync from std::sync::atomic | |
| let dissimilar_part_count = relative_to_fqp.len() - fqp.len(); | |
| iter::repeat_n("..", dissimilar_part_count).collect() | |
| } | |
| Ordering::Equal => { | |
| // linking to the same module | |
| UrlPartsBuilder::new() | |
| } | |
| } | |
| } | |
| pub(crate) fn link_tooltip( | |
| did: DefId, | |
| fragment: &Option<UrlFragment>, | |
| cx: &Context<'_>, | |
| preferred_name: Option<&str>, | |
| ) -> impl fmt::Display { | |
| fmt::from_fn(move |f| { | |
| let cache = cx.cache(); | |
| let Some((fqp, shortty)) = cache | |
| .paths | |
| .get(&did) | |
| .map(|info| (info.get_preferred_path(preferred_name), info.ty)) | |
| .or_else(|| { | |
| cache.external_paths.get(&did).map(|(fqp, shortty)| (fqp.as_slice(), *shortty)) | |
| }) | |
| else { | |
| return Ok(()); | |
| }; | |
| let fqp = if shortty == ItemType::Primitive { | |
| // primitives are documented in a crate, but not actually part of it | |
| slice::from_ref(fqp.last().unwrap()) | |
| } else { | |
| fqp | |
| }; | |
| if let &Some(UrlFragment::Item(id)) = fragment { | |
| let tcx = cx.tcx(); | |
| write!(f, "{} ", tcx.def_descr(id))?; | |
| for component in fqp { | |
| write!(f, "{component}::")?; | |
| } | |
| if shortty == ItemType::Enum && tcx.def_kind(id) == DefKind::Field { | |
| write!(f, "{}::", tcx.item_name(tcx.parent(id)))?; | |
| } | |
| write!(f, "{}", tcx.item_name(id))?; | |
| } else if !fqp.is_empty() { | |
| write!(f, "{shortty} ")?; | |
| write!(f, "{}", join_path_syms(fqp))?; | |
| } | |
| Ok(()) | |
| }) | |
| } | |
| /// Used to render a [`clean::Path`]. | |
| fn resolved_path( | |
| w: &mut fmt::Formatter<'_>, | |
| did: DefId, | |
| path: &clean::Path, | |
| print_all: bool, | |
| use_absolute: bool, | |
| cx: &Context<'_>, | |
| ) -> fmt::Result { | |
| let last = path.segments.last().unwrap(); | |
| if print_all { | |
| for seg in &path.segments[..path.segments.len() - 1] { | |
| write!(w, "{}::", if seg.name == kw::PathRoot { "" } else { seg.name.as_str() })?; | |
| } | |
| } | |
| if w.alternate() { | |
| write!(w, "{}{:#}", last.name, print_generic_args(&last.args, cx))?; | |
| } else { | |
| let path = fmt::from_fn(|f| { | |
| if use_absolute { | |
| if let Ok(HrefInfo { rust_path, .. }) = href(did, cx) { | |
| write!( | |
| f, | |
| "{path}::{anchor}", | |
| path = join_path_syms(&rust_path[..rust_path.len() - 1]), | |
| anchor = print_anchor(did, *rust_path.last().unwrap(), cx) | |
| ) | |
| } else { | |
| write!(f, "{}", last.name) | |
| } | |
| } else { | |
| write!(f, "{}", print_anchor(did, last.name, cx)) | |
| } | |
| }); | |
| write!(w, "{path}{args}", args = print_generic_args(&last.args, cx))?; | |
| } | |
| Ok(()) | |
| } | |
| fn primitive_link( | |
| f: &mut fmt::Formatter<'_>, | |
| prim: clean::PrimitiveType, | |
| name: fmt::Arguments<'_>, | |
| cx: &Context<'_>, | |
| ) -> fmt::Result { | |
| primitive_link_fragment(f, prim, name, "", cx) | |
| } | |
| fn primitive_link_fragment( | |
| f: &mut fmt::Formatter<'_>, | |
| prim: clean::PrimitiveType, | |
| name: fmt::Arguments<'_>, | |
| fragment: &str, | |
| cx: &Context<'_>, | |
| ) -> fmt::Result { | |
| let m = &cx.cache(); | |
| let mut needs_termination = false; | |
| if !f.alternate() { | |
| match m.primitive_locations.get(&prim) { | |
| Some(&def_id) if def_id.is_local() => { | |
| let len = cx.current.len(); | |
| let path = fmt::from_fn(|f| { | |
| if len == 0 { | |
| let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx()); | |
| write!(f, "{cname_sym}/")?; | |
| } else { | |
| for _ in 0..(len - 1) { | |
| f.write_str("../")?; | |
| } | |
| } | |
| Ok(()) | |
| }); | |
| write!( | |
| f, | |
| "<a class=\"primitive\" href=\"{path}primitive.{}.html{fragment}\">", | |
| prim.as_sym() | |
| )?; | |
| needs_termination = true; | |
| } | |
| Some(&def_id) => { | |
| let loc = match m.extern_locations[&def_id.krate] { | |
| ExternalLocation::Remote { ref url, is_absolute } => { | |
| let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx()); | |
| let mut builder = remote_url_prefix(url, is_absolute, cx.current.len()); | |
| builder.push(cname_sym.as_str()); | |
| Some(builder) | |
| } | |
| ExternalLocation::Local => { | |
| let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx()); | |
| Some(if cx.current.first() == Some(&cname_sym) { | |
| iter::repeat_n("..", cx.current.len() - 1).collect() | |
| } else { | |
| iter::repeat_n("..", cx.current.len()) | |
| .chain(iter::once(cname_sym.as_str())) | |
| .collect() | |
| }) | |
| } | |
| ExternalLocation::Unknown => None, | |
| }; | |
| if let Some(mut loc) = loc { | |
| loc.push_fmt(format_args!("primitive.{}.html", prim.as_sym())); | |
| write!(f, "<a class=\"primitive\" href=\"{}{fragment}\">", loc.finish())?; | |
| needs_termination = true; | |
| } | |
| } | |
| None => {} | |
| } | |
| } | |
| Display::fmt(&name, f)?; | |
| if needs_termination { | |
| write!(f, "</a>")?; | |
| } | |
| Ok(()) | |
| } | |
| fn print_tybounds( | |
| bounds: &[clean::PolyTrait], | |
| lt: &Option<clean::Lifetime>, | |
| cx: &Context<'_>, | |
| ) -> impl Display { | |
| fmt::from_fn(move |f| { | |
| bounds.iter().map(|bound| print_poly_trait(bound, cx)).joined(" + ", f)?; | |
| if let Some(lt) = lt { | |
| // We don't need to check `alternate` since we can be certain that | |
| // the lifetime doesn't contain any characters which need escaping. | |
| write!(f, " + {}", print_lifetime(lt))?; | |
| } | |
| Ok(()) | |
| }) | |
| } | |
| fn print_higher_ranked_params_with_space( | |
| params: &[clean::GenericParamDef], | |
| cx: &Context<'_>, | |
| keyword: &'static str, | |
| ) -> impl Display { | |
| fmt::from_fn(move |f| { | |
| if !params.is_empty() { | |
| f.write_str(keyword)?; | |
| Wrapped::with_angle_brackets() | |
| .wrap_fn(|f| { | |
| params.iter().map(|lt| print_generic_param_def(lt, cx)).joined(", ", f) | |
| }) | |
| .fmt(f)?; | |
| f.write_char(' ')?; | |
| } | |
| Ok(()) | |
| }) | |
| } | |
| pub(crate) fn fragment(did: DefId, tcx: TyCtxt<'_>) -> impl Display { | |
| fmt::from_fn(move |f| { | |
| let def_kind = tcx.def_kind(did); | |
| match def_kind { | |
| DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant => { | |
| let item_type = ItemType::from_def_id(did, tcx); | |
| write!(f, "#{}.{}", item_type.as_str(), tcx.item_name(did)) | |
| } | |
| DefKind::Field => { | |
| let parent_def_id = tcx.parent(did); | |
| f.write_char('#')?; | |
| if tcx.def_kind(parent_def_id) == DefKind::Variant { | |
| write!(f, "variant.{}.field", tcx.item_name(parent_def_id).as_str())?; | |
| } else { | |
| f.write_str("structfield")?; | |
| }; | |
| write!(f, ".{}", tcx.item_name(did)) | |
| } | |
| _ => Ok(()), | |
| } | |
| }) | |
| } | |
| pub(crate) fn print_anchor(did: DefId, text: Symbol, cx: &Context<'_>) -> impl Display { | |
| fmt::from_fn(move |f| { | |
| if let Ok(HrefInfo { url, kind, rust_path }) = href(did, cx) { | |
| write!( | |
| f, | |
| r#"<a class="{kind}" href="{url}{anchor}" title="{kind} {path}">{text}</a>"#, | |
| anchor = fragment(did, cx.tcx()), | |
| path = join_path_syms(rust_path), | |
| text = EscapeBodyText(text.as_str()), | |
| ) | |
| } else { | |
| f.write_str(text.as_str()) | |
| } | |
| }) | |
| } | |
| fn fmt_type( | |
| t: &clean::Type, | |
| f: &mut fmt::Formatter<'_>, | |
| use_absolute: bool, | |
| cx: &Context<'_>, | |
| ) -> fmt::Result { | |
| trace!("fmt_type(t = {t:?})"); | |
| match t { | |
| clean::Generic(name) => f.write_str(name.as_str()), | |
| clean::SelfTy => f.write_str("Self"), | |
| clean::Type::Path { path } => { | |
| // Paths like `T::Output` and `Self::Output` should be rendered with all segments. | |
| let did = path.def_id(); | |
| resolved_path(f, did, path, path.is_assoc_ty(), use_absolute, cx) | |
| } | |
| clean::DynTrait(bounds, lt) => { | |
| f.write_str("dyn ")?; | |
| print_tybounds(bounds, lt, cx).fmt(f) | |
| } | |
| clean::Infer => write!(f, "_"), | |
| clean::Primitive(clean::PrimitiveType::Never) => { | |
| primitive_link(f, PrimitiveType::Never, format_args!("!"), cx) | |
| } | |
| &clean::Primitive(prim) => primitive_link(f, prim, format_args!("{}", prim.as_sym()), cx), | |
| clean::BareFunction(decl) => { | |
| print_higher_ranked_params_with_space(&decl.generic_params, cx, "for").fmt(f)?; | |
| decl.safety.print_with_space().fmt(f)?; | |
| print_abi_with_space(decl.abi).fmt(f)?; | |
| if f.alternate() { | |
| f.write_str("fn")?; | |
| } else { | |
| primitive_link(f, PrimitiveType::Fn, format_args!("fn"), cx)?; | |
| } | |
| print_fn_decl(&decl.decl, cx).fmt(f) | |
| } | |
| clean::UnsafeBinder(binder) => { | |
| print_higher_ranked_params_with_space(&binder.generic_params, cx, "unsafe").fmt(f)?; | |
| print_type(&binder.ty, cx).fmt(f) | |
| } | |
| clean::Tuple(typs) => match &typs[..] { | |
| &[] => primitive_link(f, PrimitiveType::Unit, format_args!("()"), cx), | |
| [one] => { | |
| if let clean::Generic(name) = one { | |
| primitive_link(f, PrimitiveType::Tuple, format_args!("({name},)"), cx) | |
| } else { | |
| write!(f, "(")?; | |
| print_type(one, cx).fmt(f)?; | |
| write!(f, ",)") | |
| } | |
| } | |
| many => { | |
| let generic_names: Vec<Symbol> = many | |
| .iter() | |
| .filter_map(|t| match t { | |
| clean::Generic(name) => Some(*name), | |
| _ => None, | |
| }) | |
| .collect(); | |
| let is_generic = generic_names.len() == many.len(); | |
| if is_generic { | |
| primitive_link( | |
| f, | |
| PrimitiveType::Tuple, | |
| format_args!( | |
| "{}", | |
| Wrapped::with_parens() | |
| .wrap_fn(|f| generic_names.iter().joined(", ", f)) | |
| ), | |
| cx, | |
| ) | |
| } else { | |
| Wrapped::with_parens() | |
| .wrap_fn(|f| many.iter().map(|item| print_type(item, cx)).joined(", ", f)) | |
| .fmt(f) | |
| } | |
| } | |
| }, | |
| clean::Slice(clean::Generic(name)) => { | |
| primitive_link(f, PrimitiveType::Slice, format_args!("[{name}]"), cx) | |
| } | |
| clean::Slice(t) => Wrapped::with_square_brackets().wrap(print_type(t, cx)).fmt(f), | |
| clean::Type::Pat(t, pat) => { | |
| fmt::Display::fmt(&print_type(t, cx), f)?; | |
| write!(f, " is {pat}") | |
| } | |
| clean::Type::FieldOf(t, field) => { | |
| write!(f, "field_of!(")?; | |
| fmt::Display::fmt(&print_type(t, cx), f)?; | |
| write!(f, ", {field})") | |
| } | |
| clean::Array(clean::Generic(name), n) if !f.alternate() => primitive_link( | |
| f, | |
| PrimitiveType::Array, | |
| format_args!("[{name}; {n}]", n = Escape(n)), | |
| cx, | |
| ), | |
| clean::Array(t, n) => Wrapped::with_square_brackets() | |
| .wrap(fmt::from_fn(|f| { | |
| print_type(t, cx).fmt(f)?; | |
| f.write_str("; ")?; | |
| if f.alternate() { | |
| f.write_str(n) | |
| } else { | |
| primitive_link(f, PrimitiveType::Array, format_args!("{n}", n = Escape(n)), cx) | |
| } | |
| })) | |
| .fmt(f), | |
| clean::RawPointer(m, t) => { | |
| let m = m.ptr_str(); | |
| if matches!(**t, clean::Generic(_)) || t.is_assoc_ty() { | |
| primitive_link( | |
| f, | |
| clean::PrimitiveType::RawPointer, | |
| format_args!("*{m} {ty}", ty = WithOpts::from(f).display(print_type(t, cx))), | |
| cx, | |
| ) | |
| } else { | |
| primitive_link(f, clean::PrimitiveType::RawPointer, format_args!("*{m} "), cx)?; | |
| print_type(t, cx).fmt(f) | |
| } | |
| } | |
| clean::BorrowedRef { lifetime: l, mutability, type_: ty } => { | |
| let lt = fmt::from_fn(|f| match l { | |
| Some(l) => write!(f, "{} ", print_lifetime(l)), | |
| _ => Ok(()), | |
| }); | |
| let m = mutability.print_with_space(); | |
| let amp = if f.alternate() { "&" } else { "&" }; | |
| if let clean::Generic(name) = **ty { | |
| return primitive_link( | |
| f, | |
| PrimitiveType::Reference, | |
| format_args!("{amp}{lt}{m}{name}"), | |
| cx, | |
| ); | |
| } | |
| write!(f, "{amp}{lt}{m}")?; | |
| let needs_parens = match **ty { | |
| clean::DynTrait(ref bounds, ref trait_lt) | |
| if bounds.len() > 1 || trait_lt.is_some() => | |
| { | |
| true | |
| } | |
| clean::ImplTrait(ref bounds) if bounds.len() > 1 => true, | |
| _ => false, | |
| }; | |
| Wrapped::with_parens() | |
| .when(needs_parens) | |
| .wrap_fn(|f| fmt_type(ty, f, use_absolute, cx)) | |
| .fmt(f) | |
| } | |
| clean::ImplTrait(bounds) => { | |
| f.write_str("impl ")?; | |
| print_generic_bounds(bounds, cx).fmt(f) | |
| } | |
| clean::QPath(qpath) => print_qpath_data(qpath, cx).fmt(f), | |
| } | |
| } | |
| pub(crate) fn print_type(type_: &clean::Type, cx: &Context<'_>) -> impl Display { | |
| fmt::from_fn(move |f| fmt_type(type_, f, false, cx)) | |
| } | |
| pub(crate) fn print_path(path: &clean::Path, cx: &Context<'_>) -> impl Display { | |
| fmt::from_fn(move |f| resolved_path(f, path.def_id(), path, false, false, cx)) | |
| } | |
| fn print_qpath_data(qpath_data: &clean::QPathData, cx: &Context<'_>) -> impl Display { | |
| let clean::QPathData { ref assoc, ref self_type, should_fully_qualify, ref trait_ } = | |
| *qpath_data; | |
| fmt::from_fn(move |f| { | |
| // FIXME(inherent_associated_types): Once we support non-ADT self-types (#106719), | |
| // we need to surround them with angle brackets in some cases (e.g. `<dyn …>::P`). | |
| if let Some(trait_) = trait_ | |
| && should_fully_qualify | |
| { | |
| let opts = WithOpts::from(f); | |
| Wrapped::with_angle_brackets() | |
| .wrap(format_args!( | |
| "{} as {}", | |
| opts.display(print_type(self_type, cx)), | |
| opts.display(print_path(trait_, cx)) | |
| )) | |
| .fmt(f)? | |
| } else { | |
| print_type(self_type, cx).fmt(f)?; | |
| } | |
| f.write_str("::")?; | |
| // It's pretty unsightly to look at `<A as B>::C` in output, and | |
| // we've got hyperlinking on our side, so try to avoid longer | |
| // notation as much as possible by making `C` a hyperlink to trait | |
| // `B` to disambiguate. | |
| // | |
| // FIXME: this is still a lossy conversion and there should probably | |
| // be a better way of representing this in general? Most of | |
| // the ugliness comes from inlining across crates where | |
| // everything comes in as a fully resolved QPath (hard to | |
| // look at). | |
| if !f.alternate() { | |
| // FIXME(inherent_associated_types): We always link to the very first associated | |
| // type (in respect to source order) that bears the given name (`assoc.name`) and that is | |
| // affiliated with the computed `DefId`. This is obviously incorrect when we have | |
| // multiple impl blocks. Ideally, we would thread the `DefId` of the assoc ty itself | |
| // through here and map it to the corresponding HTML ID that was generated by | |
| // `render::Context::derive_id` when the impl blocks were rendered. | |
| // There is no such mapping unfortunately. | |
| // As a hack, we could badly imitate `derive_id` here by keeping *count* when looking | |
| // for the assoc ty `DefId` in `tcx.associated_items(self_ty_did).in_definition_order()` | |
| // considering privacy, `doc(hidden)`, etc. | |
| // I don't feel like that right now :cold_sweat:. | |
| let parent_href = match trait_ { | |
| Some(trait_) => href(trait_.def_id(), cx).ok(), | |
| None => self_type.def_id(cx.cache()).and_then(|did| href(did, cx).ok()), | |
| }; | |
| let tcx = cx.tcx(); | |
| let assoc_type_is_hidden = !cx.cache().document_hidden | |
| && trait_.as_ref().is_some_and(|trait_| { | |
| let trait_did = trait_.def_id(); | |
| tcx.associated_items(trait_did) | |
| .find_by_ident_and_kind( | |
| tcx, | |
| Ident::with_dummy_span(assoc.name), | |
| ty::AssocTag::Type, | |
| trait_did, | |
| ) | |
| .is_some_and(|assoc_item| tcx.is_doc_hidden(assoc_item.def_id)) | |
| }); | |
| if let Some(HrefInfo { url, rust_path, .. }) = parent_href | |
| && !assoc_type_is_hidden | |
| { | |
| write!( | |
| f, | |
| "<a class=\"associatedtype\" href=\"{url}#{shortty}.{name}\" \ | |
| title=\"type {path}::{name}\">{name}</a>", | |
| shortty = ItemType::AssocType, | |
| name = assoc.name, | |
| path = join_path_syms(rust_path), | |
| ) | |
| } else { | |
| write!(f, "{}", assoc.name) | |
| } | |
| } else { | |
| write!(f, "{}", assoc.name) | |
| }?; | |
| print_generic_args(&assoc.args, cx).fmt(f) | |
| }) | |
| } | |
| pub(crate) fn print_impl( | |
| impl_: &clean::Impl, | |
| use_absolute: bool, | |
| cx: &Context<'_>, | |
| ) -> impl Display { | |
| fmt::from_fn(move |f| { | |
| f.write_str("impl")?; | |
| print_generics(&impl_.generics, cx).fmt(f)?; | |
| f.write_str(" ")?; | |
| if let Some(ref ty) = impl_.trait_ { | |
| if impl_.is_negative_trait_impl() { | |
| f.write_char('!')?; | |
| } | |
| if impl_.kind.is_fake_variadic() | |
| && let Some(generics) = ty.generics() | |
| && let Ok(inner_type) = generics.exactly_one() | |
| { | |
| let last = ty.last(); | |
| if f.alternate() { | |
| write!(f, "{last}")?; | |
| } else { | |
| write!(f, "{}", print_anchor(ty.def_id(), last, cx))?; | |
| }; | |
| Wrapped::with_angle_brackets() | |
| .wrap_fn(|f| impl_.print_type(inner_type, f, use_absolute, cx)) | |
| .fmt(f)?; | |
| } else { | |
| print_path(ty, cx).fmt(f)?; | |
| } | |
| f.write_str(" for ")?; | |
| } | |
| if let Some(ty) = impl_.kind.as_blanket_ty() { | |
| fmt_type(ty, f, use_absolute, cx)?; | |
| } else { | |
| impl_.print_type(&impl_.for_, f, use_absolute, cx)?; | |
| } | |
| print_where_clause(&impl_.generics, cx, 0, Ending::Newline).maybe_display().fmt(f) | |
| }) | |
| } | |
| impl clean::Impl { | |
| fn print_type( | |
| &self, | |
| type_: &clean::Type, | |
| f: &mut fmt::Formatter<'_>, | |
| use_absolute: bool, | |
| cx: &Context<'_>, | |
| ) -> Result<(), fmt::Error> { | |
| if let clean::Type::Tuple(types) = type_ | |
| && let [clean::Type::Generic(name)] = &types[..] | |
| && (self.kind.is_fake_variadic() || self.kind.is_auto()) | |
| { | |
| // Hardcoded anchor library/core/src/primitive_docs.rs | |
| // Link should match `# Trait implementations` | |
| primitive_link_fragment( | |
| f, | |
| PrimitiveType::Tuple, | |
| format_args!("({name}₁, {name}₂, …, {name}ₙ)"), | |
| "#trait-implementations-1", | |
| cx, | |
| )?; | |
| } else if let clean::Type::Array(ty, len) = type_ | |
| && let clean::Type::Generic(name) = &**ty | |
| && &len[..] == "1" | |
| && (self.kind.is_fake_variadic() || self.kind.is_auto()) | |
| { | |
| primitive_link(f, PrimitiveType::Array, format_args!("[{name}; N]"), cx)?; | |
| } else if let clean::BareFunction(bare_fn) = &type_ | |
| && let [clean::Parameter { type_: clean::Type::Generic(name), .. }] = | |
| &bare_fn.decl.inputs[..] | |
| && (self.kind.is_fake_variadic() || self.kind.is_auto()) | |
| { | |
| // Hardcoded anchor library/core/src/primitive_docs.rs | |
| // Link should match `# Trait implementations` | |
| print_higher_ranked_params_with_space(&bare_fn.generic_params, cx, "for").fmt(f)?; | |
| bare_fn.safety.print_with_space().fmt(f)?; | |
| print_abi_with_space(bare_fn.abi).fmt(f)?; | |
| let ellipsis = if bare_fn.decl.c_variadic { ", ..." } else { "" }; | |
| primitive_link_fragment( | |
| f, | |
| PrimitiveType::Tuple, | |
| format_args!("fn({name}₁, {name}₂, …, {name}ₙ{ellipsis})"), | |
| "#trait-implementations-1", | |
| cx, | |
| )?; | |
| // Write output. | |
| if !bare_fn.decl.output.is_unit() { | |
| write!(f, " -> ")?; | |
| fmt_type(&bare_fn.decl.output, f, use_absolute, cx)?; | |
| } | |
| } else if let clean::Type::Path { path } = type_ | |
| && let Some(generics) = path.generics() | |
| && let Ok(ty) = generics.exactly_one() | |
| && self.kind.is_fake_variadic() | |
| { | |
| print_anchor(path.def_id(), path.last(), cx).fmt(f)?; | |
| Wrapped::with_angle_brackets() | |
| .wrap_fn(|f| self.print_type(ty, f, use_absolute, cx)) | |
| .fmt(f)?; | |
| } else { | |
| fmt_type(type_, f, use_absolute, cx)?; | |
| } | |
| Ok(()) | |
| } | |
| } | |
| pub(crate) fn print_params(params: &[clean::Parameter], cx: &Context<'_>) -> impl Display { | |
| fmt::from_fn(move |f| { | |
| params | |
| .iter() | |
| .map(|param| { | |
| fmt::from_fn(|f| { | |
| if param.is_splat { | |
| write!(f, "…: ")?; | |
| } else if let Some(name) = param.name { | |
| write!(f, "{name}: ")?; | |
| } | |
| print_type(¶m.type_, cx).fmt(f) | |
| }) | |
| }) | |
| .joined(", ", f) | |
| }) | |
| } | |
| // Implements Write but only counts the bytes "written". | |
| struct WriteCounter(usize); | |
| impl std::fmt::Write for WriteCounter { | |
| fn write_str(&mut self, s: &str) -> fmt::Result { | |
| self.0 += s.len(); | |
| Ok(()) | |
| } | |
| } | |
| // Implements Display by emitting the given number of spaces. | |
| #[derive(Clone, Copy)] | |
| struct Indent(usize); | |
| impl Display for Indent { | |
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
| for _ in 0..self.0 { | |
| f.write_char(' ')?; | |
| } | |
| Ok(()) | |
| } | |
| } | |
| fn print_parameter(parameter: &clean::Parameter, cx: &Context<'_>) -> impl fmt::Display { | |
| fmt::from_fn(move |f| { | |
| if let Some(self_ty) = parameter.to_receiver() { | |
| match self_ty { | |
| clean::SelfTy => f.write_str("self"), | |
| clean::BorrowedRef { lifetime, mutability, type_: clean::SelfTy } => { | |
| f.write_str(if f.alternate() { "&" } else { "&" })?; | |
| if let Some(lt) = lifetime { | |
| write!(f, "{lt} ", lt = print_lifetime(lt))?; | |
| } | |
| write!(f, "{mutability}self", mutability = mutability.print_with_space()) | |
| } | |
| _ => { | |
| f.write_str("self: ")?; | |
| print_type(self_ty, cx).fmt(f) | |
| } | |
| } | |
| } else { | |
| if parameter.is_const { | |
| write!(f, "const ")?; | |
| } | |
| if parameter.is_splat { | |
| write!(f, "…: ")?; | |
| } else if let Some(name) = parameter.name { | |
| write!(f, "{name}: ")?; | |
| } | |
| print_type(¶meter.type_, cx).fmt(f) | |
| } | |
| }) | |
| } | |
| fn print_fn_decl(fn_decl: &clean::FnDecl, cx: &Context<'_>) -> impl Display { | |
| fmt::from_fn(move |f| { | |
| let ellipsis = if fn_decl.c_variadic { ", ..." } else { "" }; | |
| Wrapped::with_parens() | |
| .wrap_fn(|f| { | |
| print_params(&fn_decl.inputs, cx).fmt(f)?; | |
| f.write_str(ellipsis) | |
| }) | |
| .fmt(f)?; | |
| fn_decl.print_output(cx).fmt(f) | |
| }) | |
| } | |
| /// * `header_len`: The length of the function header and name. In other words, the number of | |
| /// characters in the function declaration up to but not including the parentheses. | |
| /// This is expected to go into a `<pre>`/`code-header` block, so indentation and newlines | |
| /// are preserved. | |
| /// * `indent`: The number of spaces to indent each successive line with, if line-wrapping is | |
| /// necessary. | |
| pub(crate) fn full_print_fn_decl( | |
| fn_decl: &clean::FnDecl, | |
| header_len: usize, | |
| indent: usize, | |
| cx: &Context<'_>, | |
| ) -> impl Display { | |
| fmt::from_fn(move |f| { | |
| // First, generate the text form of the declaration, with no line wrapping, and count the bytes. | |
| let mut counter = WriteCounter(0); | |
| write!(&mut counter, "{:#}", fmt::from_fn(|f| { fn_decl.inner_full_print(None, f, cx) }))?; | |
| // If the text form was over 80 characters wide, we will line-wrap our output. | |
| let line_wrapping_indent = if header_len + counter.0 > 80 { Some(indent) } else { None }; | |
| // Generate the final output. This happens to accept `{:#}` formatting to get textual | |
| // output but in practice it is only formatted with `{}` to get HTML output. | |
| fn_decl.inner_full_print(line_wrapping_indent, f, cx) | |
| }) | |
| } | |
| impl clean::FnDecl { | |
| fn inner_full_print( | |
| &self, | |
| // For None, the declaration will not be line-wrapped. For Some(n), | |
| // the declaration will be line-wrapped, with an indent of n spaces. | |
| line_wrapping_indent: Option<usize>, | |
| f: &mut fmt::Formatter<'_>, | |
| cx: &Context<'_>, | |
| ) -> fmt::Result { | |
| Wrapped::with_parens() | |
| .wrap_fn(|f| { | |
| if !self.inputs.is_empty() { | |
| let line_wrapping_indent = line_wrapping_indent.map(|n| Indent(n + 4)); | |
| if let Some(indent) = line_wrapping_indent { | |
| write!(f, "\n{indent}")?; | |
| } | |
| let sep = fmt::from_fn(|f| { | |
| if let Some(indent) = line_wrapping_indent { | |
| write!(f, ",\n{indent}") | |
| } else { | |
| f.write_str(", ") | |
| } | |
| }); | |
| self.inputs.iter().map(|param| print_parameter(param, cx)).joined(sep, f)?; | |
| if line_wrapping_indent.is_some() { | |
| writeln!(f, ",")? | |
| } | |
| if self.c_variadic { | |
| match line_wrapping_indent { | |
| None => write!(f, ", ...")?, | |
| Some(indent) => writeln!(f, "{indent}...")?, | |
| }; | |
| } | |
| } | |
| if let Some(n) = line_wrapping_indent { | |
| write!(f, "{}", Indent(n))? | |
| } | |
| Ok(()) | |
| }) | |
| .fmt(f)?; | |
| self.print_output(cx).fmt(f) | |
| } | |
| fn print_output(&self, cx: &Context<'_>) -> impl Display { | |
| fmt::from_fn(move |f| { | |
| if self.output.is_unit() { | |
| return Ok(()); | |
| } | |
| f.write_str(if f.alternate() { " -> " } else { " -> " })?; | |
| print_type(&self.output, cx).fmt(f) | |
| }) | |
| } | |
| } | |
| pub(crate) fn visibility_print_with_space(item: &clean::Item, cx: &Context<'_>) -> impl Display { | |
| fmt::from_fn(move |f| { | |
| let Some(vis) = item.visibility(cx.tcx()) else { | |
| return Ok(()); | |
| }; | |
| match vis { | |
| ty::Visibility::Public => f.write_str("pub ")?, | |
| ty::Visibility::Restricted(vis_mod_id) => { | |
| // FIXME(camelid): This may not work correctly if `item_did` is a module. | |
| // However, rustdoc currently never displays a module's | |
| // visibility, so it shouldn't matter. | |
| let parent_module = | |
| find_nearest_parent_module(cx.tcx(), item.item_id.expect_def_id()); | |
| if vis_mod_id.is_crate_root() { | |
| f.write_str("pub(crate) ")?; | |
| } else if parent_module == Some(vis_mod_id) { | |
| // `pub(in foo)` where `foo` is the parent module | |
| // is the same as no visibility modifier; do nothing | |
| } else if parent_module | |
| .and_then(|parent| find_nearest_parent_module(cx.tcx(), parent.to_def_id())) | |
| == Some(vis_mod_id) | |
| { | |
| f.write_str("pub(super) ")?; | |
| } else { | |
| let path = cx.tcx().def_path(vis_mod_id.to_def_id()); | |
| debug!("path={path:?}"); | |
| // modified from `resolved_path()` to work with `DefPathData` | |
| let last_name = path.data.last().unwrap().data.get_opt_name().unwrap(); | |
| let anchor = print_anchor(vis_mod_id.to_def_id(), last_name, cx); | |
| f.write_str("pub(in ")?; | |
| for seg in &path.data[..path.data.len() - 1] { | |
| write!(f, "{}::", seg.data.get_opt_name().unwrap())?; | |
| } | |
| write!(f, "{anchor}) ")?; | |
| } | |
| } | |
| } | |
| Ok(()) | |
| }) | |
| } | |
| pub(crate) trait PrintWithSpace { | |
| fn print_with_space(&self) -> &str; | |
| } | |
| impl PrintWithSpace for hir::Safety { | |
| fn print_with_space(&self) -> &str { | |
| self.prefix_str() | |
| } | |
| } | |
| impl PrintWithSpace for hir::HeaderSafety { | |
| fn print_with_space(&self) -> &str { | |
| match self { | |
| hir::HeaderSafety::SafeTargetFeatures => "", | |
| hir::HeaderSafety::Normal(safety) => safety.print_with_space(), | |
| } | |
| } | |
| } | |
| impl PrintWithSpace for hir::IsAsync { | |
| fn print_with_space(&self) -> &str { | |
| match self { | |
| hir::IsAsync::Async(_) => "async ", | |
| hir::IsAsync::NotAsync => "", | |
| } | |
| } | |
| } | |
| impl PrintWithSpace for hir::Mutability { | |
| fn print_with_space(&self) -> &str { | |
| match self { | |
| hir::Mutability::Not => "", | |
| hir::Mutability::Mut => "mut ", | |
| } | |
| } | |
| } | |
| pub(crate) fn print_constness_with_space( | |
| c: &hir::Constness, | |
| overall_stab: Option<StableSince>, | |
| const_stab: Option<ConstStability>, | |
| ) -> &'static str { | |
| match *c { | |
| hir::Constness::Const { always } => match (overall_stab, const_stab) { | |
| // const stable... | |
| (_, Some(ConstStability { level: StabilityLevel::Stable { .. }, .. })) | |
| // ...or when feature(staged_api) is not set... | |
| | (_, None) | |
| // ...or when const unstable, but overall unstable too | |
| | (None, Some(ConstStability { level: StabilityLevel::Unstable { .. }, .. })) => { | |
| if always { | |
| // FIXME(comptime) show something when stable, currently relying on the attribute | |
| // being rendered as part of the regular attribute list. | |
| "" | |
| } else { | |
| "const " | |
| } | |
| } | |
| // const unstable (and overall stable) | |
| (Some(_), Some(ConstStability { level: StabilityLevel::Unstable { .. }, .. })) => "", | |
| }, | |
| // not const | |
| hir::Constness::NotConst => "", | |
| } | |
| } | |
| pub(crate) fn print_import(import: &clean::Import, cx: &Context<'_>) -> impl Display { | |
| fmt::from_fn(move |f| match import.kind { | |
| clean::ImportKind::Simple(name) => { | |
| if name == import.source.path.last() { | |
| write!(f, "use {};", print_import_source(&import.source, cx)) | |
| } else { | |
| write!( | |
| f, | |
| "use {source} as {name};", | |
| source = print_import_source(&import.source, cx) | |
| ) | |
| } | |
| } | |
| clean::ImportKind::Glob => { | |
| if import.source.path.segments.is_empty() { | |
| write!(f, "use *;") | |
| } else { | |
| write!(f, "use {}::*;", print_import_source(&import.source, cx)) | |
| } | |
| } | |
| }) | |
| } | |
| fn print_import_source(import_source: &clean::ImportSource, cx: &Context<'_>) -> impl Display { | |
| fmt::from_fn(move |f| match import_source.did { | |
| Some(did) => resolved_path(f, did, &import_source.path, true, false, cx), | |
| _ => { | |
| for seg in &import_source.path.segments[..import_source.path.segments.len() - 1] { | |
| write!(f, "{}::", seg.name)?; | |
| } | |
| let name = import_source.path.last(); | |
| if let hir::def::Res::PrimTy(p) = import_source.path.res { | |
| primitive_link(f, PrimitiveType::from(p), format_args!("{name}"), cx)?; | |
| } else { | |
| f.write_str(name.as_str())?; | |
| } | |
| Ok(()) | |
| } | |
| }) | |
| } | |
| fn print_assoc_item_constraint( | |
| assoc_item_constraint: &clean::AssocItemConstraint, | |
| cx: &Context<'_>, | |
| ) -> impl Display { | |
| fmt::from_fn(move |f| { | |
| f.write_str(assoc_item_constraint.assoc.name.as_str())?; | |
| print_generic_args(&assoc_item_constraint.assoc.args, cx).fmt(f)?; | |
| match assoc_item_constraint.kind { | |
| clean::AssocItemConstraintKind::Equality { ref term } => { | |
| f.write_str(" = ")?; | |
| print_term(term, cx).fmt(f)?; | |
| } | |
| clean::AssocItemConstraintKind::Bound { ref bounds } => { | |
| if !bounds.is_empty() { | |
| f.write_str(": ")?; | |
| print_generic_bounds(bounds, cx).fmt(f)?; | |
| } | |
| } | |
| } | |
| Ok(()) | |
| }) | |
| } | |
| pub(crate) fn print_abi_with_space(abi: ExternAbi) -> impl Display { | |
| fmt::from_fn(move |f| { | |
| let quot = if f.alternate() { "\"" } else { """ }; | |
| match abi { | |
| ExternAbi::Rust => Ok(()), | |
| abi => write!(f, "extern {0}{1}{0} ", quot, abi.name()), | |
| } | |
| }) | |
| } | |
| fn print_generic_arg(generic_arg: &clean::GenericArg, cx: &Context<'_>) -> impl Display { | |
| fmt::from_fn(move |f| match generic_arg { | |
| clean::GenericArg::Lifetime(lt) => f.write_str(print_lifetime(lt)), | |
| clean::GenericArg::Type(ty) => print_type(ty, cx).fmt(f), | |
| clean::GenericArg::Const(ct) => print_constant_kind(ct, cx.tcx()).fmt(f), | |
| clean::GenericArg::Infer => f.write_char('_'), | |
| }) | |
| } | |
| fn print_term(term: &clean::Term, cx: &Context<'_>) -> impl Display { | |
| fmt::from_fn(move |f| match term { | |
| clean::Term::Type(ty) => print_type(ty, cx).fmt(f), | |
| clean::Term::Constant(ct) => print_constant_kind(ct, cx.tcx()).fmt(f), | |
| }) | |
| } |