]> git.lizzy.rs Git - rust.git/commitdiff
Improve error message for lifetimes after type params.
authorKevin Butler <haqkrs@gmail.com>
Fri, 23 May 2014 19:51:21 +0000 (20:51 +0100)
committerKevin Butler <haqkrs@gmail.com>
Fri, 23 May 2014 19:51:21 +0000 (20:51 +0100)
Closes #14303.

src/libsyntax/parse/parser.rs
src/test/compile-fail/issue-14303-enum.rs [new file with mode: 0644]
src/test/compile-fail/issue-14303-fn-def.rs [new file with mode: 0644]
src/test/compile-fail/issue-14303-fncall.rs [new file with mode: 0644]
src/test/compile-fail/issue-14303-impl.rs [new file with mode: 0644]
src/test/compile-fail/issue-14303-path.rs [new file with mode: 0644]
src/test/compile-fail/issue-14303-struct.rs [new file with mode: 0644]
src/test/compile-fail/issue-14303-trait.rs [new file with mode: 0644]

index c117b5b0128474c9c0d4ad720ceb6c87d4446c20..ae10470728476a4584a11babec46f532ed09c8cc 100644 (file)
@@ -3425,6 +3425,7 @@ pub fn parse_generics(&mut self) -> ast::Generics {
             let lifetimes = self.parse_lifetimes();
             let mut seen_default = false;
             let ty_params = self.parse_seq_to_gt(Some(token::COMMA), |p| {
+                p.forbid_lifetime();
                 let ty_param = p.parse_ty_param();
                 if ty_param.default.is_some() {
                     seen_default = true;
@@ -3444,10 +3445,21 @@ fn parse_generic_values_after_lt(&mut self) -> (Vec<ast::Lifetime>, Vec<P<Ty>> )
         let lifetimes = self.parse_lifetimes();
         let result = self.parse_seq_to_gt(
             Some(token::COMMA),
-            |p| p.parse_ty(false));
+            |p| {
+                p.forbid_lifetime();
+                p.parse_ty(false)
+            }
+        );
         (lifetimes, result.into_vec())
     }
 
+    fn forbid_lifetime(&mut self) {
+        if Parser::token_is_lifetime(&self.token) {
+            self.span_fatal(self.span, "lifetime parameters must be declared \
+                                        prior to type parameters");
+        }
+    }
+
     fn parse_fn_args(&mut self, named_args: bool, allow_variadic: bool)
                      -> (Vec<Arg> , bool) {
         let sp = self.span;
diff --git a/src/test/compile-fail/issue-14303-enum.rs b/src/test/compile-fail/issue-14303-enum.rs
new file mode 100644 (file)
index 0000000..a26b7fd
--- /dev/null
@@ -0,0 +1,14 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+enum X<'a, T, 'b> {
+//~^ ERROR lifetime parameters must be declared prior to type parameters
+    A(&'a T)
+}
diff --git a/src/test/compile-fail/issue-14303-fn-def.rs b/src/test/compile-fail/issue-14303-fn-def.rs
new file mode 100644 (file)
index 0000000..aaf9541
--- /dev/null
@@ -0,0 +1,12 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn foo<'a, T, 'b>(x: &'a T) {}
+//~^ ERROR lifetime parameters must be declared prior to type parameters
diff --git a/src/test/compile-fail/issue-14303-fncall.rs b/src/test/compile-fail/issue-14303-fncall.rs
new file mode 100644 (file)
index 0000000..3a5c8bb
--- /dev/null
@@ -0,0 +1,16 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn main() {
+    range(0, 4)
+    .map(|x| x * 2)
+    .collect::<Vec<'a, uint, 'b>>()
+    //~^ ERROR lifetime parameters must be declared prior to type parameters
+}
diff --git a/src/test/compile-fail/issue-14303-impl.rs b/src/test/compile-fail/issue-14303-impl.rs
new file mode 100644 (file)
index 0000000..46d0219
--- /dev/null
@@ -0,0 +1,14 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+struct X { x: int }
+
+impl<'a, T, 'b> X {}
+//~^ ERROR lifetime parameters must be declared prior to type parameters
diff --git a/src/test/compile-fail/issue-14303-path.rs b/src/test/compile-fail/issue-14303-path.rs
new file mode 100644 (file)
index 0000000..30cc41c
--- /dev/null
@@ -0,0 +1,12 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn bar<'a, T>(x: mymodule::X<'a, T, 'b, 'c>) {}
+//~^ ERROR lifetime parameters must be declared prior to type parameters
diff --git a/src/test/compile-fail/issue-14303-struct.rs b/src/test/compile-fail/issue-14303-struct.rs
new file mode 100644 (file)
index 0000000..6edd808
--- /dev/null
@@ -0,0 +1,14 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+struct X<'a, T, 'b> {
+//~^ ERROR lifetime parameters must be declared prior to type parameters
+    x: &'a T
+}
diff --git a/src/test/compile-fail/issue-14303-trait.rs b/src/test/compile-fail/issue-14303-trait.rs
new file mode 100644 (file)
index 0000000..753acdd
--- /dev/null
@@ -0,0 +1,12 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+trait Foo<'a, T, 'b> {}
+//~^ ERROR lifetime parameters must be declared prior to type parameters