Skip to content

Commit 8b5f0cd

Browse files
Auto merge of #149936 - yotamofek:pr/rustdoc/join_path_syms_lazy, r=<try>
`join_path_syms_lazy`
2 parents dc47a69 + 258be67 commit 8b5f0cd

File tree

4 files changed

+34
-23
lines changed

4 files changed

+34
-23
lines changed

src/librustdoc/html/format.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use std::slice;
1414

1515
use itertools::{Either, Itertools};
1616
use rustc_abi::ExternAbi;
17-
use rustc_ast::join_path_syms;
1817
use rustc_data_structures::fx::FxHashSet;
1918
use rustc_hir as hir;
2019
use rustc_hir::def::{DefKind, MacroKinds};
@@ -37,6 +36,15 @@ use crate::html::escape::{Escape, EscapeBodyText};
3736
use crate::html::render::Context;
3837
use crate::passes::collect_intra_doc_links::UrlFragment;
3938

39+
pub(crate) fn join_path_syms_lazy(path: &[Symbol]) -> impl Display + '_ {
40+
fmt::from_fn(move |f| {
41+
path.iter()
42+
.copied()
43+
.map(|seg| Some(seg).filter(|seg| *seg != kw::PathRoot).maybe_display())
44+
.joined("::", f)
45+
})
46+
}
47+
4048
pub(crate) fn print_generic_bounds(
4149
bounds: &[clean::GenericBound],
4250
cx: &Context<'_>,
@@ -674,7 +682,7 @@ pub(crate) fn link_tooltip(
674682
write!(f, "{}", cx.tcx().item_name(id))?;
675683
} else if !fqp.is_empty() {
676684
write!(f, "{shortty} ")?;
677-
write!(f, "{}", join_path_syms(fqp))?;
685+
write!(f, "{}", join_path_syms_lazy(fqp))?;
678686
}
679687
Ok(())
680688
})
@@ -705,7 +713,7 @@ fn resolved_path(
705713
write!(
706714
f,
707715
"{path}::{anchor}",
708-
path = join_path_syms(&rust_path[..rust_path.len() - 1]),
716+
path = join_path_syms_lazy(&rust_path[..rust_path.len() - 1]),
709717
anchor = print_anchor(did, *rust_path.last().unwrap(), cx)
710718
)
711719
} else {
@@ -863,7 +871,7 @@ pub(crate) fn print_anchor(did: DefId, text: Symbol, cx: &Context<'_>) -> impl D
863871
f,
864872
r#"<a class="{kind}" href="{url}{anchor}" title="{kind} {path}">{text}</a>"#,
865873
anchor = fragment(did, cx.tcx()),
866-
path = join_path_syms(rust_path),
874+
path = join_path_syms_lazy(&rust_path),
867875
text = EscapeBodyText(text.as_str()),
868876
)
869877
} else {
@@ -1097,7 +1105,7 @@ fn print_qpath_data(qpath_data: &clean::QPathData, cx: &Context<'_>) -> impl Dis
10971105
title=\"type {path}::{name}\">{name}</a>",
10981106
shortty = ItemType::AssocType,
10991107
name = assoc.name,
1100-
path = join_path_syms(rust_path),
1108+
path = join_path_syms_lazy(&rust_path),
11011109
)
11021110
} else {
11031111
write!(f, "{}", assoc.name)

src/librustdoc/html/render/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::path::{Path, PathBuf};
66
use std::sync::mpsc::{Receiver, channel};
77

88
use askama::Template;
9-
use rustc_ast::join_path_syms;
109
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
1110
use rustc_hir::Attribute;
1211
use rustc_hir::attrs::AttributeKind;
@@ -30,6 +29,7 @@ use crate::formats::FormatRenderer;
3029
use crate::formats::cache::Cache;
3130
use crate::formats::item_type::ItemType;
3231
use crate::html::escape::Escape;
32+
use crate::html::format::join_path_syms_lazy;
3333
use crate::html::macro_expansion::ExpandedCode;
3434
use crate::html::markdown::{self, ErrorCodes, IdMap, plain_text_summary};
3535
use crate::html::render::span_map::Span;
@@ -228,7 +228,7 @@ impl<'tcx> Context<'tcx> {
228228
title.push_str(" in ");
229229
}
230230
// No need to include the namespace for primitive types and keywords
231-
title.push_str(&join_path_syms(&self.current));
231+
write!(title, "{}", join_path_syms_lazy(&self.current)).unwrap();
232232
};
233233
title.push_str(" - Rust");
234234
let tyname = it.type_();

src/librustdoc/html/render/print_item.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ use crate::formats::Impl;
3131
use crate::formats::item_type::ItemType;
3232
use crate::html::escape::{Escape, EscapeBodyTextWithWbr};
3333
use crate::html::format::{
34-
Ending, PrintWithSpace, full_print_fn_decl, print_abi_with_space, print_constness_with_space,
35-
print_generic_bound, print_generics, print_impl, print_import, print_type, print_where_clause,
36-
visibility_print_with_space,
34+
Ending, PrintWithSpace, full_print_fn_decl, join_path_syms_lazy, print_abi_with_space,
35+
print_constness_with_space, print_generic_bound, print_generics, print_impl, print_import,
36+
print_type, print_where_clause, visibility_print_with_space,
3737
};
3838
use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};
3939
use crate::html::render::sidebar::filters;
@@ -1451,7 +1451,7 @@ fn item_type_alias(cx: &Context<'_>, it: &clean::Item, t: &clean::TypeAlias) ->
14511451
iter::repeat_n("..", cx.current.len()).chain(iter::once("type.impl")).collect();
14521452
js_src_path.extend(target_fqp[..target_fqp.len() - 1].iter().copied());
14531453
js_src_path.push_fmt(format_args!("{target_type}.{}.js", target_fqp.last().unwrap()));
1454-
let self_path = join_path_syms(self_fqp);
1454+
let self_path = join_path_syms_lazy(self_fqp);
14551455
write!(
14561456
w,
14571457
"<script src=\"{src}\" data-self-path=\"{self_path}\" async></script>",

src/librustdoc/html/render/search_index.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use std::{io, iter};
1010
use ::serde::de::{self, Deserializer, Error as _};
1111
use ::serde::ser::{SerializeSeq, Serializer};
1212
use ::serde::{Deserialize, Serialize};
13-
use rustc_ast::join_path_syms;
1413
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
1514
use rustc_data_structures::thin_vec::ThinVec;
1615
use rustc_hir::attrs::AttributeKind;
@@ -28,6 +27,7 @@ use crate::config::ShouldMerge;
2827
use crate::error::Error;
2928
use crate::formats::cache::{Cache, OrphanImplItem};
3029
use crate::formats::item_type::ItemType;
30+
use crate::html::format::join_path_syms_lazy;
3131
use crate::html::markdown::short_markdown_summary;
3232
use crate::html::render::{self, IndexItem, IndexItemFunctionType, RenderType, RenderTypeId};
3333

@@ -971,24 +971,27 @@ struct PathData {
971971
exact_module_path: Option<Vec<Symbol>>,
972972
}
973973

974+
struct SerializedPath<'a>(&'a [Symbol]);
975+
976+
impl<'a> Serialize for SerializedPath<'a> {
977+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
978+
where
979+
S: Serializer,
980+
{
981+
serializer.collect_str(&join_path_syms_lazy(self.0))
982+
}
983+
}
984+
974985
impl Serialize for PathData {
975986
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
976987
where
977988
S: Serializer,
978989
{
979990
let mut seq = serializer.serialize_seq(None)?;
980991
seq.serialize_element(&self.ty)?;
981-
seq.serialize_element(&if self.module_path.is_empty() {
982-
String::new()
983-
} else {
984-
join_path_syms(&self.module_path)
985-
})?;
986-
if let Some(ref path) = self.exact_module_path {
987-
seq.serialize_element(&if path.is_empty() {
988-
String::new()
989-
} else {
990-
join_path_syms(path)
991-
})?;
992+
seq.serialize_element(&SerializedPath(&self.module_path))?;
993+
if let Some(path) = &self.exact_module_path {
994+
seq.serialize_element(&SerializedPath(path))?;
992995
}
993996
seq.end()
994997
}

0 commit comments

Comments
 (0)