]> git.lizzy.rs Git - rust.git/commitdiff
Fix the parsing of where-clauses for structs
authorJared Roesch <roeschinc@gmail.com>
Fri, 2 Jan 2015 12:02:50 +0000 (04:02 -0800)
committerJared Roesch <roeschinc@gmail.com>
Mon, 5 Jan 2015 01:33:31 +0000 (17:33 -0800)
src/libsyntax/parse/parser.rs
src/test/run-pass/issue-17904.rs [new file with mode: 0644]

index 37ac86a33242e54e26ec80233add9c034ccf902f..570ef67bbccfd75646d5fc7bbb5743a43b50ed76 100644 (file)
@@ -4875,11 +4875,26 @@ fn parse_item_struct(&mut self) -> ItemInfo {
             self.span_err(ty.span, "`virtual` structs have been removed from the language");
         }
 
-        self.parse_where_clause(&mut generics);
-
         let mut fields: Vec<StructField>;
         let is_tuple_like;
 
+        // There is a special case worth noting here, as reported in issue #17904.
+        // If we are parsing a tuple struct it is the case that the where clause
+        // should follow the field list. Like so:
+        //
+        // struct Foo<T>(T) where T: Copy;
+        //
+        // If we are parsing a normal record-style struct it is the case
+        // that the where clause comes before the body, and after the generics.
+        // So if we look ahead and see a brace or a where-clause we begin
+        // parsing a record style struct.
+        //
+        // Otherwise if we look ahead and see a paren we parse a tuple-style
+        // struct.
+
+        // Will parse the where-clause if it precedes the brace.
+        self.parse_where_clause(&mut generics);
+
         if self.eat(&token::OpenDelim(token::Brace)) {
             // It's a record-like struct.
             is_tuple_like = false;
@@ -4916,8 +4931,11 @@ fn parse_item_struct(&mut self) -> ItemInfo {
                                     written as `struct {};`",
                                    token::get_ident(class_name))[]);
             }
+            self.parse_where_clause(&mut generics);
             self.expect(&token::Semi);
-        } else if self.eat(&token::Semi) {
+        } else if self.token.is_keyword(keywords::Where) || self.eat(&token::Semi) {
+            // We can find a where clause here.
+            self.parse_where_clause(&mut generics);
             // It's a unit-like struct.
             is_tuple_like = true;
             fields = Vec::new();
diff --git a/src/test/run-pass/issue-17904.rs b/src/test/run-pass/issue-17904.rs
new file mode 100644 (file)
index 0000000..c297637
--- /dev/null
@@ -0,0 +1,22 @@
+// 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.
+
+// Tests that type assignability is used to search for instances when
+// making method calls, but only if there aren't any matches without
+// it.
+
+struct Foo<T> where T: Copy;
+struct Bar<T>(T) where T: Copy;
+struct Bleh<T, U>(T, U) where T: Copy, U: Sized;
+struct Baz<T> where T: Copy {
+    field: T
+}
+
+fn main() {}