1 //! HTML formatting module
3 //! This module contains a large number of `fmt::Display` implementations for
4 //! various types in `rustdoc::clean`. These implementations all currently
5 //! assume that HTML output is desired, although it may be possible to redesign
6 //! them in the future to instead emit any format desired.
11 use std::iter::{self, once};
14 use rustc_attr::{ConstStability, StabilityLevel};
15 use rustc_data_structures::captures::Captures;
16 use rustc_data_structures::fx::FxHashSet;
18 use rustc_hir::def::DefKind;
19 use rustc_hir::def_id::DefId;
20 use rustc_metadata::creader::{CStore, LoadedMacro};
22 use rustc_middle::ty::DefIdTree;
23 use rustc_middle::ty::TyCtxt;
24 use rustc_span::symbol::kw;
25 use rustc_span::{sym, Symbol};
26 use rustc_target::spec::abi::Abi;
28 use itertools::Itertools;
31 self, types::ExternalLocation, utils::find_nearest_parent_module, ExternalCrate, ItemId,
34 use crate::formats::item_type::ItemType;
35 use crate::html::escape::Escape;
36 use crate::html::render::Context;
38 use super::url_parts_builder::estimate_item_path_byte_length;
39 use super::url_parts_builder::UrlPartsBuilder;
41 pub(crate) trait Print {
42 fn print(self, buffer: &mut Buffer);
47 F: FnOnce(&mut Buffer),
49 fn print(self, buffer: &mut Buffer) {
54 impl Print for String {
55 fn print(self, buffer: &mut Buffer) {
56 buffer.write_str(&self);
60 impl Print for &'_ str {
61 fn print(self, buffer: &mut Buffer) {
62 buffer.write_str(self);
66 #[derive(Debug, Clone)]
67 pub(crate) struct Buffer {
72 impl core::fmt::Write for Buffer {
74 fn write_str(&mut self, s: &str) -> fmt::Result {
75 self.buffer.write_str(s)
79 fn write_char(&mut self, c: char) -> fmt::Result {
80 self.buffer.write_char(c)
84 fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result {
85 self.buffer.write_fmt(args)
90 pub(crate) fn empty_from(v: &Buffer) -> Buffer {
91 Buffer { for_html: v.for_html, buffer: String::new() }
94 pub(crate) fn html() -> Buffer {
95 Buffer { for_html: true, buffer: String::new() }
98 pub(crate) fn new() -> Buffer {
99 Buffer { for_html: false, buffer: String::new() }
102 pub(crate) fn is_empty(&self) -> bool {
103 self.buffer.is_empty()
106 pub(crate) fn into_inner(self) -> String {
110 pub(crate) fn insert_str(&mut self, idx: usize, s: &str) {
111 self.buffer.insert_str(idx, s);
114 pub(crate) fn push_str(&mut self, s: &str) {
115 self.buffer.push_str(s);
118 pub(crate) fn push_buffer(&mut self, other: Buffer) {
119 self.buffer.push_str(&other.buffer);
122 // Intended for consumption by write! and writeln! (std::fmt) but without
123 // the fmt::Result return type imposed by fmt::Write (and avoiding the trait
125 pub(crate) fn write_str(&mut self, s: &str) {
126 self.buffer.push_str(s);
129 // Intended for consumption by write! and writeln! (std::fmt) but without
130 // the fmt::Result return type imposed by fmt::Write (and avoiding the trait
132 pub(crate) fn write_fmt(&mut self, v: fmt::Arguments<'_>) {
134 self.buffer.write_fmt(v).unwrap();
137 pub(crate) fn to_display<T: Print>(mut self, t: T) -> String {
142 pub(crate) fn is_for_html(&self) -> bool {
146 pub(crate) fn reserve(&mut self, additional: usize) {
147 self.buffer.reserve(additional)
150 pub(crate) fn len(&self) -> usize {
155 fn comma_sep<T: fmt::Display>(
156 items: impl Iterator<Item = T>,
157 space_after_comma: bool,
158 ) -> impl fmt::Display {
159 display_fn(move |f| {
160 for (i, item) in items.enumerate() {
162 write!(f, ",{}", if space_after_comma { " " } else { "" })?;
164 fmt::Display::fmt(&item, f)?;
170 pub(crate) fn print_generic_bounds<'a, 'tcx: 'a>(
171 bounds: &'a [clean::GenericBound],
172 cx: &'a Context<'tcx>,
173 ) -> impl fmt::Display + 'a + Captures<'tcx> {
174 display_fn(move |f| {
175 let mut bounds_dup = FxHashSet::default();
177 for (i, bound) in bounds.iter().filter(|b| bounds_dup.insert(b.clone())).enumerate() {
181 fmt::Display::fmt(&bound.print(cx), f)?;
187 impl clean::GenericParamDef {
188 pub(crate) fn print<'a, 'tcx: 'a>(
190 cx: &'a Context<'tcx>,
191 ) -> impl fmt::Display + 'a + Captures<'tcx> {
192 display_fn(move |f| match &self.kind {
193 clean::GenericParamDefKind::Lifetime { outlives } => {
194 write!(f, "{}", self.name)?;
196 if !outlives.is_empty() {
198 for (i, lt) in outlives.iter().enumerate() {
202 write!(f, "{}", lt.print())?;
208 clean::GenericParamDefKind::Type { bounds, default, .. } => {
209 f.write_str(self.name.as_str())?;
211 if !bounds.is_empty() {
213 write!(f, ": {:#}", print_generic_bounds(bounds, cx))?;
215 write!(f, ": {}", print_generic_bounds(bounds, cx))?;
219 if let Some(ref ty) = default {
221 write!(f, " = {:#}", ty.print(cx))?;
223 write!(f, " = {}", ty.print(cx))?;
229 clean::GenericParamDefKind::Const { ty, default, .. } => {
231 write!(f, "const {}: {:#}", self.name, ty.print(cx))?;
233 write!(f, "const {}: {}", self.name, ty.print(cx))?;
236 if let Some(default) = default {
238 write!(f, " = {:#}", default)?;
240 write!(f, " = {}", default)?;
250 impl clean::Generics {
251 pub(crate) fn print<'a, 'tcx: 'a>(
253 cx: &'a Context<'tcx>,
254 ) -> impl fmt::Display + 'a + Captures<'tcx> {
255 display_fn(move |f| {
256 let mut real_params =
257 self.params.iter().filter(|p| !p.is_synthetic_type_param()).peekable();
258 if real_params.peek().is_none() {
263 write!(f, "<{:#}>", comma_sep(real_params.map(|g| g.print(cx)), true))
265 write!(f, "<{}>", comma_sep(real_params.map(|g| g.print(cx)), true))
271 /// * The Generics from which to emit a where-clause.
272 /// * The number of spaces to indent each line with.
273 /// * Whether the where-clause needs to add a comma and newline after the last bound.
274 pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
275 gens: &'a clean::Generics,
276 cx: &'a Context<'tcx>,
279 ) -> impl fmt::Display + 'a + Captures<'tcx> {
282 display_fn(move |f| {
283 let mut where_predicates = gens.where_predicates.iter().filter(|pred| {
284 !matches!(pred, clean::WherePredicate::BoundPredicate { bounds, .. } if bounds.is_empty())
286 display_fn(move |f| {
290 f.write_str("<br>")?;
294 clean::WherePredicate::BoundPredicate { ty, bounds, bound_params } => {
295 let ty_cx = ty.print(cx);
296 let generic_bounds = print_generic_bounds(bounds, cx);
298 if bound_params.is_empty() {
300 write!(f, "{ty_cx:#}: {generic_bounds:#}")
302 write!(f, "{ty_cx}: {generic_bounds}")
308 "for<{:#}> {ty_cx:#}: {generic_bounds:#}",
309 comma_sep(bound_params.iter().map(|lt| lt.print()), true)
314 "for<{}> {ty_cx}: {generic_bounds}",
315 comma_sep(bound_params.iter().map(|lt| lt.print()), true)
320 clean::WherePredicate::RegionPredicate { lifetime, bounds } => {
321 let mut bounds_display = String::new();
322 for bound in bounds.iter().map(|b| b.print(cx)) {
323 write!(bounds_display, "{bound} + ")?;
325 bounds_display.truncate(bounds_display.len() - " + ".len());
326 write!(f, "{}: {bounds_display}", lifetime.print())
328 clean::WherePredicate::EqPredicate { lhs, rhs } => {
330 write!(f, "{:#} == {:#}", lhs.print(cx), rhs.print(cx))
332 write!(f, "{} == {}", lhs.print(cx), rhs.print(cx))
339 if where_predicates.peek().is_none() {
343 let where_preds = comma_sep(where_predicates, false);
344 let clause = if f.alternate() {
346 // add a space so stripping <br> tags and breaking spaces still renders properly
347 format!(" where{where_preds}, ")
349 format!(" where{where_preds}")
352 let mut br_with_padding = String::with_capacity(6 * indent + 28);
353 br_with_padding.push_str("<br>");
354 for _ in 0..indent + 4 {
355 br_with_padding.push_str(" ");
357 let where_preds = where_preds.to_string().replace("<br>", &br_with_padding);
360 let mut clause = " ".repeat(indent.saturating_sub(1));
361 // add a space so stripping <br> tags and breaking spaces still renders properly
364 " <span class=\"where fmt-newline\">where{where_preds}, </span>"
368 // insert a <br> tag after a single space but before multiple spaces at the start
370 format!(" <br><span class=\"where\">where{where_preds}</span>")
372 let mut clause = br_with_padding;
373 clause.truncate(clause.len() - 5 * " ".len());
374 write!(clause, " <span class=\"where\">where{where_preds}</span>")?;
379 write!(f, "{clause}")
383 impl clean::Lifetime {
384 pub(crate) fn print(&self) -> impl fmt::Display + '_ {
389 impl clean::Constant {
390 pub(crate) fn print(&self, tcx: TyCtxt<'_>) -> impl fmt::Display + '_ {
391 let expr = self.expr(tcx);
394 if f.alternate() { f.write_str(&expr) } else { write!(f, "{}", Escape(&expr)) }
400 impl clean::PolyTrait {
401 fn print<'a, 'tcx: 'a>(
403 cx: &'a Context<'tcx>,
404 ) -> impl fmt::Display + 'a + Captures<'tcx> {
405 display_fn(move |f| {
406 if !self.generic_params.is_empty() {
411 comma_sep(self.generic_params.iter().map(|g| g.print(cx)), true)
417 comma_sep(self.generic_params.iter().map(|g| g.print(cx)), true)
422 write!(f, "{:#}", self.trait_.print(cx))
424 write!(f, "{}", self.trait_.print(cx))
430 impl clean::GenericBound {
431 pub(crate) fn print<'a, 'tcx: 'a>(
433 cx: &'a Context<'tcx>,
434 ) -> impl fmt::Display + 'a + Captures<'tcx> {
435 display_fn(move |f| match self {
436 clean::GenericBound::Outlives(lt) => write!(f, "{}", lt.print()),
437 clean::GenericBound::TraitBound(ty, modifier) => {
438 let modifier_str = match modifier {
439 hir::TraitBoundModifier::None => "",
440 hir::TraitBoundModifier::Maybe => "?",
441 // ~const is experimental; do not display those bounds in rustdoc
442 hir::TraitBoundModifier::MaybeConst => "",
445 write!(f, "{}{:#}", modifier_str, ty.print(cx))
447 write!(f, "{}{}", modifier_str, ty.print(cx))
454 impl clean::GenericArgs {
455 fn print<'a, 'tcx: 'a>(
457 cx: &'a Context<'tcx>,
458 ) -> impl fmt::Display + 'a + Captures<'tcx> {
459 display_fn(move |f| {
461 clean::GenericArgs::AngleBracketed { args, bindings } => {
462 if !args.is_empty() || !bindings.is_empty() {
466 f.write_str("<")?;
468 let mut comma = false;
469 for arg in args.iter() {
475 write!(f, "{:#}", arg.print(cx))?;
477 write!(f, "{}", arg.print(cx))?;
480 for binding in bindings.iter() {
486 write!(f, "{:#}", binding.print(cx))?;
488 write!(f, "{}", binding.print(cx))?;
494 f.write_str(">")?;
498 clean::GenericArgs::Parenthesized { inputs, output } => {
500 let mut comma = false;
501 for ty in inputs.iter() {
507 write!(f, "{:#}", ty.print(cx))?;
509 write!(f, "{}", ty.print(cx))?;
513 if let Some(ref ty) = *output {
515 write!(f, " -> {:#}", ty.print(cx))?;
517 write!(f, " -> {}", ty.print(cx))?;
527 // Possible errors when computing href link source for a `DefId`
528 #[derive(PartialEq, Eq)]
529 pub(crate) enum HrefError {
530 /// This item is known to rustdoc, but from a crate that does not have documentation generated.
532 /// This can only happen for non-local items.
536 /// Crate `a` defines a public trait and crate `b` – the target crate that depends on `a` –
537 /// implements it for a local type.
538 /// We document `b` but **not** `a` (we only _build_ the latter – with `rustc`):
541 /// rustc a.rs --crate-type=lib
542 /// rustdoc b.rs --crate-type=lib --extern=a=liba.rlib
545 /// Now, the associated items in the trait impl want to link to the corresponding item in the
546 /// trait declaration (see `html::render::assoc_href_attr`) but it's not available since their
547 /// *documentation (was) not built*.
548 DocumentationNotBuilt,
549 /// This can only happen for non-local items when `--document-private-items` is not passed.
551 // Not in external cache, href link should be in same page
555 // Panics if `syms` is empty.
556 pub(crate) fn join_with_double_colon(syms: &[Symbol]) -> String {
557 let mut s = String::with_capacity(estimate_item_path_byte_length(syms.len()));
558 s.push_str(syms[0].as_str());
559 for sym in &syms[1..] {
561 s.push_str(sym.as_str());
566 /// This function is to get the external macro path because they are not in the cache used in
567 /// `href_with_root_path`.
568 fn generate_macro_def_id_path(
571 root_path: Option<&str>,
572 ) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
573 let tcx = cx.shared.tcx;
574 let crate_name = tcx.crate_name(def_id.krate).to_string();
575 let cache = cx.cache();
577 let fqp: Vec<Symbol> = tcx
582 // extern blocks (and a few others things) have an empty name.
583 match elem.data.get_opt_name() {
584 Some(s) if !s.is_empty() => Some(s),
589 let relative = fqp.iter().map(|elem| elem.to_string());
590 let cstore = CStore::from_tcx(tcx);
591 // We need this to prevent a `panic` when this function is used from intra doc links...
592 if !cstore.has_crate_data(def_id.krate) {
593 debug!("No data for crate {}", crate_name);
594 return Err(HrefError::NotInExternalCache);
596 // Check to see if it is a macro 2.0 or built-in macro.
597 // More information in <https://rust-lang.github.io/rfcs/1584-macros.html>.
598 let is_macro_2 = match cstore.load_macro_untracked(def_id, tcx.sess) {
599 LoadedMacro::MacroDef(def, _) => {
600 // If `ast_def.macro_rules` is `true`, then it's not a macro 2.0.
601 matches!(&def.kind, ast::ItemKind::MacroDef(ast_def) if !ast_def.macro_rules)
606 let mut path = if is_macro_2 {
607 once(crate_name.clone()).chain(relative).collect()
609 vec![crate_name.clone(), relative.last().unwrap()]
612 // The minimum we can have is the crate name followed by the macro name. If shorter, then
613 // it means that that `relative` was empty, which is an error.
614 debug!("macro path cannot be empty!");
615 return Err(HrefError::NotInExternalCache);
618 if let Some(last) = path.last_mut() {
619 *last = format!("macro.{}.html", last);
622 let url = match cache.extern_locations[&def_id.krate] {
623 ExternalLocation::Remote(ref s) => {
624 // `ExternalLocation::Remote` always end with a `/`.
625 format!("{}{}", s, path.join("/"))
627 ExternalLocation::Local => {
628 // `root_path` always end with a `/`.
629 format!("{}{}/{}", root_path.unwrap_or(""), crate_name, path.join("/"))
631 ExternalLocation::Unknown => {
632 debug!("crate {} not in cache when linkifying macros", crate_name);
633 return Err(HrefError::NotInExternalCache);
636 Ok((url, ItemType::Macro, fqp))
639 pub(crate) fn href_with_root_path(
642 root_path: Option<&str>,
643 ) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
645 let def_kind = tcx.def_kind(did);
646 let did = match def_kind {
647 DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant => {
648 // documented on their parent's page
653 let cache = cx.cache();
654 let relative_to = &cx.current;
655 fn to_module_fqp(shortty: ItemType, fqp: &[Symbol]) -> &[Symbol] {
656 if shortty == ItemType::Module { fqp } else { &fqp[..fqp.len() - 1] }
660 && !cache.access_levels.is_public(did)
661 && !cache.document_private
662 && !cache.primitive_locations.values().any(|&id| id == did)
664 return Err(HrefError::Private);
667 let mut is_remote = false;
668 let (fqp, shortty, mut url_parts) = match cache.paths.get(&did) {
669 Some(&(ref fqp, shortty)) => (fqp, shortty, {
670 let module_fqp = to_module_fqp(shortty, fqp.as_slice());
671 debug!(?fqp, ?shortty, ?module_fqp);
672 href_relative_parts(module_fqp, relative_to).collect()
675 if let Some(&(ref fqp, shortty)) = cache.external_paths.get(&did) {
676 let module_fqp = to_module_fqp(shortty, fqp);
680 match cache.extern_locations[&did.krate] {
681 ExternalLocation::Remote(ref s) => {
683 let s = s.trim_end_matches('/');
684 let mut builder = UrlPartsBuilder::singleton(s);
685 builder.extend(module_fqp.iter().copied());
688 ExternalLocation::Local => {
689 href_relative_parts(module_fqp, relative_to).collect()
691 ExternalLocation::Unknown => return Err(HrefError::DocumentationNotBuilt),
694 } else if matches!(def_kind, DefKind::Macro(_)) {
695 return generate_macro_def_id_path(did, cx, root_path);
697 return Err(HrefError::NotInExternalCache);
702 if let Some(root_path) = root_path {
703 let root = root_path.trim_end_matches('/');
704 url_parts.push_front(root);
709 ItemType::Module => {
710 url_parts.push("index.html");
713 let prefix = shortty.as_str();
714 let last = fqp.last().unwrap();
715 url_parts.push_fmt(format_args!("{}.{}.html", prefix, last));
718 Ok((url_parts.finish(), shortty, fqp.to_vec()))
724 ) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
725 href_with_root_path(did, cx, None)
728 /// Both paths should only be modules.
729 /// This is because modules get their own directories; that is, `std::vec` and `std::vec::Vec` will
730 /// both need `../iter/trait.Iterator.html` to get at the iterator trait.
731 pub(crate) fn href_relative_parts<'fqp>(
733 relative_to_fqp: &[Symbol],
734 ) -> Box<dyn Iterator<Item = Symbol> + 'fqp> {
735 for (i, (f, r)) in fqp.iter().zip(relative_to_fqp.iter()).enumerate() {
736 // e.g. linking to std::iter from std::vec (`dissimilar_part_count` will be 1)
738 let dissimilar_part_count = relative_to_fqp.len() - i;
739 let fqp_module = &fqp[i..fqp.len()];
740 return box iter::repeat(sym::dotdot)
741 .take(dissimilar_part_count)
742 .chain(fqp_module.iter().copied());
745 // e.g. linking to std::sync::atomic from std::sync
746 if relative_to_fqp.len() < fqp.len() {
747 box fqp[relative_to_fqp.len()..fqp.len()].iter().copied()
748 // e.g. linking to std::sync from std::sync::atomic
749 } else if fqp.len() < relative_to_fqp.len() {
750 let dissimilar_part_count = relative_to_fqp.len() - fqp.len();
751 box iter::repeat(sym::dotdot).take(dissimilar_part_count)
752 // linking to the same module
758 /// Used to render a [`clean::Path`].
759 fn resolved_path<'cx>(
760 w: &mut fmt::Formatter<'_>,
765 cx: &'cx Context<'_>,
767 let last = path.segments.last().unwrap();
770 for seg in &path.segments[..path.segments.len() - 1] {
771 write!(w, "{}::", if seg.name == kw::PathRoot { "" } else { seg.name.as_str() })?;
775 write!(w, "{}{:#}", &last.name, last.args.print(cx))?;
777 let path = if use_absolute {
778 if let Ok((_, _, fqp)) = href(did, cx) {
781 join_with_double_colon(&fqp[..fqp.len() - 1]),
782 anchor(did, *fqp.last().unwrap(), cx)
785 last.name.to_string()
788 anchor(did, last.name, cx).to_string()
790 write!(w, "{}{}", path, last.args.print(cx))?;
796 f: &mut fmt::Formatter<'_>,
797 prim: clean::PrimitiveType,
801 primitive_link_fragment(f, prim, name, "", cx)
804 fn primitive_link_fragment(
805 f: &mut fmt::Formatter<'_>,
806 prim: clean::PrimitiveType,
812 let mut needs_termination = false;
814 match m.primitive_locations.get(&prim) {
815 Some(&def_id) if def_id.is_local() => {
816 let len = cx.current.len();
817 let len = if len == 0 { 0 } else { len - 1 };
820 "<a class=\"primitive\" href=\"{}primitive.{}.html{fragment}\">",
824 needs_termination = true;
827 let loc = match m.extern_locations[&def_id.krate] {
828 ExternalLocation::Remote(ref s) => {
829 let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
830 let builder: UrlPartsBuilder =
831 [s.as_str().trim_end_matches('/'), cname_sym.as_str()]
836 ExternalLocation::Local => {
837 let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
838 Some(if cx.current.first() == Some(&cname_sym) {
839 iter::repeat(sym::dotdot).take(cx.current.len() - 1).collect()
841 iter::repeat(sym::dotdot)
842 .take(cx.current.len())
843 .chain(iter::once(cname_sym))
847 ExternalLocation::Unknown => None,
849 if let Some(mut loc) = loc {
850 loc.push_fmt(format_args!("primitive.{}.html", prim.as_sym()));
851 write!(f, "<a class=\"primitive\" href=\"{}{fragment}\">", loc.finish())?;
852 needs_termination = true;
858 write!(f, "{}", name)?;
859 if needs_termination {
865 /// Helper to render type parameters
866 fn tybounds<'a, 'tcx: 'a>(
867 bounds: &'a [clean::PolyTrait],
868 lt: &'a Option<clean::Lifetime>,
869 cx: &'a Context<'tcx>,
870 ) -> impl fmt::Display + 'a + Captures<'tcx> {
871 display_fn(move |f| {
872 for (i, bound) in bounds.iter().enumerate() {
877 fmt::Display::fmt(&bound.print(cx), f)?;
880 if let Some(lt) = lt {
882 fmt::Display::fmt(<.print(), f)?;
888 pub(crate) fn anchor<'a, 'cx: 'a>(
891 cx: &'cx Context<'_>,
892 ) -> impl fmt::Display + 'a {
893 let parts = href(did, cx);
894 display_fn(move |f| {
895 if let Ok((url, short_ty, fqp)) = parts {
898 r#"<a class="{}" href="{}" title="{} {}">{}</a>"#,
902 join_with_double_colon(&fqp),
906 write!(f, "{}", text)
913 f: &mut fmt::Formatter<'_>,
915 cx: &'cx Context<'_>,
917 trace!("fmt_type(t = {:?})", t);
920 clean::Generic(name) => write!(f, "{}", name),
921 clean::Type::Path { ref path } => {
922 // Paths like `T::Output` and `Self::Output` should be rendered with all segments.
923 let did = path.def_id();
924 resolved_path(f, did, path, path.is_assoc_ty(), use_absolute, cx)
926 clean::DynTrait(ref bounds, ref lt) => {
927 f.write_str("dyn ")?;
928 fmt::Display::fmt(&tybounds(bounds, lt, cx), f)
930 clean::Infer => write!(f, "_"),
931 clean::Primitive(clean::PrimitiveType::Never) => {
932 primitive_link(f, PrimitiveType::Never, "!", cx)
934 clean::Primitive(prim) => primitive_link(f, prim, prim.as_sym().as_str(), cx),
935 clean::BareFunction(ref decl) => {
940 decl.print_hrtb_with_space(cx),
941 decl.unsafety.print_with_space(),
942 print_abi_with_space(decl.abi),
949 decl.print_hrtb_with_space(cx),
950 decl.unsafety.print_with_space(),
951 print_abi_with_space(decl.abi)
953 primitive_link(f, PrimitiveType::Fn, "fn", cx)?;
954 write!(f, "{}", decl.decl.print(cx))
957 clean::Tuple(ref typs) => {
959 &[] => primitive_link(f, PrimitiveType::Unit, "()", cx),
961 if let clean::Generic(name) = one {
962 primitive_link(f, PrimitiveType::Tuple, &format!("({name},)"), cx)
965 // Carry `f.alternate()` into this display w/o branching manually.
966 fmt::Display::fmt(&one.print(cx), f)?;
971 let generic_names: Vec<Symbol> = many
973 .filter_map(|t| match t {
974 clean::Generic(name) => Some(*name),
978 let is_generic = generic_names.len() == many.len();
982 PrimitiveType::Tuple,
983 &format!("({})", generic_names.iter().map(|s| s.as_str()).join(", ")),
988 for (i, item) in many.iter().enumerate() {
992 // Carry `f.alternate()` into this display w/o branching manually.
993 fmt::Display::fmt(&item.print(cx), f)?;
1000 clean::Slice(ref t) => match **t {
1001 clean::Generic(name) => {
1002 primitive_link(f, PrimitiveType::Slice, &format!("[{name}]"), cx)
1006 fmt::Display::fmt(&t.print(cx), f)?;
1010 clean::Array(ref t, ref n) => {
1011 primitive_link(f, PrimitiveType::Array, "[", cx)?;
1012 fmt::Display::fmt(&t.print(cx), f)?;
1014 primitive_link(f, PrimitiveType::Array, &format!("; {}]", n), cx)
1016 primitive_link(f, PrimitiveType::Array, &format!("; {}]", Escape(n)), cx)
1019 clean::RawPointer(m, ref t) => {
1021 hir::Mutability::Mut => "mut",
1022 hir::Mutability::Not => "const",
1025 if matches!(**t, clean::Generic(_)) || t.is_assoc_ty() {
1026 let text = if f.alternate() {
1027 format!("*{} {:#}", m, t.print(cx))
1029 format!("*{} {}", m, t.print(cx))
1031 primitive_link(f, clean::PrimitiveType::RawPointer, &text, cx)
1033 primitive_link(f, clean::PrimitiveType::RawPointer, &format!("*{} ", m), cx)?;
1034 fmt::Display::fmt(&t.print(cx), f)
1037 clean::BorrowedRef { lifetime: ref l, mutability, type_: ref ty } => {
1039 Some(l) => format!("{} ", l.print()),
1042 let m = mutability.print_with_space();
1043 let amp = if f.alternate() { "&".to_string() } else { "&".to_string() };
1045 clean::DynTrait(ref bounds, ref trait_lt)
1046 if bounds.len() > 1 || trait_lt.is_some() =>
1048 write!(f, "{}{}{}(", amp, lt, m)?;
1049 fmt_type(ty, f, use_absolute, cx)?;
1052 clean::Generic(..) => {
1055 PrimitiveType::Reference,
1056 &format!("{}{}{}", amp, lt, m),
1059 fmt_type(ty, f, use_absolute, cx)
1062 write!(f, "{}{}{}", amp, lt, m)?;
1063 fmt_type(ty, f, use_absolute, cx)
1067 clean::ImplTrait(ref bounds) => {
1069 write!(f, "impl {:#}", print_generic_bounds(bounds, cx))
1071 write!(f, "impl {}", print_generic_bounds(bounds, cx))
1074 clean::QPath { ref assoc, ref self_type, ref trait_, should_show_cast } => {
1076 if should_show_cast {
1077 write!(f, "<{:#} as {:#}>::", self_type.print(cx), trait_.print(cx))?
1079 write!(f, "{:#}::", self_type.print(cx))?
1082 if should_show_cast {
1083 write!(f, "<{} as {}>::", self_type.print(cx), trait_.print(cx))?
1085 write!(f, "{}::", self_type.print(cx))?
1088 // It's pretty unsightly to look at `<A as B>::C` in output, and
1089 // we've got hyperlinking on our side, so try to avoid longer
1090 // notation as much as possible by making `C` a hyperlink to trait
1091 // `B` to disambiguate.
1093 // FIXME: this is still a lossy conversion and there should probably
1094 // be a better way of representing this in general? Most of
1095 // the ugliness comes from inlining across crates where
1096 // everything comes in as a fully resolved QPath (hard to
1098 match href(trait_.def_id(), cx) {
1099 Ok((ref url, _, ref path)) if !f.alternate() => {
1102 "<a class=\"associatedtype\" href=\"{url}#{shortty}.{name}\" \
1103 title=\"type {path}::{name}\">{name}</a>{args}",
1105 shortty = ItemType::AssocType,
1107 path = join_with_double_colon(path),
1108 args = assoc.args.print(cx),
1111 _ => write!(f, "{}{:#}", assoc.name, assoc.args.print(cx))?,
1119 pub(crate) fn print<'b, 'a: 'b, 'tcx: 'a>(
1121 cx: &'a Context<'tcx>,
1122 ) -> impl fmt::Display + 'b + Captures<'tcx> {
1123 display_fn(move |f| fmt_type(self, f, false, cx))
1128 pub(crate) fn print<'b, 'a: 'b, 'tcx: 'a>(
1130 cx: &'a Context<'tcx>,
1131 ) -> impl fmt::Display + 'b + Captures<'tcx> {
1132 display_fn(move |f| resolved_path(f, self.def_id(), self, false, false, cx))
1137 pub(crate) fn print<'a, 'tcx: 'a>(
1140 cx: &'a Context<'tcx>,
1141 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1142 display_fn(move |f| {
1144 write!(f, "impl{:#} ", self.generics.print(cx))?;
1146 write!(f, "impl{} ", self.generics.print(cx))?;
1149 if let Some(ref ty) = self.trait_ {
1150 match self.polarity {
1151 ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => {}
1152 ty::ImplPolarity::Negative => write!(f, "!")?,
1154 fmt::Display::fmt(&ty.print(cx), f)?;
1155 write!(f, " for ")?;
1158 if let clean::Type::Tuple(types) = &self.for_ &&
1159 let [clean::Type::Generic(name)] = &types[..] &&
1160 (self.kind.is_tuple_variadic() || self.kind.is_auto()) {
1161 // Hardcoded anchor library/core/src/primitive_docs.rs
1162 // Link should match `# Trait implementations`
1163 primitive_link_fragment(f, PrimitiveType::Tuple, &format!("({name}₁, {name}₂, …, {name}ₙ)"), "#trait-implementations-1", cx)?;
1164 } else if let Some(ty) = self.kind.as_blanket_ty() {
1165 fmt_type(ty, f, use_absolute, cx)?;
1167 fmt_type(&self.for_, f, use_absolute, cx)?;
1170 fmt::Display::fmt(&print_where_clause(&self.generics, cx, 0, true), f)?;
1176 impl clean::Arguments {
1177 pub(crate) fn print<'a, 'tcx: 'a>(
1179 cx: &'a Context<'tcx>,
1180 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1181 display_fn(move |f| {
1182 for (i, input) in self.values.iter().enumerate() {
1183 if !input.name.is_empty() {
1184 write!(f, "{}: ", input.name)?;
1187 write!(f, "{:#}", input.type_.print(cx))?;
1189 write!(f, "{}", input.type_.print(cx))?;
1191 if i + 1 < self.values.len() {
1200 impl clean::FnRetTy {
1201 pub(crate) fn print<'a, 'tcx: 'a>(
1203 cx: &'a Context<'tcx>,
1204 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1205 display_fn(move |f| match self {
1206 clean::Return(clean::Tuple(tys)) if tys.is_empty() => Ok(()),
1207 clean::Return(ty) if f.alternate() => {
1208 write!(f, " -> {:#}", ty.print(cx))
1210 clean::Return(ty) => write!(f, " -> {}", ty.print(cx)),
1211 clean::DefaultReturn => Ok(()),
1216 impl clean::BareFunctionDecl {
1217 fn print_hrtb_with_space<'a, 'tcx: 'a>(
1219 cx: &'a Context<'tcx>,
1220 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1221 display_fn(move |f| {
1222 if !self.generic_params.is_empty() {
1226 comma_sep(self.generic_params.iter().map(|g| g.print(cx)), true)
1235 impl clean::FnDecl {
1236 pub(crate) fn print<'b, 'a: 'b, 'tcx: 'a>(
1238 cx: &'a Context<'tcx>,
1239 ) -> impl fmt::Display + 'b + Captures<'tcx> {
1240 display_fn(move |f| {
1241 let ellipsis = if self.c_variadic { ", ..." } else { "" };
1245 "({args:#}{ellipsis}){arrow:#}",
1246 args = self.inputs.print(cx),
1247 ellipsis = ellipsis,
1248 arrow = self.output.print(cx)
1253 "({args}{ellipsis}){arrow}",
1254 args = self.inputs.print(cx),
1255 ellipsis = ellipsis,
1256 arrow = self.output.print(cx)
1262 /// * `header_len`: The length of the function header and name. In other words, the number of
1263 /// characters in the function declaration up to but not including the parentheses.
1264 /// <br>Used to determine line-wrapping.
1265 /// * `indent`: The number of spaces to indent each successive line with, if line-wrapping is
1267 /// * `asyncness`: Whether the function is async or not.
1268 pub(crate) fn full_print<'a, 'tcx: 'a>(
1272 asyncness: hir::IsAsync,
1273 cx: &'a Context<'tcx>,
1274 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1275 display_fn(move |f| self.inner_full_print(header_len, indent, asyncness, f, cx))
1278 fn inner_full_print(
1282 asyncness: hir::IsAsync,
1283 f: &mut fmt::Formatter<'_>,
1286 let amp = if f.alternate() { "&" } else { "&" };
1287 let mut args = Buffer::html();
1288 let mut args_plain = Buffer::new();
1289 for (i, input) in self.inputs.values.iter().enumerate() {
1290 if let Some(selfty) = input.to_self() {
1292 clean::SelfValue => {
1293 args.push_str("self");
1294 args_plain.push_str("self");
1296 clean::SelfBorrowed(Some(ref lt), mtbl) => {
1297 write!(args, "{}{} {}self", amp, lt.print(), mtbl.print_with_space());
1298 write!(args_plain, "&{} {}self", lt.print(), mtbl.print_with_space());
1300 clean::SelfBorrowed(None, mtbl) => {
1301 write!(args, "{}{}self", amp, mtbl.print_with_space());
1302 write!(args_plain, "&{}self", mtbl.print_with_space());
1304 clean::SelfExplicit(ref typ) => {
1306 write!(args, "self: {:#}", typ.print(cx));
1308 write!(args, "self: {}", typ.print(cx));
1310 write!(args_plain, "self: {:#}", typ.print(cx));
1315 args.push_str("<br>");
1318 args.push_str("const ");
1319 args_plain.push_str("const ");
1321 if !input.name.is_empty() {
1322 write!(args, "{}: ", input.name);
1323 write!(args_plain, "{}: ", input.name);
1327 write!(args, "{:#}", input.type_.print(cx));
1329 write!(args, "{}", input.type_.print(cx));
1331 write!(args_plain, "{:#}", input.type_.print(cx));
1333 if i + 1 < self.inputs.values.len() {
1335 args_plain.push_str(",");
1339 let mut args_plain = format!("({})", args_plain.into_inner());
1340 let mut args = args.into_inner();
1342 if self.c_variadic {
1343 args.push_str(",<br> ...");
1344 args_plain.push_str(", ...");
1348 let arrow = if let hir::IsAsync::Async = asyncness {
1349 let output = self.sugared_async_return_type();
1350 arrow_plain = format!("{:#}", output.print(cx));
1351 if f.alternate() { arrow_plain.clone() } else { format!("{}", output.print(cx)) }
1353 arrow_plain = format!("{:#}", self.output.print(cx));
1354 if f.alternate() { arrow_plain.clone() } else { format!("{}", self.output.print(cx)) }
1357 let declaration_len = header_len + args_plain.len() + arrow_plain.len();
1358 let output = if declaration_len > 80 {
1359 let full_pad = format!("<br>{}", " ".repeat(indent + 4));
1360 let close_pad = format!("<br>{}", " ".repeat(indent));
1362 "({pad}{args}{close}){arrow}",
1363 pad = if self.inputs.values.is_empty() { "" } else { &full_pad },
1364 args = args.replace("<br>", &full_pad),
1369 format!("({args}){arrow}", args = args.replace("<br>", " "), arrow = arrow)
1373 write!(f, "{}", output.replace("<br>", "\n"))
1375 write!(f, "{}", output)
1380 impl clean::Visibility {
1381 pub(crate) fn print_with_space<'a, 'tcx: 'a>(
1384 cx: &'a Context<'tcx>,
1385 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1386 use std::fmt::Write as _;
1388 let to_print: Cow<'static, str> = match self {
1389 clean::Public => "pub ".into(),
1390 clean::Inherited => "".into(),
1391 clean::Visibility::Restricted(vis_did) => {
1392 // FIXME(camelid): This may not work correctly if `item_did` is a module.
1393 // However, rustdoc currently never displays a module's
1394 // visibility, so it shouldn't matter.
1395 let parent_module = find_nearest_parent_module(cx.tcx(), item_did.expect_def_id());
1397 if vis_did.is_crate_root() {
1398 "pub(crate) ".into()
1399 } else if parent_module == Some(vis_did) {
1400 // `pub(in foo)` where `foo` is the parent module
1401 // is the same as no visibility modifier
1403 } else if parent_module
1404 .and_then(|parent| find_nearest_parent_module(cx.tcx(), parent))
1407 "pub(super) ".into()
1409 let path = cx.tcx().def_path(vis_did);
1410 debug!("path={:?}", path);
1411 // modified from `resolved_path()` to work with `DefPathData`
1412 let last_name = path.data.last().unwrap().data.get_opt_name().unwrap();
1413 let anchor = anchor(vis_did, last_name, cx).to_string();
1415 let mut s = "pub(in ".to_owned();
1416 for seg in &path.data[..path.data.len() - 1] {
1417 let _ = write!(s, "{}::", seg.data.get_opt_name().unwrap());
1419 let _ = write!(s, "{}) ", anchor);
1424 display_fn(move |f| write!(f, "{}", to_print))
1427 /// This function is the same as print_with_space, except that it renders no links.
1428 /// It's used for macros' rendered source view, which is syntax highlighted and cannot have
1430 pub(crate) fn to_src_with_space<'a, 'tcx: 'a>(
1434 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1435 let to_print = match self {
1436 clean::Public => "pub ".to_owned(),
1437 clean::Inherited => String::new(),
1438 clean::Visibility::Restricted(vis_did) => {
1439 // FIXME(camelid): This may not work correctly if `item_did` is a module.
1440 // However, rustdoc currently never displays a module's
1441 // visibility, so it shouldn't matter.
1442 let parent_module = find_nearest_parent_module(tcx, item_did);
1444 if vis_did.is_crate_root() {
1445 "pub(crate) ".to_owned()
1446 } else if parent_module == Some(vis_did) {
1447 // `pub(in foo)` where `foo` is the parent module
1448 // is the same as no visibility modifier
1450 } else if parent_module.and_then(|parent| find_nearest_parent_module(tcx, parent))
1453 "pub(super) ".to_owned()
1455 format!("pub(in {}) ", tcx.def_path_str(vis_did))
1459 display_fn(move |f| f.write_str(&to_print))
1463 pub(crate) trait PrintWithSpace {
1464 fn print_with_space(&self) -> &str;
1467 impl PrintWithSpace for hir::Unsafety {
1468 fn print_with_space(&self) -> &str {
1470 hir::Unsafety::Unsafe => "unsafe ",
1471 hir::Unsafety::Normal => "",
1476 impl PrintWithSpace for hir::IsAsync {
1477 fn print_with_space(&self) -> &str {
1479 hir::IsAsync::Async => "async ",
1480 hir::IsAsync::NotAsync => "",
1485 impl PrintWithSpace for hir::Mutability {
1486 fn print_with_space(&self) -> &str {
1488 hir::Mutability::Not => "",
1489 hir::Mutability::Mut => "mut ",
1494 pub(crate) fn print_constness_with_space(
1496 s: Option<ConstStability>,
1499 // const stable or when feature(staged_api) is not set
1501 hir::Constness::Const,
1502 Some(ConstStability { level: StabilityLevel::Stable { .. }, .. }),
1504 | (hir::Constness::Const, None) => "const ",
1505 // const unstable or not const
1510 impl clean::Import {
1511 pub(crate) fn print<'a, 'tcx: 'a>(
1513 cx: &'a Context<'tcx>,
1514 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1515 display_fn(move |f| match self.kind {
1516 clean::ImportKind::Simple(name) => {
1517 if name == self.source.path.last() {
1518 write!(f, "use {};", self.source.print(cx))
1520 write!(f, "use {} as {};", self.source.print(cx), name)
1523 clean::ImportKind::Glob => {
1524 if self.source.path.segments.is_empty() {
1527 write!(f, "use {}::*;", self.source.print(cx))
1534 impl clean::ImportSource {
1535 pub(crate) fn print<'a, 'tcx: 'a>(
1537 cx: &'a Context<'tcx>,
1538 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1539 display_fn(move |f| match self.did {
1540 Some(did) => resolved_path(f, did, &self.path, true, false, cx),
1542 for seg in &self.path.segments[..self.path.segments.len() - 1] {
1543 write!(f, "{}::", seg.name)?;
1545 let name = self.path.last();
1546 if let hir::def::Res::PrimTy(p) = self.path.res {
1547 primitive_link(f, PrimitiveType::from(p), name.as_str(), cx)?;
1549 write!(f, "{}", name)?;
1557 impl clean::TypeBinding {
1558 pub(crate) fn print<'a, 'tcx: 'a>(
1560 cx: &'a Context<'tcx>,
1561 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1562 display_fn(move |f| {
1563 f.write_str(self.assoc.name.as_str())?;
1565 write!(f, "{:#}", self.assoc.args.print(cx))?;
1567 write!(f, "{}", self.assoc.args.print(cx))?;
1570 clean::TypeBindingKind::Equality { ref term } => {
1572 write!(f, " = {:#}", term.print(cx))?;
1574 write!(f, " = {}", term.print(cx))?;
1577 clean::TypeBindingKind::Constraint { ref bounds } => {
1578 if !bounds.is_empty() {
1580 write!(f, ": {:#}", print_generic_bounds(bounds, cx))?;
1582 write!(f, ": {}", print_generic_bounds(bounds, cx))?;
1592 pub(crate) fn print_abi_with_space(abi: Abi) -> impl fmt::Display {
1593 display_fn(move |f| {
1594 let quot = if f.alternate() { "\"" } else { """ };
1596 Abi::Rust => Ok(()),
1597 abi => write!(f, "extern {0}{1}{0} ", quot, abi.name()),
1602 pub(crate) fn print_default_space<'a>(v: bool) -> &'a str {
1603 if v { "default " } else { "" }
1606 impl clean::GenericArg {
1607 pub(crate) fn print<'a, 'tcx: 'a>(
1609 cx: &'a Context<'tcx>,
1610 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1611 display_fn(move |f| match self {
1612 clean::GenericArg::Lifetime(lt) => fmt::Display::fmt(<.print(), f),
1613 clean::GenericArg::Type(ty) => fmt::Display::fmt(&ty.print(cx), f),
1614 clean::GenericArg::Const(ct) => fmt::Display::fmt(&ct.print(cx.tcx()), f),
1615 clean::GenericArg::Infer => fmt::Display::fmt("_", f),
1620 impl clean::types::Term {
1621 pub(crate) fn print<'a, 'tcx: 'a>(
1623 cx: &'a Context<'tcx>,
1624 ) -> impl fmt::Display + 'a + Captures<'tcx> {
1626 clean::types::Term::Type(ty) => ty.print(cx),
1632 pub(crate) fn display_fn(
1633 f: impl FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result,
1634 ) -> impl fmt::Display {
1635 struct WithFormatter<F>(Cell<Option<F>>);
1637 impl<F> fmt::Display for WithFormatter<F>
1639 F: FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result,
1641 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1642 (self.0.take()).unwrap()(f)
1646 WithFormatter(Cell::new(Some(f)))