Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

librustdoc: more usages of Joined::joined #136599

Merged
merged 3 commits into from
Feb 19, 2025
Merged
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
6 changes: 3 additions & 3 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1150,9 +1150,9 @@ fn clean_args_from_types_and_body_id<'tcx>(
Arguments {
values: types
.iter()
.enumerate()
.map(|(i, ty)| Argument {
name: name_from_pat(body.params[i].pat),
.zip(body.params)
.map(|(ty, param)| Argument {
name: name_from_pat(param.pat),
type_: clean_ty(ty, cx),
is_const: false,
})
Expand Down
71 changes: 45 additions & 26 deletions src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::assert_matches::debug_assert_matches;
use std::fmt::Write as _;
use std::fmt::{self, Display, Write as _};
use std::mem;
use std::sync::LazyLock as Lazy;

Expand All @@ -24,6 +24,7 @@ use crate::clean::{
clean_middle_ty, inline,
};
use crate::core::DocContext;
use crate::display::Joined as _;

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -250,16 +251,20 @@ pub(crate) fn qpath_to_string(p: &hir::QPath<'_>) -> String {
hir::QPath::LangItem(lang_item, ..) => return lang_item.name().to_string(),
};

let mut s = String::new();
for (i, seg) in segments.iter().enumerate() {
if i > 0 {
s.push_str("::");
}
if seg.ident.name != kw::PathRoot {
s.push_str(seg.ident.as_str());
}
}
s
fmt::from_fn(|f| {
segments
.iter()
.map(|seg| {
fmt::from_fn(|f| {
if seg.ident.name != kw::PathRoot {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The logic here is weird (pre-existing).
The kw::PathRoot should only ever be the first segment, I think. And we wouldn't want to join two "::"s together anyways.

write!(f, "{}", seg.ident)?;
}
Ok(())
})
})
.joined("::", f)
})
.to_string()
}

pub(crate) fn build_deref_target_impls(
Expand Down Expand Up @@ -299,35 +304,49 @@ pub(crate) fn name_from_pat(p: &hir::Pat<'_>) -> Symbol {

Symbol::intern(&match p.kind {
// FIXME(never_patterns): does this make sense?
PatKind::Wild | PatKind::Err(_) | PatKind::Never | PatKind::Struct(..) => {
PatKind::Wild
| PatKind::Err(_)
| PatKind::Never
| PatKind::Struct(..)
| PatKind::Range(..) => {
return kw::Underscore;
}
PatKind::Binding(_, _, ident, _) => return ident.name,
PatKind::Box(p) | PatKind::Ref(p, _) | PatKind::Guard(p, _) => return name_from_pat(p),
PatKind::TupleStruct(ref p, ..)
| PatKind::Expr(PatExpr { kind: PatExprKind::Path(ref p), .. }) => qpath_to_string(p),
PatKind::Or(pats) => {
pats.iter().map(|p| name_from_pat(p).to_string()).collect::<Vec<String>>().join(" | ")
fmt::from_fn(|f| pats.iter().map(|p| name_from_pat(p)).joined(" | ", f)).to_string()
}
PatKind::Tuple(elts, _) => {
format!("({})", fmt::from_fn(|f| elts.iter().map(|p| name_from_pat(p)).joined(", ", f)))
}
PatKind::Tuple(elts, _) => format!(
"({})",
elts.iter().map(|p| name_from_pat(p).to_string()).collect::<Vec<String>>().join(", ")
),
PatKind::Box(p) => return name_from_pat(p),
PatKind::Deref(p) => format!("deref!({})", name_from_pat(p)),
PatKind::Ref(p, _) => return name_from_pat(p),
PatKind::Expr(..) => {
warn!(
"tried to get argument name from PatKind::Expr, which is silly in function arguments"
);
return Symbol::intern("()");
}
PatKind::Guard(p, _) => return name_from_pat(p),
PatKind::Range(..) => return kw::Underscore,
PatKind::Slice(begin, ref mid, end) => {
let begin = begin.iter().map(|p| name_from_pat(p).to_string());
let mid = mid.as_ref().map(|p| format!("..{}", name_from_pat(p))).into_iter();
let end = end.iter().map(|p| name_from_pat(p).to_string());
format!("[{}]", begin.chain(mid).chain(end).collect::<Vec<_>>().join(", "))
PatKind::Slice(begin, mid, end) => {
fn print_pat<'a>(pat: &'a Pat<'a>, wild: bool) -> impl Display + 'a {
fmt::from_fn(move |f| {
if wild {
f.write_str("..")?;
}
name_from_pat(pat).fmt(f)
})
}

format!(
"[{}]",
fmt::from_fn(|f| {
let begin = begin.iter().map(|p| print_pat(p, false));
let mid = mid.map(|p| print_pat(p, true));
let end = end.iter().map(|p| print_pat(p, false));
begin.chain(mid).chain(end).joined(", ", f)
})
)
}
})
}
Expand Down
16 changes: 10 additions & 6 deletions src/librustdoc/doctest/make.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Logic for transforming the raw code given by the user into something actually
//! runnable, e.g. by adding a `main` function if it doesn't already exist.

use std::fmt::{self, Write as _};
use std::io;
use std::sync::Arc;

Expand All @@ -17,6 +18,7 @@ use rustc_span::symbol::sym;
use tracing::debug;

use super::GlobalTestOptions;
use crate::display::Joined as _;
use crate::html::markdown::LangString;

/// This struct contains information about the doctest itself which is then used to generate
Expand Down Expand Up @@ -232,13 +234,15 @@ impl DocTestBuilder {

// add extra 4 spaces for each line to offset the code block
if opts.insert_indent_space {
prog.push_str(
&everything_else
write!(
prog,
"{}",
fmt::from_fn(|f| everything_else
.lines()
.map(|line| format!(" {}", line))
.collect::<Vec<String>>()
.join("\n"),
);
.map(|line| fmt::from_fn(move |f| write!(f, " {line}")))
.joined("\n", f))
)
.unwrap();
} else {
prog.push_str(everything_else);
};
Expand Down
28 changes: 12 additions & 16 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::cmp::Ordering;
use std::fmt;
use std::fmt::Display;

use itertools::Itertools;
use rinja::Template;
use rustc_abi::VariantIdx;
use rustc_data_structures::captures::Captures;
Expand Down Expand Up @@ -514,11 +513,7 @@ fn item_module(w: &mut String, cx: &Context<'_>, item: &clean::Item, items: &[cl
class = myitem.type_(),
unsafety_flag = unsafety_flag,
href = item_path(myitem.type_(), myitem.name.unwrap().as_str()),
title = [myitem.type_().to_string(), full_path(cx, myitem)]
.iter()
.filter_map(|s| if !s.is_empty() { Some(s.as_str()) } else { None })
.collect::<Vec<_>>()
.join(" "),
title = format_args!("{} {}", myitem.type_(), full_path(cx, myitem)),
),
);
}
Expand Down Expand Up @@ -915,7 +910,7 @@ fn item_trait(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
w,
format_args!(
"<div class=\"stab must_implement\">At least one of the `{}` methods is required.</div>",
list.iter().join("`, `")
fmt::from_fn(|f| list.iter().joined("`, `", f))
),
);
}
Expand Down Expand Up @@ -1168,17 +1163,18 @@ fn item_trait(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
js_src_path.extend(cx.current.iter().copied());
js_src_path.push_fmt(format_args!("{}.{}.js", it.type_(), it.name.unwrap()));
}
let extern_crates = extern_crates
.into_iter()
.map(|cnum| tcx.crate_name(cnum).to_string())
.collect::<Vec<_>>()
.join(",");
let (extern_before, extern_after) =
if extern_crates.is_empty() { ("", "") } else { (" data-ignore-extern-crates=\"", "\"") };
let extern_crates = fmt::from_fn(|f| {
if !extern_crates.is_empty() {
f.write_str(" data-ignore-extern-crates=\"")?;
extern_crates.iter().map(|&cnum| tcx.crate_name(cnum)).joined(",", f)?;
f.write_str("\"")?;
}
Ok(())
});
write_str(
w,
format_args!(
"<script src=\"{src}\"{extern_before}{extern_crates}{extern_after} async></script>",
"<script src=\"{src}\"{extern_crates} async></script>",
src = js_src_path.finish()
),
);
Expand Down Expand Up @@ -1400,7 +1396,7 @@ fn item_type_alias(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean
.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 = self_fqp.iter().map(Symbol::as_str).collect::<Vec<&str>>().join("::");
let self_path = fmt::from_fn(|f| self_fqp.iter().joined("::", f));
write_str(
w,
format_args!(
Expand Down
Loading