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

Don't display mut in arguments for functions documentation #81328

Closed
Closed
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
60 changes: 34 additions & 26 deletions compiler/rustc_hir_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl PpAnn for hir::Crate<'_> {
Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)),
Nested::ForeignItem(id) => state.print_foreign_item(self.foreign_item(id)),
Nested::Body(id) => state.print_expr(&self.body(id).value),
Nested::BodyParamPat(id, i) => state.print_pat(&self.body(id).params[i].pat),
Nested::BodyParamPat(id, i) => state.print_pat(&self.body(id).params[i].pat, true),
}
}
}
Expand All @@ -74,7 +74,7 @@ impl PpAnn for &dyn rustc_hir::intravisit::Map<'_> {
Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)),
Nested::ForeignItem(id) => state.print_foreign_item(self.foreign_item(id)),
Nested::Body(id) => state.print_expr(&self.body(id).value),
Nested::BodyParamPat(id, i) => state.print_pat(&self.body(id).params[i].pat),
Nested::BodyParamPat(id, i) => state.print_pat(&self.body(id).params[i].pat, true),
}
}
}
Expand All @@ -88,7 +88,7 @@ pub struct State<'a> {
impl<'a> State<'a> {
pub fn print_node(&mut self, node: Node<'_>) {
match node {
Node::Param(a) => self.print_param(&a),
Node::Param(a) => self.print_param(&a, true),
Node::Item(a) => self.print_item(&a),
Node::ForeignItem(a) => self.print_foreign_item(&a),
Node::TraitItem(a) => self.print_trait_item(a),
Expand All @@ -100,7 +100,7 @@ impl<'a> State<'a> {
Node::PathSegment(a) => self.print_path_segment(&a),
Node::Ty(a) => self.print_type(&a),
Node::TraitRef(a) => self.print_trait_ref(&a),
Node::Binding(a) | Node::Pat(a) => self.print_pat(&a),
Node::Binding(a) | Node::Pat(a) => self.print_pat(&a, true),
Node::Arm(a) => self.print_arm(&a),
Node::Block(a) => {
// Containing cbox, will be closed by print-block at `}`.
Expand Down Expand Up @@ -208,8 +208,8 @@ pub fn bounds_to_string<'b>(bounds: impl IntoIterator<Item = &'b hir::GenericBou
to_string(NO_ANN, |s| s.print_bounds("", bounds))
}

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

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

pub fn print_local_decl(&mut self, loc: &hir::Local<'_>) {
self.print_pat(&loc.pat);
self.print_pat(&loc.pat, true);
if let Some(ref ty) = loc.ty {
self.word_space(":");
self.print_type(&ty);
Expand Down Expand Up @@ -1858,7 +1858,7 @@ impl<'a> State<'a> {
}
}

pub fn print_pat(&mut self, pat: &hir::Pat<'_>) {
pub fn print_pat(&mut self, pat: &hir::Pat<'_>, print_mut: bool) {
self.maybe_print_comment(pat.span.lo());
self.ann.pre(self, AnnNode::Pat(pat));
// Pat isn't normalized, but the beauty of it
Expand All @@ -1867,6 +1867,10 @@ impl<'a> State<'a> {
PatKind::Wild => self.s.word("_"),
PatKind::Binding(binding_mode, _, ident, ref sub) => {
match binding_mode {
hir::BindingAnnotation::Ref | hir::BindingAnnotation::RefMut if !print_mut => {
self.word_nbsp("ref");
}
hir::BindingAnnotation::Mutable if !print_mut => {}
hir::BindingAnnotation::Ref => {
self.word_nbsp("ref");
self.print_mutability(hir::Mutability::Not, false);
Expand All @@ -1883,24 +1887,26 @@ impl<'a> State<'a> {
self.print_ident(ident);
if let Some(ref p) = *sub {
self.s.word("@");
self.print_pat(&p);
self.print_pat(&p, print_mut);
}
}
PatKind::TupleStruct(ref qpath, ref elts, ddpos) => {
self.print_qpath(qpath, true);
self.popen();
if let Some(ddpos) = ddpos {
self.commasep(Inconsistent, &elts[..ddpos], |s, p| s.print_pat(&p));
self.commasep(Inconsistent, &elts[..ddpos], |s, p| s.print_pat(&p, print_mut));
if ddpos != 0 {
self.word_space(",");
}
self.s.word("..");
if ddpos != elts.len() {
self.s.word(",");
self.commasep(Inconsistent, &elts[ddpos..], |s, p| s.print_pat(&p));
self.commasep(Inconsistent, &elts[ddpos..], |s, p| {
s.print_pat(&p, print_mut)
});
}
} else {
self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(&p));
self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(&p, print_mut));
}
self.pclose();
}
Expand All @@ -1920,7 +1926,7 @@ impl<'a> State<'a> {
s.print_ident(f.ident);
s.word_nbsp(":");
}
s.print_pat(&f.pat);
s.print_pat(&f.pat, print_mut);
s.end()
},
|f| f.pat.span,
Expand All @@ -1935,22 +1941,24 @@ impl<'a> State<'a> {
self.s.word("}");
}
PatKind::Or(ref pats) => {
self.strsep("|", true, Inconsistent, &pats[..], |s, p| s.print_pat(&p));
self.strsep("|", true, Inconsistent, &pats[..], |s, p| s.print_pat(&p, print_mut));
}
PatKind::Tuple(ref elts, ddpos) => {
self.popen();
if let Some(ddpos) = ddpos {
self.commasep(Inconsistent, &elts[..ddpos], |s, p| s.print_pat(&p));
self.commasep(Inconsistent, &elts[..ddpos], |s, p| s.print_pat(&p, print_mut));
if ddpos != 0 {
self.word_space(",");
}
self.s.word("..");
if ddpos != elts.len() {
self.s.word(",");
self.commasep(Inconsistent, &elts[ddpos..], |s, p| s.print_pat(&p));
self.commasep(Inconsistent, &elts[ddpos..], |s, p| {
s.print_pat(&p, print_mut)
});
}
} else {
self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(&p));
self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(&p, print_mut));
if elts.len() == 1 {
self.s.word(",");
}
Expand All @@ -1963,7 +1971,7 @@ impl<'a> State<'a> {
if is_range_inner {
self.popen();
}
self.print_pat(&inner);
self.print_pat(&inner, print_mut);
if is_range_inner {
self.pclose();
}
Expand All @@ -1975,7 +1983,7 @@ impl<'a> State<'a> {
if is_range_inner {
self.popen();
}
self.print_pat(&inner);
self.print_pat(&inner, print_mut);
if is_range_inner {
self.pclose();
}
Expand All @@ -1996,31 +2004,31 @@ impl<'a> State<'a> {
}
PatKind::Slice(ref before, ref slice, ref after) => {
self.s.word("[");
self.commasep(Inconsistent, &before[..], |s, p| s.print_pat(&p));
self.commasep(Inconsistent, &before[..], |s, p| s.print_pat(&p, print_mut));
if let Some(ref p) = *slice {
if !before.is_empty() {
self.word_space(",");
}
if let PatKind::Wild = p.kind {
// Print nothing.
} else {
self.print_pat(&p);
self.print_pat(&p, print_mut);
}
self.s.word("..");
if !after.is_empty() {
self.word_space(",");
}
}
self.commasep(Inconsistent, &after[..], |s, p| s.print_pat(&p));
self.commasep(Inconsistent, &after[..], |s, p| s.print_pat(&p, print_mut));
self.s.word("]");
}
}
self.ann.post(self, AnnNode::Pat(pat))
}

pub fn print_param(&mut self, arg: &hir::Param<'_>) {
pub fn print_param(&mut self, arg: &hir::Param<'_>, print_mut: bool) {
self.print_outer_attributes(&arg.attrs);
self.print_pat(&arg.pat);
self.print_pat(&arg.pat, print_mut);
}

pub fn print_arm(&mut self, arm: &hir::Arm<'_>) {
Expand All @@ -2033,7 +2041,7 @@ impl<'a> State<'a> {
self.ann.pre(self, AnnNode::Arm(arm));
self.ibox(0);
self.print_outer_attributes(&arm.attrs);
self.print_pat(&arm.pat);
self.print_pat(&arm.pat, true);
self.s.space();
if let Some(ref g) = arm.guard {
match g {
Expand All @@ -2045,7 +2053,7 @@ impl<'a> State<'a> {
hir::Guard::IfLet(pat, e) => {
self.word_nbsp("if");
self.word_nbsp("let");
self.print_pat(&pat);
self.print_pat(&pat, true);
self.s.space();
self.word_space("=");
self.print_expr(&e);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/check/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.map(|f| match self.tcx.sess.source_map().span_to_snippet(f.pat.span) {
Ok(f) => f,
Err(_) => rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| {
s.print_pat(f.pat)
s.print_pat(f.pat, true)
}),
})
.collect::<Vec<String>>()
Expand Down
5 changes: 4 additions & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,10 @@ impl<'a> Clean<Arguments> for (&'a [hir::Ty<'a>], hir::BodyId) {
.iter()
.enumerate()
.map(|(i, ty)| Argument {
name: Symbol::intern(&rustc_hir_pretty::param_to_string(&body.params[i])),
name: Symbol::intern(&rustc_hir_pretty::param_to_string(
&body.params[i],
false,
)),
type_: ty.clean(cx),
})
.collect(),
Expand Down
15 changes: 15 additions & 0 deletions src/test/rustdoc/elided-self-type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Checks that `: Self` never gets emitted for simple `self` parameters.

#![crate_name = "foo"]

pub struct Foo;

// @!has foo/struct.Foo.html '//*[@class="impl-items"]' Self
impl Foo {
pub fn by_value(self) {}
pub fn by_value_mut(mut self) {}
pub fn by_ref(&self) {}
pub fn by_mut_ref(&mut self) {}
pub fn by_value_explicit(self: Self) {}
pub fn by_value_mut_explicit(mut self: Self) {}
}
16 changes: 16 additions & 0 deletions src/test/rustdoc/mut-params.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Rustdoc shouldn't display `mut` in function arguments, which are
// implementation details. Regression test for #81289.

#![crate_name = "foo"]

pub struct Foo;

// @!has foo/struct.Foo.html '//*[@class="impl-items"]//*[@class="method"]' 'mut'
impl Foo {
pub fn foo(mut self) {}

pub fn bar(mut bar: ()) {}
}

// @!has foo/fn.baz.html '//*[@class="rust fn"]' 'mut'
pub fn baz(mut foo: Foo) {}