Skip to content

Commit 3d9923d

Browse files
committed
rollup merge of #20424: jroesch/tuple-struct-where-clause-fix
Fixes #17904. All the cases that I believe we should support are detailed in the test case, let me know if there is there is any more desired behavior. cc @japaric. r? @nikomatsakis or whoever is appropriate.
2 parents 0dd0742 + c02fac4 commit 3d9923d

File tree

5 files changed

+127
-42
lines changed

5 files changed

+127
-42
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 89 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4885,67 +4885,116 @@ impl<'a> Parser<'a> {
48854885
self.span_err(ty.span, "`virtual` structs have been removed from the language");
48864886
}
48874887

4888-
self.parse_where_clause(&mut generics);
4888+
// There is a special case worth noting here, as reported in issue #17904.
4889+
// If we are parsing a tuple struct it is the case that the where clause
4890+
// should follow the field list. Like so:
4891+
//
4892+
// struct Foo<T>(T) where T: Copy;
4893+
//
4894+
// If we are parsing a normal record-style struct it is the case
4895+
// that the where clause comes before the body, and after the generics.
4896+
// So if we look ahead and see a brace or a where-clause we begin
4897+
// parsing a record style struct.
4898+
//
4899+
// Otherwise if we look ahead and see a paren we parse a tuple-style
4900+
// struct.
4901+
4902+
let (fields, ctor_id) = if self.token.is_keyword(keywords::Where) {
4903+
self.parse_where_clause(&mut generics);
4904+
if self.eat(&token::Semi) {
4905+
// If we see a: `struct Foo<T> where T: Copy;` style decl.
4906+
(Vec::new(), Some(ast::DUMMY_NODE_ID))
4907+
} else {
4908+
// If we see: `struct Foo<T> where T: Copy { ... }`
4909+
(self.parse_record_struct_body(&class_name), None)
4910+
}
4911+
// No `where` so: `struct Foo<T>;`
4912+
} else if self.eat(&token::Semi) {
4913+
(Vec::new(), Some(ast::DUMMY_NODE_ID))
4914+
// Record-style struct definition
4915+
} else if self.token == token::OpenDelim(token::Brace) {
4916+
let fields = self.parse_record_struct_body(&class_name);
4917+
(fields, None)
4918+
// Tuple-style struct definition with optional where-clause.
4919+
} else {
4920+
let fields = self.parse_tuple_struct_body(&class_name, &mut generics);
4921+
(fields, Some(ast::DUMMY_NODE_ID))
4922+
};
48894923

4890-
let mut fields: Vec<StructField>;
4891-
let is_tuple_like;
4924+
(class_name,
4925+
ItemStruct(P(ast::StructDef {
4926+
fields: fields,
4927+
ctor_id: ctor_id,
4928+
}), generics),
4929+
None)
4930+
}
48924931

4932+
pub fn parse_record_struct_body(&mut self, class_name: &ast::Ident) -> Vec<StructField> {
4933+
let mut fields = Vec::new();
48934934
if self.eat(&token::OpenDelim(token::Brace)) {
4894-
// It's a record-like struct.
4895-
is_tuple_like = false;
4896-
fields = Vec::new();
48974935
while self.token != token::CloseDelim(token::Brace) {
48984936
fields.push(self.parse_struct_decl_field(true));
48994937
}
4938+
49004939
if fields.len() == 0 {
49014940
self.fatal(format!("unit-like struct definition should be \
4902-
written as `struct {};`",
4903-
token::get_ident(class_name))[]);
4941+
written as `struct {};`",
4942+
token::get_ident(class_name.clone()))[]);
49044943
}
4944+
49054945
self.bump();
4906-
} else if self.check(&token::OpenDelim(token::Paren)) {
4907-
// It's a tuple-like struct.
4908-
is_tuple_like = true;
4909-
fields = self.parse_unspanned_seq(
4946+
} else {
4947+
let token_str = self.this_token_to_string();
4948+
self.fatal(format!("expected `where`, or `{}` after struct \
4949+
name, found `{}`", "{",
4950+
token_str)[]);
4951+
}
4952+
4953+
fields
4954+
}
4955+
4956+
pub fn parse_tuple_struct_body(&mut self,
4957+
class_name: &ast::Ident,
4958+
generics: &mut ast::Generics)
4959+
-> Vec<StructField> {
4960+
// This is the case where we find `struct Foo<T>(T) where T: Copy;`
4961+
if self.check(&token::OpenDelim(token::Paren)) {
4962+
let fields = self.parse_unspanned_seq(
49104963
&token::OpenDelim(token::Paren),
49114964
&token::CloseDelim(token::Paren),
49124965
seq_sep_trailing_allowed(token::Comma),
49134966
|p| {
4914-
let attrs = p.parse_outer_attributes();
4915-
let lo = p.span.lo;
4916-
let struct_field_ = ast::StructField_ {
4917-
kind: UnnamedField(p.parse_visibility()),
4918-
id: ast::DUMMY_NODE_ID,
4919-
ty: p.parse_ty_sum(),
4920-
attrs: attrs,
4921-
};
4922-
spanned(lo, p.span.hi, struct_field_)
4923-
});
4967+
let attrs = p.parse_outer_attributes();
4968+
let lo = p.span.lo;
4969+
let struct_field_ = ast::StructField_ {
4970+
kind: UnnamedField(p.parse_visibility()),
4971+
id: ast::DUMMY_NODE_ID,
4972+
ty: p.parse_ty_sum(),
4973+
attrs: attrs,
4974+
};
4975+
spanned(lo, p.span.hi, struct_field_)
4976+
});
4977+
49244978
if fields.len() == 0 {
49254979
self.fatal(format!("unit-like struct definition should be \
4926-
written as `struct {};`",
4927-
token::get_ident(class_name))[]);
4980+
written as `struct {};`",
4981+
token::get_ident(class_name.clone()))[]);
49284982
}
4983+
4984+
self.parse_where_clause(generics);
49294985
self.expect(&token::Semi);
4930-
} else if self.eat(&token::Semi) {
4931-
// It's a unit-like struct.
4932-
is_tuple_like = true;
4933-
fields = Vec::new();
4986+
fields
4987+
// This is the case where we just see struct Foo<T> where T: Copy;
4988+
} else if self.token.is_keyword(keywords::Where) {
4989+
self.parse_where_clause(generics);
4990+
self.expect(&token::Semi);
4991+
Vec::new()
4992+
// This case is where we see: `struct Foo<T>;`
49344993
} else {
49354994
let token_str = self.this_token_to_string();
4936-
self.fatal(format!("expected `{}`, `(`, or `;` after struct \
4937-
name, found `{}`", "{",
4938-
token_str)[])
4995+
self.fatal(format!("expected `where`, `{}`, `(`, or `;` after struct \
4996+
name, found `{}`", "{", token_str)[]);
49394997
}
4940-
4941-
let _ = ast::DUMMY_NODE_ID; // FIXME: Workaround for crazy bug.
4942-
let new_id = ast::DUMMY_NODE_ID;
4943-
(class_name,
4944-
ItemStruct(P(ast::StructDef {
4945-
fields: fields,
4946-
ctor_id: if is_tuple_like { Some(new_id) } else { None },
4947-
}), generics),
4948-
None)
49494998
}
49504999

49515000
/// Parse a structure field declaration

src/libsyntax/print/pprust.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,6 @@ impl<'a> State<'a> {
10561056
span: codemap::Span) -> IoResult<()> {
10571057
try!(self.print_ident(ident));
10581058
try!(self.print_generics(generics));
1059-
try!(self.print_where_clause(generics));
10601059
if ast_util::struct_def_is_tuple_like(struct_def) {
10611060
if !struct_def.fields.is_empty() {
10621061
try!(self.popen());
@@ -1075,10 +1074,12 @@ impl<'a> State<'a> {
10751074
));
10761075
try!(self.pclose());
10771076
}
1077+
try!(self.print_where_clause(generics));
10781078
try!(word(&mut self.s, ";"));
10791079
try!(self.end());
10801080
self.end() // close the outer-box
10811081
} else {
1082+
try!(self.print_where_clause(generics));
10821083
try!(self.nbsp());
10831084
try!(self.bopen());
10841085
try!(self.hardbreak_if_not_bol());

src/test/compile-fail/issue-17904.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
struct Baz<U> where U: Eq(U); //This is parsed as the new Fn* style parenthesis syntax.
12+
struct Baz<U> where U: Eq(U) -> R; // Notice this parses as well.
13+
struct Baz<U>(U) where U: Eq; // This rightfully signals no error as well.
14+
struct Foo<T> where T: Copy, (T); //~ ERROR unexpected token in `where` clause
15+
struct Bar<T> { x: T } where T: Copy //~ ERROR expected item, found `where`
16+
17+
fn main() {}

src/test/compile-fail/unsized.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
// Test syntax checks for `type` keyword.
1212

13-
struct S1 for type; //~ ERROR expected `{`, `(`, or `;` after struct name, found `for`
13+
struct S1 for type; //~ ERROR expected `where`, `{`, `(`, or `;` after struct name, found `for`
1414

1515
pub fn main() {
1616
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
struct Foo<T> where T: Copy;
12+
struct Bar<T>(T) where T: Copy;
13+
struct Bleh<T, U>(T, U) where T: Copy, U: Sized;
14+
struct Baz<T> where T: Copy {
15+
field: T
16+
}
17+
18+
fn main() {}

0 commit comments

Comments
 (0)