Skip to content

syntax: Extra diagnostics for _ used in an identifier position #32506

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

Merged
merged 1 commit into from
Mar 31, 2016
Merged
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
18 changes: 6 additions & 12 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,12 @@ impl<'a> Parser<'a> {
self.bug("ident interpolation not converted to real token");
}
_ => {
let token_str = self.this_token_to_string();
Err(self.fatal(&format!("expected ident, found `{}`",
token_str)))
let mut err = self.fatal(&format!("expected identifier, found `{}`",
self.this_token_to_string()));
if self.token == token::Underscore {
err.fileline_note(self.span, "`_` is a wildcard pattern, not an identifier");
}
Err(err)
}
}
}
Expand Down Expand Up @@ -3782,12 +3785,6 @@ impl<'a> Parser<'a> {
fn parse_pat_ident(&mut self,
binding_mode: ast::BindingMode)
-> PResult<'a, PatKind> {
if !self.token.is_plain_ident() {
let span = self.span;
let tok_str = self.this_token_to_string();
return Err(self.span_fatal(span,
&format!("expected identifier, found `{}`", tok_str)))
}
let ident = self.parse_ident()?;
let last_span = self.last_span;
let name = codemap::Spanned{span: last_span, node: ident};
Expand Down Expand Up @@ -3847,9 +3844,6 @@ impl<'a> Parser<'a> {
Visibility::Inherited => self.span.lo,
Visibility::Public => self.last_span.lo,
};
if !self.token.is_plain_ident() {
return Err(self.fatal("expected ident"));
}
let name = self.parse_ident()?;
self.expect(&token::Colon)?;
let ty = self.parse_ty_sum()?;
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/cfg-empty-codemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

// compile-flags: --cfg ""

// error-pattern: expected ident, found
// error-pattern: expected identifier, found

pub fn main() {
}
4 changes: 2 additions & 2 deletions src/test/compile-fail/issue-20616-8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Type_5_<'a> = Type_1_<'a, ()>;
//type Type_7 = Box<(),,>; // error: expected type, found `,`


type Type_8<'a,,> = &'a (); //~ error: expected ident, found `,`
type Type_8<'a,,> = &'a (); //~ error: expected identifier, found `,`


//type Type_9<T,,> = Box<T>; // error: expected ident, found `,`
//type Type_9<T,,> = Box<T>; // error: expected identifier, found `,`
4 changes: 2 additions & 2 deletions src/test/compile-fail/issue-20616-9.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Type_5_<'a> = Type_1_<'a, ()>;
//type Type_7 = Box<(),,>; // error: expected type, found `,`


//type Type_8<'a,,> = &'a (); // error: expected ident, found `,`
//type Type_8<'a,,> = &'a (); // error: expected identifier, found `,`


type Type_9<T,,> = Box<T>; //~ error: expected ident, found `,`
type Type_9<T,,> = Box<T>; //~ error: expected identifier, found `,`
21 changes: 21 additions & 0 deletions src/test/parse-fail/issue-32501.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2016 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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 a = 0;
let _b = 0;
let _ = 0;
let mut b = 0;
let mut _b = 0;
let mut _ = 0; //~ ERROR expected identifier, found `_`
//~^ NOTE `_` is a wildcard pattern, not an identifier
}
2 changes: 1 addition & 1 deletion src/test/parse-fail/paamayim-nekudotayim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
// http://phpsadness.com/sad/1

fn main() {
::; //~ ERROR expected ident, found `;`
::; //~ ERROR expected identifier, found `;`
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn bar() {
let b = Box::Bar::<isize,usize>::new(); // OK

let b = Box::Bar::()::new();
//~^ ERROR expected ident, found `(`
//~^ ERROR expected identifier, found `(`
}

fn main() { }