Skip to content

Commit 5b82898

Browse files
Don't display mut in arguments for functions documentation
1 parent 4d0dd02 commit 5b82898

File tree

4 files changed

+55
-28
lines changed

4 files changed

+55
-28
lines changed

compiler/rustc_hir_pretty/src/lib.rs

+34-26
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl PpAnn for hir::Crate<'_> {
5959
Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)),
6060
Nested::ForeignItem(id) => state.print_foreign_item(self.foreign_item(id)),
6161
Nested::Body(id) => state.print_expr(&self.body(id).value),
62-
Nested::BodyParamPat(id, i) => state.print_pat(&self.body(id).params[i].pat),
62+
Nested::BodyParamPat(id, i) => state.print_pat(&self.body(id).params[i].pat, true),
6363
}
6464
}
6565
}
@@ -74,7 +74,7 @@ impl PpAnn for &dyn rustc_hir::intravisit::Map<'_> {
7474
Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)),
7575
Nested::ForeignItem(id) => state.print_foreign_item(self.foreign_item(id)),
7676
Nested::Body(id) => state.print_expr(&self.body(id).value),
77-
Nested::BodyParamPat(id, i) => state.print_pat(&self.body(id).params[i].pat),
77+
Nested::BodyParamPat(id, i) => state.print_pat(&self.body(id).params[i].pat, true),
7878
}
7979
}
8080
}
@@ -88,7 +88,7 @@ pub struct State<'a> {
8888
impl<'a> State<'a> {
8989
pub fn print_node(&mut self, node: Node<'_>) {
9090
match node {
91-
Node::Param(a) => self.print_param(&a),
91+
Node::Param(a) => self.print_param(&a, true),
9292
Node::Item(a) => self.print_item(&a),
9393
Node::ForeignItem(a) => self.print_foreign_item(&a),
9494
Node::TraitItem(a) => self.print_trait_item(a),
@@ -100,7 +100,7 @@ impl<'a> State<'a> {
100100
Node::PathSegment(a) => self.print_path_segment(&a),
101101
Node::Ty(a) => self.print_type(&a),
102102
Node::TraitRef(a) => self.print_trait_ref(&a),
103-
Node::Binding(a) | Node::Pat(a) => self.print_pat(&a),
103+
Node::Binding(a) | Node::Pat(a) => self.print_pat(&a, true),
104104
Node::Arm(a) => self.print_arm(&a),
105105
Node::Block(a) => {
106106
// Containing cbox, will be closed by print-block at `}`.
@@ -208,8 +208,8 @@ pub fn bounds_to_string<'b>(bounds: impl IntoIterator<Item = &'b hir::GenericBou
208208
to_string(NO_ANN, |s| s.print_bounds("", bounds))
209209
}
210210

211-
pub fn param_to_string(arg: &hir::Param<'_>) -> String {
212-
to_string(NO_ANN, |s| s.print_param(arg))
211+
pub fn param_to_string(arg: &hir::Param<'_>, print_mut: bool) -> String {
212+
to_string(NO_ANN, |s| s.print_param(arg, print_mut))
213213
}
214214

215215
pub fn ty_to_string(ty: &hir::Ty<'_>) -> String {
@@ -1677,7 +1677,7 @@ impl<'a> State<'a> {
16771677
}
16781678

16791679
pub fn print_local_decl(&mut self, loc: &hir::Local<'_>) {
1680-
self.print_pat(&loc.pat);
1680+
self.print_pat(&loc.pat, true);
16811681
if let Some(ref ty) = loc.ty {
16821682
self.word_space(":");
16831683
self.print_type(&ty);
@@ -1858,7 +1858,7 @@ impl<'a> State<'a> {
18581858
}
18591859
}
18601860

1861-
pub fn print_pat(&mut self, pat: &hir::Pat<'_>) {
1861+
pub fn print_pat(&mut self, pat: &hir::Pat<'_>, print_mut: bool) {
18621862
self.maybe_print_comment(pat.span.lo());
18631863
self.ann.pre(self, AnnNode::Pat(pat));
18641864
// Pat isn't normalized, but the beauty of it
@@ -1867,6 +1867,10 @@ impl<'a> State<'a> {
18671867
PatKind::Wild => self.s.word("_"),
18681868
PatKind::Binding(binding_mode, _, ident, ref sub) => {
18691869
match binding_mode {
1870+
hir::BindingAnnotation::Ref | hir::BindingAnnotation::RefMut if !print_mut => {
1871+
self.word_nbsp("ref");
1872+
}
1873+
hir::BindingAnnotation::Mutable if !print_mut => {}
18701874
hir::BindingAnnotation::Ref => {
18711875
self.word_nbsp("ref");
18721876
self.print_mutability(hir::Mutability::Not, false);
@@ -1883,24 +1887,26 @@ impl<'a> State<'a> {
18831887
self.print_ident(ident);
18841888
if let Some(ref p) = *sub {
18851889
self.s.word("@");
1886-
self.print_pat(&p);
1890+
self.print_pat(&p, print_mut);
18871891
}
18881892
}
18891893
PatKind::TupleStruct(ref qpath, ref elts, ddpos) => {
18901894
self.print_qpath(qpath, true);
18911895
self.popen();
18921896
if let Some(ddpos) = ddpos {
1893-
self.commasep(Inconsistent, &elts[..ddpos], |s, p| s.print_pat(&p));
1897+
self.commasep(Inconsistent, &elts[..ddpos], |s, p| s.print_pat(&p, print_mut));
18941898
if ddpos != 0 {
18951899
self.word_space(",");
18961900
}
18971901
self.s.word("..");
18981902
if ddpos != elts.len() {
18991903
self.s.word(",");
1900-
self.commasep(Inconsistent, &elts[ddpos..], |s, p| s.print_pat(&p));
1904+
self.commasep(Inconsistent, &elts[ddpos..], |s, p| {
1905+
s.print_pat(&p, print_mut)
1906+
});
19011907
}
19021908
} else {
1903-
self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(&p));
1909+
self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(&p, print_mut));
19041910
}
19051911
self.pclose();
19061912
}
@@ -1920,7 +1926,7 @@ impl<'a> State<'a> {
19201926
s.print_ident(f.ident);
19211927
s.word_nbsp(":");
19221928
}
1923-
s.print_pat(&f.pat);
1929+
s.print_pat(&f.pat, print_mut);
19241930
s.end()
19251931
},
19261932
|f| f.pat.span,
@@ -1935,22 +1941,24 @@ impl<'a> State<'a> {
19351941
self.s.word("}");
19361942
}
19371943
PatKind::Or(ref pats) => {
1938-
self.strsep("|", true, Inconsistent, &pats[..], |s, p| s.print_pat(&p));
1944+
self.strsep("|", true, Inconsistent, &pats[..], |s, p| s.print_pat(&p, print_mut));
19391945
}
19401946
PatKind::Tuple(ref elts, ddpos) => {
19411947
self.popen();
19421948
if let Some(ddpos) = ddpos {
1943-
self.commasep(Inconsistent, &elts[..ddpos], |s, p| s.print_pat(&p));
1949+
self.commasep(Inconsistent, &elts[..ddpos], |s, p| s.print_pat(&p, print_mut));
19441950
if ddpos != 0 {
19451951
self.word_space(",");
19461952
}
19471953
self.s.word("..");
19481954
if ddpos != elts.len() {
19491955
self.s.word(",");
1950-
self.commasep(Inconsistent, &elts[ddpos..], |s, p| s.print_pat(&p));
1956+
self.commasep(Inconsistent, &elts[ddpos..], |s, p| {
1957+
s.print_pat(&p, print_mut)
1958+
});
19511959
}
19521960
} else {
1953-
self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(&p));
1961+
self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(&p, print_mut));
19541962
if elts.len() == 1 {
19551963
self.s.word(",");
19561964
}
@@ -1963,7 +1971,7 @@ impl<'a> State<'a> {
19631971
if is_range_inner {
19641972
self.popen();
19651973
}
1966-
self.print_pat(&inner);
1974+
self.print_pat(&inner, print_mut);
19671975
if is_range_inner {
19681976
self.pclose();
19691977
}
@@ -1975,7 +1983,7 @@ impl<'a> State<'a> {
19751983
if is_range_inner {
19761984
self.popen();
19771985
}
1978-
self.print_pat(&inner);
1986+
self.print_pat(&inner, print_mut);
19791987
if is_range_inner {
19801988
self.pclose();
19811989
}
@@ -1996,31 +2004,31 @@ impl<'a> State<'a> {
19962004
}
19972005
PatKind::Slice(ref before, ref slice, ref after) => {
19982006
self.s.word("[");
1999-
self.commasep(Inconsistent, &before[..], |s, p| s.print_pat(&p));
2007+
self.commasep(Inconsistent, &before[..], |s, p| s.print_pat(&p, print_mut));
20002008
if let Some(ref p) = *slice {
20012009
if !before.is_empty() {
20022010
self.word_space(",");
20032011
}
20042012
if let PatKind::Wild = p.kind {
20052013
// Print nothing.
20062014
} else {
2007-
self.print_pat(&p);
2015+
self.print_pat(&p, print_mut);
20082016
}
20092017
self.s.word("..");
20102018
if !after.is_empty() {
20112019
self.word_space(",");
20122020
}
20132021
}
2014-
self.commasep(Inconsistent, &after[..], |s, p| s.print_pat(&p));
2022+
self.commasep(Inconsistent, &after[..], |s, p| s.print_pat(&p, print_mut));
20152023
self.s.word("]");
20162024
}
20172025
}
20182026
self.ann.post(self, AnnNode::Pat(pat))
20192027
}
20202028

2021-
pub fn print_param(&mut self, arg: &hir::Param<'_>) {
2029+
pub fn print_param(&mut self, arg: &hir::Param<'_>, print_mut: bool) {
20222030
self.print_outer_attributes(&arg.attrs);
2023-
self.print_pat(&arg.pat);
2031+
self.print_pat(&arg.pat, print_mut);
20242032
}
20252033

20262034
pub fn print_arm(&mut self, arm: &hir::Arm<'_>) {
@@ -2033,7 +2041,7 @@ impl<'a> State<'a> {
20332041
self.ann.pre(self, AnnNode::Arm(arm));
20342042
self.ibox(0);
20352043
self.print_outer_attributes(&arm.attrs);
2036-
self.print_pat(&arm.pat);
2044+
self.print_pat(&arm.pat, true);
20372045
self.s.space();
20382046
if let Some(ref g) = arm.guard {
20392047
match g {
@@ -2045,7 +2053,7 @@ impl<'a> State<'a> {
20452053
hir::Guard::IfLet(pat, e) => {
20462054
self.word_nbsp("if");
20472055
self.word_nbsp("let");
2048-
self.print_pat(&pat);
2056+
self.print_pat(&pat, true);
20492057
self.s.space();
20502058
self.word_space("=");
20512059
self.print_expr(&e);

compiler/rustc_typeck/src/check/pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14081408
.map(|f| match self.tcx.sess.source_map().span_to_snippet(f.pat.span) {
14091409
Ok(f) => f,
14101410
Err(_) => rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| {
1411-
s.print_pat(f.pat)
1411+
s.print_pat(f.pat, true)
14121412
}),
14131413
})
14141414
.collect::<Vec<String>>()

src/librustdoc/clean/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,10 @@ impl<'a> Clean<Arguments> for (&'a [hir::Ty<'a>], hir::BodyId) {
962962
.iter()
963963
.enumerate()
964964
.map(|(i, ty)| Argument {
965-
name: Symbol::intern(&rustc_hir_pretty::param_to_string(&body.params[i])),
965+
name: Symbol::intern(&rustc_hir_pretty::param_to_string(
966+
&body.params[i],
967+
false,
968+
)),
966969
type_: ty.clean(cx),
967970
})
968971
.collect(),

src/test/rustdoc/mut-params.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
// @!has foo/struct.Foo.html '//*[@class="impl-items"]//*[@class="method"]' 'mut'
9+
impl Foo {
10+
pub fn foo(mut self) {}
11+
12+
pub fn bar(mut bar: ()) {}
13+
}
14+
15+
// @!has foo/fn.baz.html '//*[@class="rust fn"]' 'mut'
16+
pub fn baz(mut foo: Foo) {}

0 commit comments

Comments
 (0)