about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-01-12 20:44:02 +0000
committerbors <bors@rust-lang.org>2017-01-12 20:44:02 +0000
commite35717814686ea3e3d44a8f5c1c20d1cd50be82a (patch)
tree1b4d764f36a18863999d7c2f5b4f3642d63b837f /src/test
parent27b9e6d450590751fca863312a8cf10f289cf1f2 (diff)
parent7972c1905beb9d1169475f42231b25d0bc9e83e6 (diff)
downloadrust-e35717814686ea3e3d44a8f5c1c20d1cd50be82a.tar.gz
rust-e35717814686ea3e3d44a8f5c1c20d1cd50be82a.zip
Auto merge of #38814 - Ralith:cfg-fields, r=jseyfried
syntax: enable attributes and cfg on struct fields

This enables conditional compilation of field initializers in a struct literal, simplifying construction of structs whose fields are themselves conditionally present. For example, the intializer for the constant in the following becomes legal, and has the intuitive effect:

```rust
struct Foo {
    #[cfg(unix)]
    bar: (),
}

const FOO: Foo = Foo {
    #[cfg(unix)]
    bar: (),
};
```

It's not clear to me whether this calls for the full RFC process, but the implementation was simple enough that I figured I'd begin the conversation with code.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/compile-fail/struct-field-attr-feature-gate.rs20
-rw-r--r--src/test/compile-fail/struct-field-cfg.rs30
2 files changed, 50 insertions, 0 deletions
diff --git a/src/test/compile-fail/struct-field-attr-feature-gate.rs b/src/test/compile-fail/struct-field-attr-feature-gate.rs
new file mode 100644
index 00000000000..665c3fd67f4
--- /dev/null
+++ b/src/test/compile-fail/struct-field-attr-feature-gate.rs
@@ -0,0 +1,20 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+struct Foo {
+    present: (),
+}
+
+fn main() {
+    let foo = Foo { #[cfg(all())] present: () };
+    //~^ ERROR attributes on struct pattern or literal fields are unstable
+    let Foo { #[cfg(all())] present: () } = foo;
+    //~^ ERROR attributes on struct pattern or literal fields are unstable
+}
diff --git a/src/test/compile-fail/struct-field-cfg.rs b/src/test/compile-fail/struct-field-cfg.rs
new file mode 100644
index 00000000000..9fb130f4d54
--- /dev/null
+++ b/src/test/compile-fail/struct-field-cfg.rs
@@ -0,0 +1,30 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(struct_field_attributes)]
+
+struct Foo {
+    present: (),
+}
+
+fn main() {
+    let foo = Foo { #[cfg(all())] present: () };
+    let _ = Foo { #[cfg(any())] present: () };
+    //~^ ERROR missing field `present` in initializer of `Foo`
+    let _ = Foo { present: (), #[cfg(any())] absent: () };
+    let _ = Foo { present: (), #[cfg(all())] absent: () };
+    //~^ ERROR struct `Foo` has no field named `absent`
+    let Foo { #[cfg(all())] present: () } = foo;
+    let Foo { #[cfg(any())] present: () } = foo;
+    //~^ ERROR pattern does not mention field `present`
+    let Foo { present: (), #[cfg(any())] absent: () } = foo;
+    let Foo { present: (), #[cfg(all())] absent: () } = foo;
+    //~^ ERROR struct `Foo` does not have a field named `absent`
+}