Skip to content

rustdoc: get rid of CURRENT_DEPTH #84288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 43 additions & 40 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use std::cell::Cell;
use std::fmt;
use std::iter;

use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::FxHashSet;
Expand All @@ -16,12 +17,10 @@ use rustc_span::def_id::{DefId, CRATE_DEF_INDEX};
use rustc_target::spec::abi::Abi;

use crate::clean::{self, utils::find_nearest_parent_module, PrimitiveType};
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::Context;
use crate::html::render::CURRENT_DEPTH;

crate trait Print {
fn print(self, buffer: &mut Buffer);
Expand Down Expand Up @@ -497,7 +496,7 @@ crate fn href_relative_parts<'a>(fqp: &'a [String], relative_to_fqp: &'a [String
if f != r {
let dissimilar_part_count = relative_to_fqp.len() - i;
let fqp_module = fqp[i..fqp.len()].iter().map(String::as_str);
return std::iter::repeat("..").take(dissimilar_part_count).chain(fqp_module).collect();
return iter::repeat("..").take(dissimilar_part_count).chain(fqp_module).collect();
}
}
// e.g. linking to std::sync::atomic from std::sync
Expand All @@ -506,7 +505,7 @@ crate fn href_relative_parts<'a>(fqp: &'a [String], relative_to_fqp: &'a [String
// e.g. linking to std::sync from std::sync::atomic
} else if fqp.len() < relative_to_fqp.len() {
let dissimilar_part_count = relative_to_fqp.len() - fqp.len();
std::iter::repeat("..").take(dissimilar_part_count).collect()
iter::repeat("..").take(dissimilar_part_count).collect()
// linking to the same module
} else {
Vec::new()
Expand Down Expand Up @@ -555,13 +554,14 @@ fn primitive_link(
f: &mut fmt::Formatter<'_>,
prim: clean::PrimitiveType,
name: &str,
m: &Cache,
cx: &Context<'_>,
) -> fmt::Result {
let m = &cx.cache();
let mut needs_termination = false;
if !f.alternate() {
match m.primitive_locations.get(&prim) {
Some(&def_id) if def_id.is_local() => {
let len = CURRENT_DEPTH.with(|s| s.get());
let len = cx.current.len();
let len = if len == 0 { 0 } else { len - 1 };
write!(
f,
Expand All @@ -572,20 +572,28 @@ fn primitive_link(
needs_termination = true;
}
Some(&def_id) => {
let cname_str;
let loc = match m.extern_locations[&def_id.krate] {
(ref cname, _, ExternalLocation::Remote(ref s)) => Some((cname, s.to_string())),
(ref cname, _, ExternalLocation::Remote(ref s)) => {
cname_str = cname.as_str();
Some(vec![s.trim_end_matches('/'), &cname_str[..]])
}
(ref cname, _, ExternalLocation::Local) => {
let len = CURRENT_DEPTH.with(|s| s.get());
Some((cname, "../".repeat(len)))
cname_str = cname.as_str();
Some(if cx.current.first().map(|x| &x[..]) == Some(&cname_str[..]) {
iter::repeat("..").take(cx.current.len() - 1).collect()
} else {
let cname = iter::once(&cname_str[..]);
iter::repeat("..").take(cx.current.len()).chain(cname).collect()
})
}
(.., ExternalLocation::Unknown) => None,
};
if let Some((cname, root)) = loc {
if let Some(loc) = loc {
write!(
f,
"<a class=\"primitive\" href=\"{}{}/primitive.{}.html\">",
root,
cname,
"<a class=\"primitive\" href=\"{}/primitive.{}.html\">",
loc.join("/"),
prim.to_url_str()
)?;
needs_termination = true;
Expand Down Expand Up @@ -660,7 +668,7 @@ fn fmt_type<'cx>(
fmt::Display::fmt(&tybounds(param_names, cx), f)
}
clean::Infer => write!(f, "_"),
clean::Primitive(prim) => primitive_link(f, prim, prim.as_str(), &cx.cache()),
clean::Primitive(prim) => primitive_link(f, prim, prim.as_str(), cx),
clean::BareFunction(ref decl) => {
if f.alternate() {
write!(
Expand All @@ -679,46 +687,46 @@ fn fmt_type<'cx>(
decl.unsafety.print_with_space(),
print_abi_with_space(decl.abi)
)?;
primitive_link(f, PrimitiveType::Fn, "fn", &cx.cache())?;
primitive_link(f, PrimitiveType::Fn, "fn", cx)?;
write!(f, "{}", decl.decl.print(cx))
}
}
clean::Tuple(ref typs) => {
match &typs[..] {
&[] => primitive_link(f, PrimitiveType::Unit, "()", &cx.cache()),
&[] => primitive_link(f, PrimitiveType::Unit, "()", cx),
&[ref one] => {
primitive_link(f, PrimitiveType::Tuple, "(", &cx.cache())?;
primitive_link(f, PrimitiveType::Tuple, "(", cx)?;
// Carry `f.alternate()` into this display w/o branching manually.
fmt::Display::fmt(&one.print(cx), f)?;
primitive_link(f, PrimitiveType::Tuple, ",)", &cx.cache())
primitive_link(f, PrimitiveType::Tuple, ",)", cx)
}
many => {
primitive_link(f, PrimitiveType::Tuple, "(", &cx.cache())?;
primitive_link(f, PrimitiveType::Tuple, "(", cx)?;
for (i, item) in many.iter().enumerate() {
if i != 0 {
write!(f, ", ")?;
}
fmt::Display::fmt(&item.print(cx), f)?;
}
primitive_link(f, PrimitiveType::Tuple, ")", &cx.cache())
primitive_link(f, PrimitiveType::Tuple, ")", cx)
}
}
}
clean::Slice(ref t) => {
primitive_link(f, PrimitiveType::Slice, "[", &cx.cache())?;
primitive_link(f, PrimitiveType::Slice, "[", cx)?;
fmt::Display::fmt(&t.print(cx), f)?;
primitive_link(f, PrimitiveType::Slice, "]", &cx.cache())
primitive_link(f, PrimitiveType::Slice, "]", cx)
}
clean::Array(ref t, ref n) => {
primitive_link(f, PrimitiveType::Array, "[", &cx.cache())?;
primitive_link(f, PrimitiveType::Array, "[", cx)?;
fmt::Display::fmt(&t.print(cx), f)?;
if f.alternate() {
primitive_link(f, PrimitiveType::Array, &format!("; {}]", n), &cx.cache())
primitive_link(f, PrimitiveType::Array, &format!("; {}]", n), cx)
} else {
primitive_link(f, PrimitiveType::Array, &format!("; {}]", Escape(n)), &cx.cache())
primitive_link(f, PrimitiveType::Array, &format!("; {}]", Escape(n)), cx)
}
}
clean::Never => primitive_link(f, PrimitiveType::Never, "!", &cx.cache()),
clean::Never => primitive_link(f, PrimitiveType::Never, "!", cx),
clean::RawPointer(m, ref t) => {
let m = match m {
hir::Mutability::Mut => "mut",
Expand All @@ -731,24 +739,19 @@ fn fmt_type<'cx>(
f,
clean::PrimitiveType::RawPointer,
&format!("*{} {:#}", m, t.print(cx)),
&cx.cache(),
cx,
)
} else {
primitive_link(
f,
clean::PrimitiveType::RawPointer,
&format!("*{} {}", m, t.print(cx)),
&cx.cache(),
cx,
)
}
}
_ => {
primitive_link(
f,
clean::PrimitiveType::RawPointer,
&format!("*{} ", m),
&cx.cache(),
)?;
primitive_link(f, clean::PrimitiveType::RawPointer, &format!("*{} ", m), cx)?;
fmt::Display::fmt(&t.print(cx), f)
}
}
Expand All @@ -770,14 +773,14 @@ fn fmt_type<'cx>(
f,
PrimitiveType::Slice,
&format!("{}{}{}[{:#}]", amp, lt, m, bt.print(cx)),
&cx.cache(),
cx,
)
} else {
primitive_link(
f,
PrimitiveType::Slice,
&format!("{}{}{}[{}]", amp, lt, m, bt.print(cx)),
&cx.cache(),
cx,
)
}
}
Expand All @@ -786,14 +789,14 @@ fn fmt_type<'cx>(
f,
PrimitiveType::Slice,
&format!("{}{}{}[", amp, lt, m),
&cx.cache(),
cx,
)?;
if f.alternate() {
write!(f, "{:#}", bt.print(cx))?;
} else {
write!(f, "{}", bt.print(cx))?;
}
primitive_link(f, PrimitiveType::Slice, "]", &cx.cache())
primitive_link(f, PrimitiveType::Slice, "]", cx)
}
}
}
Expand All @@ -807,7 +810,7 @@ fn fmt_type<'cx>(
f,
PrimitiveType::Reference,
&format!("{}{}{}", amp, lt, m),
&cx.cache(),
cx,
)?;
fmt_type(&ty, f, use_absolute, cx)
}
Expand Down Expand Up @@ -1292,7 +1295,7 @@ impl clean::ImportSource {
}
let name = self.path.last_name();
if let hir::def::Res::PrimTy(p) = self.path.res {
primitive_link(f, PrimitiveType::from(p), &*name, &cx.cache())?;
primitive_link(f, PrimitiveType::from(p), &*name, cx)?;
} else {
write!(f, "{}", name)?;
}
Expand Down
10 changes: 1 addition & 9 deletions src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_span::{symbol::sym, Symbol};
use super::cache::{build_index, ExternalLocation};
use super::print_item::{full_path, item_path, print_item};
use super::write_shared::write_shared;
use super::{print_sidebar, settings, AllTypes, NameDoc, StylePath, BASIC_KEYWORDS, CURRENT_DEPTH};
use super::{print_sidebar, settings, AllTypes, NameDoc, StylePath, BASIC_KEYWORDS};

use crate::clean::{self, AttributesExt};
use crate::config::RenderOptions;
Expand Down Expand Up @@ -168,12 +168,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()
Expand Down Expand Up @@ -482,8 +476,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
cache: Rc::new(cache),
};

CURRENT_DEPTH.with(|s| s.set(0));

// Write shared runs within a flock; disable thread dispatching of IO temporarily.
Rc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(true);
write_shared(&cx, &krate, index, &md_opts)?;
Expand Down
3 changes: 0 additions & 3 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ mod write_shared;
crate use context::*;
crate use write_shared::FILES_UNVERSIONED;

use std::cell::Cell;
use std::collections::VecDeque;
use std::default::Default;
use std::fmt;
Expand Down Expand Up @@ -209,8 +208,6 @@ crate struct StylePath {
crate disabled: bool,
}

thread_local!(crate static CURRENT_DEPTH: Cell<usize> = Cell::new(0));

fn write_srclink(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer) {
if let Some(l) = cx.src_href(item) {
write!(buf, "<a class=\"srclink\" href=\"{}\" title=\"goto source code\">[src]</a>", l)
Expand Down