Skip to content

Commit e1195c2

Browse files
committed
Auto merge of #32506 - petrochenkov:use, r=Manishearth
syntax: Extra diagnostics for `_` used in an identifier position Closes #32501
2 parents 3399d19 + 1cbdf4e commit e1195c2

File tree

7 files changed

+34
-19
lines changed

7 files changed

+34
-19
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,12 @@ impl<'a> Parser<'a> {
574574
self.bug("ident interpolation not converted to real token");
575575
}
576576
_ => {
577-
let token_str = self.this_token_to_string();
578-
Err(self.fatal(&format!("expected ident, found `{}`",
579-
token_str)))
577+
let mut err = self.fatal(&format!("expected identifier, found `{}`",
578+
self.this_token_to_string()));
579+
if self.token == token::Underscore {
580+
err.fileline_note(self.span, "`_` is a wildcard pattern, not an identifier");
581+
}
582+
Err(err)
580583
}
581584
}
582585
}
@@ -3782,12 +3785,6 @@ impl<'a> Parser<'a> {
37823785
fn parse_pat_ident(&mut self,
37833786
binding_mode: ast::BindingMode)
37843787
-> PResult<'a, PatKind> {
3785-
if !self.token.is_plain_ident() {
3786-
let span = self.span;
3787-
let tok_str = self.this_token_to_string();
3788-
return Err(self.span_fatal(span,
3789-
&format!("expected identifier, found `{}`", tok_str)))
3790-
}
37913788
let ident = self.parse_ident()?;
37923789
let last_span = self.last_span;
37933790
let name = codemap::Spanned{span: last_span, node: ident};
@@ -3847,9 +3844,6 @@ impl<'a> Parser<'a> {
38473844
Visibility::Inherited => self.span.lo,
38483845
Visibility::Public => self.last_span.lo,
38493846
};
3850-
if !self.token.is_plain_ident() {
3851-
return Err(self.fatal("expected ident"));
3852-
}
38533847
let name = self.parse_ident()?;
38543848
self.expect(&token::Colon)?;
38553849
let ty = self.parse_ty_sum()?;

src/test/compile-fail/cfg-empty-codemap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
// compile-flags: --cfg ""
1414

15-
// error-pattern: expected ident, found
15+
// error-pattern: expected identifier, found
1616

1717
pub fn main() {
1818
}

src/test/compile-fail/issue-20616-8.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type Type_5_<'a> = Type_1_<'a, ()>;
4040
//type Type_7 = Box<(),,>; // error: expected type, found `,`
4141

4242

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

4545

46-
//type Type_9<T,,> = Box<T>; // error: expected ident, found `,`
46+
//type Type_9<T,,> = Box<T>; // error: expected identifier, found `,`

src/test/compile-fail/issue-20616-9.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type Type_5_<'a> = Type_1_<'a, ()>;
4040
//type Type_7 = Box<(),,>; // error: expected type, found `,`
4141

4242

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

4545

46-
type Type_9<T,,> = Box<T>; //~ error: expected ident, found `,`
46+
type Type_9<T,,> = Box<T>; //~ error: expected identifier, found `,`

src/test/parse-fail/issue-32501.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2016 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 a = 0;
15+
let _b = 0;
16+
let _ = 0;
17+
let mut b = 0;
18+
let mut _b = 0;
19+
let mut _ = 0; //~ ERROR expected identifier, found `_`
20+
//~^ NOTE `_` is a wildcard pattern, not an identifier
21+
}

src/test/parse-fail/paamayim-nekudotayim.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
// http://phpsadness.com/sad/1
1414

1515
fn main() {
16-
::; //~ ERROR expected ident, found `;`
16+
::; //~ ERROR expected identifier, found `;`
1717
}

src/test/parse-fail/unboxed-closure-sugar-used-on-struct-3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn bar() {
2424
let b = Box::Bar::<isize,usize>::new(); // OK
2525

2626
let b = Box::Bar::()::new();
27-
//~^ ERROR expected ident, found `(`
27+
//~^ ERROR expected identifier, found `(`
2828
}
2929

3030
fn main() { }

0 commit comments

Comments
 (0)