Skip to content

Commit 1c648fc

Browse files
committed
rustc: Strip struct fields and enum variants
Closes #11085
1 parent d5d5c50 commit 1c648fc

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

src/librustc/front/config.rs

+37-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use syntax::fold::ast_fold;
1313
use syntax::{ast, fold, attr};
14+
use syntax::codemap;
1415

1516
struct Context<'a> {
1617
in_cfg: 'a |attrs: &[ast::Attribute]| -> bool,
@@ -109,12 +110,47 @@ fn fold_item_underscore(cx: &Context, item: &ast::item_) -> ast::item_ {
109110
.collect();
110111
ast::item_trait((*a).clone(), (*b).clone(), methods)
111112
}
112-
ref item => (*item).clone(),
113+
ast::item_struct(def, ref generics) => {
114+
ast::item_struct(fold_struct(cx, def), generics.clone())
115+
}
116+
ast::item_enum(ref def, ref generics) => {
117+
let mut variants = def.variants.iter().map(|c| c.clone()).filter(|m| {
118+
(cx.in_cfg)(m.node.attrs)
119+
}).map(|v| {
120+
match v.node.kind {
121+
ast::tuple_variant_kind(..) => v,
122+
ast::struct_variant_kind(def) => {
123+
let def = fold_struct(cx, def);
124+
@codemap::Spanned {
125+
node: ast::variant_ {
126+
kind: ast::struct_variant_kind(def),
127+
..v.node.clone()
128+
},
129+
..*v
130+
}
131+
}
132+
}
133+
});
134+
ast::item_enum(ast::enum_def {
135+
variants: variants.collect(),
136+
}, generics.clone())
137+
}
138+
ref item => item.clone(),
113139
};
114140

115141
fold::noop_fold_item_underscore(&item, cx)
116142
}
117143

144+
fn fold_struct(cx: &Context, def: &ast::struct_def) -> @ast::struct_def {
145+
let mut fields = def.fields.iter().map(|c| c.clone()).filter(|m| {
146+
(cx.in_cfg)(m.node.attrs)
147+
});
148+
@ast::struct_def {
149+
fields: fields.collect(),
150+
ctor_id: def.ctor_id,
151+
}
152+
}
153+
118154
fn retain_stmt(cx: &Context, stmt: @ast::Stmt) -> bool {
119155
match stmt.node {
120156
ast::StmtDecl(decl, _) => {

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

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2012-2013 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+
// compile-flags: --cfg foo
12+
// xfail-fast
13+
14+
#[feature(struct_variant)];
15+
16+
struct Foo {
17+
#[cfg(fail)]
18+
bar: baz,
19+
foo: int,
20+
}
21+
22+
struct Foo2 {
23+
#[cfg(foo)]
24+
foo: int,
25+
}
26+
27+
enum Bar1 {
28+
Bar1_1,
29+
#[cfg(fail)]
30+
Bar1_2(NotAType),
31+
}
32+
33+
enum Bar2 {
34+
#[cfg(fail)]
35+
Bar2_1(NotAType),
36+
}
37+
38+
enum Bar3 {
39+
Bar3_1 {
40+
#[cfg(fail)]
41+
foo: int,
42+
bar: int,
43+
}
44+
}
45+
46+
fn main() {
47+
let _f = Foo { foo: 3 };
48+
let _f = Foo2 { foo: 3 };
49+
50+
match Bar1_1 {
51+
Bar1_1 => {}
52+
}
53+
54+
let _f = Bar3_1 { bar: 3 };
55+
}

0 commit comments

Comments
 (0)