Skip to content

Commit 8000482

Browse files
committed
Disallow parsing of struct variants with 0 fields
Make struct variant syntax more consistent with struct syntax and fix an assert in middle::typeck. Fix #19003
1 parent 0b7b4f0 commit 8000482

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5175,7 +5175,15 @@ impl<'a> Parser<'a> {
51755175
if self.eat(&token::OpenDelim(token::Brace)) {
51765176
// Parse a struct variant.
51775177
all_nullary = false;
5178-
kind = StructVariantKind(self.parse_struct_def());
5178+
let start_span = self.span;
5179+
let struct_def = self.parse_struct_def();
5180+
if struct_def.fields.len() == 0 {
5181+
self.span_err(start_span,
5182+
format!("unit-like struct variant should be written \
5183+
without braces, as `{},`",
5184+
token::get_ident(ident)).as_slice());
5185+
}
5186+
kind = StructVariantKind(struct_def);
51795187
} else if self.token == token::OpenDelim(token::Paren) {
51805188
all_nullary = false;
51815189
let arg_tys = self.parse_enum_variant_seq(
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
enum Foo {
12+
Bar {} //~ ERROR unit-like struct variant should be written without braces, as `Bar,`
13+
}

0 commit comments

Comments
 (0)