]> git.lizzy.rs Git - rust.git/commitdiff
Improve some expected/found error messages from parser
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Wed, 18 Jan 2017 16:01:04 +0000 (19:01 +0300)
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Tue, 24 Jan 2017 19:56:02 +0000 (22:56 +0300)
src/libsyntax/parse/parser.rs
src/test/compile-fail/issue-20616-3.rs
src/test/compile-fail/issue-20616-4.rs
src/test/compile-fail/issue-20616-5.rs
src/test/compile-fail/issue-20616-6.rs
src/test/compile-fail/issue-20616-7.rs
src/test/compile-fail/issue-20616-8.rs
src/test/compile-fail/issue-20616-9.rs
src/test/parse-fail/bounds-lifetime-3.rs
src/test/parse-fail/bounds-lifetime-where-2.rs

index c589f1a7aaaa00481c8955a6d00a27b45c4ea96d..939f126640d99f1e6fb799b58e155ce22ee36ac6 100644 (file)
@@ -192,14 +192,22 @@ pub enum TokenType {
     Token(token::Token),
     Keyword(keywords::Keyword),
     Operator,
+    Lifetime,
+    Ident,
+    Path,
+    Type,
 }
 
 impl TokenType {
     fn to_string(&self) -> String {
         match *self {
             TokenType::Token(ref t) => format!("`{}`", Parser::token_to_string(t)),
-            TokenType::Operator => "an operator".to_string(),
             TokenType::Keyword(kw) => format!("`{}`", kw.name()),
+            TokenType::Operator => "an operator".to_string(),
+            TokenType::Lifetime => "lifetime".to_string(),
+            TokenType::Ident => "identifier".to_string(),
+            TokenType::Path => "path".to_string(),
+            TokenType::Type => "type".to_string(),
         }
     }
 }
@@ -552,6 +560,33 @@ pub fn check_reserved_keywords(&mut self) {
         }
     }
 
+    fn check_ident(&mut self) -> bool {
+        if self.token.is_ident() {
+            true
+        } else {
+            self.expected_tokens.push(TokenType::Ident);
+            false
+        }
+    }
+
+    fn check_path(&mut self) -> bool {
+        if self.token.is_path_start() {
+            true
+        } else {
+            self.expected_tokens.push(TokenType::Path);
+            false
+        }
+    }
+
+    fn check_type(&mut self) -> bool {
+        if self.token.can_begin_type() {
+            true
+        } else {
+            self.expected_tokens.push(TokenType::Type);
+            false
+        }
+    }
+
     /// Expect and consume an `&`. If `&&` is seen, replace it with a single
     /// `&` and continue. If an `&` is not seen, signal an error.
     fn expect_and(&mut self) -> PResult<'a, ()> {
@@ -1802,7 +1837,10 @@ pub fn eat_lifetime(&mut self) -> Option<Lifetime> {
                     name: ident.name
                 })
             }
-            _ => None
+            _ => {
+                self.expected_tokens.push(TokenType::Lifetime);
+                None
+            }
         }
     }
 
@@ -3953,7 +3991,7 @@ fn parse_ty_param_bounds(&mut self) -> PResult<'a, TyParamBounds>
                                   "`?` may only modify trait bounds, not lifetime bounds");
                 }
                 bounds.push(RegionTyParamBound(lifetime));
-            } else if self.token.is_keyword(keywords::For) || self.token.is_path_start() {
+            } else {if self.check_keyword(keywords::For) || self.check_path() {
                 let poly_trait_ref = self.parse_poly_trait_ref()?;
                 let modifier = if question.is_some() {
                     TraitBoundModifier::Maybe
@@ -3963,7 +4001,7 @@ fn parse_ty_param_bounds(&mut self) -> PResult<'a, TyParamBounds>
                 bounds.push(TraitTyParamBound(poly_trait_ref, modifier));
             } else {
                 break
-            }
+            }}
 
             // Trailing plus is not allowed for now and we have to detect it.
             let is_bound_start = |token: &token::Token| {
@@ -4047,7 +4085,7 @@ pub fn parse_generic_params(&mut self) -> PResult<'a, (Vec<LifetimeDef>, Vec<TyP
                     self.span_err(self.prev_span,
                         "lifetime parameters must be declared prior to type parameters");
                 }
-            } else if self.token.is_ident() {
+            } else {if self.check_ident() {
                 // Parse type parameter.
                 ty_params.push(self.parse_ty_param(attrs)?);
                 seen_ty_param = true;
@@ -4059,7 +4097,7 @@ pub fn parse_generic_params(&mut self) -> PResult<'a, (Vec<LifetimeDef>, Vec<TyP
                         &format!("trailing attribute after {} parameters", param_kind));
                 }
                 break
-            }
+            }}
 
             if !self.eat(&token::Comma) {
                 break
@@ -4105,7 +4143,6 @@ fn parse_generic_args(&mut self) -> PResult<'a, (Vec<Lifetime>, Vec<P<Ty>>, Vec<
         let mut seen_type = false;
         let mut seen_binding = false;
         loop {
-            let eq_is_next = self.look_ahead(1, |t| t == &token::Eq); // borrowck workaround
             if let Some(lifetime) = self.eat_lifetime() {
                 // Parse lifetime argument.
                 lifetimes.push(lifetime);
@@ -4113,7 +4150,7 @@ fn parse_generic_args(&mut self) -> PResult<'a, (Vec<Lifetime>, Vec<P<Ty>>, Vec<
                     self.span_err(self.prev_span,
                         "lifetime parameters must be declared prior to type parameters");
                 }
-            } else if self.token.is_ident() && eq_is_next {
+            } else {if self.check_ident() && self.look_ahead(1, |t| t == &token::Eq) {
                 // Parse associated type binding.
                 let lo = self.span.lo;
                 let ident = self.parse_ident()?;
@@ -4126,7 +4163,7 @@ fn parse_generic_args(&mut self) -> PResult<'a, (Vec<Lifetime>, Vec<P<Ty>>, Vec<
                     span: mk_sp(lo, self.prev_span.hi),
                 });
                 seen_binding = true;
-            } else if self.token.can_begin_type() {
+            } else if self.check_type() {
                 // Parse type argument.
                 types.push(self.parse_ty()?);
                 if seen_binding {
@@ -4136,7 +4173,7 @@ fn parse_generic_args(&mut self) -> PResult<'a, (Vec<Lifetime>, Vec<P<Ty>>, Vec<
                 seen_type = true;
             } else {
                 break
-            }
+            }}
 
             if !self.eat(&token::Comma) {
                 break
@@ -4192,7 +4229,7 @@ pub fn parse_where_clause(&mut self) -> PResult<'a, WhereClause> {
                         bounds: bounds,
                     }
                 ));
-            } else if self.token.can_begin_type() {
+            } else {if self.check_type() {
                 // Parse optional `for<'a, 'b>`.
                 // This `for` is parsed greedily and applies to the whole predicate,
                 // the bounded type can have its own `for` applying only to it.
@@ -4230,7 +4267,7 @@ pub fn parse_where_clause(&mut self) -> PResult<'a, WhereClause> {
                 }
             } else {
                 break
-            }
+            }}
 
             if !self.eat(&token::Comma) {
                 break
index 9a5972a7a1641ae0b85e416d02ba6c727e802fd6..e5ed46d2cb3b01c575be7f351eae779f0ce94d6d 100644 (file)
@@ -22,7 +22,7 @@
 //type Type_2 = Type_1_<'static ()>; // error: expected `,` or `>` after lifetime name, found `(`
 
 
-type Type_3<T> = Box<T,,>; //~ error: expected `>`, found `,`
+type Type_3<T> = Box<T,,>; //~ error: expected one of `>`, identifier, lifetime, or type, found `,`
 
 
 //type Type_4<T> = Type_1_<'static,, T>; // error: expected type, found `,`
index 1567698e476ba0b90af6307247abc98543b8e271..9b731289e138bd17aa845f7977fda72cbf40f508 100644 (file)
@@ -25,7 +25,8 @@
 //type Type_3<T> = Box<T,,>; // error: expected type, found `,`
 
 
-type Type_4<T> = Type_1_<'static,, T>; //~ error: expected `>`, found `,`
+type Type_4<T> = Type_1_<'static,, T>;
+//~^ error: expected one of `>`, identifier, lifetime, or type, found `,`
 
 
 type Type_5_<'a> = Type_1_<'a, ()>;
index c5a0624574d0e2b79b529a2d0c9bc1242a210ee3..5e3b024da9a075c86f4726cd3e9cecff732857e8 100644 (file)
@@ -31,7 +31,8 @@
 type Type_5_<'a> = Type_1_<'a, ()>;
 
 
-type Type_5<'a> = Type_1_<'a, (),,>; //~ error: expected `>`, found `,`
+type Type_5<'a> = Type_1_<'a, (),,>;
+//~^ error: expected one of `>`, identifier, lifetime, or type, found `,`
 
 
 //type Type_6 = Type_5_<'a,,>; // error: expected type, found `,`
index 56578409546ff758ee98d6789e4a645fc1fbcbda..b6ee26f9f62b88a87be7445a4f779c1f10af3b99 100644 (file)
@@ -34,7 +34,8 @@
 //type Type_5<'a> = Type_1_<'a, (),,>; // error: expected type, found `,`
 
 
-type Type_6 = Type_5_<'a,,>; //~ error: expected `>`, found `,`
+type Type_6 = Type_5_<'a,,>;
+//~^ error: expected one of `>`, identifier, lifetime, or type, found `,`
 
 
 //type Type_7 = Box<(),,>; // error: expected type, found `,`
index ecd0a467cf6b069bdaf6596086adc1a47354d4db..fef3dd4e31d5ad787e6beb68815ccc307055e1bf 100644 (file)
@@ -37,7 +37,7 @@
 //type Type_6 = Type_5_<'a,,>; // error: expected type, found `,`
 
 
-type Type_7 = Box<(),,>; //~ error: expected `>`, found `,`
+type Type_7 = Box<(),,>; //~ error: expected one of `>`, identifier, lifetime, or type, found `,`
 
 
 //type Type_8<'a,,> = &'a (); // error: expected ident, found `,`
index 535672c65e46e457eabd39e170ac73968878323e..b7bef47c4f4425bed0f00262cb6b6bf602036ef2 100644 (file)
@@ -40,7 +40,7 @@
 //type Type_7 = Box<(),,>; // error: expected type, found `,`
 
 
-type Type_8<'a,,> = &'a (); //~ error: expected `>`, found `,`
+type Type_8<'a,,> = &'a (); //~ error: expected one of `>`, identifier, or lifetime, found `,`
 
 
 //type Type_9<T,,> = Box<T>; // error: expected identifier, found `,`
index b666a8b67aa3f09a7334ffcbcc96fdf272bd2401..5c16d24cef854e1ac2a458a313a85916f0e44ba9 100644 (file)
@@ -43,4 +43,4 @@
 //type Type_8<'a,,> = &'a (); // error: expected identifier, found `,`
 
 
-type Type_9<T,,> = Box<T>; //~ error: expected `>`, found `,`
+type Type_9<T,,> = Box<T>; //~ error: expected one of `>`, identifier, or lifetime, found `,`
index be7c197245e49bb87760bcb1ba2ba31aeae9ee01..e04431598156c8febff8ac6672e3c31df4746a7e 100644 (file)
@@ -10,6 +10,6 @@
 
 // compile-flags: -Z parse-only
 
-type A = for<,> fn(); //~ ERROR expected `>`, found `,`
+type A = for<,> fn(); //~ ERROR expected one of `>`, identifier, or lifetime, found `,`
 
 fn main() {}
index 97dcd5cc5f8c8adb9deac6500ab5ed8b3d241b52..ffcacdf357df5abd00b60d8c1a689f062aea9136 100644 (file)
@@ -10,6 +10,6 @@
 
 // compile-flags: -Z parse-only
 
-type A where , = u8; //~ ERROR expected `=`, found `,`
+type A where , = u8; //~ ERROR expected one of `=`, lifetime, or type, found `,`
 
 fn main() {}