about summary refs log tree commit diff
path: root/src/libsyntax/parse
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-07-27 11:40:12 +0000
committerbors <bors@rust-lang.org>2017-07-27 11:40:12 +0000
commitf60d373422656da31863e3e7961142c420b28809 (patch)
tree28f0b51d871913178782800e2d1a8f4b5e0f7ec9 /src/libsyntax/parse
parentda4d49002d9294380e3df9d2bcd306ea5dd2c9c0 (diff)
parentdda30f690206870ed6f8fc14216e92cdc1f2687a (diff)
downloadrust-f60d373422656da31863e3e7961142c420b28809.tar.gz
rust-f60d373422656da31863e3e7961142c420b28809.zip
Auto merge of #43489 - petrochenkov:mutref, r=GuillaumeGomez
Better diagnostics and recovery for `mut ref` in patterns

Fixes https://github.com/rust-lang/rust/issues/43286
Supersedes https://github.com/rust-lang/rust/pull/43451

r? @GuillaumeGomez
Diffstat (limited to 'src/libsyntax/parse')
-rw-r--r--src/libsyntax/parse/parser.rs14
1 files changed, 12 insertions, 2 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index 2cd84d202ff..af9a198b983 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -3523,8 +3523,18 @@ impl<'a> Parser<'a> {
             }
             // At this point, token != _, &, &&, (, [
             _ => if self.eat_keyword(keywords::Mut) {
-                // Parse mut ident @ pat
-                pat = self.parse_pat_ident(BindingMode::ByValue(Mutability::Mutable))?;
+                // Parse mut ident @ pat / mut ref ident @ pat
+                let mutref_span = self.prev_span.to(self.span);
+                let binding_mode = if self.eat_keyword(keywords::Ref) {
+                    self.diagnostic()
+                        .struct_span_err(mutref_span, "the order of `mut` and `ref` is incorrect")
+                        .span_suggestion(mutref_span, "try switching the order", "ref mut".into())
+                        .emit();
+                    BindingMode::ByRef(Mutability::Mutable)
+                } else {
+                    BindingMode::ByValue(Mutability::Mutable)
+                };
+                pat = self.parse_pat_ident(binding_mode)?;
             } else if self.eat_keyword(keywords::Ref) {
                 // Parse ref ident @ pat / ref mut ident @ pat
                 let mutbl = self.parse_mutability();