Skip to content
Draft
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
18 changes: 13 additions & 5 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use std::slice;

use itertools::{Either, Itertools};
use rustc_abi::ExternAbi;
use rustc_ast::join_path_syms;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, MacroKinds};
Expand All @@ -37,6 +36,15 @@ use crate::html::escape::{Escape, EscapeBodyText};
use crate::html::render::Context;
use crate::passes::collect_intra_doc_links::UrlFragment;

pub(crate) fn join_path_syms_lazy(path: &[Symbol]) -> impl Display + '_ {
fmt::from_fn(move |f| {
path.iter()
.copied()
.map(|seg| Some(seg).filter(|seg| *seg != kw::PathRoot).maybe_display())
.joined("::", f)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using intersperse instead?

})
}

pub(crate) fn print_generic_bounds(
bounds: &[clean::GenericBound],
cx: &Context<'_>,
Expand Down Expand Up @@ -674,7 +682,7 @@ pub(crate) fn link_tooltip(
write!(f, "{}", cx.tcx().item_name(id))?;
} else if !fqp.is_empty() {
write!(f, "{shortty} ")?;
write!(f, "{}", join_path_syms(fqp))?;
write!(f, "{}", join_path_syms_lazy(fqp))?;
}
Ok(())
})
Expand Down Expand Up @@ -705,7 +713,7 @@ fn resolved_path(
write!(
f,
"{path}::{anchor}",
path = join_path_syms(&rust_path[..rust_path.len() - 1]),
path = join_path_syms_lazy(&rust_path[..rust_path.len() - 1]),
anchor = print_anchor(did, *rust_path.last().unwrap(), cx)
)
} else {
Expand Down Expand Up @@ -863,7 +871,7 @@ pub(crate) fn print_anchor(did: DefId, text: Symbol, cx: &Context<'_>) -> impl D
f,
r#"<a class="{kind}" href="{url}{anchor}" title="{kind} {path}">{text}</a>"#,
anchor = fragment(did, cx.tcx()),
path = join_path_syms(rust_path),
path = join_path_syms_lazy(&rust_path),
text = EscapeBodyText(text.as_str()),
)
} else {
Expand Down Expand Up @@ -1097,7 +1105,7 @@ fn print_qpath_data(qpath_data: &clean::QPathData, cx: &Context<'_>) -> impl Dis
title=\"type {path}::{name}\">{name}</a>",
shortty = ItemType::AssocType,
name = assoc.name,
path = join_path_syms(rust_path),
path = join_path_syms_lazy(&rust_path),
)
} else {
write!(f, "{}", assoc.name)
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::path::{Path, PathBuf};
use std::sync::mpsc::{Receiver, channel};

use askama::Template;
use rustc_ast::join_path_syms;
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
use rustc_hir::Attribute;
use rustc_hir::attrs::AttributeKind;
Expand All @@ -30,6 +29,7 @@ use crate::formats::FormatRenderer;
use crate::formats::cache::Cache;
use crate::formats::item_type::ItemType;
use crate::html::escape::Escape;
use crate::html::format::join_path_syms_lazy;
use crate::html::macro_expansion::ExpandedCode;
use crate::html::markdown::{self, ErrorCodes, IdMap, plain_text_summary};
use crate::html::render::span_map::Span;
Expand Down Expand Up @@ -228,7 +228,7 @@ impl<'tcx> Context<'tcx> {
title.push_str(" in ");
}
// No need to include the namespace for primitive types and keywords
title.push_str(&join_path_syms(&self.current));
write!(title, "{}", join_path_syms_lazy(&self.current)).unwrap();
};
title.push_str(" - Rust");
let tyname = it.type_();
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ use crate::formats::Impl;
use crate::formats::item_type::ItemType;
use crate::html::escape::{Escape, EscapeBodyTextWithWbr};
use crate::html::format::{
Ending, PrintWithSpace, full_print_fn_decl, print_abi_with_space, print_constness_with_space,
print_generic_bound, print_generics, print_impl, print_import, print_type, print_where_clause,
visibility_print_with_space,
Ending, PrintWithSpace, full_print_fn_decl, join_path_syms_lazy, print_abi_with_space,
print_constness_with_space, print_generic_bound, print_generics, print_impl, print_import,
print_type, print_where_clause, visibility_print_with_space,
};
use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};
use crate::html::render::sidebar::filters;
Expand Down Expand Up @@ -1451,7 +1451,7 @@ fn item_type_alias(cx: &Context<'_>, it: &clean::Item, t: &clean::TypeAlias) ->
iter::repeat_n("..", cx.current.len()).chain(iter::once("type.impl")).collect();
js_src_path.extend(target_fqp[..target_fqp.len() - 1].iter().copied());
js_src_path.push_fmt(format_args!("{target_type}.{}.js", target_fqp.last().unwrap()));
let self_path = join_path_syms(self_fqp);
let self_path = join_path_syms_lazy(self_fqp);
write!(
w,
"<script src=\"{src}\" data-self-path=\"{self_path}\" async></script>",
Expand Down
27 changes: 15 additions & 12 deletions src/librustdoc/html/render/search_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use std::{io, iter};
use ::serde::de::{self, Deserializer, Error as _};
use ::serde::ser::{SerializeSeq, Serializer};
use ::serde::{Deserialize, Serialize};
use rustc_ast::join_path_syms;
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
use rustc_data_structures::thin_vec::ThinVec;
use rustc_hir::attrs::AttributeKind;
Expand All @@ -28,6 +27,7 @@ use crate::config::ShouldMerge;
use crate::error::Error;
use crate::formats::cache::{Cache, OrphanImplItem};
use crate::formats::item_type::ItemType;
use crate::html::format::join_path_syms_lazy;
use crate::html::markdown::short_markdown_summary;
use crate::html::render::{self, IndexItem, IndexItemFunctionType, RenderType, RenderTypeId};

Expand Down Expand Up @@ -971,24 +971,27 @@ struct PathData {
exact_module_path: Option<Vec<Symbol>>,
}

struct SerializedPath<'a>(&'a [Symbol]);

impl<'a> Serialize for SerializedPath<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(&join_path_syms_lazy(self.0))
}
}

impl Serialize for PathData {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut seq = serializer.serialize_seq(None)?;
seq.serialize_element(&self.ty)?;
seq.serialize_element(&if self.module_path.is_empty() {
String::new()
} else {
join_path_syms(&self.module_path)
})?;
if let Some(ref path) = self.exact_module_path {
seq.serialize_element(&if path.is_empty() {
String::new()
} else {
join_path_syms(path)
})?;
seq.serialize_element(&SerializedPath(&self.module_path))?;
if let Some(path) = &self.exact_module_path {
seq.serialize_element(&SerializedPath(path))?;
}
seq.end()
}
Expand Down
Loading