Skip to content

Commit 7ccb754

Browse files
Add help in case ref and mut are switched
1 parent d84693b commit 7ccb754

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/libsyntax/parse/parser.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,18 @@ impl<'a> Parser<'a> {
640640
match self.token {
641641
token::Ident(i) => {
642642
if self.token.is_reserved_ident() {
643-
self.span_err(self.span, &format!("expected identifier, found {}",
644-
self.this_token_descr()));
643+
let desc = self.this_token_descr();
644+
if self.token.is_keyword(keywords::Ref) {
645+
let mut err = self.span_fatal(self.span,
646+
&format!("expected identifier, found {}",
647+
desc));
648+
err.span_suggestion(self.span,
649+
"if you want to make a mutable reference, write:",
650+
"ref mut".to_owned());
651+
err.emit();
652+
} else {
653+
self.span_err(self.span, &format!("expected identifier, found {}", desc));
654+
}
645655
}
646656
self.bump();
647657
Ok(i)
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z parse-only
12+
13+
fn main() {
14+
let mut ref y = &x; //~ ERROR expected identifier, found keyword `ref`
15+
//~| HELP if you want to make a mutable reference, write:
16+
//~^^ ERROR expected one of `:`, `;`, `=`, or `@`, found `y`
17+
}

0 commit comments

Comments
 (0)