Implement empty struct with braces (RFC 218)

This commit is contained in:
Vadim Petrochenkov
2015-09-10 22:46:52 +03:00
parent cff0411706
commit 5fa6e857c9
11 changed files with 114 additions and 133 deletions

View File

@@ -2231,14 +2231,6 @@ impl<'a> Parser<'a> {
&[token::CloseDelim(token::Brace)]));
}
if fields.is_empty() && base.is_none() {
let last_span = self.last_span;
self.span_err(last_span,
"structure literal must either \
have at least one field or use \
structure update syntax");
}
hi = self.span.hi;
try!(self.expect(&token::CloseDelim(token::Brace)));
ex = ExprStruct(pth, fields, base);
@@ -4713,14 +4705,14 @@ impl<'a> Parser<'a> {
(Vec::new(), Some(ast::DUMMY_NODE_ID))
} else {
// If we see: `struct Foo<T> where T: Copy { ... }`
(try!(self.parse_record_struct_body(&class_name)), None)
(try!(self.parse_record_struct_body()), None)
}
// No `where` so: `struct Foo<T>;`
} else if try!(self.eat(&token::Semi) ){
(Vec::new(), Some(ast::DUMMY_NODE_ID))
// Record-style struct definition
} else if self.token == token::OpenDelim(token::Brace) {
let fields = try!(self.parse_record_struct_body(&class_name));
let fields = try!(self.parse_record_struct_body());
(fields, None)
// Tuple-style struct definition with optional where-clause.
} else if self.token == token::OpenDelim(token::Paren) {
@@ -4740,20 +4732,13 @@ impl<'a> Parser<'a> {
None))
}
pub fn parse_record_struct_body(&mut self,
class_name: &ast::Ident) -> PResult<Vec<StructField>> {
pub fn parse_record_struct_body(&mut self) -> PResult<Vec<StructField>> {
let mut fields = Vec::new();
if try!(self.eat(&token::OpenDelim(token::Brace)) ){
while self.token != token::CloseDelim(token::Brace) {
fields.push(try!(self.parse_struct_decl_field(true)));
}
if fields.is_empty() {
return Err(self.fatal(&format!("unit-like struct definition should be \
written as `struct {};`",
class_name)));
}
try!(self.bump());
} else {
let token_str = self.this_token_to_string();

View File

@@ -1855,34 +1855,34 @@ impl<'a> State<'a> {
fields: &[ast::Field],
wth: &Option<P<ast::Expr>>) -> io::Result<()> {
try!(self.print_path(path, true, 0));
if !(fields.is_empty() && wth.is_none()) {
try!(word(&mut self.s, "{"));
try!(self.commasep_cmnt(
Consistent,
&fields[..],
|s, field| {
try!(s.ibox(indent_unit));
try!(s.print_ident(field.ident.node));
try!(s.word_space(":"));
try!(s.print_expr(&*field.expr));
s.end()
},
|f| f.span));
match *wth {
Some(ref expr) => {
try!(self.ibox(indent_unit));
if !fields.is_empty() {
try!(word(&mut self.s, ","));
try!(space(&mut self.s));
}
try!(word(&mut self.s, ".."));
try!(self.print_expr(&**expr));
try!(self.end());
try!(word(&mut self.s, "{"));
try!(self.commasep_cmnt(
Consistent,
&fields[..],
|s, field| {
try!(s.ibox(indent_unit));
try!(s.print_ident(field.ident.node));
try!(s.word_space(":"));
try!(s.print_expr(&*field.expr));
s.end()
},
|f| f.span));
match *wth {
Some(ref expr) => {
try!(self.ibox(indent_unit));
if !fields.is_empty() {
try!(word(&mut self.s, ","));
try!(space(&mut self.s));
}
_ => try!(word(&mut self.s, ",")),
try!(word(&mut self.s, ".."));
try!(self.print_expr(&**expr));
try!(self.end());
}
_ => if !fields.is_empty() {
try!(word(&mut self.s, ","))
}
try!(word(&mut self.s, "}"));
}
try!(word(&mut self.s, "}"));
Ok(())
}