about summary refs log tree commit diff
diff options
context:
space:
mode:
authorvarkor <github@varkor.com>2019-02-07 10:10:11 +0100
committervarkor <github@varkor.com>2019-02-07 15:02:17 +0100
commitbbdcc4e7ce697bbd6ad398987c5e4ec4928f5da4 (patch)
tree507022857394927fbb9adb152dcb55f70887ea72
parent899d013fefba27b66c655af4199ccecf8fb9fdae (diff)
downloadrust-bbdcc4e7ce697bbd6ad398987c5e4ec4928f5da4.tar.gz
rust-bbdcc4e7ce697bbd6ad398987c5e4ec4928f5da4.zip
Adjust parser generic parameter errors
-rw-r--r--src/libsyntax/parse/parser.rs53
-rw-r--r--src/libsyntax/parse/token.rs1
-rw-r--r--src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-1.rs2
-rw-r--r--src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-1.stderr2
-rw-r--r--src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-2.rs2
-rw-r--r--src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-2.stderr2
-rw-r--r--src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-3.rs2
-rw-r--r--src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-3.stderr2
-rw-r--r--src/test/ui/const-generics/const-expression-parameter.rs19
-rw-r--r--src/test/ui/const-generics/const-expression-parameter.stderr26
-rw-r--r--src/test/ui/issues/issue-20616-3.rs5
-rw-r--r--src/test/ui/issues/issue-20616-3.stderr4
-rw-r--r--src/test/ui/issues/issue-20616-7.rs5
-rw-r--r--src/test/ui/issues/issue-20616-7.stderr4
-rw-r--r--src/test/ui/issues/issue-20616-8.rs5
-rw-r--r--src/test/ui/issues/issue-20616-8.stderr4
-rw-r--r--src/test/ui/issues/issue-20616-9.rs5
-rw-r--r--src/test/ui/issues/issue-20616-9.stderr4
18 files changed, 93 insertions, 54 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index 2ff450af423..7640f0bdf06 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -948,10 +948,6 @@ impl<'a> Parser<'a> {
         }
     }
 
-    fn check_const_param(&mut self) -> bool {
-        self.check_keyword(keywords::Const)
-    }
-
     fn check_const_arg(&mut self) -> bool {
         if self.token.can_begin_const_arg() {
             true
@@ -1046,7 +1042,8 @@ impl<'a> Parser<'a> {
     }
 
     /// Attempt to consume a `<`. If `<<` is seen, replace it with a single
-    /// `<` and continue. If a `<` is not seen, return false.
+    /// `<` and continue. If `<-` is seen, replace it with a single `<`
+    /// and continue. If a `<` is not seen, return false.
     ///
     /// This is meant to be used when parsing generics on a path to get the
     /// starting token.
@@ -1062,6 +1059,11 @@ impl<'a> Parser<'a> {
                 self.bump_with(token::Lt, span);
                 true
             }
+            token::LArrow => {
+                let span = self.span.with_lo(self.span.lo() + BytePos(1));
+                self.bump_with(token::BinOp(token::Minus), span);
+                true
+            }
             _ => false,
         };
 
@@ -5518,7 +5520,6 @@ impl<'a> Parser<'a> {
     /// trailing comma and erroneous trailing attributes.
     crate fn parse_generic_params(&mut self) -> PResult<'a, Vec<ast::GenericParam>> {
         let mut params = Vec::new();
-        let mut prev_param: Option<ParamKindOrd> = None;
         loop {
             let attrs = self.parse_outer_attributes()?;
             if self.check_lifetime() {
@@ -5536,29 +5537,21 @@ impl<'a> Parser<'a> {
                     bounds,
                     kind: ast::GenericParamKind::Lifetime,
                 });
-                prev_param = Some(ParamKindOrd::Lifetime);
-            } else if self.check_const_param() {
+            } else if self.check_keyword(keywords::Const) {
                 // Parse const parameter.
                 params.push(self.parse_const_param(attrs)?);
-                prev_param = Some(ParamKindOrd::Const);
             } else if self.check_ident() {
                 // Parse type parameter.
                 params.push(self.parse_ty_param(attrs)?);
-                prev_param = Some(ParamKindOrd::Type);
             } else {
                 // Check for trailing attributes and stop parsing.
-                if !attrs.is_empty() {
-                    if let Some(prev_param) = prev_param {
-                        self.struct_span_err(
-                            attrs[0].span,
-                            &format!(
-                                "trailing attribute after {} parameter",
-                                prev_param,
-                            ),
-                        )
-                        .span_label(attrs[0].span, "attributes must go before parameters")
-                        .emit();
-                    }
+                if !attrs.is_empty() && !params.is_empty() {
+                    self.struct_span_err(
+                        attrs[0].span,
+                        &format!("trailing attribute after generic parameter"),
+                    )
+                    .span_label(attrs[0].span, "attributes must go before parameters")
+                    .emit();
                 }
                 break
             }
@@ -5774,19 +5767,25 @@ impl<'a> Parser<'a> {
                 });
                 assoc_ty_bindings.push(span);
             } else if self.check_const_arg() {
+                // FIXME(const_generics): to distinguish between idents for types and consts,
+                // we should introduce a GenericArg::Ident in the AST and distinguish when
+                // lowering to the HIR. For now, idents for const args are not permitted.
+
                 // Parse const argument.
                 let expr = if let token::OpenDelim(token::Brace) = self.token {
                     self.parse_block_expr(None, self.span, BlockCheckMode::Default, ThinVec::new())?
-                } else if self.token.can_begin_literal_or_bool() {
-                    let lit = self.parse_lit()?;
-                    self.mk_expr(lit.span, ExprKind::Lit(lit), ThinVec::new())
-                } else {
+                } else if self.token.is_ident() {
                     // FIXME(const_generics): to distinguish between idents for types and consts,
                     // we should introduce a GenericArg::Ident in the AST and distinguish when
                     // lowering to the HIR. For now, idents for const args are not permitted.
                     return Err(
                         self.fatal("identifiers may currently not be used for const generics")
                     );
+                } else {
+                    // FIXME(const_generics): this currently conflicts with emplacement syntax
+                    // with negative integer literals.
+                    let lit = self.parse_lit()?;
+                    self.mk_expr(lit.span, ExprKind::Lit(lit), ThinVec::new())
                 };
                 let value = AnonConst {
                     id: ast::DUMMY_NODE_ID,
@@ -6444,7 +6443,7 @@ impl<'a> Parser<'a> {
         //     `<` (LIFETIME|IDENT) `,` - first generic parameter in a list
         //     `<` (LIFETIME|IDENT) `:` - generic parameter with bounds
         //     `<` (LIFETIME|IDENT) `=` - generic parameter with a default
-        //     `<` const IDENT          - generic const parameter
+        //     `<` const                - generic const parameter
         // The only truly ambiguous case is
         //     `<` IDENT `>` `::` IDENT ...
         // we disambiguate it in favor of generics (`impl<T> ::absolute::Path<T> { ... }`)
diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs
index dad32072739..c3885f0d04d 100644
--- a/src/libsyntax/parse/token.rs
+++ b/src/libsyntax/parse/token.rs
@@ -284,7 +284,6 @@ impl Token {
         match self {
             OpenDelim(Brace) => true,
             Interpolated(ref nt) => match nt.0 {
-                NtExpr(..) => true,
                 NtBlock(..) => true,
                 NtLiteral(..) => true,
                 _ => false,
diff --git a/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-1.rs b/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-1.rs
index 2ef5f13f3d4..ca5fdd9da85 100644
--- a/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-1.rs
+++ b/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-1.rs
@@ -7,7 +7,7 @@
 struct RefIntPair<'a, 'b>(&'a u32, &'b u32);
 
 impl<#[rustc_1] 'a, 'b, #[oops]> RefIntPair<'a, 'b> {
-    //~^ ERROR trailing attribute after lifetime parameter
+    //~^ ERROR trailing attribute after generic parameter
 }
 
 fn main() {
diff --git a/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-1.stderr b/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-1.stderr
index 97c0a19a3da..55e7a987784 100644
--- a/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-1.stderr
+++ b/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-1.stderr
@@ -1,4 +1,4 @@
-error: trailing attribute after lifetime parameter
+error: trailing attribute after generic parameter
   --> $DIR/attrs-with-no-formal-in-generics-1.rs:9:25
    |
 LL | impl<#[rustc_1] 'a, 'b, #[oops]> RefIntPair<'a, 'b> {
diff --git a/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-2.rs b/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-2.rs
index f58ddd5fbda..c795612acf0 100644
--- a/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-2.rs
+++ b/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-2.rs
@@ -7,6 +7,6 @@
 struct RefAny<'a, T>(&'a T);
 
 impl<#[rustc_1] 'a, #[rustc_2] T, #[oops]> RefAny<'a, T> {}
-//~^ ERROR trailing attribute after type parameter
+//~^ ERROR trailing attribute after generic parameter
 
 fn main() {}
diff --git a/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-2.stderr b/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-2.stderr
index ff7ced77f25..acd0ae3678a 100644
--- a/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-2.stderr
+++ b/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-2.stderr
@@ -1,4 +1,4 @@
-error: trailing attribute after type parameter
+error: trailing attribute after generic parameter
   --> $DIR/attrs-with-no-formal-in-generics-2.rs:9:35
    |
 LL | impl<#[rustc_1] 'a, #[rustc_2] T, #[oops]> RefAny<'a, T> {}
diff --git a/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-3.rs b/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-3.rs
index 44a7c9d7c8b..3cfc70b4185 100644
--- a/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-3.rs
+++ b/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-3.rs
@@ -6,7 +6,7 @@ struct RefIntPair<'a, 'b>(&'a u32, &'b u32);
 
 fn hof_lt<Q>(_: Q)
     where Q: for <#[allow(unused)] 'a, 'b, #[oops]> Fn(RefIntPair<'a,'b>) -> &'b u32
-    //~^ ERROR trailing attribute after lifetime parameter
+    //~^ ERROR trailing attribute after generic parameter
 {}
 
 fn main() {}
diff --git a/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-3.stderr b/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-3.stderr
index b383e21e161..b9ca0097467 100644
--- a/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-3.stderr
+++ b/src/test/ui/attrs-with-no-formal-in-generics/attrs-with-no-formal-in-generics-3.stderr
@@ -1,4 +1,4 @@
-error: trailing attribute after lifetime parameter
+error: trailing attribute after generic parameter
   --> $DIR/attrs-with-no-formal-in-generics-3.rs:8:44
    |
 LL |     where Q: for <#[allow(unused)] 'a, 'b, #[oops]> Fn(RefIntPair<'a,'b>) -> &'b u32
diff --git a/src/test/ui/const-generics/const-expression-parameter.rs b/src/test/ui/const-generics/const-expression-parameter.rs
new file mode 100644
index 00000000000..c582c596bfc
--- /dev/null
+++ b/src/test/ui/const-generics/const-expression-parameter.rs
@@ -0,0 +1,19 @@
+#![feature(const_generics)]
+//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
+
+fn u32_identity<const X: u32>() -> u32 {
+    //~^ ERROR const generics in any position are currently unsupported
+    5
+}
+
+fn foo_a() {
+    u32_identity::<-1>(); //~ ERROR expected identifier, found `<-`
+}
+
+fn foo_b() {
+    u32_identity::<1 + 2>(); //~ ERROR expected one of `,` or `>`, found `+`
+}
+
+fn main() {
+    u32_identity::<5>(); // ok
+}
diff --git a/src/test/ui/const-generics/const-expression-parameter.stderr b/src/test/ui/const-generics/const-expression-parameter.stderr
new file mode 100644
index 00000000000..1dd3a960316
--- /dev/null
+++ b/src/test/ui/const-generics/const-expression-parameter.stderr
@@ -0,0 +1,26 @@
+error: expected identifier, found `<-`
+  --> $DIR/const-expression-parameter.rs:10:19
+   |
+LL |     u32_identity::<-1>(); //~ ERROR expected identifier, found `<-`
+   |                   ^^ expected identifier
+
+error: expected one of `,` or `>`, found `+`
+  --> $DIR/const-expression-parameter.rs:14:22
+   |
+LL |     u32_identity::<1 + 2>(); //~ ERROR expected one of `,` or `>`, found `+`
+   |                      ^ expected one of `,` or `>` here
+
+warning: the feature `const_generics` is incomplete and may cause the compiler to crash
+  --> $DIR/const-expression-parameter.rs:1:12
+   |
+LL | #![feature(const_generics)]
+   |            ^^^^^^^^^^^^^^
+
+error: const generics in any position are currently unsupported
+  --> $DIR/const-expression-parameter.rs:4:23
+   |
+LL | fn u32_identity<const X: u32>() -> u32 {
+   |                       ^
+
+error: aborting due to 3 previous errors
+
diff --git a/src/test/ui/issues/issue-20616-3.rs b/src/test/ui/issues/issue-20616-3.rs
index 9f983f74f5b..9bfd5bf2313 100644
--- a/src/test/ui/issues/issue-20616-3.rs
+++ b/src/test/ui/issues/issue-20616-3.rs
@@ -1,5 +1,3 @@
-// ignore-tidy-linelength
-
 // We need all these 9 issue-20616-N.rs files
 // because we can only catch one parsing error at a time
 
@@ -12,7 +10,8 @@ type Type_1_<'a, T> = &'a T;
 //type Type_2 = Type_1_<'static ()>; // error: expected `,` or `>` after lifetime name, found `(`
 
 
-type Type_3<T> = Box<T,,>; //~ error: expected one of `>`, const, identifier, lifetime, or type, found `,`
+type Type_3<T> = Box<T,,>;
+//~^ error: expected one of `>`, const, identifier, lifetime, or type, found `,`
 
 
 //type Type_4<T> = Type_1_<'static,, T>; // error: expected type, found `,`
diff --git a/src/test/ui/issues/issue-20616-3.stderr b/src/test/ui/issues/issue-20616-3.stderr
index e8edb5ba70f..f51fb949c74 100644
--- a/src/test/ui/issues/issue-20616-3.stderr
+++ b/src/test/ui/issues/issue-20616-3.stderr
@@ -1,7 +1,7 @@
 error: expected one of `>`, const, identifier, lifetime, or type, found `,`
-  --> $DIR/issue-20616-3.rs:15:24
+  --> $DIR/issue-20616-3.rs:13:24
    |
-LL | type Type_3<T> = Box<T,,>; //~ error: expected one of `>`, const, identifier, lifetime, or type, found `,`
+LL | type Type_3<T> = Box<T,,>;
    |                        ^ expected one of `>`, const, identifier, lifetime, or type here
 
 error: aborting due to previous error
diff --git a/src/test/ui/issues/issue-20616-7.rs b/src/test/ui/issues/issue-20616-7.rs
index 277b4524506..ffd1620c1d3 100644
--- a/src/test/ui/issues/issue-20616-7.rs
+++ b/src/test/ui/issues/issue-20616-7.rs
@@ -1,5 +1,3 @@
-// ignore-tidy-linelength
-
 // We need all these 9 issue-20616-N.rs files
 // because we can only catch one parsing error at a time
 
@@ -27,7 +25,8 @@ type Type_5_<'a> = Type_1_<'a, ()>;
 //type Type_6 = Type_5_<'a,,>; // error: expected type, found `,`
 
 
-type Type_7 = Box<(),,>; //~ error: expected one of `>`, const, identifier, lifetime, or type, found `,`
+type Type_7 = Box<(),,>;
+//~^ error: expected one of `>`, const, identifier, lifetime, or type, found `,`
 
 
 //type Type_8<'a,,> = &'a (); // error: expected ident, found `,`
diff --git a/src/test/ui/issues/issue-20616-7.stderr b/src/test/ui/issues/issue-20616-7.stderr
index c1422d849fc..c0e108375be 100644
--- a/src/test/ui/issues/issue-20616-7.stderr
+++ b/src/test/ui/issues/issue-20616-7.stderr
@@ -1,7 +1,7 @@
 error: expected one of `>`, const, identifier, lifetime, or type, found `,`
-  --> $DIR/issue-20616-7.rs:30:22
+  --> $DIR/issue-20616-7.rs:28:22
    |
-LL | type Type_7 = Box<(),,>; //~ error: expected one of `>`, const, identifier, lifetime, or type, found `,`
+LL | type Type_7 = Box<(),,>;
    |                      ^ expected one of `>`, const, identifier, lifetime, or type here
 
 error: aborting due to previous error
diff --git a/src/test/ui/issues/issue-20616-8.rs b/src/test/ui/issues/issue-20616-8.rs
index 756119e0127..c9e8b61e50b 100644
--- a/src/test/ui/issues/issue-20616-8.rs
+++ b/src/test/ui/issues/issue-20616-8.rs
@@ -1,5 +1,3 @@
-// ignore-tidy-linelength
-
 // We need all these 9 issue-20616-N.rs files
 // because we can only catch one parsing error at a time
 
@@ -30,7 +28,8 @@ type Type_5_<'a> = Type_1_<'a, ()>;
 //type Type_7 = Box<(),,>; // error: expected type, found `,`
 
 
-type Type_8<'a,,> = &'a (); //~ error: expected one of `>`, `const`, identifier, or lifetime, found `,`
+type Type_8<'a,,> = &'a ();
+//~^ error: expected one of `>`, `const`, identifier, or lifetime, found `,`
 
 
 //type Type_9<T,,> = Box<T>; // error: expected identifier, found `,`
diff --git a/src/test/ui/issues/issue-20616-8.stderr b/src/test/ui/issues/issue-20616-8.stderr
index cfe3ec57712..0ef9192f1e7 100644
--- a/src/test/ui/issues/issue-20616-8.stderr
+++ b/src/test/ui/issues/issue-20616-8.stderr
@@ -1,7 +1,7 @@
 error: expected one of `>`, `const`, identifier, or lifetime, found `,`
-  --> $DIR/issue-20616-8.rs:33:16
+  --> $DIR/issue-20616-8.rs:31:16
    |
-LL | type Type_8<'a,,> = &'a (); //~ error: expected one of `>`, `const`, identifier, or lifetime, found `,`
+LL | type Type_8<'a,,> = &'a ();
    |                ^ expected one of `>`, `const`, identifier, or lifetime here
 
 error: aborting due to previous error
diff --git a/src/test/ui/issues/issue-20616-9.rs b/src/test/ui/issues/issue-20616-9.rs
index 6074fbb2204..1c509f26fd6 100644
--- a/src/test/ui/issues/issue-20616-9.rs
+++ b/src/test/ui/issues/issue-20616-9.rs
@@ -1,5 +1,3 @@
-// ignore-tidy-linelength
-
 // We need all these 9 issue-20616-N.rs files
 // because we can only catch one parsing error at a time
 
@@ -33,4 +31,5 @@ type Type_5_<'a> = Type_1_<'a, ()>;
 //type Type_8<'a,,> = &'a (); // error: expected identifier, found `,`
 
 
-type Type_9<T,,> = Box<T>; //~ error: expected one of `>`, `const`, identifier, or lifetime, found `,`
+type Type_9<T,,> = Box<T>;
+//~^ error: expected one of `>`, `const`, identifier, or lifetime, found `,`
diff --git a/src/test/ui/issues/issue-20616-9.stderr b/src/test/ui/issues/issue-20616-9.stderr
index 960e8c8c086..5fd1400a2e8 100644
--- a/src/test/ui/issues/issue-20616-9.stderr
+++ b/src/test/ui/issues/issue-20616-9.stderr
@@ -1,7 +1,7 @@
 error: expected one of `>`, `const`, identifier, or lifetime, found `,`
-  --> $DIR/issue-20616-9.rs:36:15
+  --> $DIR/issue-20616-9.rs:34:15
    |
-LL | type Type_9<T,,> = Box<T>; //~ error: expected one of `>`, `const`, identifier, or lifetime, found `,`
+LL | type Type_9<T,,> = Box<T>;
    |               ^ expected one of `>`, `const`, identifier, or lifetime here
 
 error: aborting due to previous error