Skip to content

Commit a874e8e

Browse files
Improve code
1 parent ca3f622 commit a874e8e

File tree

6 files changed

+55
-39
lines changed

6 files changed

+55
-39
lines changed

src/librustdoc/clean/inline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ pub(crate) fn item_relative_path(tcx: TyCtxt<'_>, def_id: DefId) -> Vec<Symbol>
227227
tcx.def_path(def_id).data.into_iter().filter_map(|elem| elem.data.get_opt_name()).collect()
228228
}
229229

230-
/// Get the path to an item in a URL sense: we use it to generate the URL to the actual item.
230+
/// Get the public Rust path to an item. This is used to generate the URL to the item's page.
231231
///
232232
/// In particular: we handle macro differently: if it's not a macro 2.0 oe a built-in macro, then
233233
/// it is generated at the top-level of the crate and its path will be `[crate_name, macro_name]`.

src/librustdoc/clean/types.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use crate::clean::utils::{is_literal_expr, print_evaluated_const};
4040
use crate::core::DocContext;
4141
use crate::formats::cache::Cache;
4242
use crate::formats::item_type::ItemType;
43+
use crate::html::format::HrefInfo;
4344
use crate::html::render::Context;
4445
use crate::passes::collect_intra_doc_links::UrlFragment;
4546

@@ -519,16 +520,16 @@ impl Item {
519520
.iter()
520521
.filter_map(|ItemLink { link: s, link_text, page_id: id, fragment }| {
521522
debug!(?id);
522-
if let Ok((mut href, ..)) = href(*id, cx) {
523-
debug!(?href);
523+
if let Ok(HrefInfo { mut url, .. }) = href(*id, cx) {
524+
debug!(?url);
524525
if let Some(ref fragment) = *fragment {
525-
fragment.render(&mut href, cx.tcx())
526+
fragment.render(&mut url, cx.tcx())
526527
}
527528
Some(RenderedLink {
528529
original_text: s.clone(),
529530
new_text: link_text.clone(),
530531
tooltip: link_tooltip(*id, fragment, cx).to_string(),
531-
href,
532+
href: url,
532533
})
533534
} else {
534535
None

src/librustdoc/display.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,18 @@ pub(crate) trait Joined: IntoIterator {
1010
///
1111
/// The performance of `joined` is slightly better than `format`, since it doesn't need to use a `Cell` to keep track of whether [`fmt`](Display::fmt)
1212
/// was already called (`joined`'s API doesn't allow it be called more than once).
13-
fn joined(self, sep: impl Display, f: &mut Formatter<'_>) -> fmt::Result;
13+
fn joined(&mut self, sep: impl Display, f: &mut Formatter<'_>) -> fmt::Result;
1414
}
1515

1616
impl<I, T> Joined for I
1717
where
18-
I: IntoIterator<Item = T>,
18+
I: Iterator<Item = T>,
1919
T: Display,
2020
{
21-
fn joined(self, sep: impl Display, f: &mut Formatter<'_>) -> fmt::Result {
22-
let mut iter = self.into_iter();
23-
let Some(first) = iter.next() else { return Ok(()) };
21+
fn joined(&mut self, sep: impl Display, f: &mut Formatter<'_>) -> fmt::Result {
22+
let Some(first) = self.next() else { return Ok(()) };
2423
first.fmt(f)?;
25-
for item in iter {
24+
for item in self {
2625
sep.fmt(f)?;
2726
item.fmt(f)?;
2827
}

src/librustdoc/html/format.rs

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -349,13 +349,23 @@ pub(crate) enum HrefError {
349349
UnnamableItem,
350350
}
351351

352+
/// Type representing information of an `href` attribute.
353+
pub(crate) struct HrefInfo {
354+
/// URL to the item page.
355+
pub(crate) url: String,
356+
/// Kind of the item (used to generate the `title` attribute).
357+
pub(crate) kind: ItemType,
358+
/// Rust path to the item (used to generate the `title` attribute).
359+
pub(crate) rust_path: Vec<Symbol>,
360+
}
361+
352362
/// This function is to get the external macro path because they are not in the cache used in
353363
/// `href_with_root_path`.
354364
fn generate_macro_def_id_path(
355365
def_id: DefId,
356366
cx: &Context<'_>,
357367
root_path: Option<&str>,
358-
) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
368+
) -> Result<HrefInfo, HrefError> {
359369
let tcx = cx.tcx();
360370
let crate_name = tcx.crate_name(def_id.krate);
361371
let cache = cx.cache();
@@ -384,9 +394,11 @@ fn generate_macro_def_id_path(
384394
return Err(HrefError::NotInExternalCache);
385395
}
386396

387-
let fqp = path.clone();
388-
if let Some(last) = path.last_mut() {
389-
*last = Symbol::intern(&format!("{}.{last}.html", item_type.as_str()));
397+
// FIXME: Try to use `iter().chain().once()` instead.
398+
let mut prev = None;
399+
if let Some(last) = path.pop() {
400+
path.push(Symbol::intern(&format!("{}.{last}.html", item_type.as_str())));
401+
prev = Some(last);
390402
}
391403

392404
let url = match cache.extern_locations[&def_id.krate] {
@@ -407,7 +419,11 @@ fn generate_macro_def_id_path(
407419
return Err(HrefError::NotInExternalCache);
408420
}
409421
};
410-
Ok((url, item_type, fqp))
422+
if let Some(prev) = prev {
423+
path.pop();
424+
path.push(prev);
425+
}
426+
Ok(HrefInfo { url, kind: item_type, rust_path: path })
411427
}
412428

413429
/// If the function succeeded, it will return the full URL to the item, its type and a `Vec`
@@ -418,7 +434,7 @@ fn generate_item_def_id_path(
418434
cx: &Context<'_>,
419435
root_path: Option<&str>,
420436
original_def_kind: DefKind,
421-
) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
437+
) -> Result<HrefInfo, HrefError> {
422438
use rustc_middle::traits::ObligationCause;
423439
use rustc_trait_selection::infer::TyCtxtInferExt;
424440
use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt;
@@ -454,7 +470,7 @@ fn generate_item_def_id_path(
454470
let kind = ItemType::from_def_kind(original_def_kind, Some(def_kind));
455471
url_parts = format!("{url_parts}#{kind}.{}", tcx.item_name(original_def_id))
456472
};
457-
Ok((url_parts, shortty, fqp))
473+
Ok(HrefInfo { url: url_parts, kind: shortty, rust_path: fqp })
458474
}
459475

460476
/// Checks if the given defid refers to an item that is unnamable, such as one defined in a const block.
@@ -529,7 +545,7 @@ pub(crate) fn href_with_root_path(
529545
original_did: DefId,
530546
cx: &Context<'_>,
531547
root_path: Option<&str>,
532-
) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
548+
) -> Result<HrefInfo, HrefError> {
533549
let tcx = cx.tcx();
534550
let def_kind = tcx.def_kind(original_did);
535551
let did = match def_kind {
@@ -595,14 +611,14 @@ pub(crate) fn href_with_root_path(
595611
}
596612
}
597613
};
598-
let url_parts = make_href(root_path, shortty, url_parts, fqp, is_remote);
599-
Ok((url_parts, shortty, fqp.clone()))
614+
Ok(HrefInfo {
615+
url: make_href(root_path, shortty, url_parts, fqp, is_remote),
616+
kind: shortty,
617+
rust_path: fqp.clone(),
618+
})
600619
}
601620

602-
pub(crate) fn href(
603-
did: DefId,
604-
cx: &Context<'_>,
605-
) -> Result<(String, ItemType, Vec<Symbol>), HrefError> {
621+
pub(crate) fn href(did: DefId, cx: &Context<'_>) -> Result<HrefInfo, HrefError> {
606622
href_with_root_path(did, cx, None)
607623
}
608624

@@ -689,12 +705,12 @@ fn resolved_path(
689705
} else {
690706
let path = fmt::from_fn(|f| {
691707
if use_absolute {
692-
if let Ok((_, _, fqp)) = href(did, cx) {
708+
if let Ok(HrefInfo { rust_path, .. }) = href(did, cx) {
693709
write!(
694710
f,
695711
"{path}::{anchor}",
696-
path = join_path_syms(&fqp[..fqp.len() - 1]),
697-
anchor = print_anchor(did, *fqp.last().unwrap(), cx)
712+
path = join_path_syms(&rust_path[..rust_path.len() - 1]),
713+
anchor = print_anchor(did, *rust_path.last().unwrap(), cx)
698714
)
699715
} else {
700716
write!(f, "{}", last.name)
@@ -823,12 +839,11 @@ fn print_higher_ranked_params_with_space(
823839

824840
pub(crate) fn print_anchor(did: DefId, text: Symbol, cx: &Context<'_>) -> impl Display {
825841
fmt::from_fn(move |f| {
826-
let parts = href(did, cx);
827-
if let Ok((url, short_ty, fqp)) = parts {
842+
if let Ok(HrefInfo { url, kind, rust_path }) = href(did, cx) {
828843
write!(
829844
f,
830-
r#"<a class="{short_ty}" href="{url}" title="{short_ty} {path}">{text}</a>"#,
831-
path = join_path_syms(fqp),
845+
r#"<a class="{kind}" href="{url}" title="{kind} {path}">{text}</a>"#,
846+
path = join_path_syms(rust_path),
832847
text = EscapeBodyText(text.as_str()),
833848
)
834849
} else {
@@ -1055,14 +1070,14 @@ fn print_qpath_data(qpath_data: &clean::QPathData, cx: &Context<'_>) -> impl Dis
10551070
None => self_type.def_id(cx.cache()).and_then(|did| href(did, cx).ok()),
10561071
};
10571072

1058-
if let Some((url, _, path)) = parent_href {
1073+
if let Some(HrefInfo { url, rust_path, .. }) = parent_href {
10591074
write!(
10601075
f,
10611076
"<a class=\"associatedtype\" href=\"{url}#{shortty}.{name}\" \
10621077
title=\"type {path}::{name}\">{name}</a>",
10631078
shortty = ItemType::AssocType,
10641079
name = assoc.name,
1065-
path = join_path_syms(path),
1080+
path = join_path_syms(rust_path),
10661081
)
10671082
} else {
10681083
write!(f, "{}", assoc.name)

src/librustdoc/html/highlight.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use super::format;
2121
use crate::clean::PrimitiveType;
2222
use crate::display::Joined as _;
2323
use crate::html::escape::EscapeBodyText;
24+
use crate::html::format::HrefInfo;
2425
use crate::html::macro_expansion::ExpandedCode;
2526
use crate::html::render::span_map::{DUMMY_SP, Span};
2627
use crate::html::render::{Context, LinkFromSrc};
@@ -1357,19 +1358,19 @@ fn generate_link_to_def(
13571358
LinkFromSrc::External(def_id) => {
13581359
format::href_with_root_path(*def_id, context, Some(href_context.root_path))
13591360
.ok()
1360-
.map(|(url, _, _)| url)
1361+
.map(|HrefInfo { url, .. }| url)
13611362
}
13621363
LinkFromSrc::Primitive(prim) => format::href_with_root_path(
13631364
PrimitiveType::primitive_locations(context.tcx())[prim],
13641365
context,
13651366
Some(href_context.root_path),
13661367
)
13671368
.ok()
1368-
.map(|(url, _, _)| url),
1369+
.map(|HrefInfo { url, .. }| url),
13691370
LinkFromSrc::Doc(def_id) => {
13701371
format::href_with_root_path(*def_id, context, Some(href_context.root_path))
13711372
.ok()
1372-
.map(|(doc_link, _, _)| doc_link)
1373+
.map(|HrefInfo { url, .. }| url)
13731374
}
13741375
}
13751376
})

src/librustdoc/html/render/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ use crate::formats::cache::Cache;
7474
use crate::formats::item_type::ItemType;
7575
use crate::html::escape::Escape;
7676
use crate::html::format::{
77-
Ending, HrefError, PrintWithSpace, full_print_fn_decl, href, print_abi_with_space,
77+
Ending, HrefError, HrefInfo, PrintWithSpace, full_print_fn_decl, href, print_abi_with_space,
7878
print_constness_with_space, print_default_space, print_generic_bounds, print_generics,
7979
print_impl, print_path, print_type, print_where_clause, visibility_print_with_space,
8080
};
@@ -982,7 +982,7 @@ fn assoc_href_attr(
982982
};
983983

984984
match href(did.expect_def_id(), cx) {
985-
Ok((url, ..)) => Href::Url(url, item_type),
985+
Ok(HrefInfo { url, .. }) => Href::Url(url, item_type),
986986
// The link is broken since it points to an external crate that wasn't documented.
987987
// Do not create any link in such case. This is better than falling back to a
988988
// dummy anchor like `#{item_type}.{name}` representing the `id` of *this* impl item

0 commit comments

Comments
 (0)