Auto merge of #84288 - notriddle:short-links, r=jyn514
rustdoc: get rid of CURRENT_DEPTH Fixes #82742
This commit is contained in:
commit
8108e17faa
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::iter;
|
||||||
|
|
||||||
use rustc_data_structures::captures::Captures;
|
use rustc_data_structures::captures::Captures;
|
||||||
use rustc_data_structures::fx::FxHashSet;
|
use rustc_data_structures::fx::FxHashSet;
|
||||||
@ -16,12 +17,10 @@ use rustc_span::def_id::{DefId, CRATE_DEF_INDEX};
|
|||||||
use rustc_target::spec::abi::Abi;
|
use rustc_target::spec::abi::Abi;
|
||||||
|
|
||||||
use crate::clean::{self, utils::find_nearest_parent_module, PrimitiveType};
|
use crate::clean::{self, utils::find_nearest_parent_module, PrimitiveType};
|
||||||
use crate::formats::cache::Cache;
|
|
||||||
use crate::formats::item_type::ItemType;
|
use crate::formats::item_type::ItemType;
|
||||||
use crate::html::escape::Escape;
|
use crate::html::escape::Escape;
|
||||||
use crate::html::render::cache::ExternalLocation;
|
use crate::html::render::cache::ExternalLocation;
|
||||||
use crate::html::render::Context;
|
use crate::html::render::Context;
|
||||||
use crate::html::render::CURRENT_DEPTH;
|
|
||||||
|
|
||||||
crate trait Print {
|
crate trait Print {
|
||||||
fn print(self, buffer: &mut Buffer);
|
fn print(self, buffer: &mut Buffer);
|
||||||
@ -497,7 +496,7 @@ crate fn href_relative_parts<'a>(fqp: &'a [String], relative_to_fqp: &'a [String
|
|||||||
if f != r {
|
if f != r {
|
||||||
let dissimilar_part_count = relative_to_fqp.len() - i;
|
let dissimilar_part_count = relative_to_fqp.len() - i;
|
||||||
let fqp_module = fqp[i..fqp.len()].iter().map(String::as_str);
|
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
|
// e.g. linking to std::sync::atomic from std::sync
|
||||||
@ -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
|
// e.g. linking to std::sync from std::sync::atomic
|
||||||
} else if fqp.len() < relative_to_fqp.len() {
|
} else if fqp.len() < relative_to_fqp.len() {
|
||||||
let dissimilar_part_count = relative_to_fqp.len() - 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
|
// linking to the same module
|
||||||
} else {
|
} else {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
@ -555,13 +554,14 @@ fn primitive_link(
|
|||||||
f: &mut fmt::Formatter<'_>,
|
f: &mut fmt::Formatter<'_>,
|
||||||
prim: clean::PrimitiveType,
|
prim: clean::PrimitiveType,
|
||||||
name: &str,
|
name: &str,
|
||||||
m: &Cache,
|
cx: &Context<'_>,
|
||||||
) -> fmt::Result {
|
) -> fmt::Result {
|
||||||
|
let m = &cx.cache();
|
||||||
let mut needs_termination = false;
|
let mut needs_termination = false;
|
||||||
if !f.alternate() {
|
if !f.alternate() {
|
||||||
match m.primitive_locations.get(&prim) {
|
match m.primitive_locations.get(&prim) {
|
||||||
Some(&def_id) if def_id.is_local() => {
|
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 };
|
let len = if len == 0 { 0 } else { len - 1 };
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
@ -572,20 +572,28 @@ fn primitive_link(
|
|||||||
needs_termination = true;
|
needs_termination = true;
|
||||||
}
|
}
|
||||||
Some(&def_id) => {
|
Some(&def_id) => {
|
||||||
|
let cname_str;
|
||||||
let loc = match m.extern_locations[&def_id.krate] {
|
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) => {
|
(ref cname, _, ExternalLocation::Local) => {
|
||||||
let len = CURRENT_DEPTH.with(|s| s.get());
|
cname_str = cname.as_str();
|
||||||
Some((cname, "../".repeat(len)))
|
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,
|
(.., ExternalLocation::Unknown) => None,
|
||||||
};
|
};
|
||||||
if let Some((cname, root)) = loc {
|
if let Some(loc) = loc {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"<a class=\"primitive\" href=\"{}{}/primitive.{}.html\">",
|
"<a class=\"primitive\" href=\"{}/primitive.{}.html\">",
|
||||||
root,
|
loc.join("/"),
|
||||||
cname,
|
|
||||||
prim.to_url_str()
|
prim.to_url_str()
|
||||||
)?;
|
)?;
|
||||||
needs_termination = true;
|
needs_termination = true;
|
||||||
@ -660,7 +668,7 @@ fn fmt_type<'cx>(
|
|||||||
fmt::Display::fmt(&tybounds(param_names, cx), f)
|
fmt::Display::fmt(&tybounds(param_names, cx), f)
|
||||||
}
|
}
|
||||||
clean::Infer => write!(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) => {
|
clean::BareFunction(ref decl) => {
|
||||||
if f.alternate() {
|
if f.alternate() {
|
||||||
write!(
|
write!(
|
||||||
@ -679,46 +687,46 @@ fn fmt_type<'cx>(
|
|||||||
decl.unsafety.print_with_space(),
|
decl.unsafety.print_with_space(),
|
||||||
print_abi_with_space(decl.abi)
|
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))
|
write!(f, "{}", decl.decl.print(cx))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
clean::Tuple(ref typs) => {
|
clean::Tuple(ref typs) => {
|
||||||
match &typs[..] {
|
match &typs[..] {
|
||||||
&[] => primitive_link(f, PrimitiveType::Unit, "()", &cx.cache()),
|
&[] => primitive_link(f, PrimitiveType::Unit, "()", cx),
|
||||||
&[ref one] => {
|
&[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.
|
// Carry `f.alternate()` into this display w/o branching manually.
|
||||||
fmt::Display::fmt(&one.print(cx), f)?;
|
fmt::Display::fmt(&one.print(cx), f)?;
|
||||||
primitive_link(f, PrimitiveType::Tuple, ",)", &cx.cache())
|
primitive_link(f, PrimitiveType::Tuple, ",)", cx)
|
||||||
}
|
}
|
||||||
many => {
|
many => {
|
||||||
primitive_link(f, PrimitiveType::Tuple, "(", &cx.cache())?;
|
primitive_link(f, PrimitiveType::Tuple, "(", cx)?;
|
||||||
for (i, item) in many.iter().enumerate() {
|
for (i, item) in many.iter().enumerate() {
|
||||||
if i != 0 {
|
if i != 0 {
|
||||||
write!(f, ", ")?;
|
write!(f, ", ")?;
|
||||||
}
|
}
|
||||||
fmt::Display::fmt(&item.print(cx), 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) => {
|
clean::Slice(ref t) => {
|
||||||
primitive_link(f, PrimitiveType::Slice, "[", &cx.cache())?;
|
primitive_link(f, PrimitiveType::Slice, "[", cx)?;
|
||||||
fmt::Display::fmt(&t.print(cx), f)?;
|
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) => {
|
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)?;
|
fmt::Display::fmt(&t.print(cx), f)?;
|
||||||
if f.alternate() {
|
if f.alternate() {
|
||||||
primitive_link(f, PrimitiveType::Array, &format!("; {}]", n), &cx.cache())
|
primitive_link(f, PrimitiveType::Array, &format!("; {}]", n), cx)
|
||||||
} else {
|
} 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) => {
|
clean::RawPointer(m, ref t) => {
|
||||||
let m = match m {
|
let m = match m {
|
||||||
hir::Mutability::Mut => "mut",
|
hir::Mutability::Mut => "mut",
|
||||||
@ -731,24 +739,19 @@ fn fmt_type<'cx>(
|
|||||||
f,
|
f,
|
||||||
clean::PrimitiveType::RawPointer,
|
clean::PrimitiveType::RawPointer,
|
||||||
&format!("*{} {:#}", m, t.print(cx)),
|
&format!("*{} {:#}", m, t.print(cx)),
|
||||||
&cx.cache(),
|
cx,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
primitive_link(
|
primitive_link(
|
||||||
f,
|
f,
|
||||||
clean::PrimitiveType::RawPointer,
|
clean::PrimitiveType::RawPointer,
|
||||||
&format!("*{} {}", m, t.print(cx)),
|
&format!("*{} {}", m, t.print(cx)),
|
||||||
&cx.cache(),
|
cx,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
primitive_link(
|
primitive_link(f, clean::PrimitiveType::RawPointer, &format!("*{} ", m), cx)?;
|
||||||
f,
|
|
||||||
clean::PrimitiveType::RawPointer,
|
|
||||||
&format!("*{} ", m),
|
|
||||||
&cx.cache(),
|
|
||||||
)?;
|
|
||||||
fmt::Display::fmt(&t.print(cx), f)
|
fmt::Display::fmt(&t.print(cx), f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -770,14 +773,14 @@ fn fmt_type<'cx>(
|
|||||||
f,
|
f,
|
||||||
PrimitiveType::Slice,
|
PrimitiveType::Slice,
|
||||||
&format!("{}{}{}[{:#}]", amp, lt, m, bt.print(cx)),
|
&format!("{}{}{}[{:#}]", amp, lt, m, bt.print(cx)),
|
||||||
&cx.cache(),
|
cx,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
primitive_link(
|
primitive_link(
|
||||||
f,
|
f,
|
||||||
PrimitiveType::Slice,
|
PrimitiveType::Slice,
|
||||||
&format!("{}{}{}[{}]", amp, lt, m, bt.print(cx)),
|
&format!("{}{}{}[{}]", amp, lt, m, bt.print(cx)),
|
||||||
&cx.cache(),
|
cx,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -786,14 +789,14 @@ fn fmt_type<'cx>(
|
|||||||
f,
|
f,
|
||||||
PrimitiveType::Slice,
|
PrimitiveType::Slice,
|
||||||
&format!("{}{}{}[", amp, lt, m),
|
&format!("{}{}{}[", amp, lt, m),
|
||||||
&cx.cache(),
|
cx,
|
||||||
)?;
|
)?;
|
||||||
if f.alternate() {
|
if f.alternate() {
|
||||||
write!(f, "{:#}", bt.print(cx))?;
|
write!(f, "{:#}", bt.print(cx))?;
|
||||||
} else {
|
} else {
|
||||||
write!(f, "{}", bt.print(cx))?;
|
write!(f, "{}", bt.print(cx))?;
|
||||||
}
|
}
|
||||||
primitive_link(f, PrimitiveType::Slice, "]", &cx.cache())
|
primitive_link(f, PrimitiveType::Slice, "]", cx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -807,7 +810,7 @@ fn fmt_type<'cx>(
|
|||||||
f,
|
f,
|
||||||
PrimitiveType::Reference,
|
PrimitiveType::Reference,
|
||||||
&format!("{}{}{}", amp, lt, m),
|
&format!("{}{}{}", amp, lt, m),
|
||||||
&cx.cache(),
|
cx,
|
||||||
)?;
|
)?;
|
||||||
fmt_type(&ty, f, use_absolute, cx)
|
fmt_type(&ty, f, use_absolute, cx)
|
||||||
}
|
}
|
||||||
@ -1292,7 +1295,7 @@ impl clean::ImportSource {
|
|||||||
}
|
}
|
||||||
let name = self.path.last_name();
|
let name = self.path.last_name();
|
||||||
if let hir::def::Res::PrimTy(p) = self.path.res {
|
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 {
|
} else {
|
||||||
write!(f, "{}", name)?;
|
write!(f, "{}", name)?;
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ use rustc_span::{symbol::sym, Symbol};
|
|||||||
use super::cache::{build_index, ExternalLocation};
|
use super::cache::{build_index, ExternalLocation};
|
||||||
use super::print_item::{full_path, item_path, print_item};
|
use super::print_item::{full_path, item_path, print_item};
|
||||||
use super::write_shared::write_shared;
|
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::clean::{self, AttributesExt};
|
||||||
use crate::config::RenderOptions;
|
use crate::config::RenderOptions;
|
||||||
@ -168,12 +168,6 @@ impl<'tcx> Context<'tcx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn render_item(&self, it: &clean::Item, pushname: bool) -> String {
|
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() {
|
let mut title = if it.is_primitive() || it.is_keyword() {
|
||||||
// No need to include the namespace for primitive types and keywords
|
// No need to include the namespace for primitive types and keywords
|
||||||
String::new()
|
String::new()
|
||||||
@ -482,8 +476,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
|
|||||||
cache: Rc::new(cache),
|
cache: Rc::new(cache),
|
||||||
};
|
};
|
||||||
|
|
||||||
CURRENT_DEPTH.with(|s| s.set(0));
|
|
||||||
|
|
||||||
// Write shared runs within a flock; disable thread dispatching of IO temporarily.
|
// Write shared runs within a flock; disable thread dispatching of IO temporarily.
|
||||||
Rc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(true);
|
Rc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(true);
|
||||||
write_shared(&cx, &krate, index, &md_opts)?;
|
write_shared(&cx, &krate, index, &md_opts)?;
|
||||||
|
@ -35,7 +35,6 @@ mod write_shared;
|
|||||||
crate use context::*;
|
crate use context::*;
|
||||||
crate use write_shared::FILES_UNVERSIONED;
|
crate use write_shared::FILES_UNVERSIONED;
|
||||||
|
|
||||||
use std::cell::Cell;
|
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
@ -209,8 +208,6 @@ crate struct StylePath {
|
|||||||
crate disabled: bool,
|
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) {
|
fn write_srclink(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer) {
|
||||||
if let Some(l) = cx.src_href(item) {
|
if let Some(l) = cx.src_href(item) {
|
||||||
write!(buf, "<a class=\"srclink\" href=\"{}\" title=\"goto source code\">[src]</a>", l)
|
write!(buf, "<a class=\"srclink\" href=\"{}\" title=\"goto source code\">[src]</a>", l)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user