From 3f1ab9db6a0bbce2b61d910954335515c60260fe Mon Sep 17 00:00:00 2001 From: Camelid Date: Wed, 3 Mar 2021 20:20:34 -0800 Subject: [PATCH 1/4] Don't use `CURRENT_DEPTH` in `clean/types.rs` This part actually wasn't that hard. Ending the use of it in `html/format.rs` is probably going to be *a lot* harder. --- src/librustdoc/clean/types.rs | 12 ++++-------- src/librustdoc/html/render/context.rs | 4 ++++ src/librustdoc/html/render/mod.rs | 7 ++++--- src/librustdoc/html/render/print_item.rs | 3 ++- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index e560843383ec5..bb0257ebc44e9 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -190,8 +190,8 @@ impl Item { self.attrs.collapsed_doc_value() } - crate fn links(&self, cache: &Cache) -> Vec { - self.attrs.links(self.def_id.krate, cache) + crate fn links(&self, cache: &Cache, depth: usize) -> Vec { + self.attrs.links(self.def_id.krate, cache, depth) } crate fn is_crate(&self) -> bool { @@ -847,9 +847,8 @@ impl Attributes { /// Gets links as a vector /// /// Cache must be populated before call - crate fn links(&self, krate: CrateNum, cache: &Cache) -> Vec { + crate fn links(&self, krate: CrateNum, cache: &Cache, depth: usize) -> Vec { use crate::html::format::href; - use crate::html::render::CURRENT_DEPTH; self.links .iter() @@ -873,10 +872,7 @@ impl Attributes { None => { if let Some(ref fragment) = *fragment { let url = match cache.extern_locations.get(&krate) { - Some(&(_, _, ExternalLocation::Local)) => { - let depth = CURRENT_DEPTH.with(|l| l.get()); - "../".repeat(depth) - } + Some(&(_, _, ExternalLocation::Local)) => "../".repeat(depth), Some(&(_, _, ExternalLocation::Remote(ref s))) => s.to_string(), Some(&(_, _, ExternalLocation::Unknown)) | None => String::from( // NOTE: intentionally doesn't pass crate name to avoid having diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 976168a9ac407..fcb1f5cce12c2 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -71,6 +71,10 @@ crate struct Context<'tcx> { } impl<'tcx> Context<'tcx> { + pub(super) fn depth(&self) -> usize { + self.current.len() + } + pub(super) fn path(&self, filename: &str) -> PathBuf { // We use splitn vs Path::extension here because we might get a filename // like `style.min.css` and we want to process that into diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 50cae50c2c3eb..33c474bd0131b 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -670,7 +670,8 @@ fn document_short( return; } if let Some(s) = item.doc_value() { - let mut summary_html = MarkdownSummaryLine(&s, &item.links(&cx.cache)).into_string(); + let mut summary_html = + MarkdownSummaryLine(&s, &item.links(&cx.cache, cx.depth())).into_string(); if s.contains('\n') { let link = @@ -709,7 +710,7 @@ fn document_full( ) { if let Some(s) = cx.shared.maybe_collapsed_doc_value(item) { debug!("Doc block: =====\n{}\n=====", s); - render_markdown(w, cx, &*s, item.links(&cx.cache), prefix, is_hidden); + render_markdown(w, cx, &*s, item.links(&cx.cache, cx.depth()), prefix, is_hidden); } else if !prefix.is_empty() { if is_hidden { w.write_str("
"); @@ -1487,7 +1488,7 @@ fn render_impl( "
{}
", Markdown( &*dox, - &i.impl_item.links(&cx.cache), + &i.impl_item.links(&cx.cache, cx.depth()), &mut ids, cx.shared.codes, cx.shared.edition, diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 6cdd3838023e6..9650d0e322b45 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -297,7 +297,8 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl ", name = *myitem.name.as_ref().unwrap(), stab_tags = extra_info_tags(myitem, item, cx.tcx()), - docs = MarkdownSummaryLine(&doc_value, &myitem.links(&cx.cache)).into_string(), + docs = MarkdownSummaryLine(&doc_value, &myitem.links(&cx.cache, cx.depth())) + .into_string(), class = myitem.type_(), add = add, stab = stab.unwrap_or_else(String::new), From a3f56b355823d8f3a906413ef288ce9a06369601 Mon Sep 17 00:00:00 2001 From: Camelid Date: Wed, 3 Mar 2021 20:38:58 -0800 Subject: [PATCH 2/4] Remove many uses of `CURRENT_DEPTH` from `html/format.rs` --- src/librustdoc/clean/utils.rs | 5 +- src/librustdoc/html/format.rs | 319 ++++++++++-------- src/librustdoc/html/render/mod.rs | 91 +++-- src/librustdoc/html/render/print_item.rs | 106 +++--- src/librustdoc/html/render/write_shared.rs | 2 +- .../passes/calculate_doc_coverage.rs | 6 +- 6 files changed, 307 insertions(+), 222 deletions(-) diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index a64e8c21c462e..73abab26e7696 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -414,7 +414,10 @@ crate fn resolve_type(cx: &mut DocContext<'_>, path: Path, id: hir::HirId) -> Ty return Generic(kw::SelfUpper); } Res::Def(DefKind::TyParam, _) if path.segments.len() == 1 => { - return Generic(Symbol::intern(&format!("{:#}", path.print(&cx.cache)))); + // FIXME(camelid): I hope passing 0 as the depth doesn't break anything.... + // Then again, I think used to be 0 anyway through CURRENT_DEPTH + // because (I think) rendering hadn't started yet. + return Generic(Symbol::intern(&format!("{:#}", path.print(&cx.cache, 0)))); } Res::SelfTy(..) | Res::Def(DefKind::TyParam | DefKind::AssocTy, _) => true, _ => false, diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index bea0e75832c33..9e171afcc1f07 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -147,24 +147,27 @@ fn comma_sep(items: impl Iterator) -> impl fmt::Displ crate fn print_generic_bounds<'a>( bounds: &'a [clean::GenericBound], cache: &'a Cache, + depth: usize, ) -> impl fmt::Display + 'a { display_fn(move |f| { let mut bounds_dup = FxHashSet::default(); - for (i, bound) in - bounds.iter().filter(|b| bounds_dup.insert(b.print(cache).to_string())).enumerate() + for (i, bound) in bounds + .iter() + .filter(|b| bounds_dup.insert(b.print(cache, depth).to_string())) + .enumerate() { if i > 0 { f.write_str(" + ")?; } - fmt::Display::fmt(&bound.print(cache), f)?; + fmt::Display::fmt(&bound.print(cache, depth), f)?; } Ok(()) }) } impl clean::GenericParamDef { - crate fn print<'a>(&'a self, cache: &'a Cache) -> impl fmt::Display + 'a { + crate fn print<'a>(&'a self, cache: &'a Cache, depth: usize) -> impl fmt::Display + 'a { display_fn(move |f| match self.kind { clean::GenericParamDefKind::Lifetime => write!(f, "{}", self.name), clean::GenericParamDefKind::Type { ref bounds, ref default, .. } => { @@ -172,17 +175,17 @@ impl clean::GenericParamDef { if !bounds.is_empty() { if f.alternate() { - write!(f, ": {:#}", print_generic_bounds(bounds, cache))?; + write!(f, ": {:#}", print_generic_bounds(bounds, cache, depth))?; } else { - write!(f, ": {}", print_generic_bounds(bounds, cache))?; + write!(f, ": {}", print_generic_bounds(bounds, cache, depth))?; } } if let Some(ref ty) = default { if f.alternate() { - write!(f, " = {:#}", ty.print(cache))?; + write!(f, " = {:#}", ty.print(cache, depth))?; } else { - write!(f, " = {}", ty.print(cache))?; + write!(f, " = {}", ty.print(cache, depth))?; } } @@ -190,9 +193,9 @@ impl clean::GenericParamDef { } clean::GenericParamDefKind::Const { ref ty, .. } => { if f.alternate() { - write!(f, "const {}: {:#}", self.name, ty.print(cache)) + write!(f, "const {}: {:#}", self.name, ty.print(cache, depth)) } else { - write!(f, "const {}: {}", self.name, ty.print(cache)) + write!(f, "const {}: {}", self.name, ty.print(cache, depth)) } } }) @@ -200,7 +203,7 @@ impl clean::GenericParamDef { } impl clean::Generics { - crate fn print<'a>(&'a self, cache: &'a Cache) -> impl fmt::Display + 'a { + crate fn print<'a>(&'a self, cache: &'a Cache, depth: usize) -> impl fmt::Display + 'a { display_fn(move |f| { let real_params = self.params.iter().filter(|p| !p.is_synthetic_type_param()).collect::>(); @@ -208,16 +211,20 @@ impl clean::Generics { return Ok(()); } if f.alternate() { - write!(f, "<{:#}>", comma_sep(real_params.iter().map(|g| g.print(cache)))) + write!(f, "<{:#}>", comma_sep(real_params.iter().map(|g| g.print(cache, depth)))) } else { - write!(f, "<{}>", comma_sep(real_params.iter().map(|g| g.print(cache)))) + write!( + f, + "<{}>", + comma_sep(real_params.iter().map(|g| g.print(cache, depth))) + ) } }) } } impl<'a> WhereClause<'a> { - crate fn print<'b>(&'b self, cache: &'b Cache) -> impl fmt::Display + 'b { + crate fn print<'b>(&'b self, cache: &'b Cache, depth: usize) -> impl fmt::Display + 'b { display_fn(move |f| { let &WhereClause { gens, indent, end_newline } = self; if gens.where_predicates.is_empty() { @@ -246,14 +253,14 @@ impl<'a> WhereClause<'a> { if f.alternate() { clause.push_str(&format!( "{:#}: {:#}", - ty.print(cache), - print_generic_bounds(bounds, cache) + ty.print(cache, depth), + print_generic_bounds(bounds, cache, depth) )); } else { clause.push_str(&format!( "{}: {}", - ty.print(cache), - print_generic_bounds(bounds, cache) + ty.print(cache, depth), + print_generic_bounds(bounds, cache, depth) )); } } @@ -263,7 +270,7 @@ impl<'a> WhereClause<'a> { lifetime.print(), bounds .iter() - .map(|b| b.print(cache).to_string()) + .map(|b| b.print(cache, depth).to_string()) .collect::>() .join(" + ") )); @@ -272,14 +279,14 @@ impl<'a> WhereClause<'a> { if f.alternate() { clause.push_str(&format!( "{:#} == {:#}", - lhs.print(cache), - rhs.print(cache) + lhs.print(cache, depth), + rhs.print(cache, depth) )); } else { clause.push_str(&format!( "{} == {}", - lhs.print(cache), - rhs.print(cache) + lhs.print(cache, depth), + rhs.print(cache, depth) )); } } @@ -332,34 +339,34 @@ impl clean::Constant { } impl clean::PolyTrait { - fn print<'a>(&'a self, cache: &'a Cache) -> impl fmt::Display + 'a { + fn print<'a>(&'a self, cache: &'a Cache, depth: usize) -> impl fmt::Display + 'a { display_fn(move |f| { if !self.generic_params.is_empty() { if f.alternate() { write!( f, "for<{:#}> ", - comma_sep(self.generic_params.iter().map(|g| g.print(cache))) + comma_sep(self.generic_params.iter().map(|g| g.print(cache, depth))) )?; } else { write!( f, "for<{}> ", - comma_sep(self.generic_params.iter().map(|g| g.print(cache))) + comma_sep(self.generic_params.iter().map(|g| g.print(cache, depth))) )?; } } if f.alternate() { - write!(f, "{:#}", self.trait_.print(cache)) + write!(f, "{:#}", self.trait_.print(cache, depth)) } else { - write!(f, "{}", self.trait_.print(cache)) + write!(f, "{}", self.trait_.print(cache, depth)) } }) } } impl clean::GenericBound { - crate fn print<'a>(&'a self, cache: &'a Cache) -> impl fmt::Display + 'a { + crate fn print<'a>(&'a self, cache: &'a Cache, depth: usize) -> impl fmt::Display + 'a { display_fn(move |f| match self { clean::GenericBound::Outlives(lt) => write!(f, "{}", lt.print()), clean::GenericBound::TraitBound(ty, modifier) => { @@ -369,9 +376,9 @@ impl clean::GenericBound { hir::TraitBoundModifier::MaybeConst => "?const", }; if f.alternate() { - write!(f, "{}{:#}", modifier_str, ty.print(cache)) + write!(f, "{}{:#}", modifier_str, ty.print(cache, depth)) } else { - write!(f, "{}{}", modifier_str, ty.print(cache)) + write!(f, "{}{}", modifier_str, ty.print(cache, depth)) } } }) @@ -379,7 +386,7 @@ impl clean::GenericBound { } impl clean::GenericArgs { - fn print<'a>(&'a self, cache: &'a Cache) -> impl fmt::Display + 'a { + fn print<'a>(&'a self, cache: &'a Cache, depth: usize) -> impl fmt::Display + 'a { display_fn(move |f| { match self { clean::GenericArgs::AngleBracketed { args, bindings } => { @@ -396,9 +403,9 @@ impl clean::GenericArgs { } comma = true; if f.alternate() { - write!(f, "{:#}", arg.print(cache))?; + write!(f, "{:#}", arg.print(cache, depth))?; } else { - write!(f, "{}", arg.print(cache))?; + write!(f, "{}", arg.print(cache, depth))?; } } for binding in bindings { @@ -407,9 +414,9 @@ impl clean::GenericArgs { } comma = true; if f.alternate() { - write!(f, "{:#}", binding.print(cache))?; + write!(f, "{:#}", binding.print(cache, depth))?; } else { - write!(f, "{}", binding.print(cache))?; + write!(f, "{}", binding.print(cache, depth))?; } } if f.alternate() { @@ -428,17 +435,17 @@ impl clean::GenericArgs { } comma = true; if f.alternate() { - write!(f, "{:#}", ty.print(cache))?; + write!(f, "{:#}", ty.print(cache, depth))?; } else { - write!(f, "{}", ty.print(cache))?; + write!(f, "{}", ty.print(cache, depth))?; } } f.write_str(")")?; if let Some(ref ty) = *output { if f.alternate() { - write!(f, " -> {:#}", ty.print(cache))?; + write!(f, " -> {:#}", ty.print(cache, depth))?; } else { - write!(f, " -> {}", ty.print(cache))?; + write!(f, " -> {}", ty.print(cache, depth))?; } } } @@ -449,19 +456,19 @@ impl clean::GenericArgs { } impl clean::PathSegment { - crate fn print<'a>(&'a self, cache: &'a Cache) -> impl fmt::Display + 'a { + crate fn print<'a>(&'a self, cache: &'a Cache, depth: usize) -> impl fmt::Display + 'a { display_fn(move |f| { if f.alternate() { - write!(f, "{}{:#}", self.name, self.args.print(cache)) + write!(f, "{}{:#}", self.name, self.args.print(cache, depth)) } else { - write!(f, "{}{}", self.name, self.args.print(cache)) + write!(f, "{}{}", self.name, self.args.print(cache, depth)) } }) } } impl clean::Path { - crate fn print<'a>(&'a self, cache: &'a Cache) -> impl fmt::Display + 'a { + crate fn print<'a>(&'a self, cache: &'a Cache, depth: usize) -> impl fmt::Display + 'a { display_fn(move |f| { if self.global { f.write_str("::")? @@ -472,9 +479,9 @@ impl clean::Path { f.write_str("::")? } if f.alternate() { - write!(f, "{:#}", seg.print(cache))?; + write!(f, "{:#}", seg.print(cache, depth))?; } else { - write!(f, "{}", seg.print(cache))?; + write!(f, "{}", seg.print(cache, depth))?; } } Ok(()) @@ -531,6 +538,7 @@ fn resolved_path( print_all: bool, use_absolute: bool, cache: &Cache, + depth: usize, ) -> fmt::Result { let last = path.segments.last().unwrap(); @@ -540,7 +548,7 @@ fn resolved_path( } } if w.alternate() { - write!(w, "{}{:#}", &last.name, last.args.print(cache))?; + write!(w, "{}{:#}", &last.name, last.args.print(cache, depth))?; } else { let path = if use_absolute { if let Some((_, _, fqp)) = href(did, cache) { @@ -555,7 +563,7 @@ fn resolved_path( } else { anchor(did, &*last.name.as_str(), cache).to_string() }; - write!(w, "{}{}", path, last.args.print(cache))?; + write!(w, "{}{}", path, last.args.print(cache, depth))?; } Ok(()) } @@ -565,13 +573,13 @@ fn primitive_link( prim: clean::PrimitiveType, name: &str, m: &Cache, + depth: usize, ) -> fmt::Result { let mut needs_termination = false; if !f.alternate() { match m.primitive_locations.get(&prim) { Some(&def_id) if def_id.is_local() => { - let len = CURRENT_DEPTH.with(|s| s.get()); - let len = if len == 0 { 0 } else { len - 1 }; + let len = if depth == 0 { 0 } else { depth - 1 }; write!( f, "", @@ -583,10 +591,7 @@ fn primitive_link( Some(&def_id) => { let loc = match m.extern_locations[&def_id.krate] { (ref cname, _, ExternalLocation::Remote(ref s)) => Some((cname, s.to_string())), - (ref cname, _, ExternalLocation::Local) => { - let len = CURRENT_DEPTH.with(|s| s.get()); - Some((cname, "../".repeat(len))) - } + (ref cname, _, ExternalLocation::Local) => Some((cname, "../".repeat(depth))), (.., ExternalLocation::Unknown) => None, }; if let Some((cname, root)) = loc { @@ -614,12 +619,13 @@ fn primitive_link( fn tybounds<'a>( param_names: &'a Option>, cache: &'a Cache, + depth: usize, ) -> impl fmt::Display + 'a { display_fn(move |f| match *param_names { Some(ref params) => { for param in params { write!(f, " + ")?; - fmt::Display::fmt(¶m.print(cache), f)?; + fmt::Display::fmt(¶m.print(cache, depth), f)?; } Ok(()) } @@ -650,6 +656,7 @@ fn fmt_type( f: &mut fmt::Formatter<'_>, use_absolute: bool, cache: &Cache, + depth: usize, ) -> fmt::Result { debug!("fmt_type(t = {:?})", t); @@ -660,69 +667,69 @@ fn fmt_type( f.write_str("dyn ")?; } // Paths like `T::Output` and `Self::Output` should be rendered with all segments. - resolved_path(f, did, path, is_generic, use_absolute, cache)?; - fmt::Display::fmt(&tybounds(param_names, cache), f) + resolved_path(f, did, path, is_generic, use_absolute, cache, depth)?; + fmt::Display::fmt(&tybounds(param_names, cache, depth), f) } clean::Infer => write!(f, "_"), - clean::Primitive(prim) => primitive_link(f, prim, prim.as_str(), cache), + clean::Primitive(prim) => primitive_link(f, prim, prim.as_str(), cache, depth), clean::BareFunction(ref decl) => { if f.alternate() { write!( f, "{:#}{}{:#}fn{:#}", - decl.print_hrtb_with_space(cache), + decl.print_hrtb_with_space(cache, depth), decl.unsafety.print_with_space(), print_abi_with_space(decl.abi), - decl.decl.print(cache) + decl.decl.print(cache, depth) ) } else { write!( f, "{}{}{}", - decl.print_hrtb_with_space(cache), + decl.print_hrtb_with_space(cache, depth), decl.unsafety.print_with_space(), print_abi_with_space(decl.abi) )?; - primitive_link(f, PrimitiveType::Fn, "fn", cache)?; - write!(f, "{}", decl.decl.print(cache)) + primitive_link(f, PrimitiveType::Fn, "fn", cache, depth)?; + write!(f, "{}", decl.decl.print(cache, depth)) } } clean::Tuple(ref typs) => { match &typs[..] { - &[] => primitive_link(f, PrimitiveType::Unit, "()", cache), + &[] => primitive_link(f, PrimitiveType::Unit, "()", cache, depth), &[ref one] => { - primitive_link(f, PrimitiveType::Tuple, "(", cache)?; + primitive_link(f, PrimitiveType::Tuple, "(", cache, depth)?; // Carry `f.alternate()` into this display w/o branching manually. - fmt::Display::fmt(&one.print(cache), f)?; - primitive_link(f, PrimitiveType::Tuple, ",)", cache) + fmt::Display::fmt(&one.print(cache, depth), f)?; + primitive_link(f, PrimitiveType::Tuple, ",)", cache, depth) } many => { - primitive_link(f, PrimitiveType::Tuple, "(", cache)?; + primitive_link(f, PrimitiveType::Tuple, "(", cache, depth)?; for (i, item) in many.iter().enumerate() { if i != 0 { write!(f, ", ")?; } - fmt::Display::fmt(&item.print(cache), f)?; + fmt::Display::fmt(&item.print(cache, depth), f)?; } - primitive_link(f, PrimitiveType::Tuple, ")", cache) + primitive_link(f, PrimitiveType::Tuple, ")", cache, depth) } } } clean::Slice(ref t) => { - primitive_link(f, PrimitiveType::Slice, "[", cache)?; - fmt::Display::fmt(&t.print(cache), f)?; - primitive_link(f, PrimitiveType::Slice, "]", cache) + primitive_link(f, PrimitiveType::Slice, "[", cache, depth)?; + fmt::Display::fmt(&t.print(cache, depth), f)?; + primitive_link(f, PrimitiveType::Slice, "]", cache, depth) } clean::Array(ref t, ref n) => { - primitive_link(f, PrimitiveType::Array, "[", cache)?; - fmt::Display::fmt(&t.print(cache), f)?; + primitive_link(f, PrimitiveType::Array, "[", cache, depth)?; + fmt::Display::fmt(&t.print(cache, depth), f)?; if f.alternate() { - primitive_link(f, PrimitiveType::Array, &format!("; {}]", n), cache) + primitive_link(f, PrimitiveType::Array, &format!("; {}]", n), cache, depth) } else { - primitive_link(f, PrimitiveType::Array, &format!("; {}]", Escape(n)), cache) + primitive_link(f, PrimitiveType::Array, &format!("; {}]", Escape(n)), cache, depth) } } - clean::Never => primitive_link(f, PrimitiveType::Never, "!", cache), + clean::Never => primitive_link(f, PrimitiveType::Never, "!", cache, depth), clean::RawPointer(m, ref t) => { let m = match m { hir::Mutability::Mut => "mut", @@ -734,15 +741,17 @@ fn fmt_type( primitive_link( f, clean::PrimitiveType::RawPointer, - &format!("*{} {:#}", m, t.print(cache)), + &format!("*{} {:#}", m, t.print(cache, depth)), cache, + depth, ) } else { primitive_link( f, clean::PrimitiveType::RawPointer, - &format!("*{} {}", m, t.print(cache)), + &format!("*{} {}", m, t.print(cache, depth)), cache, + depth, ) } } @@ -752,8 +761,9 @@ fn fmt_type( clean::PrimitiveType::RawPointer, &format!("*{} ", m), cache, + depth, )?; - fmt::Display::fmt(&t.print(cache), f) + fmt::Display::fmt(&t.print(cache, depth), f) } } } @@ -773,15 +783,17 @@ fn fmt_type( primitive_link( f, PrimitiveType::Slice, - &format!("{}{}{}[{:#}]", amp, lt, m, bt.print(cache)), + &format!("{}{}{}[{:#}]", amp, lt, m, bt.print(cache, depth)), cache, + depth, ) } else { primitive_link( f, PrimitiveType::Slice, - &format!("{}{}{}[{}]", amp, lt, m, bt.print(cache)), + &format!("{}{}{}[{}]", amp, lt, m, bt.print(cache, depth)), cache, + depth, ) } } @@ -791,19 +803,20 @@ fn fmt_type( PrimitiveType::Slice, &format!("{}{}{}[", amp, lt, m), cache, + depth, )?; if f.alternate() { - write!(f, "{:#}", bt.print(cache))?; + write!(f, "{:#}", bt.print(cache, depth))?; } else { - write!(f, "{}", bt.print(cache))?; + write!(f, "{}", bt.print(cache, depth))?; } - primitive_link(f, PrimitiveType::Slice, "]", cache) + primitive_link(f, PrimitiveType::Slice, "]", cache, depth) } } } clean::ResolvedPath { param_names: Some(ref v), .. } if !v.is_empty() => { write!(f, "{}{}{}(", amp, lt, m)?; - fmt_type(&ty, f, use_absolute, cache)?; + fmt_type(&ty, f, use_absolute, cache, depth)?; write!(f, ")") } clean::Generic(..) => { @@ -812,20 +825,21 @@ fn fmt_type( PrimitiveType::Reference, &format!("{}{}{}", amp, lt, m), cache, + depth, )?; - fmt_type(&ty, f, use_absolute, cache) + fmt_type(&ty, f, use_absolute, cache, depth) } _ => { write!(f, "{}{}{}", amp, lt, m)?; - fmt_type(&ty, f, use_absolute, cache) + fmt_type(&ty, f, use_absolute, cache, depth) } } } clean::ImplTrait(ref bounds) => { if f.alternate() { - write!(f, "impl {:#}", print_generic_bounds(bounds, cache)) + write!(f, "impl {:#}", print_generic_bounds(bounds, cache, depth)) } else { - write!(f, "impl {}", print_generic_bounds(bounds, cache)) + write!(f, "impl {}", print_generic_bounds(bounds, cache, depth)) } } clean::QPath { ref name, ref self_type, ref trait_ } => { @@ -837,15 +851,25 @@ fn fmt_type( }; if f.alternate() { if should_show_cast { - write!(f, "<{:#} as {:#}>::", self_type.print(cache), trait_.print(cache))? + write!( + f, + "<{:#} as {:#}>::", + self_type.print(cache, depth), + trait_.print(cache, depth) + )? } else { - write!(f, "{:#}::", self_type.print(cache))? + write!(f, "{:#}::", self_type.print(cache, depth))? } } else { if should_show_cast { - write!(f, "<{} as {}>::", self_type.print(cache), trait_.print(cache))? + write!( + f, + "<{} as {}>::", + self_type.print(cache, depth), + trait_.print(cache, depth) + )? } else { - write!(f, "{}::", self_type.print(cache))? + write!(f, "{}::", self_type.print(cache, depth))? } }; match *trait_ { @@ -886,52 +910,57 @@ fn fmt_type( } impl clean::Type { - crate fn print<'b, 'a: 'b>(&'a self, cache: &'b Cache) -> impl fmt::Display + 'b { - display_fn(move |f| fmt_type(self, f, false, cache)) + crate fn print<'b, 'a: 'b>(&'a self, cache: &'b Cache, depth: usize) -> impl fmt::Display + 'b { + display_fn(move |f| fmt_type(self, f, false, cache, depth)) } } impl clean::Impl { - crate fn print<'a>(&'a self, cache: &'a Cache, use_absolute: bool) -> impl fmt::Display + 'a { + crate fn print<'a>( + &'a self, + cache: &'a Cache, + use_absolute: bool, + depth: usize, + ) -> impl fmt::Display + 'a { display_fn(move |f| { if f.alternate() { - write!(f, "impl{:#} ", self.generics.print(cache))?; + write!(f, "impl{:#} ", self.generics.print(cache, depth))?; } else { - write!(f, "impl{} ", self.generics.print(cache))?; + write!(f, "impl{} ", self.generics.print(cache, depth))?; } if let Some(ref ty) = self.trait_ { if self.negative_polarity { write!(f, "!")?; } - fmt::Display::fmt(&ty.print(cache), f)?; + fmt::Display::fmt(&ty.print(cache, depth), f)?; write!(f, " for ")?; } if let Some(ref ty) = self.blanket_impl { - fmt_type(ty, f, use_absolute, cache)?; + fmt_type(ty, f, use_absolute, cache, depth)?; } else { - fmt_type(&self.for_, f, use_absolute, cache)?; + fmt_type(&self.for_, f, use_absolute, cache, depth)?; } let where_clause = WhereClause { gens: &self.generics, indent: 0, end_newline: true }; - fmt::Display::fmt(&where_clause.print(cache), f)?; + fmt::Display::fmt(&where_clause.print(cache, depth), f)?; Ok(()) }) } } impl clean::Arguments { - crate fn print<'a>(&'a self, cache: &'a Cache) -> impl fmt::Display + 'a { + crate fn print<'a>(&'a self, cache: &'a Cache, depth: usize) -> impl fmt::Display + 'a { display_fn(move |f| { for (i, input) in self.values.iter().enumerate() { if !input.name.is_empty() { write!(f, "{}: ", input.name)?; } if f.alternate() { - write!(f, "{:#}", input.type_.print(cache))?; + write!(f, "{:#}", input.type_.print(cache, depth))?; } else { - write!(f, "{}", input.type_.print(cache))?; + write!(f, "{}", input.type_.print(cache, depth))?; } if i + 1 < self.values.len() { write!(f, ", ")?; @@ -943,21 +972,29 @@ impl clean::Arguments { } impl clean::FnRetTy { - crate fn print<'a>(&'a self, cache: &'a Cache) -> impl fmt::Display + 'a { + crate fn print<'a>(&'a self, cache: &'a Cache, depth: usize) -> impl fmt::Display + 'a { display_fn(move |f| match self { clean::Return(clean::Tuple(tys)) if tys.is_empty() => Ok(()), - clean::Return(ty) if f.alternate() => write!(f, " -> {:#}", ty.print(cache)), - clean::Return(ty) => write!(f, " -> {}", ty.print(cache)), + clean::Return(ty) if f.alternate() => write!(f, " -> {:#}", ty.print(cache, depth)), + clean::Return(ty) => write!(f, " -> {}", ty.print(cache, depth)), clean::DefaultReturn => Ok(()), }) } } impl clean::BareFunctionDecl { - fn print_hrtb_with_space<'a>(&'a self, cache: &'a Cache) -> impl fmt::Display + 'a { + fn print_hrtb_with_space<'a>( + &'a self, + cache: &'a Cache, + depth: usize, + ) -> impl fmt::Display + 'a { display_fn(move |f| { if !self.generic_params.is_empty() { - write!(f, "for<{}> ", comma_sep(self.generic_params.iter().map(|g| g.print(cache)))) + write!( + f, + "for<{}> ", + comma_sep(self.generic_params.iter().map(|g| g.print(cache, depth))) + ) } else { Ok(()) } @@ -966,24 +1003,24 @@ impl clean::BareFunctionDecl { } impl clean::FnDecl { - crate fn print<'a>(&'a self, cache: &'a Cache) -> impl fmt::Display + 'a { + crate fn print<'a>(&'a self, cache: &'a Cache, depth: usize) -> impl fmt::Display + 'a { display_fn(move |f| { let ellipsis = if self.c_variadic { ", ..." } else { "" }; if f.alternate() { write!( f, "({args:#}{ellipsis}){arrow:#}", - args = self.inputs.print(cache), + args = self.inputs.print(cache, depth), ellipsis = ellipsis, - arrow = self.output.print(cache) + arrow = self.output.print(cache, depth) ) } else { write!( f, "({args}{ellipsis}){arrow}", - args = self.inputs.print(cache), + args = self.inputs.print(cache, depth), ellipsis = ellipsis, - arrow = self.output.print(cache) + arrow = self.output.print(cache, depth) ) } }) @@ -991,7 +1028,7 @@ impl clean::FnDecl { } impl Function<'_> { - crate fn print<'b, 'a: 'b>(&'a self, cache: &'b Cache) -> impl fmt::Display + 'b { + crate fn print<'b, 'a: 'b>(&'a self, cache: &'b Cache, depth: usize) -> impl fmt::Display + 'b { display_fn(move |f| { let &Function { decl, header_len, indent, asyncness } = self; let amp = if f.alternate() { "&" } else { "&" }; @@ -1027,11 +1064,11 @@ impl Function<'_> { } clean::SelfExplicit(ref typ) => { if f.alternate() { - args.push_str(&format!("self: {:#}", typ.print(cache))); + args.push_str(&format!("self: {:#}", typ.print(cache, depth))); } else { - args.push_str(&format!("self: {}", typ.print(cache))); + args.push_str(&format!("self: {}", typ.print(cache, depth))); } - args_plain.push_str(&format!("self: {:#}", typ.print(cache))); + args_plain.push_str(&format!("self: {:#}", typ.print(cache, depth))); } } } else { @@ -1045,11 +1082,11 @@ impl Function<'_> { } if f.alternate() { - args.push_str(&format!("{:#}", input.type_.print(cache))); + args.push_str(&format!("{:#}", input.type_.print(cache, depth))); } else { - args.push_str(&input.type_.print(cache).to_string()); + args.push_str(&input.type_.print(cache, depth).to_string()); } - args_plain.push_str(&format!("{:#}", input.type_.print(cache))); + args_plain.push_str(&format!("{:#}", input.type_.print(cache, depth))); } if i + 1 < decl.inputs.values.len() { args.push(','); @@ -1070,11 +1107,11 @@ impl Function<'_> { Cow::Borrowed(&decl.output) }; - let arrow_plain = format!("{:#}", &output.print(cache)); + let arrow_plain = format!("{:#}", &output.print(cache, depth)); let arrow = if f.alternate() { - format!("{:#}", &output.print(cache)) + format!("{:#}", &output.print(cache, depth)) } else { - output.print(cache).to_string() + output.print(cache, depth).to_string() }; let declaration_len = header_len + args_plain.len() + arrow_plain.len(); @@ -1198,20 +1235,20 @@ impl PrintWithSpace for hir::Mutability { } impl clean::Import { - crate fn print<'b, 'a: 'b>(&'a self, cache: &'b Cache) -> impl fmt::Display + 'b { + crate fn print<'b, 'a: 'b>(&'a self, cache: &'b Cache, depth: usize) -> impl fmt::Display + 'b { display_fn(move |f| match self.kind { clean::ImportKind::Simple(name) => { if name == self.source.path.last() { - write!(f, "use {};", self.source.print(cache)) + write!(f, "use {};", self.source.print(cache, depth)) } else { - write!(f, "use {} as {};", self.source.print(cache), name) + write!(f, "use {} as {};", self.source.print(cache, depth), name) } } clean::ImportKind::Glob => { if self.source.path.segments.is_empty() { write!(f, "use *;") } else { - write!(f, "use {}::*;", self.source.print(cache)) + write!(f, "use {}::*;", self.source.print(cache, depth)) } } }) @@ -1219,16 +1256,16 @@ impl clean::Import { } impl clean::ImportSource { - crate fn print<'b, 'a: 'b>(&'a self, cache: &'b Cache) -> impl fmt::Display + 'b { + crate fn print<'b, 'a: 'b>(&'a self, cache: &'b Cache, depth: usize) -> impl fmt::Display + 'b { display_fn(move |f| match self.did { - Some(did) => resolved_path(f, did, &self.path, true, false, cache), + Some(did) => resolved_path(f, did, &self.path, true, false, cache, depth), _ => { for seg in &self.path.segments[..self.path.segments.len() - 1] { write!(f, "{}::", seg.name)?; } let name = self.path.last_name(); if let hir::def::Res::PrimTy(p) = self.path.res { - primitive_link(f, PrimitiveType::from(p), &*name, cache)?; + primitive_link(f, PrimitiveType::from(p), &*name, cache, depth)?; } else { write!(f, "{}", name)?; } @@ -1239,23 +1276,23 @@ impl clean::ImportSource { } impl clean::TypeBinding { - crate fn print<'b, 'a: 'b>(&'a self, cache: &'b Cache) -> impl fmt::Display + 'b { + crate fn print<'b, 'a: 'b>(&'a self, cache: &'b Cache, depth: usize) -> impl fmt::Display + 'b { display_fn(move |f| { f.write_str(&*self.name.as_str())?; match self.kind { clean::TypeBindingKind::Equality { ref ty } => { if f.alternate() { - write!(f, " = {:#}", ty.print(cache))?; + write!(f, " = {:#}", ty.print(cache, depth))?; } else { - write!(f, " = {}", ty.print(cache))?; + write!(f, " = {}", ty.print(cache, depth))?; } } clean::TypeBindingKind::Constraint { ref bounds } => { if !bounds.is_empty() { if f.alternate() { - write!(f, ": {:#}", print_generic_bounds(bounds, cache))?; + write!(f, ": {:#}", print_generic_bounds(bounds, cache, depth))?; } else { - write!(f, ": {}", print_generic_bounds(bounds, cache))?; + write!(f, ": {}", print_generic_bounds(bounds, cache, depth))?; } } } @@ -1280,10 +1317,10 @@ crate fn print_default_space<'a>(v: bool) -> &'a str { } impl clean::GenericArg { - crate fn print<'b, 'a: 'b>(&'a self, cache: &'b Cache) -> impl fmt::Display + 'b { + crate fn print<'b, 'a: 'b>(&'a self, cache: &'b Cache, depth: usize) -> impl fmt::Display + 'b { display_fn(move |f| match self { clean::GenericArg::Lifetime(lt) => fmt::Display::fmt(<.print(), f), - clean::GenericArg::Type(ty) => fmt::Display::fmt(&ty.print(cache), f), + clean::GenericArg::Type(ty) => fmt::Display::fmt(&ty.print(cache, depth), f), clean::GenericArg::Const(ct) => fmt::Display::fmt(&ct.print(), f), }) } diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 33c474bd0131b..120833f23e7a9 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -929,7 +929,7 @@ fn assoc_const( it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()), naive_assoc_href(it, link, cx.cache()), it.name.as_ref().unwrap(), - ty.print(cx.cache()) + ty.print(cx.cache(), cx.depth()) ); } @@ -941,6 +941,7 @@ fn assoc_type( link: AssocItemLink<'_>, extra: &str, cache: &Cache, + depth: usize, ) { write!( w, @@ -950,10 +951,10 @@ fn assoc_type( it.name.as_ref().unwrap() ); if !bounds.is_empty() { - write!(w, ": {}", print_generic_bounds(bounds, cache)) + write!(w, ": {}", print_generic_bounds(bounds, cache, depth)) } if let Some(default) = default { - write!(w, " = {}", default.print(cache)) + write!(w, " = {}", default.print(cache, depth)) } } @@ -1029,7 +1030,7 @@ fn render_assoc_item( print_default_space(meth.is_default()), print_abi_with_space(header.abi), name, - g.print(cx.cache()) + g.print(cx.cache(), cx.depth()) ) .len(); let (indent, end_newline) = if parent == ItemType::Trait { @@ -1052,11 +1053,12 @@ fn render_assoc_item( print_abi_with_space(header.abi), href = href, name = name, - generics = g.print(cx.cache()), + generics = g.print(cx.cache(), cx.depth()), decl = Function { decl: d, header_len, indent, asyncness: header.asyncness } - .print(cx.cache()), - spotlight = spotlight_decl(&d, cx.cache()), - where_clause = WhereClause { gens: g, indent, end_newline }.print(cx.cache()) + .print(cx.cache(), cx.depth()), + spotlight = spotlight_decl(&d, cx.cache(), cx.depth()), + where_clause = + WhereClause { gens: g, indent, end_newline }.print(cx.cache(), cx.depth()) ) } match *item.kind { @@ -1084,6 +1086,7 @@ fn render_assoc_item( link, if parent == ItemType::Trait { " " } else { "" }, cx.cache(), + cx.depth(), ), _ => panic!("render_assoc_item called on non-associated-item"), } @@ -1172,9 +1175,9 @@ fn render_assoc_items( AssocItemRender::DerefFor { trait_, type_, deref_mut_ } => { let id = cx.derive_id(small_url_encode(format!( "deref-methods-{:#}", - type_.print(cx.cache()) + type_.print(cx.cache(), cx.depth()) ))); - debug!("Adding {} to deref id map", type_.print(cx.cache())); + debug!("Adding {} to deref id map", type_.print(cx.cache(), cx.depth())); cx.deref_id_map .borrow_mut() .insert(type_.def_id_full(cx.cache()).unwrap(), id.clone()); @@ -1185,8 +1188,8 @@ fn render_assoc_items( \ ", id = id, - trait_ = trait_.print(cx.cache()), - type_ = type_.print(cx.cache()), + trait_ = trait_.print(cx.cache(), cx.depth()), + type_ = type_.print(cx.cache(), cx.depth()), ); RenderMode::ForDeref { mut_: deref_mut_ } } @@ -1338,7 +1341,7 @@ fn should_render_item(item: &clean::Item, deref_mut_: bool, cache: &Cache) -> bo } } -fn spotlight_decl(decl: &clean::FnDecl, cache: &Cache) -> String { +fn spotlight_decl(decl: &clean::FnDecl, cache: &Cache, depth: usize) -> String { let mut out = Buffer::html(); let mut trait_ = String::new(); @@ -1354,16 +1357,16 @@ fn spotlight_decl(decl: &clean::FnDecl, cache: &Cache) -> String { &mut out, "

Notable traits for {}

\ ", - impl_.for_.print(cache) + impl_.for_.print(cache, depth) ); - trait_.push_str(&impl_.for_.print(cache).to_string()); + trait_.push_str(&impl_.for_.print(cache, depth).to_string()); } //use the "where" class here to make it small write!( &mut out, "{}", - impl_.print(cache, false) + impl_.print(cache, false, depth) ); let t_did = impl_.trait_.def_id_full(cache).unwrap(); for it in &impl_.items { @@ -1377,6 +1380,7 @@ fn spotlight_decl(decl: &clean::FnDecl, cache: &Cache) -> String { AssocItemLink::GotoSource(t_did, &FxHashSet::default()), "", cache, + depth, ); out.push_str(";"); } @@ -1422,9 +1426,12 @@ fn render_impl( let id = cx.derive_id(match i.inner_impl().trait_ { Some(ref t) => { if is_on_foreign_type { - get_id_for_impl_on_foreign_type(&i.inner_impl().for_, t, cx.cache()) + get_id_for_impl_on_foreign_type(&i.inner_impl().for_, t, cx.cache(), cx.depth()) } else { - format!("impl-{}", small_url_encode(format!("{:#}", t.print(cx.cache())))) + format!( + "impl-{}", + small_url_encode(format!("{:#}", t.print(cx.cache(), cx.depth()))) + ) } } None => "impl".to_string(), @@ -1436,7 +1443,7 @@ fn render_impl( }; if let Some(use_absolute) = use_absolute { write!(w, "

", id, aliases); - write!(w, "{}", i.inner_impl().print(cx.cache(), use_absolute)); + write!(w, "{}", i.inner_impl().print(cx.cache(), use_absolute, cx.depth())); if show_def_docs { for it in &i.inner_impl().items { if let clean::TypedefItem(ref tydef, _) = *it.kind { @@ -1449,6 +1456,7 @@ fn render_impl( AssocItemLink::Anchor(None), "", cx.cache(), + cx.depth(), ); w.write_str(";"); } @@ -1461,7 +1469,7 @@ fn render_impl( "

{}", id, aliases, - i.inner_impl().print(cx.cache(), false) + i.inner_impl().print(cx.cache(), false, cx.depth()) ); } write!(w, "", id); @@ -1561,6 +1569,7 @@ fn render_impl( link.anchor(&id), "", cx.cache(), + cx.depth(), ); w.write_str("

"); } @@ -1582,7 +1591,16 @@ fn render_impl( clean::AssocTypeItem(ref bounds, ref default) => { let id = cx.derive_id(format!("{}.{}", item_type, name)); write!(w, "

", id, item_type, extra_class); - assoc_type(w, item, bounds, default.as_ref(), link.anchor(&id), "", cx.cache()); + assoc_type( + w, + item, + bounds, + default.as_ref(), + link.anchor(&id), + "", + cx.cache(), + cx.depth(), + ); w.write_str("

"); } clean::StrippedItem(..) => return, @@ -1925,9 +1943,10 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) { .iter() .filter_map(|it| { if let Some(ref i) = it.inner_impl().trait_ { - let i_display = format!("{:#}", i.print(cx.cache())); + let i_display = format!("{:#}", i.print(cx.cache(), cx.depth())); let out = Escape(&i_display); - let encoded = small_url_encode(format!("{:#}", i.print(cx.cache()))); + let encoded = + small_url_encode(format!("{:#}", i.print(cx.cache(), cx.depth()))); let generated = format!( "{}{}", encoded, @@ -2039,8 +2058,11 @@ fn sidebar_deref_methods(cx: &Context<'_>, out: &mut Buffer, impl_: &Impl, v: &V out, "Methods from {}<Target={}>", id, - Escape(&format!("{:#}", impl_.inner_impl().trait_.as_ref().unwrap().print(c))), - Escape(&format!("{:#}", real_target.print(c))), + Escape(&format!( + "{:#}", + impl_.inner_impl().trait_.as_ref().unwrap().print(c, cx.depth()) + )), + Escape(&format!("{:#}", real_target.print(c, cx.depth()))), ); // We want links' order to be reproducible so we don't use unstable sort. ret.sort(); @@ -2097,17 +2119,26 @@ fn get_id_for_impl_on_foreign_type( for_: &clean::Type, trait_: &clean::Type, cache: &Cache, + depth: usize, ) -> String { - small_url_encode(format!("impl-{:#}-for-{:#}", trait_.print(cache), for_.print(cache))) + small_url_encode(format!( + "impl-{:#}-for-{:#}", + trait_.print(cache, depth), + for_.print(cache, depth) + )) } -fn extract_for_impl_name(item: &clean::Item, cache: &Cache) -> Option<(String, String)> { +fn extract_for_impl_name( + item: &clean::Item, + cache: &Cache, + depth: usize, +) -> Option<(String, String)> { match *item.kind { clean::ItemKind::ImplItem(ref i) => { if let Some(ref trait_) = i.trait_ { Some(( - format!("{:#}", i.for_.print(cache)), - get_id_for_impl_on_foreign_type(&i.for_, trait_, cache), + format!("{:#}", i.for_.print(cache, depth)), + get_id_for_impl_on_foreign_type(&i.for_, trait_, cache, depth), )) } else { None @@ -2195,7 +2226,7 @@ fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean .def_id_full(cx.cache()) .map_or(false, |d| !cx.cache.paths.contains_key(&d)) }) - .filter_map(|i| extract_for_impl_name(&i.impl_item, cx.cache())) + .filter_map(|i| extract_for_impl_name(&i.impl_item, cx.cache(), cx.depth())) .collect::>(); if !res.is_empty() { diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 9650d0e322b45..030cf548ab002 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -266,7 +266,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl w, "{}{}", myitem.visibility.print_with_space(cx.tcx(), myitem.def_id, cx.cache()), - import.print(cx.cache()) + import.print(cx.cache(), cx.depth()) ); } @@ -373,7 +373,7 @@ fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean:: f.header.unsafety.print_with_space(), print_abi_with_space(f.header.abi), it.name.as_ref().unwrap(), - f.generics.print(cx.cache()) + f.generics.print(cx.cache(), cx.depth()) ) .len(); w.write_str("
");
@@ -388,18 +388,18 @@ fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::
         unsafety = f.header.unsafety.print_with_space(),
         abi = print_abi_with_space(f.header.abi),
         name = it.name.as_ref().unwrap(),
-        generics = f.generics.print(cx.cache()),
-        where_clause =
-            WhereClause { gens: &f.generics, indent: 0, end_newline: true }.print(cx.cache()),
+        generics = f.generics.print(cx.cache(), cx.depth()),
+        where_clause = WhereClause { gens: &f.generics, indent: 0, end_newline: true }
+            .print(cx.cache(), cx.depth()),
         decl = Function { decl: &f.decl, header_len, indent: 0, asyncness: f.header.asyncness }
-            .print(cx.cache()),
-        spotlight = spotlight_decl(&f.decl, cx.cache()),
+            .print(cx.cache(), cx.depth()),
+        spotlight = spotlight_decl(&f.decl, cx.cache(), cx.depth()),
     );
     document(w, cx, it, None)
 }
 
 fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Trait) {
-    let bounds = bounds(&t.bounds, false, cx.cache());
+    let bounds = bounds(&t.bounds, false, cx.cache(), cx.depth());
     let types = t.items.iter().filter(|m| m.is_associated_type()).collect::>();
     let consts = t.items.iter().filter(|m| m.is_associated_const()).collect::>();
     let required = t.items.iter().filter(|m| m.is_ty_method()).collect::>();
@@ -416,13 +416,13 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
             t.unsafety.print_with_space(),
             if t.is_auto { "auto " } else { "" },
             it.name.as_ref().unwrap(),
-            t.generics.print(cx.cache()),
+            t.generics.print(cx.cache(), cx.depth()),
             bounds
         );
 
         if !t.generics.where_predicates.is_empty() {
             let where_ = WhereClause { gens: &t.generics, indent: 0, end_newline: true };
-            write!(w, "{}", where_.print(cx.cache()));
+            write!(w, "{}", where_.print(cx.cache(), cx.depth()));
         } else {
             w.write_str(" ");
         }
@@ -595,8 +595,8 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
         let (mut synthetic, mut concrete): (Vec<&&Impl>, Vec<&&Impl>) =
             local.iter().partition(|i| i.inner_impl().synthetic);
 
-        synthetic.sort_by(|a, b| compare_impl(a, b, cx.cache()));
-        concrete.sort_by(|a, b| compare_impl(a, b, cx.cache()));
+        synthetic.sort_by(|a, b| compare_impl(a, b, cx.cache(), cx.depth()));
+        concrete.sort_by(|a, b| compare_impl(a, b, cx.cache(), cx.depth()));
 
         if !foreign.is_empty() {
             write_small_section_header(w, "foreign-impls", "Implementations on Foreign Types", "");
@@ -701,9 +701,10 @@ fn item_trait_alias(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clea
         w,
         "trait {}{}{} = {};
", it.name.as_ref().unwrap(), - t.generics.print(cx.cache()), - WhereClause { gens: &t.generics, indent: 0, end_newline: true }.print(cx.cache()), - bounds(&t.bounds, true, cx.cache()) + t.generics.print(cx.cache(), cx.depth()), + WhereClause { gens: &t.generics, indent: 0, end_newline: true } + .print(cx.cache(), cx.depth()), + bounds(&t.bounds, true, cx.cache(), cx.depth()) ); document(w, cx, it, None); @@ -722,10 +723,10 @@ fn item_opaque_ty(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean: w, "type {}{}{where_clause} = impl {bounds};", it.name.as_ref().unwrap(), - t.generics.print(cx.cache()), - where_clause = - WhereClause { gens: &t.generics, indent: 0, end_newline: true }.print(cx.cache()), - bounds = bounds(&t.bounds, false, cx.cache()) + t.generics.print(cx.cache(), cx.depth()), + where_clause = WhereClause { gens: &t.generics, indent: 0, end_newline: true } + .print(cx.cache(), cx.depth()), + bounds = bounds(&t.bounds, false, cx.cache(), cx.depth()) ); document(w, cx, it, None); @@ -744,10 +745,10 @@ fn item_typedef(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::T w, "type {}{}{where_clause} = {type_};", it.name.as_ref().unwrap(), - t.generics.print(cx.cache()), - where_clause = - WhereClause { gens: &t.generics, indent: 0, end_newline: true }.print(cx.cache()), - type_ = t.type_.print(cx.cache()) + t.generics.print(cx.cache(), cx.depth()), + where_clause = WhereClause { gens: &t.generics, indent: 0, end_newline: true } + .print(cx.cache(), cx.depth()), + type_ = t.type_.print(cx.cache(), cx.depth()) ); document(w, cx, it, None); @@ -794,7 +795,7 @@ fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Uni id = id, name = name, shortty = ItemType::StructField, - ty = ty.print(cx.cache()) + ty = ty.print(cx.cache(), cx.depth()) ); if let Some(stability_class) = field.stability_class(cx.tcx()) { write!(w, "", stab = stability_class); @@ -814,8 +815,9 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum "{}enum {}{}{}", it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()), it.name.as_ref().unwrap(), - e.generics.print(cx.cache()), - WhereClause { gens: &e.generics, indent: 0, end_newline: true }.print(cx.cache()) + e.generics.print(cx.cache(), cx.depth()), + WhereClause { gens: &e.generics, indent: 0, end_newline: true } + .print(cx.cache(), cx.depth()) ); if e.variants.is_empty() && !e.variants_stripped { w.write_str(" {}"); @@ -833,7 +835,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum if i > 0 { w.write_str(", ") } - write!(w, "{}", ty.print(cx.cache())); + write!(w, "{}", ty.print(cx.cache(), cx.depth())); } w.write_str(")"); } @@ -880,7 +882,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum if i > 0 { w.write_str(", "); } - write!(w, "{}", ty.print(cx.cache())); + write!(w, "{}", ty.print(cx.cache(), cx.depth())); } w.write_str(")"); } @@ -917,7 +919,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum ", id = id, f = field.name.as_ref().unwrap(), - t = ty.print(cx.cache()) + t = ty.print(cx.cache(), cx.depth()) ); document(w, cx, field, Some(variant)); } @@ -988,7 +990,7 @@ fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean:: "{vis}const {name}: {typ}", vis = it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()), name = it.name.as_ref().unwrap(), - typ = c.type_.print(cx.cache()), + typ = c.type_.print(cx.cache(), cx.depth()), ); if c.value.is_some() || c.is_literal { @@ -1055,7 +1057,7 @@ fn item_struct(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St item_type = ItemType::StructField, id = id, name = field.name.as_ref().unwrap(), - ty = ty.print(cx.cache()) + ty = ty.print(cx.cache(), cx.depth()) ); document(w, cx, field, Some(it)); } @@ -1073,7 +1075,7 @@ fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St vis = it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()), mutability = s.mutability.print_with_space(), name = it.name.as_ref().unwrap(), - typ = s.type_.print(cx.cache()) + typ = s.type_.print(cx.cache(), cx.depth()) ); document(w, cx, it, None) } @@ -1148,7 +1150,12 @@ pub(super) fn item_path(ty: ItemType, name: &str) -> String { } } -fn bounds(t_bounds: &[clean::GenericBound], trait_alias: bool, cache: &Cache) -> String { +fn bounds( + t_bounds: &[clean::GenericBound], + trait_alias: bool, + cache: &Cache, + depth: usize, +) -> String { let mut bounds = String::new(); if !t_bounds.is_empty() { if !trait_alias { @@ -1158,7 +1165,7 @@ fn bounds(t_bounds: &[clean::GenericBound], trait_alias: bool, cache: &Cache) -> if i > 0 { bounds.push_str(" + "); } - bounds.push_str(&p.print(cache).to_string()); + bounds.push_str(&p.print(cache, depth).to_string()); } } bounds @@ -1188,9 +1195,9 @@ fn render_stability_since( ) } -fn compare_impl<'a, 'b>(lhs: &'a &&Impl, rhs: &'b &&Impl, cache: &Cache) -> Ordering { - let lhs = format!("{}", lhs.inner_impl().print(cache, false)); - let rhs = format!("{}", rhs.inner_impl().print(cache, false)); +fn compare_impl<'a, 'b>(lhs: &'a &&Impl, rhs: &'b &&Impl, cache: &Cache, depth: usize) -> Ordering { + let lhs = format!("{}", lhs.inner_impl().print(cache, false, depth)); + let rhs = format!("{}", rhs.inner_impl().print(cache, false, depth)); // lhs and rhs are formatted as HTML, which may be unnecessary compare_names(&lhs, &rhs) @@ -1248,8 +1255,12 @@ fn render_union( it.name.as_ref().unwrap() ); if let Some(g) = g { - write!(w, "{}", g.print(cx.cache())); - write!(w, "{}", WhereClause { gens: g, indent: 0, end_newline: true }.print(cx.cache())); + write!(w, "{}", g.print(cx.cache(), cx.depth())); + write!( + w, + "{}", + WhereClause { gens: g, indent: 0, end_newline: true }.print(cx.cache(), cx.depth()) + ); } write!(w, " {{\n{}", tab); @@ -1260,7 +1271,7 @@ fn render_union( " {}{}: {},\n{}", field.visibility.print_with_space(cx.tcx(), field.def_id, cx.cache()), field.name.as_ref().unwrap(), - ty.print(cx.cache()), + ty.print(cx.cache(), cx.depth()), tab ); } @@ -1290,7 +1301,7 @@ fn render_struct( it.name.as_ref().unwrap() ); if let Some(g) = g { - write!(w, "{}", g.print(cx.cache())) + write!(w, "{}", g.print(cx.cache(), cx.depth())) } match ty { CtorKind::Fictive => { @@ -1298,7 +1309,8 @@ fn render_struct( write!( w, "{}", - WhereClause { gens: g, indent: 0, end_newline: true }.print(cx.cache()) + WhereClause { gens: g, indent: 0, end_newline: true } + .print(cx.cache(), cx.depth()) ) } let mut has_visible_fields = false; @@ -1311,7 +1323,7 @@ fn render_struct( tab, field.visibility.print_with_space(cx.tcx(), field.def_id, cx.cache()), field.name.as_ref().unwrap(), - ty.print(cx.cache()) + ty.print(cx.cache(), cx.depth()) ); has_visible_fields = true; } @@ -1342,7 +1354,7 @@ fn render_struct( w, "{}{}", field.visibility.print_with_space(cx.tcx(), field.def_id, cx.cache()), - ty.print(cx.cache()) + ty.print(cx.cache(), cx.depth()) ) } _ => unreachable!(), @@ -1353,7 +1365,8 @@ fn render_struct( write!( w, "{}", - WhereClause { gens: g, indent: 0, end_newline: false }.print(cx.cache()) + WhereClause { gens: g, indent: 0, end_newline: false } + .print(cx.cache(), cx.depth()) ) } w.write_str(";"); @@ -1364,7 +1377,8 @@ fn render_struct( write!( w, "{}", - WhereClause { gens: g, indent: 0, end_newline: false }.print(cx.cache()) + WhereClause { gens: g, indent: 0, end_newline: false } + .print(cx.cache(), cx.depth()) ) } w.write_str(";"); diff --git a/src/librustdoc/html/render/write_shared.rs b/src/librustdoc/html/render/write_shared.rs index cbf0f9a4927c6..d4b889c3a399b 100644 --- a/src/librustdoc/html/render/write_shared.rs +++ b/src/librustdoc/html/render/write_shared.rs @@ -471,7 +471,7 @@ themePicker.onblur = handleThemeButtonsBlur; None } else { Some(Implementor { - text: imp.inner_impl().print(cx.cache(), false).to_string(), + text: imp.inner_impl().print(cx.cache(), false, cx.depth()).to_string(), synthetic: imp.inner_impl().synthetic, types: collect_paths_for_type(imp.inner_impl().for_.clone(), cx.cache()), }) diff --git a/src/librustdoc/passes/calculate_doc_coverage.rs b/src/librustdoc/passes/calculate_doc_coverage.rs index 0e975128628ac..48ad10031f8c4 100644 --- a/src/librustdoc/passes/calculate_doc_coverage.rs +++ b/src/librustdoc/passes/calculate_doc_coverage.rs @@ -216,8 +216,8 @@ impl<'a, 'b> fold::DocFolder for CoverageCalculator<'a, 'b> { if let Some(ref tr) = impl_.trait_ { debug!( "impl {:#} for {:#} in {}", - tr.print(&self.ctx.cache), - impl_.for_.print(&self.ctx.cache), + tr.print(&self.ctx.cache, 0), + impl_.for_.print(&self.ctx.cache, 0), filename, ); @@ -228,7 +228,7 @@ impl<'a, 'b> fold::DocFolder for CoverageCalculator<'a, 'b> { // inherent impls *can* be documented, and those docs show up, but in most // cases it doesn't make sense, as all methods on a type are in one single // impl block - debug!("impl {:#} in {}", impl_.for_.print(&self.ctx.cache), filename); + debug!("impl {:#} in {}", impl_.for_.print(&self.ctx.cache, 0), filename); } } _ => { From 80a0dc25250b4e21679ca53f5a8b4bd6bb404179 Mon Sep 17 00:00:00 2001 From: Camelid Date: Wed, 3 Mar 2021 21:40:32 -0800 Subject: [PATCH 3/4] Remove last uses of `CURRENT_DEPTH` in `html/format.rs` --- src/librustdoc/clean/mod.rs | 6 ++- src/librustdoc/clean/types.rs | 2 +- src/librustdoc/html/format.rs | 24 +++++---- src/librustdoc/html/render/mod.rs | 29 +++++++---- src/librustdoc/html/render/print_item.rs | 64 +++++++++++++++++------- 5 files changed, 85 insertions(+), 40 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 29eb67f023f0b..c53482e264586 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2292,14 +2292,16 @@ impl Clean for (&hir::MacroDef<'_>, Option) { if matchers.len() <= 1 { format!( "{}macro {}{} {{\n ...\n}}", - vis.print_with_space(cx.tcx, def_id, &cx.cache), + // FIXME(camelid): this might create broken links! + vis.print_with_space(cx.tcx, def_id, &cx.cache, 0), name, matchers.iter().map(|span| span.to_src(cx)).collect::(), ) } else { format!( "{}macro {} {{\n{}}}", - vis.print_with_space(cx.tcx, def_id, &cx.cache), + // FIXME(camelid): this might create broken links! + vis.print_with_space(cx.tcx, def_id, &cx.cache, 0), name, matchers .iter() diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index bb0257ebc44e9..eae73bd16260f 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -855,7 +855,7 @@ impl Attributes { .filter_map(|ItemLink { link: s, link_text, did, fragment }| { match *did { Some(did) => { - if let Some((mut href, ..)) = href(did, cache) { + if let Some((mut href, ..)) = href(did, cache, depth) { if let Some(ref fragment) = *fragment { href.push('#'); href.push_str(fragment); diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 9e171afcc1f07..2ccc4479e4ea9 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -20,7 +20,6 @@ use crate::formats::cache::Cache; use crate::formats::item_type::ItemType; use crate::html::escape::Escape; use crate::html::render::cache::ExternalLocation; -use crate::html::render::CURRENT_DEPTH; crate trait Print { fn print(self, buffer: &mut Buffer); @@ -489,12 +488,11 @@ impl clean::Path { } } -crate fn href(did: DefId, cache: &Cache) -> Option<(String, ItemType, Vec)> { +crate fn href(did: DefId, cache: &Cache, depth: usize) -> Option<(String, ItemType, Vec)> { if !did.is_local() && !cache.access_levels.is_public(did) && !cache.document_private { return None; } - let depth = CURRENT_DEPTH.with(|l| l.get()); let (fqp, shortty, mut url) = match cache.paths.get(&did) { Some(&(ref fqp, shortty)) => (fqp, shortty, "../".repeat(depth)), None => { @@ -551,17 +549,17 @@ fn resolved_path( write!(w, "{}{:#}", &last.name, last.args.print(cache, depth))?; } else { let path = if use_absolute { - if let Some((_, _, fqp)) = href(did, cache) { + if let Some((_, _, fqp)) = href(did, cache, depth) { format!( "{}::{}", fqp[..fqp.len() - 1].join("::"), - anchor(did, fqp.last().unwrap(), cache) + anchor(did, fqp.last().unwrap(), cache, depth) ) } else { last.name.to_string() } } else { - anchor(did, &*last.name.as_str(), cache).to_string() + anchor(did, &*last.name.as_str(), cache, depth).to_string() }; write!(w, "{}{}", path, last.args.print(cache, depth))?; } @@ -633,9 +631,14 @@ fn tybounds<'a>( }) } -crate fn anchor<'a>(did: DefId, text: &'a str, cache: &'a Cache) -> impl fmt::Display + 'a { +crate fn anchor<'a>( + did: DefId, + text: &'a str, + cache: &'a Cache, + depth: usize, +) -> impl fmt::Display + 'a { display_fn(move |f| { - if let Some((url, short_ty, fqp)) = href(did, cache) { + if let Some((url, short_ty, fqp)) = href(did, cache, depth) { write!( f, r#"{}"#, @@ -884,7 +887,7 @@ fn fmt_type( // everything comes in as a fully resolved QPath (hard to // look at). box clean::ResolvedPath { did, ref param_names, .. } => { - match href(did, cache) { + match href(did, cache, depth) { Some((ref url, _, ref path)) if !f.alternate() => { write!( f, @@ -1143,6 +1146,7 @@ impl clean::Visibility { tcx: TyCtxt<'tcx>, item_did: DefId, cache: &Cache, + depth: usize, ) -> impl fmt::Display + 'tcx { use rustc_span::symbol::kw; @@ -1174,7 +1178,7 @@ impl clean::Visibility { path.data[0].data.get_opt_name().expect("modules are always named"); // modified from `resolved_path()` to work with `DefPathData` let last_name = path.data.last().unwrap().data.get_opt_name().unwrap(); - let anchor = anchor(vis_did, &last_name.as_str(), cache).to_string(); + let anchor = anchor(vis_did, &last_name.as_str(), cache, depth).to_string(); let mut s = "pub(".to_owned(); if path.data.len() != 1 diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 120833f23e7a9..b1b49616635b3 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -674,8 +674,10 @@ fn document_short( MarkdownSummaryLine(&s, &item.links(&cx.cache, cx.depth())).into_string(); if s.contains('\n') { - let link = - format!(r#" Read more"#, naive_assoc_href(item, link, cx.cache())); + let link = format!( + r#" Read more"#, + naive_assoc_href(item, link, cx.cache(), cx.depth()) + ); if let Some(idx) = summary_html.rfind("

") { summary_html.insert_str(idx, &link); @@ -894,7 +896,12 @@ fn render_impls( w.write_str(&impls.join("")); } -fn naive_assoc_href(it: &clean::Item, link: AssocItemLink<'_>, cache: &Cache) -> String { +fn naive_assoc_href( + it: &clean::Item, + link: AssocItemLink<'_>, + cache: &Cache, + depth: usize, +) -> String { use crate::formats::item_type::ItemType::*; let name = it.name.as_ref().unwrap(); @@ -908,7 +915,7 @@ fn naive_assoc_href(it: &clean::Item, link: AssocItemLink<'_>, cache: &Cache) -> AssocItemLink::Anchor(Some(ref id)) => format!("#{}", id), AssocItemLink::Anchor(None) => anchor, AssocItemLink::GotoSource(did, _) => { - href(did, cache).map(|p| format!("{}{}", p.0, anchor)).unwrap_or(anchor) + href(did, cache, depth).map(|p| format!("{}{}", p.0, anchor)).unwrap_or(anchor) } } } @@ -926,8 +933,8 @@ fn assoc_const( w, "{}{}const {}: {}", extra, - it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()), - naive_assoc_href(it, link, cx.cache()), + it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache(), cx.depth()), + naive_assoc_href(it, link, cx.cache(), cx.depth()), it.name.as_ref().unwrap(), ty.print(cx.cache(), cx.depth()) ); @@ -947,7 +954,7 @@ fn assoc_type( w, "{}type {}", extra, - naive_assoc_href(it, link, cache), + naive_assoc_href(it, link, cache, depth), it.name.as_ref().unwrap() ); if !bounds.is_empty() { @@ -1018,12 +1025,14 @@ fn render_assoc_item( ItemType::TyMethod }; - href(did, cx.cache()).map(|p| format!("{}#{}.{}", p.0, ty, name)).unwrap_or(anchor) + href(did, cx.cache(), cx.depth()) + .map(|p| format!("{}#{}.{}", p.0, ty, name)) + .unwrap_or(anchor) } }; let mut header_len = format!( "{}{}{}{}{}{:#}fn {}{:#}", - meth.visibility.print_with_space(cx.tcx(), meth.def_id, cx.cache()), + meth.visibility.print_with_space(cx.tcx(), meth.def_id, cx.cache(), cx.depth()), header.constness.print_with_space(), header.asyncness.print_with_space(), header.unsafety.print_with_space(), @@ -1045,7 +1054,7 @@ fn render_assoc_item( "{}{}{}{}{}{}{}fn {name}\ {generics}{decl}{spotlight}{where_clause}", if parent == ItemType::Trait { " " } else { "" }, - meth.visibility.print_with_space(cx.tcx(), meth.def_id, cx.cache()), + meth.visibility.print_with_space(cx.tcx(), meth.def_id, cx.cache(), cx.depth()), header.constness.print_with_space(), header.asyncness.print_with_space(), header.unsafety.print_with_space(), diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 030cf548ab002..d574bb04bc879 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -247,15 +247,30 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl Some(ref src) => write!( w, "{}extern crate {} as {};", - myitem.visibility.print_with_space(cx.tcx(), myitem.def_id, cx.cache()), - anchor(myitem.def_id, &*src.as_str(), cx.cache()), + myitem.visibility.print_with_space( + cx.tcx(), + myitem.def_id, + cx.cache(), + cx.depth(), + ), + anchor(myitem.def_id, &*src.as_str(), cx.cache(), cx.depth()), myitem.name.as_ref().unwrap(), ), None => write!( w, "{}extern crate {};", - myitem.visibility.print_with_space(cx.tcx(), myitem.def_id, cx.cache()), - anchor(myitem.def_id, &*myitem.name.as_ref().unwrap().as_str(), cx.cache()), + myitem.visibility.print_with_space( + cx.tcx(), + myitem.def_id, + cx.cache(), + cx.depth(), + ), + anchor( + myitem.def_id, + &*myitem.name.as_ref().unwrap().as_str(), + cx.cache(), + cx.depth(), + ), ), } w.write_str(""); @@ -265,7 +280,12 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl write!( w, "{}{}", - myitem.visibility.print_with_space(cx.tcx(), myitem.def_id, cx.cache()), + myitem.visibility.print_with_space( + cx.tcx(), + myitem.def_id, + cx.cache(), + cx.depth() + ), import.print(cx.cache(), cx.depth()) ); } @@ -367,7 +387,7 @@ fn extra_info_tags(item: &clean::Item, parent: &clean::Item, tcx: TyCtxt<'_>) -> fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::Function) { let header_len = format!( "{}{}{}{}{:#}fn {}{:#}", - it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()), + it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache(), cx.depth()), f.header.constness.print_with_space(), f.header.asyncness.print_with_space(), f.header.unsafety.print_with_space(), @@ -382,7 +402,7 @@ fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean:: w, "{vis}{constness}{asyncness}{unsafety}{abi}fn \ {name}{generics}{decl}{spotlight}{where_clause}", - vis = it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()), + vis = it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache(), cx.depth()), constness = f.header.constness.print_with_space(), asyncness = f.header.asyncness.print_with_space(), unsafety = f.header.unsafety.print_with_space(), @@ -412,7 +432,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra write!( w, "{}{}{}trait {}{}{}", - it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()), + it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache(), cx.depth()), t.unsafety.print_with_space(), if t.is_auto { "auto " } else { "" }, it.name.as_ref().unwrap(), @@ -813,7 +833,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum write!( w, "{}enum {}{}{}", - it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()), + it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache(), cx.depth()), it.name.as_ref().unwrap(), e.generics.print(cx.cache(), cx.depth()), WhereClause { gens: &e.generics, indent: 0, end_newline: true } @@ -988,7 +1008,7 @@ fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean:: write!( w, "{vis}const {name}: {typ}", - vis = it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()), + vis = it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache(), cx.depth()), name = it.name.as_ref().unwrap(), typ = c.type_.print(cx.cache(), cx.depth()), ); @@ -1072,7 +1092,7 @@ fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St write!( w, "{vis}static {mutability}{name}: {typ}", - vis = it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()), + vis = it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache(), cx.depth()), mutability = s.mutability.print_with_space(), name = it.name.as_ref().unwrap(), typ = s.type_.print(cx.cache(), cx.depth()) @@ -1086,7 +1106,7 @@ fn item_foreign_type(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) { write!( w, " {}type {};\n}}", - it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()), + it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache(), cx.depth()), it.name.as_ref().unwrap(), ); @@ -1250,7 +1270,7 @@ fn render_union( write!( w, "{}{}{}", - it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()), + it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache(), cx.depth()), if structhead { "union " } else { "" }, it.name.as_ref().unwrap() ); @@ -1269,7 +1289,7 @@ fn render_union( write!( w, " {}{}: {},\n{}", - field.visibility.print_with_space(cx.tcx(), field.def_id, cx.cache()), + field.visibility.print_with_space(cx.tcx(), field.def_id, cx.cache(), cx.depth()), field.name.as_ref().unwrap(), ty.print(cx.cache(), cx.depth()), tab @@ -1296,7 +1316,7 @@ fn render_struct( write!( w, "{}{}{}", - it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()), + it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache(), cx.depth()), if structhead { "struct " } else { "" }, it.name.as_ref().unwrap() ); @@ -1321,7 +1341,12 @@ fn render_struct( w, "\n{} {}{}: {},", tab, - field.visibility.print_with_space(cx.tcx(), field.def_id, cx.cache()), + field.visibility.print_with_space( + cx.tcx(), + field.def_id, + cx.cache(), + cx.depth() + ), field.name.as_ref().unwrap(), ty.print(cx.cache(), cx.depth()) ); @@ -1353,7 +1378,12 @@ fn render_struct( write!( w, "{}{}", - field.visibility.print_with_space(cx.tcx(), field.def_id, cx.cache()), + field.visibility.print_with_space( + cx.tcx(), + field.def_id, + cx.cache(), + cx.depth() + ), ty.print(cx.cache(), cx.depth()) ) } From 0e1ff40c5b700567de2bf1e17a3bb89a921e65cf Mon Sep 17 00:00:00 2001 From: Camelid Date: Wed, 3 Mar 2021 21:41:28 -0800 Subject: [PATCH 4/4] Remove `CURRENT_DEPTH` Hooray! --- src/librustdoc/html/render/context.rs | 10 +--------- src/librustdoc/html/render/mod.rs | 4 +--- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index fcb1f5cce12c2..475e633f77ab0 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -19,7 +19,7 @@ use super::print_item::{full_path, item_path, print_item}; use super::write_shared::write_shared; use super::{ print_sidebar, settings, AllTypes, NameDoc, SharedContext, StylePath, BASIC_KEYWORDS, - CURRENT_DEPTH, INITIAL_IDS, + INITIAL_IDS, }; use crate::clean::{self, AttributesExt}; @@ -106,12 +106,6 @@ impl<'tcx> Context<'tcx> { } fn render_item(&self, it: &clean::Item, pushname: bool) -> String { - // A little unfortunate that this is done like this, but it sure - // does make formatting *a lot* nicer. - CURRENT_DEPTH.with(|slot| { - slot.set(self.current.len()); - }); - let mut title = if it.is_primitive() || it.is_keyword() { // No need to include the namespace for primitive types and keywords String::new() @@ -422,8 +416,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { redirections: if generate_redirect_map { Some(Default::default()) } else { None }, }; - CURRENT_DEPTH.with(|s| s.set(0)); - // Write shared runs within a flock; disable thread dispatching of IO temporarily. Arc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(true); write_shared(&cx, &krate, index, &md_opts)?; diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index b1b49616635b3..e8257ad362151 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -34,7 +34,7 @@ mod write_shared; crate use context::*; -use std::cell::{Cell, RefCell}; +use std::cell::RefCell; use std::collections::VecDeque; use std::default::Default; use std::fmt; @@ -291,8 +291,6 @@ crate struct StylePath { crate disabled: bool, } -thread_local!(crate static CURRENT_DEPTH: Cell = Cell::new(0)); - crate const INITIAL_IDS: [&'static str; 15] = [ "main", "search",