diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 093ab0073f0db..30099cb61752b 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -640,8 +640,18 @@ impl<'a> Parser<'a> { match self.token { token::Ident(i) => { if self.token.is_reserved_ident() { - self.span_err(self.span, &format!("expected identifier, found {}", - self.this_token_descr())); + let desc = self.this_token_descr(); + if self.token.is_keyword(keywords::Ref) { + let mut err = self.span_fatal(self.span, + &format!("expected identifier, found {}", + desc)); + err.span_suggestion(self.span, + "if you want to make a mutable reference, write:", + "ref mut".to_owned()); + err.emit(); + } else { + self.span_err(self.span, &format!("expected identifier, found {}", desc)); + } } self.bump(); Ok(i) diff --git a/src/test/parse-fail/expected-identifier.rs b/src/test/parse-fail/expected-identifier.rs new file mode 100644 index 0000000000000..fa4a4132448d3 --- /dev/null +++ b/src/test/parse-fail/expected-identifier.rs @@ -0,0 +1,17 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags: -Z parse-only + +fn main() { + let mut ref y = &x; //~ ERROR expected identifier, found keyword `ref` + //~| HELP if you want to make a mutable reference, write: + //~^^ ERROR expected one of `:`, `;`, `=`, or `@`, found `y` +}