1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
use attr::HasAttrs;
use feature_gate::{feature_err, EXPLAIN_STMT_ATTR_SYNTAX, Features, get_features, GateIssue};
use {fold, attr};
use ast;
use codemap::Spanned;
use parse::ParseSess;
use ptr::P;
use util::small_vector::SmallVector;
pub struct StripUnconfigured<'a> {
pub should_test: bool,
pub sess: &'a ParseSess,
pub features: Option<&'a Features>,
}
pub fn features(mut krate: ast::Crate, sess: &ParseSess, should_test: bool)
-> (ast::Crate, Features) {
let features;
{
let mut strip_unconfigured = StripUnconfigured {
should_test: should_test,
sess: sess,
features: None,
};
let unconfigured_attrs = krate.attrs.clone();
let err_count = sess.span_diagnostic.err_count();
if let Some(attrs) = strip_unconfigured.configure(krate.attrs) {
krate.attrs = attrs;
} else {
krate.attrs = Vec::new();
krate.module.items = Vec::new();
return (krate, Features::new());
}
features = get_features(&sess.span_diagnostic, &krate.attrs);
if err_count == sess.span_diagnostic.err_count() {
strip_unconfigured.features = Some(&features);
strip_unconfigured.configure(unconfigured_attrs);
}
}
(krate, features)
}
macro_rules! configure {
($this:ident, $node:ident) => {
match $this.configure($node) {
Some(node) => node,
None => return Default::default(),
}
}
}
impl<'a> StripUnconfigured<'a> {
pub fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
let node = self.process_cfg_attrs(node);
Some(node)
}
pub fn process_cfg_attrs<T: HasAttrs>(&mut self, node: T) -> T {
node.map_attrs(|attrs| {
attrs.into_iter().filter_map(|attr| self.process_cfg_attr(attr)).collect()
})
}
fn process_cfg_attr(&mut self, attr: ast::Attribute) -> Option<ast::Attribute> {
if !attr.check_name("cfg_attr") {
return Some(attr);
}
let attr_list = match attr.meta_item_list() {
Some(attr_list) => attr_list,
None => {
let msg = "expected `#[cfg_attr(<cfg pattern>, <attr>)]`";
self.sess.span_diagnostic.span_err(attr.span, msg);
return None;
}
};
let (cfg, mi) = match (attr_list.len(), attr_list.get(0), attr_list.get(1)) {
(2, Some(cfg), Some(mi)) => (cfg, mi),
_ => {
let msg = "expected `#[cfg_attr(<cfg pattern>, <attr>)]`";
self.sess.span_diagnostic.span_err(attr.span, msg);
return None;
}
};
use attr::cfg_matches;
match (cfg.meta_item(), mi.meta_item()) {
(Some(cfg), Some(mi)) =>
if cfg_matches(&cfg, self.sess, self.features) {
self.process_cfg_attr(ast::Attribute {
id: attr::mk_attr_id(),
style: attr.style,
value: mi.clone(),
is_sugared_doc: false,
span: mi.span,
})
} else {
None
},
_ => {
let msg = "unexpected literal(s) in `#[cfg_attr(<cfg pattern>, <attr>)]`";
self.sess.span_diagnostic.span_err(attr.span, msg);
None
}
}
}
pub fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool {
attrs.iter().all(|attr| {
if !self.should_test && is_test_or_bench(attr) {
return false;
}
let mis = match attr.value.node {
ast::MetaItemKind::List(ref mis) if is_cfg(&attr) => mis,
_ => return true
};
if mis.len() != 1 {
self.sess.span_diagnostic.span_err(attr.span, "expected 1 cfg-pattern");
return true;
}
if !mis[0].is_meta_item() {
self.sess.span_diagnostic.span_err(mis[0].span, "unexpected literal");
return true;
}
attr::cfg_matches(mis[0].meta_item().unwrap(), self.sess, self.features)
})
}
fn visit_expr_attrs(&mut self, attrs: &[ast::Attribute]) {
for attr in attrs.iter() {
if !self.features.map(|features| features.stmt_expr_attributes).unwrap_or(true) {
let mut err = feature_err(&self.sess,
"stmt_expr_attributes",
attr.span,
GateIssue::Language,
EXPLAIN_STMT_ATTR_SYNTAX);
if attr.is_sugared_doc {
err.help("`///` is for documentation comments. For a plain comment, use `//`.");
}
err.emit();
}
}
}
pub fn configure_foreign_mod(&mut self, foreign_mod: ast::ForeignMod) -> ast::ForeignMod {
ast::ForeignMod {
abi: foreign_mod.abi,
items: foreign_mod.items.into_iter().filter_map(|item| self.configure(item)).collect(),
}
}
fn configure_variant_data(&mut self, vdata: ast::VariantData) -> ast::VariantData {
match vdata {
ast::VariantData::Struct(fields, id) => {
let fields = fields.into_iter().filter_map(|field| self.configure(field));
ast::VariantData::Struct(fields.collect(), id)
}
ast::VariantData::Tuple(fields, id) => {
let fields = fields.into_iter().filter_map(|field| self.configure(field));
ast::VariantData::Tuple(fields.collect(), id)
}
ast::VariantData::Unit(id) => ast::VariantData::Unit(id)
}
}
pub fn configure_item_kind(&mut self, item: ast::ItemKind) -> ast::ItemKind {
match item {
ast::ItemKind::Struct(def, generics) => {
ast::ItemKind::Struct(self.configure_variant_data(def), generics)
}
ast::ItemKind::Union(def, generics) => {
ast::ItemKind::Union(self.configure_variant_data(def), generics)
}
ast::ItemKind::Enum(def, generics) => {
let variants = def.variants.into_iter().filter_map(|v| {
self.configure(v).map(|v| {
Spanned {
node: ast::Variant_ {
name: v.node.name,
attrs: v.node.attrs,
data: self.configure_variant_data(v.node.data),
disr_expr: v.node.disr_expr,
},
span: v.span
}
})
});
ast::ItemKind::Enum(ast::EnumDef {
variants: variants.collect(),
}, generics)
}
item => item,
}
}
pub fn configure_expr_kind(&mut self, expr_kind: ast::ExprKind) -> ast::ExprKind {
match expr_kind {
ast::ExprKind::Match(m, arms) => {
let arms = arms.into_iter().filter_map(|a| self.configure(a)).collect();
ast::ExprKind::Match(m, arms)
}
ast::ExprKind::Struct(path, fields, base) => {
let fields = fields.into_iter()
.filter_map(|field| {
self.visit_struct_field_attrs(field.attrs());
self.configure(field)
})
.collect();
ast::ExprKind::Struct(path, fields, base)
}
_ => expr_kind,
}
}
pub fn configure_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
self.visit_expr_attrs(expr.attrs());
if let Some(attr) = expr.attrs().iter().find(|a| is_cfg(a) || is_test_or_bench(a)) {
let msg = "removing an expression is not supported in this position";
self.sess.span_diagnostic.span_err(attr.span, msg);
}
self.process_cfg_attrs(expr)
}
pub fn configure_stmt(&mut self, stmt: ast::Stmt) -> Option<ast::Stmt> {
self.configure(stmt)
}
pub fn configure_struct_expr_field(&mut self, field: ast::Field) -> Option<ast::Field> {
if !self.features.map(|features| features.struct_field_attributes).unwrap_or(true) {
if !field.attrs.is_empty() {
let mut err = feature_err(&self.sess,
"struct_field_attributes",
field.span,
GateIssue::Language,
"attributes on struct literal fields are unstable");
err.emit();
}
}
self.configure(field)
}
pub fn configure_pat(&mut self, pattern: P<ast::Pat>) -> P<ast::Pat> {
pattern.map(|mut pattern| {
if let ast::PatKind::Struct(path, fields, etc) = pattern.node {
let fields = fields.into_iter()
.filter_map(|field| {
self.visit_struct_field_attrs(field.attrs());
self.configure(field)
})
.collect();
pattern.node = ast::PatKind::Struct(path, fields, etc);
}
pattern
})
}
fn visit_struct_field_attrs(&mut self, attrs: &[ast::Attribute]) {
for attr in attrs.iter() {
if !self.features.map(|features| features.struct_field_attributes).unwrap_or(true) {
let mut err = feature_err(
&self.sess,
"struct_field_attributes",
attr.span,
GateIssue::Language,
"attributes on struct pattern or literal fields are unstable");
err.emit();
}
}
}
}
impl<'a> fold::Folder for StripUnconfigured<'a> {
fn fold_foreign_mod(&mut self, foreign_mod: ast::ForeignMod) -> ast::ForeignMod {
let foreign_mod = self.configure_foreign_mod(foreign_mod);
fold::noop_fold_foreign_mod(foreign_mod, self)
}
fn fold_item_kind(&mut self, item: ast::ItemKind) -> ast::ItemKind {
let item = self.configure_item_kind(item);
fold::noop_fold_item_kind(item, self)
}
fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
let mut expr = self.configure_expr(expr).unwrap();
expr.node = self.configure_expr_kind(expr.node);
P(fold::noop_fold_expr(expr, self))
}
fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
let mut expr = configure!(self, expr).unwrap();
expr.node = self.configure_expr_kind(expr.node);
Some(P(fold::noop_fold_expr(expr, self)))
}
fn fold_stmt(&mut self, stmt: ast::Stmt) -> SmallVector<ast::Stmt> {
match self.configure_stmt(stmt) {
Some(stmt) => fold::noop_fold_stmt(stmt, self),
None => return SmallVector::new(),
}
}
fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
fold::noop_fold_item(configure!(self, item), self)
}
fn fold_impl_item(&mut self, item: ast::ImplItem) -> SmallVector<ast::ImplItem> {
fold::noop_fold_impl_item(configure!(self, item), self)
}
fn fold_trait_item(&mut self, item: ast::TraitItem) -> SmallVector<ast::TraitItem> {
fold::noop_fold_trait_item(configure!(self, item), self)
}
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
mac
}
fn fold_pat(&mut self, pattern: P<ast::Pat>) -> P<ast::Pat> {
fold::noop_fold_pat(self.configure_pat(pattern), self)
}
}
fn is_cfg(attr: &ast::Attribute) -> bool {
attr.check_name("cfg")
}
pub fn is_test_or_bench(attr: &ast::Attribute) -> bool {
attr.check_name("test") || attr.check_name("bench")
}