Skip to content

Commit

Permalink
Rollup merge of #106855 - klensy:rd-s, r=notriddle
Browse files Browse the repository at this point in the history
rustdoc: few small cleanups
  • Loading branch information
matthiaskrgr authored Jan 14, 2023
2 parents 9db8e6d + 8998bd3 commit bc0c816
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 28 deletions.
9 changes: 5 additions & 4 deletions src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
}

// Index this method for searching later on.
if let Some(ref s) = item.name.or_else(|| {
if let Some(s) = item.name.or_else(|| {
if item.is_stripped() {
None
} else if let clean::ImportItem(ref i) = *item.kind &&
Expand Down Expand Up @@ -317,14 +317,15 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
short_markdown_summary(x.as_str(), &item.link_names(self.cache))
});
let ty = item.type_();
let name = s.to_string();
if ty != ItemType::StructField || u16::from_str_radix(&name, 10).is_err() {
if ty != ItemType::StructField
|| u16::from_str_radix(s.as_str(), 10).is_err()
{
// In case this is a field from a tuple struct, we don't add it into
// the search index because its name is something like "0", which is
// not useful for rustdoc search.
self.cache.search_index.push(IndexItem {
ty,
name,
name: s,
path: join_with_double_colon(path),
desc,
parent,
Expand Down
21 changes: 13 additions & 8 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ fn generate_macro_def_id_path(
root_path: Option<&str>,
) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
let tcx = cx.shared.tcx;
let crate_name = tcx.crate_name(def_id.krate).to_string();
let crate_name = tcx.crate_name(def_id.krate);
let cache = cx.cache();

let fqp: Vec<Symbol> = tcx
Expand All @@ -584,7 +584,7 @@ fn generate_macro_def_id_path(
}
})
.collect();
let mut relative = fqp.iter().map(|elem| elem.to_string());
let mut relative = fqp.iter().copied();
let cstore = CStore::from_tcx(tcx);
// We need this to prevent a `panic` when this function is used from intra doc links...
if !cstore.has_crate_data(def_id.krate) {
Expand All @@ -602,9 +602,9 @@ fn generate_macro_def_id_path(
};

let mut path = if is_macro_2 {
once(crate_name.clone()).chain(relative).collect()
once(crate_name).chain(relative).collect()
} else {
vec![crate_name.clone(), relative.next_back().unwrap()]
vec![crate_name, relative.next_back().unwrap()]
};
if path.len() < 2 {
// The minimum we can have is the crate name followed by the macro name. If shorter, then
Expand All @@ -614,17 +614,22 @@ fn generate_macro_def_id_path(
}

if let Some(last) = path.last_mut() {
*last = format!("macro.{}.html", last);
*last = Symbol::intern(&format!("macro.{}.html", last.as_str()));
}

let url = match cache.extern_locations[&def_id.krate] {
ExternalLocation::Remote(ref s) => {
// `ExternalLocation::Remote` always end with a `/`.
format!("{}{}", s, path.join("/"))
format!("{}{}", s, path.iter().map(|p| p.as_str()).join("/"))
}
ExternalLocation::Local => {
// `root_path` always end with a `/`.
format!("{}{}/{}", root_path.unwrap_or(""), crate_name, path.join("/"))
format!(
"{}{}/{}",
root_path.unwrap_or(""),
crate_name,
path.iter().map(|p| p.as_str()).join("/")
)
}
ExternalLocation::Unknown => {
debug!("crate {} not in cache when linkifying macros", crate_name);
Expand Down Expand Up @@ -1050,7 +1055,7 @@ fn fmt_type<'cx>(
_ => String::new(),
};
let m = mutability.print_with_space();
let amp = if f.alternate() { "&".to_string() } else { "&amp;".to_string() };
let amp = if f.alternate() { "&" } else { "&amp;" };
match **ty {
clean::DynTrait(ref bounds, ref trait_lt)
if bounds.len() > 1 || trait_lt.is_some() =>
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use rustc_hir::def_id::DefId;
use rustc_hir::HirId;
use rustc_middle::ty::TyCtxt;
use rustc_span::edition::Edition;
use rustc_span::Span;
use rustc_span::{Span, Symbol};

use once_cell::sync::Lazy;
use std::borrow::Cow;
Expand Down Expand Up @@ -198,7 +198,7 @@ fn slugify(c: char) -> Option<char> {

#[derive(Clone, Debug)]
pub struct Playground {
pub crate_name: Option<String>,
pub crate_name: Option<Symbol>,
pub url: String,
}

Expand Down Expand Up @@ -290,7 +290,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
.map(|l| map_line(l).for_code())
.intersperse("\n".into())
.collect::<String>();
let krate = krate.as_ref().map(|s| &**s);
let krate = krate.as_ref().map(|s| s.as_str());
let (test, _, _) =
doctest::make_test(&test, krate, false, &Default::default(), edition, None);
let channel = if test.contains("#![feature(") { "&amp;version=nightly" } else { "" };
Expand Down
5 changes: 2 additions & 3 deletions src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
// If user passed in `--playground-url` arg, we fill in crate name here
let mut playground = None;
if let Some(url) = playground_url {
playground =
Some(markdown::Playground { crate_name: Some(krate.name(tcx).to_string()), url });
playground = Some(markdown::Playground { crate_name: Some(krate.name(tcx)), url });
}
let mut layout = layout::Layout {
logo: String::new(),
Expand All @@ -491,7 +490,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
}
(sym::html_playground_url, Some(s)) => {
playground = Some(markdown::Playground {
crate_name: Some(krate.name(tcx).to_string()),
crate_name: Some(krate.name(tcx)),
url: s.to_string(),
});
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub(crate) fn ensure_trailing_slash(v: &str) -> impl fmt::Display + '_ {
#[derive(Debug)]
pub(crate) struct IndexItem {
pub(crate) ty: ItemType,
pub(crate) name: String,
pub(crate) name: Symbol,
pub(crate) path: String,
pub(crate) desc: String,
pub(crate) parent: Option<DefId>,
Expand Down Expand Up @@ -2769,8 +2769,8 @@ fn collect_paths_for_type(first_ty: clean::Type, cache: &Cache) -> Vec<String> {
let mut work = VecDeque::new();

let mut process_path = |did: DefId| {
let get_extern = || cache.external_paths.get(&did).map(|s| s.0.clone());
let fqp = cache.exact_paths.get(&did).cloned().or_else(get_extern);
let get_extern = || cache.external_paths.get(&did).map(|s| &s.0);
let fqp = cache.exact_paths.get(&did).or_else(get_extern);

if let Some(path) = fqp {
out.push(join_with_double_colon(&path));
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,8 +1027,8 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
.chain(std::iter::once("implementors"))
.collect();
if let Some(did) = it.item_id.as_def_id() &&
let get_extern = { || cache.external_paths.get(&did).map(|s| s.0.clone()) } &&
let Some(fqp) = cache.exact_paths.get(&did).cloned().or_else(get_extern) {
let get_extern = { || cache.external_paths.get(&did).map(|s| &s.0) } &&
let Some(fqp) = cache.exact_paths.get(&did).or_else(get_extern) {
js_src_path.extend(fqp[..fqp.len() - 1].iter().copied());
js_src_path.push_fmt(format_args!("{}.{}.js", it.type_(), fqp.last().unwrap()));
} else {
Expand Down
10 changes: 5 additions & 5 deletions src/librustdoc/html/render/search_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub(crate) fn build_index<'tcx>(
.map_or_else(String::new, |s| short_markdown_summary(&s, &item.link_names(cache)));
cache.search_index.push(IndexItem {
ty: item.type_(),
name: item.name.unwrap().to_string(),
name: item.name.unwrap(),
path: join_with_double_colon(&fqp[..fqp.len() - 1]),
desc,
parent: Some(parent),
Expand All @@ -58,8 +58,8 @@ pub(crate) fn build_index<'tcx>(
// Sort search index items. This improves the compressibility of the search index.
cache.search_index.sort_unstable_by(|k1, k2| {
// `sort_unstable_by_key` produces lifetime errors
let k1 = (&k1.path, &k1.name, &k1.ty, &k1.parent);
let k2 = (&k2.path, &k2.name, &k2.ty, &k2.parent);
let k1 = (&k1.path, k1.name.as_str(), &k1.ty, &k1.parent);
let k2 = (&k2.path, k2.name.as_str(), &k2.ty, &k2.parent);
std::cmp::Ord::cmp(&k1, &k2)
});

Expand Down Expand Up @@ -240,7 +240,7 @@ pub(crate) fn build_index<'tcx>(
)?;
crate_data.serialize_field(
"n",
&self.items.iter().map(|item| &item.name).collect::<Vec<_>>(),
&self.items.iter().map(|item| item.name.as_str()).collect::<Vec<_>>(),
)?;
crate_data.serialize_field(
"q",
Expand Down Expand Up @@ -299,7 +299,7 @@ pub(crate) fn build_index<'tcx>(
)?;
crate_data.serialize_field(
"p",
&self.paths.iter().map(|(it, s)| (it, s.to_string())).collect::<Vec<_>>(),
&self.paths.iter().map(|(it, s)| (it, s.as_str())).collect::<Vec<_>>(),
)?;
if has_aliases {
crate_data.serialize_field("a", &self.aliases)?;
Expand Down

0 comments on commit bc0c816

Please sign in to comment.