Skip to content

Commit 1eb42f1

Browse files
committed
Add feature gate
1 parent 605a472 commit 1eb42f1

7 files changed

+52
-2
lines changed

src/doc/reference.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1178,17 +1178,19 @@ let px: i32 = match p { Point(x, _) => x };
11781178
```
11791179

11801180
A _unit-like struct_ is a structure without any fields, defined by leaving off
1181-
the list of fields entirely. Such structure implicitly defines a constant of
1181+
the list of fields entirely. Such a structure implicitly defines a constant of
11821182
its type with the same name. For example:
11831183

11841184
```
1185+
# #![feature(braced_empty_structs)]
11851186
struct Cookie;
11861187
let c = [Cookie, Cookie {}, Cookie, Cookie {}];
11871188
```
11881189

11891190
is equivalent to
11901191

11911192
```
1193+
# #![feature(braced_empty_structs)]
11921194
struct Cookie {}
11931195
const Cookie: Cookie = Cookie {};
11941196
let c = [Cookie, Cookie {}, Cookie, Cookie {}];
@@ -2420,6 +2422,7 @@ The currently implemented features of the reference compiler are:
24202422
terms of encapsulation).
24212423
* - `default_type_parameter_fallback` - Allows type parameter defaults to
24222424
influence type inference.
2425+
* - `braced_empty_structs` - Allows use of empty structs with braces.
24232426

24242427
If a feature is promoted to a language feature, then all existing programs will
24252428
start to receive compilation warnings about `#![feature]` directives which enabled

src/libsyntax/feature_gate.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ const KNOWN_FEATURES: &'static [(&'static str, &'static str, Option<u32>, Status
191191

192192
// allow `#[unwind]`
193193
("unwind_attributes", "1.4.0", None, Active),
194+
195+
// allow empty structs/enum variants with braces
196+
("braced_empty_structs", "1.5.0", None, Active),
194197
];
195198
// (changing above list without updating src/doc/reference.md makes @cmr sad)
196199

@@ -775,7 +778,7 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> {
775778
}
776779
}
777780

778-
ast::ItemStruct(..) => {
781+
ast::ItemStruct(ref def, _) => {
779782
if attr::contains_name(&i.attrs[..], "simd") {
780783
self.gate_feature("simd", i.span,
781784
"SIMD types are experimental and possibly buggy");
@@ -794,6 +797,10 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> {
794797
}
795798
}
796799
}
800+
if def.fields.is_empty() && def.ctor_id.is_none() {
801+
self.gate_feature("braced_empty_structs", i.span,
802+
"empty structs with braces are unstable");
803+
}
797804
}
798805

799806
ast::ItemDefaultImpl(..) => {
@@ -843,6 +850,12 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> {
843850
"box expression syntax is experimental; \
844851
you can call `Box::new` instead.");
845852
}
853+
ast::ExprStruct(_, ref fields, ref expr) => {
854+
if fields.is_empty() && expr.is_none() {
855+
self.gate_feature("braced_empty_structs", e.span,
856+
"empty structs with braces are unstable");
857+
}
858+
}
846859
_ => {}
847860
}
848861
visit::walk_expr(self, e);
@@ -867,6 +880,12 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> {
867880
pattern.span,
868881
"box pattern syntax is experimental");
869882
}
883+
ast::PatStruct(_, ref fields, dotdot) => {
884+
if fields.is_empty() && !dotdot {
885+
self.gate_feature("braced_empty_structs", pattern.span,
886+
"empty structs with braces are unstable");
887+
}
888+
}
870889
_ => {}
871890
}
872891
visit::walk_pat(self, pattern)

src/test/compile-fail/empty-struct-with-braces-1.rs

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
// Empty struct defined with braces shouldn't add names into value namespace
1212

13+
#![feature(braced_empty_structs)]
14+
1315
struct Empty {}
1416

1517
fn main() {

src/test/compile-fail/empty-struct-with-braces-2.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
// Empty struct defined with braces shouldn't add names into value namespace
1212

13+
#![feature(braced_empty_structs)]
1314
#![deny(warnings)]
1415

1516
struct Empty {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2015 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+
// Feature gate test for empty struct with braces
12+
13+
struct Empty {} //~ ERROR empty structs with braces are unstable
14+
15+
fn main() {
16+
let e = Empty {}; //~ ERROR empty structs with braces are unstable
17+
18+
match e {
19+
Empty {} => {} //~ ERROR empty structs with braces are unstable
20+
}
21+
}

src/test/run-pass/empty-struct-with-braces.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// Empty struct defined with braces add names into type namespace
1212
// Empty struct defined without braces add names into both type and value namespaces
1313

14+
#![feature(braced_empty_structs)]
15+
1416
struct Empty1 {}
1517
struct Empty2;
1618
struct Empty3 {}

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

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
//`#[cfg]` on struct field permits empty unusable struct
1212

13+
#![feature(braced_empty_structs)]
14+
1315
struct S {
1416
#[cfg(untrue)]
1517
a: int,

0 commit comments

Comments
 (0)