summary refs log tree commit diff
path: root/src/libsyntax/ext
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/libsyntax/ext
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/libsyntax/ext')
-rw-r--r--src/libsyntax/ext/build.rs8
-rw-r--r--src/libsyntax/ext/expand.rs1
2 files changed, 8 insertions, 1 deletions
diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs
index 28f0c297303..688df96ffa3 100644
--- a/src/libsyntax/ext/build.rs
+++ b/src/libsyntax/ext/build.rs
@@ -699,7 +699,13 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
         self.expr(b.span, ast::ExprKind::Block(b))
     }
     fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field {
-        ast::Field { ident: respan(span, name), expr: e, span: span, is_shorthand: false }
+        ast::Field {
+            ident: respan(span, name),
+            expr: e,
+            span: span,
+            is_shorthand: false,
+            attrs: ast::ThinVec::new(),
+        }
     }
     fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr> {
         self.expr(span, ast::ExprKind::Struct(path, fields, None))
diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs
index d45dbd3f723..201e8d69494 100644
--- a/src/libsyntax/ext/expand.rs
+++ b/src/libsyntax/ext/expand.rs
@@ -679,6 +679,7 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
     }
 
     fn fold_pat(&mut self, pat: P<ast::Pat>) -> P<ast::Pat> {
+        let pat = self.cfg.configure_pat(pat);
         match pat.node {
             PatKind::Mac(_) => {}
             _ => return noop_fold_pat(pat, self),