Skip to content

Commit adb96ec

Browse files
committed
Provide specific label for patern parsing error
1 parent d491734 commit adb96ec

11 files changed

+40
-44
lines changed

src/libsyntax/ext/expand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ impl<'a> Parser<'a> {
10301030
}
10311031
},
10321032
AstFragmentKind::Ty => AstFragment::Ty(self.parse_ty()?),
1033-
AstFragmentKind::Pat => AstFragment::Pat(self.parse_pat()?),
1033+
AstFragmentKind::Pat => AstFragment::Pat(self.parse_pat(None)?),
10341034
})
10351035
}
10361036

src/libsyntax/ext/quote.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ pub fn parse_item_panic(parser: &mut Parser) -> Option<P<Item>> {
419419
}
420420

421421
pub fn parse_pat_panic(parser: &mut Parser) -> P<Pat> {
422-
panictry!(parser.parse_pat())
422+
panictry!(parser.parse_pat(None))
423423
}
424424

425425
pub fn parse_arm_panic(parser: &mut Parser) -> Arm {

src/libsyntax/ext/tt/macro_parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
881881
FatalError.raise();
882882
}
883883
},
884-
"pat" => token::NtPat(panictry!(p.parse_pat())),
884+
"pat" => token::NtPat(panictry!(p.parse_pat(None))),
885885
"expr" => token::NtExpr(panictry!(p.parse_expr())),
886886
"literal" => token::NtLiteral(panictry!(p.parse_literal_maybe_minus())),
887887
"ty" => token::NtTy(panictry!(p.parse_ty())),

src/libsyntax/parse/parser.rs

+24-18
Original file line numberDiff line numberDiff line change
@@ -1827,10 +1827,7 @@ impl<'a> Parser<'a> {
18271827
debug!("parse_arg_general parse_pat (require_name:{})",
18281828
require_name);
18291829
self.eat_incorrect_doc_comment("method arguments");
1830-
let pat = self.parse_pat().map_err(|mut err| {
1831-
err.span_label(self.span, "expected argument name");
1832-
err
1833-
})?;
1830+
let pat = self.parse_pat(Some("argument name"))?;
18341831

18351832
if let Err(mut err) = self.expect(&token::Colon) {
18361833
// If we find a pattern followed by an identifier, it could be an (incorrect)
@@ -1879,7 +1876,7 @@ impl<'a> Parser<'a> {
18791876
// Recover from attempting to parse the argument as a type without pattern.
18801877
err.cancel();
18811878
mem::replace(self, parser_snapshot_before_ty);
1882-
let pat = self.parse_pat()?;
1879+
let pat = self.parse_pat(Some("argument name"))?;
18831880
self.expect(&token::Colon)?;
18841881
let ty = self.parse_ty()?;
18851882

@@ -1917,7 +1914,7 @@ impl<'a> Parser<'a> {
19171914

19181915
/// Parse an argument in a lambda header e.g. |arg, arg|
19191916
fn parse_fn_block_arg(&mut self) -> PResult<'a, Arg> {
1920-
let pat = self.parse_pat()?;
1917+
let pat = self.parse_pat(Some("argument name"))?;
19211918
let t = if self.eat(&token::Colon) {
19221919
self.parse_ty()?
19231920
} else {
@@ -3784,7 +3781,7 @@ impl<'a> Parser<'a> {
37843781
"`..` can only be used once per tuple or tuple struct pattern");
37853782
}
37863783
} else if !self.check(&token::CloseDelim(token::Paren)) {
3787-
fields.push(self.parse_pat()?);
3784+
fields.push(self.parse_pat(None)?);
37883785
} else {
37893786
break
37903787
}
@@ -3840,7 +3837,7 @@ impl<'a> Parser<'a> {
38403837
}
38413838
}
38423839

3843-
let subpat = self.parse_pat()?;
3840+
let subpat = self.parse_pat(None)?;
38443841
if before_slice && self.eat(&token::DotDot) {
38453842
slice = Some(subpat);
38463843
before_slice = false;
@@ -3865,7 +3862,7 @@ impl<'a> Parser<'a> {
38653862
// Parsing a pattern of the form "fieldname: pat"
38663863
let fieldname = self.parse_field_name()?;
38673864
self.bump();
3868-
let pat = self.parse_pat()?;
3865+
let pat = self.parse_pat(None)?;
38693866
hi = pat.span;
38703867
(pat, fieldname, false)
38713868
} else {
@@ -4067,7 +4064,7 @@ impl<'a> Parser<'a> {
40674064
/// "top-level" patterns in a match arm, `for` loop, `let`, &c. (in contrast
40684065
/// to subpatterns within such).
40694066
fn parse_top_level_pat(&mut self) -> PResult<'a, P<Pat>> {
4070-
let pat = self.parse_pat()?;
4067+
let pat = self.parse_pat(None)?;
40714068
if self.token == token::Comma {
40724069
// An unexpected comma after a top-level pattern is a clue that the
40734070
// user (perhaps more accustomed to some other language) forgot the
@@ -4099,13 +4096,17 @@ impl<'a> Parser<'a> {
40994096
}
41004097

41014098
/// Parse a pattern.
4102-
pub fn parse_pat(&mut self) -> PResult<'a, P<Pat>> {
4103-
self.parse_pat_with_range_pat(true)
4099+
pub fn parse_pat(&mut self, expected: Option<&'static str>) -> PResult<'a, P<Pat>> {
4100+
self.parse_pat_with_range_pat(true, expected)
41044101
}
41054102

41064103
/// Parse a pattern, with a setting whether modern range patterns e.g. `a..=b`, `a..b` are
41074104
/// allowed.
4108-
fn parse_pat_with_range_pat(&mut self, allow_range_pat: bool) -> PResult<'a, P<Pat>> {
4105+
fn parse_pat_with_range_pat(
4106+
&mut self,
4107+
allow_range_pat: bool,
4108+
expected: Option<&'static str>,
4109+
) -> PResult<'a, P<Pat>> {
41094110
maybe_whole!(self, NtPat, |x| x);
41104111

41114112
let lo = self.span;
@@ -4121,7 +4122,7 @@ impl<'a> Parser<'a> {
41214122
err.span_label(self.span, "unexpected lifetime");
41224123
return Err(err);
41234124
}
4124-
let subpat = self.parse_pat_with_range_pat(false)?;
4125+
let subpat = self.parse_pat_with_range_pat(false, expected)?;
41254126
pat = PatKind::Ref(subpat, mutbl);
41264127
}
41274128
token::OpenDelim(token::Paren) => {
@@ -4167,7 +4168,7 @@ impl<'a> Parser<'a> {
41674168
pat = self.parse_pat_ident(BindingMode::ByRef(mutbl))?;
41684169
} else if self.eat_keyword(keywords::Box) {
41694170
// Parse box pat
4170-
let subpat = self.parse_pat_with_range_pat(false)?;
4171+
let subpat = self.parse_pat_with_range_pat(false, None)?;
41714172
pat = PatKind::Box(subpat);
41724173
} else if self.token.is_ident() && !self.token.is_reserved_ident() &&
41734174
self.parse_as_ident() {
@@ -4267,9 +4268,14 @@ impl<'a> Parser<'a> {
42674268
}
42684269
Err(mut err) => {
42694270
self.cancel(&mut err);
4270-
let msg = format!("expected pattern, found {}", self.this_token_descr());
4271+
let expected = expected.unwrap_or("pattern");
4272+
let msg = format!(
4273+
"expected {}, found {}",
4274+
expected,
4275+
self.this_token_descr(),
4276+
);
42714277
let mut err = self.fatal(&msg);
4272-
err.span_label(self.span, "expected pattern");
4278+
err.span_label(self.span, format!("expected {}", expected));
42734279
return Err(err);
42744280
}
42754281
}
@@ -4313,7 +4319,7 @@ impl<'a> Parser<'a> {
43134319
-> PResult<'a, PatKind> {
43144320
let ident = self.parse_ident()?;
43154321
let sub = if self.eat(&token::At) {
4316-
Some(self.parse_pat()?)
4322+
Some(self.parse_pat(Some("binding pattern"))?)
43174323
} else {
43184324
None
43194325
};

src/libsyntax/util/parser_testing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub fn string_to_item (source_str : String) -> Option<P<ast::Item>> {
6868
pub fn string_to_pat(source_str: String) -> P<ast::Pat> {
6969
let ps = ParseSess::new(FilePathMapping::empty());
7070
with_error_checking_parse(source_str, &ps, |p| {
71-
p.parse_pat()
71+
p.parse_pat(None)
7272
})
7373
}
7474

src/test/ui/parser/issue-33413.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
// compile-flags: -Z parse-only
1212

1313
impl S {
14-
fn f(*, a: u8) -> u8 {} //~ ERROR expected pattern, found `*`
14+
fn f(*, a: u8) -> u8 {}
15+
//~^ ERROR expected argument name, found `*`
1516
}

src/test/ui/parser/issue-33413.stderr

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
error: expected pattern, found `*`
1+
error: expected argument name, found `*`
22
--> $DIR/issue-33413.rs:14:10
33
|
4-
LL | fn f(*, a: u8) -> u8 {} //~ ERROR expected pattern, found `*`
5-
| ^
6-
| |
7-
| expected pattern
8-
| expected argument name
4+
LL | fn f(*, a: u8) -> u8 {}
5+
| ^ expected argument name
96

107
error: aborting due to previous error
118

src/test/ui/parser/lifetime-in-pattern.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error: unexpected lifetime `'a` in pattern
22
--> $DIR/lifetime-in-pattern.rs:13:10
33
|
44
LL | fn test(&'a str) {
5-
| ^^
6-
| |
7-
| unexpected lifetime
8-
| expected argument name
5+
| ^^ unexpected lifetime
96

107
error: aborting due to previous error
118

src/test/ui/parser/removed-syntax-mode.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010

1111
// compile-flags: -Z parse-only
1212

13-
fn f(+x: isize) {} //~ ERROR expected pattern, found `+`
13+
fn f(+x: isize) {}
14+
//~^ ERROR expected argument name, found `+`
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
error: expected pattern, found `+`
1+
error: expected argument name, found `+`
22
--> $DIR/removed-syntax-mode.rs:13:6
33
|
4-
LL | fn f(+x: isize) {} //~ ERROR expected pattern, found `+`
5-
| ^
6-
| |
7-
| expected pattern
8-
| expected argument name
4+
LL | fn f(+x: isize) {}
5+
| ^ expected argument name
96

107
error: aborting due to previous error
118

src/test/ui/self/self-vs-path-ambiguity.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error: unexpected lifetime `'a` in pattern
22
--> $DIR/self-vs-path-ambiguity.rs:19:11
33
|
44
LL | fn i(&'a self::S: &S) {} //~ ERROR unexpected lifetime `'a` in pattern
5-
| ^^
6-
| |
7-
| unexpected lifetime
8-
| expected argument name
5+
| ^^ unexpected lifetime
96

107
error: aborting due to previous error
118

0 commit comments

Comments
 (0)