Skip to content

Commit b05ab18

Browse files
author
Thomas Bahn
committed
Fix pretty printing an AST representing &(mut ident)
`PatKind::Ref(PatKind::Ident(BindingMode::ByValue(Mutability::Mut), ..), ..)` is an AST representing `&(mut ident)`. It was errorneously printed as `&mut ident` which reparsed into a syntactically different AST. This affected help diagnostics in the parser.
1 parent b1964e6 commit b05ab18

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -2420,7 +2420,15 @@ impl<'a> State<'a> {
24202420
if mutbl == ast::Mutability::Mut {
24212421
self.s.word("mut ");
24222422
}
2423-
self.print_pat(inner);
2423+
if let PatKind::Ident(ast::BindingMode::ByValue(ast::Mutability::Mut), ..) =
2424+
inner.kind
2425+
{
2426+
self.popen();
2427+
self.print_pat(inner);
2428+
self.pclose();
2429+
} else {
2430+
self.print_pat(inner);
2431+
}
24242432
}
24252433
PatKind::Lit(ref e) => self.print_expr(&**e),
24262434
PatKind::Range(ref begin, ref end, Spanned { node: ref end_kind, .. }) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Regression test for correct pretty-printing of an AST representing `&(mut x)` in help
2+
// suggestion diagnostic.
3+
4+
fn main() {
5+
let mut &x = &0;
6+
//~^ ERROR `mut` must be attached to each individual binding
7+
//~| HELP add `mut` to each binding
8+
//~| SUGGESTION &(mut x)
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: `mut` must be attached to each individual binding
2+
--> $DIR/issue-80186-mut-binding-help-suggestion.rs:5:9
3+
|
4+
LL | let mut &x = &0;
5+
| ^^^^^^ help: add `mut` to each binding: `&(mut x)`
6+
|
7+
= note: `mut` may be followed by `variable` and `variable @ pattern`
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)