Skip to content

Commit 9040b06

Browse files
committed
Auto merge of #28286 - matklad:remove-dead-code, r=eddyb
There is a dead code in libsyntax/parser/parse.rs, when parsing structs. Two functions are involved: * [parse_item_struct](https://github.com/rust-lang/rust/blob/cd9c9f048f6aa0be091cd9835771ba0712bead4e/src/libsyntax/parse/parser.rs#L4691) * [parse_tuple_struct_body](https://github.com/rust-lang/rust/blob/cd9c9f048f6aa0be091cd9835771ba0712bead4e/src/libsyntax/parse/parser.rs#L4769) The problem is that both functions handle the case with unit structs. But because `parse_tuple_struct_body` is called from `parse_item_struct`, it never faces this case. This PR removes unit struct case from `parse_tuple_struct_body` function. I tested with `make -j8 check-statge1`.
2 parents fd230ff + c87a58f commit 9040b06

File tree

1 file changed

+31
-38
lines changed

1 file changed

+31
-38
lines changed

src/libsyntax/parse/parser.rs

+31-38
Original file line numberDiff line numberDiff line change
@@ -4723,9 +4723,13 @@ impl<'a> Parser<'a> {
47234723
let fields = try!(self.parse_record_struct_body(&class_name));
47244724
(fields, None)
47254725
// Tuple-style struct definition with optional where-clause.
4726-
} else {
4726+
} else if self.token == token::OpenDelim(token::Paren) {
47274727
let fields = try!(self.parse_tuple_struct_body(&class_name, &mut generics));
47284728
(fields, Some(ast::DUMMY_NODE_ID))
4729+
} else {
4730+
let token_str = self.this_token_to_string();
4731+
return Err(self.fatal(&format!("expected `where`, `{{`, `(`, or `;` after struct \
4732+
name, found `{}`", token_str)))
47294733
};
47304734

47314735
Ok((class_name,
@@ -4753,8 +4757,8 @@ impl<'a> Parser<'a> {
47534757
try!(self.bump());
47544758
} else {
47554759
let token_str = self.this_token_to_string();
4756-
return Err(self.fatal(&format!("expected `where`, or `{}` after struct \
4757-
name, found `{}`", "{",
4760+
return Err(self.fatal(&format!("expected `where`, or `{{` after struct \
4761+
name, found `{}`",
47584762
token_str)));
47594763
}
47604764

@@ -4766,43 +4770,32 @@ impl<'a> Parser<'a> {
47664770
generics: &mut ast::Generics)
47674771
-> PResult<Vec<StructField>> {
47684772
// This is the case where we find `struct Foo<T>(T) where T: Copy;`
4769-
if self.check(&token::OpenDelim(token::Paren)) {
4770-
let fields = try!(self.parse_unspanned_seq(
4771-
&token::OpenDelim(token::Paren),
4772-
&token::CloseDelim(token::Paren),
4773-
seq_sep_trailing_allowed(token::Comma),
4774-
|p| {
4775-
let attrs = p.parse_outer_attributes();
4776-
let lo = p.span.lo;
4777-
let struct_field_ = ast::StructField_ {
4778-
kind: UnnamedField(try!(p.parse_visibility())),
4779-
id: ast::DUMMY_NODE_ID,
4780-
ty: try!(p.parse_ty_sum()),
4781-
attrs: attrs,
4782-
};
4783-
Ok(spanned(lo, p.span.hi, struct_field_))
4784-
}));
4785-
4786-
if fields.is_empty() {
4787-
return Err(self.fatal(&format!("unit-like struct definition should be \
4788-
written as `struct {};`",
4789-
class_name)));
4790-
}
4773+
// Unit like structs are handled in parse_item_struct function
4774+
let fields = try!(self.parse_unspanned_seq(
4775+
&token::OpenDelim(token::Paren),
4776+
&token::CloseDelim(token::Paren),
4777+
seq_sep_trailing_allowed(token::Comma),
4778+
|p| {
4779+
let attrs = p.parse_outer_attributes();
4780+
let lo = p.span.lo;
4781+
let struct_field_ = ast::StructField_ {
4782+
kind: UnnamedField(try!(p.parse_visibility())),
4783+
id: ast::DUMMY_NODE_ID,
4784+
ty: try!(p.parse_ty_sum()),
4785+
attrs: attrs,
4786+
};
4787+
Ok(spanned(lo, p.span.hi, struct_field_))
4788+
}));
47914789

4792-
generics.where_clause = try!(self.parse_where_clause());
4793-
try!(self.expect(&token::Semi));
4794-
Ok(fields)
4795-
// This is the case where we just see struct Foo<T> where T: Copy;
4796-
} else if self.token.is_keyword(keywords::Where) {
4797-
generics.where_clause = try!(self.parse_where_clause());
4798-
try!(self.expect(&token::Semi));
4799-
Ok(Vec::new())
4800-
// This case is where we see: `struct Foo<T>;`
4801-
} else {
4802-
let token_str = self.this_token_to_string();
4803-
Err(self.fatal(&format!("expected `where`, `{}`, `(`, or `;` after struct \
4804-
name, found `{}`", "{", token_str)))
4790+
if fields.is_empty() {
4791+
return Err(self.fatal(&format!("unit-like struct definition should be \
4792+
written as `struct {};`",
4793+
class_name)));
48054794
}
4795+
4796+
generics.where_clause = try!(self.parse_where_clause());
4797+
try!(self.expect(&token::Semi));
4798+
Ok(fields)
48064799
}
48074800

48084801
/// Parse a structure field declaration

0 commit comments

Comments
 (0)