Skip to content

Commit c54932c

Browse files
committed
Fix the parsing of where-clauses for structs
1 parent 5773bde commit c54932c

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4875,11 +4875,26 @@ impl<'a> Parser<'a> {
48754875
self.span_err(ty.span, "`virtual` structs have been removed from the language");
48764876
}
48774877

4878-
self.parse_where_clause(&mut generics);
4879-
48804878
let mut fields: Vec<StructField>;
48814879
let is_tuple_like;
48824880

4881+
// There is a special case worth noting here, as reported in issue #17904.
4882+
// If we are parsing a tuple struct it is the case that the where clause
4883+
// should follow the field list. Like so:
4884+
//
4885+
// struct Foo<T>(T) where T: Copy;
4886+
//
4887+
// If we are parsing a normal record-style struct it is the case
4888+
// that the where clause comes before the body, and after the generics.
4889+
// So if we look ahead and see a brace or a where-clause we begin
4890+
// parsing a record style struct.
4891+
//
4892+
// Otherwise if we look ahead and see a paren we parse a tuple-style
4893+
// struct.
4894+
4895+
// Will parse the where-clause if it precedes the brace.
4896+
self.parse_where_clause(&mut generics);
4897+
48834898
if self.eat(&token::OpenDelim(token::Brace)) {
48844899
// It's a record-like struct.
48854900
is_tuple_like = false;
@@ -4916,8 +4931,11 @@ impl<'a> Parser<'a> {
49164931
written as `struct {};`",
49174932
token::get_ident(class_name))[]);
49184933
}
4934+
self.parse_where_clause(&mut generics);
49194935
self.expect(&token::Semi);
4920-
} else if self.eat(&token::Semi) {
4936+
} else if self.token.is_keyword(keywords::Where) || self.eat(&token::Semi) {
4937+
// We can find a where clause here.
4938+
self.parse_where_clause(&mut generics);
49214939
// It's a unit-like struct.
49224940
is_tuple_like = true;
49234941
fields = Vec::new();

src/test/run-pass/issue-17904.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Tests that type assignability is used to search for instances when
12+
// making method calls, but only if there aren't any matches without
13+
// it.
14+
15+
struct Foo<T> where T: Copy;
16+
struct Bar<T>(T) where T: Copy;
17+
struct Bleh<T, U>(T, U) where T: Copy, U: Sized;
18+
struct Baz<T> where T: Copy {
19+
field: T
20+
}
21+
22+
fn main() {}

0 commit comments

Comments
 (0)