Skip to content

Commit 327762f

Browse files
authored
Rollup merge of #81831 - LeSeulArtichaut:81289-mut-arg, r=camelid
Don't display `mut` in arguments for functions documentation Fixes #81289 by reverting #80799, as requested in #81328 (comment). Supersedes #81328. r? ``@camelid`` cc ``@jyn514``
2 parents 0b6876c + 089ee27 commit 327762f

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ impl<'a> Clean<Arguments> for (&'a [hir::Ty<'a>], hir::BodyId) {
961961
.iter()
962962
.enumerate()
963963
.map(|(i, ty)| Argument {
964-
name: Symbol::intern(&rustc_hir_pretty::param_to_string(&body.params[i])),
964+
name: name_from_pat(&body.params[i].pat),
965965
type_: ty.clean(cx),
966966
})
967967
.collect(),

src/librustdoc/clean/utils.rs

+67
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,25 @@ crate fn strip_path(path: &Path) -> Path {
195195
Path { global: path.global, res: path.res, segments }
196196
}
197197

198+
crate fn qpath_to_string(p: &hir::QPath<'_>) -> String {
199+
let segments = match *p {
200+
hir::QPath::Resolved(_, ref path) => &path.segments,
201+
hir::QPath::TypeRelative(_, ref segment) => return segment.ident.to_string(),
202+
hir::QPath::LangItem(lang_item, ..) => return lang_item.name().to_string(),
203+
};
204+
205+
let mut s = String::new();
206+
for (i, seg) in segments.iter().enumerate() {
207+
if i > 0 {
208+
s.push_str("::");
209+
}
210+
if seg.ident.name != kw::PathRoot {
211+
s.push_str(&seg.ident.as_str());
212+
}
213+
}
214+
s
215+
}
216+
198217
crate fn build_deref_target_impls(cx: &DocContext<'_>, items: &[Item], ret: &mut Vec<Item>) {
199218
let tcx = cx.tcx;
200219

@@ -232,6 +251,54 @@ impl ToSource for rustc_span::Span {
232251
}
233252
}
234253

254+
crate fn name_from_pat(p: &hir::Pat<'_>) -> Symbol {
255+
use rustc_hir::*;
256+
debug!("trying to get a name from pattern: {:?}", p);
257+
258+
Symbol::intern(&match p.kind {
259+
PatKind::Wild => return kw::Underscore,
260+
PatKind::Binding(_, _, ident, _) => return ident.name,
261+
PatKind::TupleStruct(ref p, ..) | PatKind::Path(ref p) => qpath_to_string(p),
262+
PatKind::Struct(ref name, ref fields, etc) => format!(
263+
"{} {{ {}{} }}",
264+
qpath_to_string(name),
265+
fields
266+
.iter()
267+
.map(|fp| format!("{}: {}", fp.ident, name_from_pat(&fp.pat)))
268+
.collect::<Vec<String>>()
269+
.join(", "),
270+
if etc { ", .." } else { "" }
271+
),
272+
PatKind::Or(ref pats) => pats
273+
.iter()
274+
.map(|p| name_from_pat(&**p).to_string())
275+
.collect::<Vec<String>>()
276+
.join(" | "),
277+
PatKind::Tuple(ref elts, _) => format!(
278+
"({})",
279+
elts.iter()
280+
.map(|p| name_from_pat(&**p).to_string())
281+
.collect::<Vec<String>>()
282+
.join(", ")
283+
),
284+
PatKind::Box(ref p) => return name_from_pat(&**p),
285+
PatKind::Ref(ref p, _) => return name_from_pat(&**p),
286+
PatKind::Lit(..) => {
287+
warn!(
288+
"tried to get argument name from PatKind::Lit, which is silly in function arguments"
289+
);
290+
return Symbol::intern("()");
291+
}
292+
PatKind::Range(..) => return kw::Underscore,
293+
PatKind::Slice(ref begin, ref mid, ref end) => {
294+
let begin = begin.iter().map(|p| name_from_pat(&**p).to_string());
295+
let mid = mid.as_ref().map(|p| format!("..{}", name_from_pat(&**p))).into_iter();
296+
let end = end.iter().map(|p| name_from_pat(&**p).to_string());
297+
format!("[{}]", begin.chain(mid).chain(end).collect::<Vec<_>>().join(", "))
298+
}
299+
})
300+
}
301+
235302
crate fn print_const(cx: &DocContext<'_>, n: &'tcx ty::Const<'_>) -> String {
236303
match n.val {
237304
ty::ConstKind::Unevaluated(def, _, promoted) => {

src/test/rustdoc/mut-params.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Rustdoc shouldn't display `mut` in function arguments, which are
2+
// implementation details. Regression test for #81289.
3+
4+
#![crate_name = "foo"]
5+
6+
pub struct Foo;
7+
8+
// @count foo/struct.Foo.html '//*[@class="impl-items"]//*[@class="method"]' 2
9+
// @!has - '//*[@class="impl-items"]//*[@class="method"]' 'mut'
10+
impl Foo {
11+
pub fn foo(mut self) {}
12+
13+
pub fn bar(mut bar: ()) {}
14+
}
15+
16+
// @count foo/fn.baz.html '//*[@class="rust fn"]' 1
17+
// @!has - '//*[@class="rust fn"]' 'mut'
18+
pub fn baz(mut foo: Foo) {}

src/test/rustdoc/range-arg-pattern.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![crate_name = "foo"]
22

33
// @has foo/fn.f.html
4-
// @has - '//*[@class="rust fn"]' 'pub fn f(0u8 ...255: u8)'
4+
// @has - '//*[@class="rust fn"]' 'pub fn f(_: u8)'
55
pub fn f(0u8...255: u8) {}

0 commit comments

Comments
 (0)