Skip to content

Commit 375cb2e

Browse files
committed
Improve some expected/found error messages from parser
1 parent a8f5047 commit 375cb2e

10 files changed

+61
-21
lines changed

src/libsyntax/parse/parser.rs

+49-12
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,22 @@ pub enum TokenType {
192192
Token(token::Token),
193193
Keyword(keywords::Keyword),
194194
Operator,
195+
Lifetime,
196+
Ident,
197+
Path,
198+
Type,
195199
}
196200

197201
impl TokenType {
198202
fn to_string(&self) -> String {
199203
match *self {
200204
TokenType::Token(ref t) => format!("`{}`", Parser::token_to_string(t)),
201-
TokenType::Operator => "an operator".to_string(),
202205
TokenType::Keyword(kw) => format!("`{}`", kw.name()),
206+
TokenType::Operator => "an operator".to_string(),
207+
TokenType::Lifetime => "lifetime".to_string(),
208+
TokenType::Ident => "identifier".to_string(),
209+
TokenType::Path => "path".to_string(),
210+
TokenType::Type => "type".to_string(),
203211
}
204212
}
205213
}
@@ -552,6 +560,33 @@ impl<'a> Parser<'a> {
552560
}
553561
}
554562

563+
fn check_ident(&mut self) -> bool {
564+
if self.token.is_ident() {
565+
true
566+
} else {
567+
self.expected_tokens.push(TokenType::Ident);
568+
false
569+
}
570+
}
571+
572+
fn check_path(&mut self) -> bool {
573+
if self.token.is_path_start() {
574+
true
575+
} else {
576+
self.expected_tokens.push(TokenType::Path);
577+
false
578+
}
579+
}
580+
581+
fn check_type(&mut self) -> bool {
582+
if self.token.can_begin_type() {
583+
true
584+
} else {
585+
self.expected_tokens.push(TokenType::Type);
586+
false
587+
}
588+
}
589+
555590
/// Expect and consume an `&`. If `&&` is seen, replace it with a single
556591
/// `&` and continue. If an `&` is not seen, signal an error.
557592
fn expect_and(&mut self) -> PResult<'a, ()> {
@@ -1802,7 +1837,10 @@ impl<'a> Parser<'a> {
18021837
name: ident.name
18031838
})
18041839
}
1805-
_ => None
1840+
_ => {
1841+
self.expected_tokens.push(TokenType::Lifetime);
1842+
None
1843+
}
18061844
}
18071845
}
18081846

@@ -3953,7 +3991,7 @@ impl<'a> Parser<'a> {
39533991
"`?` may only modify trait bounds, not lifetime bounds");
39543992
}
39553993
bounds.push(RegionTyParamBound(lifetime));
3956-
} else if self.token.is_keyword(keywords::For) || self.token.is_path_start() {
3994+
} else {if self.check_keyword(keywords::For) || self.check_path() {
39573995
let poly_trait_ref = self.parse_poly_trait_ref()?;
39583996
let modifier = if question.is_some() {
39593997
TraitBoundModifier::Maybe
@@ -3963,7 +4001,7 @@ impl<'a> Parser<'a> {
39634001
bounds.push(TraitTyParamBound(poly_trait_ref, modifier));
39644002
} else {
39654003
break
3966-
}
4004+
}}
39674005

39684006
// Trailing plus is not allowed for now and we have to detect it.
39694007
let is_bound_start = |token: &token::Token| {
@@ -4047,7 +4085,7 @@ impl<'a> Parser<'a> {
40474085
self.span_err(self.prev_span,
40484086
"lifetime parameters must be declared prior to type parameters");
40494087
}
4050-
} else if self.token.is_ident() {
4088+
} else {if self.check_ident() {
40514089
// Parse type parameter.
40524090
ty_params.push(self.parse_ty_param(attrs)?);
40534091
seen_ty_param = true;
@@ -4059,7 +4097,7 @@ impl<'a> Parser<'a> {
40594097
&format!("trailing attribute after {} parameters", param_kind));
40604098
}
40614099
break
4062-
}
4100+
}}
40634101

40644102
if !self.eat(&token::Comma) {
40654103
break
@@ -4105,15 +4143,14 @@ impl<'a> Parser<'a> {
41054143
let mut seen_type = false;
41064144
let mut seen_binding = false;
41074145
loop {
4108-
let eq_is_next = self.look_ahead(1, |t| t == &token::Eq); // borrowck workaround
41094146
if let Some(lifetime) = self.eat_lifetime() {
41104147
// Parse lifetime argument.
41114148
lifetimes.push(lifetime);
41124149
if seen_type || seen_binding {
41134150
self.span_err(self.prev_span,
41144151
"lifetime parameters must be declared prior to type parameters");
41154152
}
4116-
} else if self.token.is_ident() && eq_is_next {
4153+
} else {if self.check_ident() && self.look_ahead(1, |t| t == &token::Eq) {
41174154
// Parse associated type binding.
41184155
let lo = self.span.lo;
41194156
let ident = self.parse_ident()?;
@@ -4126,7 +4163,7 @@ impl<'a> Parser<'a> {
41264163
span: mk_sp(lo, self.prev_span.hi),
41274164
});
41284165
seen_binding = true;
4129-
} else if self.token.can_begin_type() {
4166+
} else if self.check_type() {
41304167
// Parse type argument.
41314168
types.push(self.parse_ty()?);
41324169
if seen_binding {
@@ -4136,7 +4173,7 @@ impl<'a> Parser<'a> {
41364173
seen_type = true;
41374174
} else {
41384175
break
4139-
}
4176+
}}
41404177

41414178
if !self.eat(&token::Comma) {
41424179
break
@@ -4192,7 +4229,7 @@ impl<'a> Parser<'a> {
41924229
bounds: bounds,
41934230
}
41944231
));
4195-
} else if self.token.can_begin_type() {
4232+
} else {if self.check_type() {
41964233
// Parse optional `for<'a, 'b>`.
41974234
// This `for` is parsed greedily and applies to the whole predicate,
41984235
// the bounded type can have its own `for` applying only to it.
@@ -4230,7 +4267,7 @@ impl<'a> Parser<'a> {
42304267
}
42314268
} else {
42324269
break
4233-
}
4270+
}}
42344271

42354272
if !self.eat(&token::Comma) {
42364273
break

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type Type_1_<'a, T> = &'a T;
2222
//type Type_2 = Type_1_<'static ()>; // error: expected `,` or `>` after lifetime name, found `(`
2323

2424

25-
type Type_3<T> = Box<T,,>; //~ error: expected `>`, found `,`
25+
type Type_3<T> = Box<T,,>; //~ error: expected one of `>`, identifier, lifetime, or type, found `,`
2626

2727

2828
//type Type_4<T> = Type_1_<'static,, T>; // error: expected type, found `,`

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ type Type_1_<'a, T> = &'a T;
2525
//type Type_3<T> = Box<T,,>; // error: expected type, found `,`
2626

2727

28-
type Type_4<T> = Type_1_<'static,, T>; //~ error: expected `>`, found `,`
28+
type Type_4<T> = Type_1_<'static,, T>;
29+
//~^ error: expected one of `>`, identifier, lifetime, or type, found `,`
2930

3031

3132
type Type_5_<'a> = Type_1_<'a, ()>;

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ type Type_1_<'a, T> = &'a T;
3131
type Type_5_<'a> = Type_1_<'a, ()>;
3232

3333

34-
type Type_5<'a> = Type_1_<'a, (),,>; //~ error: expected `>`, found `,`
34+
type Type_5<'a> = Type_1_<'a, (),,>;
35+
//~^ error: expected one of `>`, identifier, lifetime, or type, found `,`
3536

3637

3738
//type Type_6 = Type_5_<'a,,>; // error: expected type, found `,`

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ type Type_5_<'a> = Type_1_<'a, ()>;
3434
//type Type_5<'a> = Type_1_<'a, (),,>; // error: expected type, found `,`
3535

3636

37-
type Type_6 = Type_5_<'a,,>; //~ error: expected `>`, found `,`
37+
type Type_6 = Type_5_<'a,,>;
38+
//~^ error: expected one of `>`, identifier, lifetime, or type, found `,`
3839

3940

4041
//type Type_7 = Box<(),,>; // error: expected type, found `,`

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type Type_5_<'a> = Type_1_<'a, ()>;
3737
//type Type_6 = Type_5_<'a,,>; // error: expected type, found `,`
3838

3939

40-
type Type_7 = Box<(),,>; //~ error: expected `>`, found `,`
40+
type Type_7 = Box<(),,>; //~ error: expected one of `>`, identifier, lifetime, or type, found `,`
4141

4242

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

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

+1-1
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 `>`, found `,`
43+
type Type_8<'a,,> = &'a (); //~ error: expected one of `>`, identifier, or lifetime, found `,`
4444

4545

4646
//type Type_9<T,,> = Box<T>; // error: expected identifier, found `,`

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ type Type_5_<'a> = Type_1_<'a, ()>;
4343
//type Type_8<'a,,> = &'a (); // error: expected identifier, found `,`
4444

4545

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

src/test/parse-fail/bounds-lifetime-3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

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

13-
type A = for<,> fn(); //~ ERROR expected `>`, found `,`
13+
type A = for<,> fn(); //~ ERROR expected one of `>`, identifier, or lifetime, found `,`
1414

1515
fn main() {}

src/test/parse-fail/bounds-lifetime-where-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

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

13-
type A where , = u8; //~ ERROR expected `=`, found `,`
13+
type A where , = u8; //~ ERROR expected one of `=`, lifetime, or type, found `,`
1414

1515
fn main() {}

0 commit comments

Comments
 (0)