From c8da5697e0721641856c8e38b666a9798e9f761d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 7 Sep 2015 20:08:57 +0300 Subject: [PATCH 1/3] libsyntax: remove dead code from parser.rs Both `parse_tuple_struct_body` and `parse_item_struct` handled the case of unit like struct. The redundancy is removed, `parse_tuple_struct_body` now handles only real tuple structs. --- src/libsyntax/parse/parser.rs | 59 ++++++++++++++--------------------- 1 file changed, 24 insertions(+), 35 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 0772d124db8e4..b13522af82d91 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -4769,43 +4769,32 @@ impl<'a> Parser<'a> { generics: &mut ast::Generics) -> PResult> { // This is the case where we find `struct Foo(T) where T: Copy;` - if self.check(&token::OpenDelim(token::Paren)) { - let fields = try!(self.parse_unspanned_seq( - &token::OpenDelim(token::Paren), - &token::CloseDelim(token::Paren), - seq_sep_trailing_allowed(token::Comma), - |p| { - let attrs = p.parse_outer_attributes(); - let lo = p.span.lo; - let struct_field_ = ast::StructField_ { - kind: UnnamedField(try!(p.parse_visibility())), - id: ast::DUMMY_NODE_ID, - ty: try!(p.parse_ty_sum()), - attrs: attrs, - }; - Ok(spanned(lo, p.span.hi, struct_field_)) - })); - - if fields.is_empty() { - return Err(self.fatal(&format!("unit-like struct definition should be \ - written as `struct {};`", - class_name))); - } + // Unit like structs are handled in parse_item_struct function + let fields = try!(self.parse_unspanned_seq( + &token::OpenDelim(token::Paren), + &token::CloseDelim(token::Paren), + seq_sep_trailing_allowed(token::Comma), + |p| { + let attrs = p.parse_outer_attributes(); + let lo = p.span.lo; + let struct_field_ = ast::StructField_ { + kind: UnnamedField(try!(p.parse_visibility())), + id: ast::DUMMY_NODE_ID, + ty: try!(p.parse_ty_sum()), + attrs: attrs, + }; + Ok(spanned(lo, p.span.hi, struct_field_)) + })); - generics.where_clause = try!(self.parse_where_clause()); - try!(self.expect(&token::Semi)); - Ok(fields) - // This is the case where we just see struct Foo where T: Copy; - } else if self.token.is_keyword(keywords::Where) { - generics.where_clause = try!(self.parse_where_clause()); - try!(self.expect(&token::Semi)); - Ok(Vec::new()) - // This case is where we see: `struct Foo;` - } else { - let token_str = self.this_token_to_string(); - Err(self.fatal(&format!("expected `where`, `{}`, `(`, or `;` after struct \ - name, found `{}`", "{", token_str))) + if fields.is_empty() { + return Err(self.fatal(&format!("unit-like struct definition should be \ + written as `struct {};`", + class_name))); } + + generics.where_clause = try!(self.parse_where_clause()); + try!(self.expect(&token::Semi)); + Ok(fields) } /// Parse a structure field declaration From 0e96c2823649b92fee835046e7073e428408931a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 7 Sep 2015 22:15:36 +0300 Subject: [PATCH 2/3] libsyntax: restore lost error message --- src/libsyntax/parse/parser.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index b13522af82d91..543ca8851106a 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -4726,9 +4726,13 @@ impl<'a> Parser<'a> { let fields = try!(self.parse_record_struct_body(&class_name)); (fields, None) // Tuple-style struct definition with optional where-clause. - } else { + } else if self.token == token::OpenDelim(token::Paren) { let fields = try!(self.parse_tuple_struct_body(&class_name, &mut generics)); (fields, Some(ast::DUMMY_NODE_ID)) + } else { + let token_str = self.this_token_to_string(); + return Err(self.fatal(&format!("expected `where`, `{}`, `(`, or `;` after struct \ + name, found `{}`", "{", token_str))) }; Ok((class_name, From c87a58fe4b5c2f64ecd1ad3697bdb0403c76af4c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 10 Sep 2015 15:14:24 +0300 Subject: [PATCH 3/3] libsyntax: minor clean up Escape `{` in format strings as `{{`, instead of using a substitution --- src/libsyntax/parse/parser.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 543ca8851106a..a1ea5daf063bc 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -4731,8 +4731,8 @@ impl<'a> Parser<'a> { (fields, Some(ast::DUMMY_NODE_ID)) } else { let token_str = self.this_token_to_string(); - return Err(self.fatal(&format!("expected `where`, `{}`, `(`, or `;` after struct \ - name, found `{}`", "{", token_str))) + return Err(self.fatal(&format!("expected `where`, `{{`, `(`, or `;` after struct \ + name, found `{}`", token_str))) }; Ok((class_name, @@ -4760,8 +4760,8 @@ impl<'a> Parser<'a> { try!(self.bump()); } else { let token_str = self.this_token_to_string(); - return Err(self.fatal(&format!("expected `where`, or `{}` after struct \ - name, found `{}`", "{", + return Err(self.fatal(&format!("expected `where`, or `{{` after struct \ + name, found `{}`", token_str))); }