Skip to content

Commit 296c361

Browse files
committed
Added stmt_expr_attribute feature gate
1 parent c56b47a commit 296c361

11 files changed

+469
-63
lines changed

src/doc/reference.md

+3
Original file line numberDiff line numberDiff line change
@@ -2368,6 +2368,9 @@ The currently implemented features of the reference compiler are:
23682368
influence type inference.
23692369
* - `braced_empty_structs` - Allows use of empty structs and enum variants with braces.
23702370

2371+
* - `stmt_expr_attributes` - Allows attributes on expressions and
2372+
non-item statements.
2373+
23712374
If a feature is promoted to a language feature, then all existing programs will
23722375
start to receive compilation warnings about `#![feature]` directives which enabled
23732376
the new feature (because the directive is no longer necessary). However, if a

src/libsyntax/attr.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ use ast::{Stmt, StmtDecl, StmtExpr, StmtMac, StmtSemi, DeclItem, DeclLocal};
2020
use ast::{Expr, Item, Local, Decl};
2121
use codemap::{Span, Spanned, spanned, dummy_spanned};
2222
use codemap::BytePos;
23+
use config::CfgDiag;
2324
use diagnostic::SpanHandler;
24-
use feature_gate::GatedCfg;
25+
use feature_gate::{GatedCfg, GatedCfgAttr};
2526
use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
2627
use parse::token::{InternedString, intern_and_get_ident};
2728
use parse::token;
@@ -358,26 +359,35 @@ pub fn requests_inline(attrs: &[Attribute]) -> bool {
358359
}
359360

360361
/// Tests if a cfg-pattern matches the cfg set
361-
pub fn cfg_matches(diagnostic: &SpanHandler, cfgs: &[P<MetaItem>], cfg: &ast::MetaItem,
362-
feature_gated_cfgs: &mut Vec<GatedCfg>) -> bool {
362+
pub fn cfg_matches<T: CfgDiag>(cfgs: &[P<MetaItem>],
363+
cfg: &ast::MetaItem,
364+
diag: &mut T) -> bool {
363365
match cfg.node {
364366
ast::MetaList(ref pred, ref mis) if &pred[..] == "any" =>
365-
mis.iter().any(|mi| cfg_matches(diagnostic, cfgs, &**mi, feature_gated_cfgs)),
367+
mis.iter().any(|mi| cfg_matches(cfgs, &**mi, diag)),
366368
ast::MetaList(ref pred, ref mis) if &pred[..] == "all" =>
367-
mis.iter().all(|mi| cfg_matches(diagnostic, cfgs, &**mi, feature_gated_cfgs)),
369+
mis.iter().all(|mi| cfg_matches(cfgs, &**mi, diag)),
368370
ast::MetaList(ref pred, ref mis) if &pred[..] == "not" => {
369371
if mis.len() != 1 {
370-
diagnostic.span_err(cfg.span, "expected 1 cfg-pattern");
372+
diag.emit_error(|diagnostic| {
373+
diagnostic.span_err(cfg.span, "expected 1 cfg-pattern");
374+
});
371375
return false;
372376
}
373-
!cfg_matches(diagnostic, cfgs, &*mis[0], feature_gated_cfgs)
377+
!cfg_matches(cfgs, &*mis[0], diag)
374378
}
375379
ast::MetaList(ref pred, _) => {
376-
diagnostic.span_err(cfg.span, &format!("invalid predicate `{}`", pred));
380+
diag.emit_error(|diagnostic| {
381+
diagnostic.span_err(cfg.span,
382+
&format!("invalid predicate `{}`", pred));
383+
});
377384
false
378385
},
379386
ast::MetaWord(_) | ast::MetaNameValue(..) => {
380-
feature_gated_cfgs.extend(GatedCfg::gate(cfg));
387+
diag.flag_gated(|feature_gated_cfgs| {
388+
feature_gated_cfgs.extend(
389+
GatedCfg::gate(cfg).map(GatedCfgAttr::GatedCfg));
390+
});
381391
contains(cfgs, cfg)
382392
}
383393
}

src/libsyntax/config.rs

+210-39
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010

1111
use attr::AttrMetaMethods;
1212
use diagnostic::SpanHandler;
13-
use feature_gate::GatedCfg;
13+
use feature_gate::GatedCfgAttr;
1414
use fold::Folder;
1515
use {ast, fold, attr};
16+
use visit;
1617
use codemap::{Spanned, respan};
1718
use ptr::P;
1819

@@ -28,20 +29,26 @@ struct Context<'a, F> where F: FnMut(&[ast::Attribute]) -> bool {
2829
// Support conditional compilation by transforming the AST, stripping out
2930
// any items that do not belong in the current configuration
3031
pub fn strip_unconfigured_items(diagnostic: &SpanHandler, krate: ast::Crate,
31-
feature_gated_cfgs: &mut Vec<GatedCfg>)
32+
feature_gated_cfgs: &mut Vec<GatedCfgAttr>)
3233
-> ast::Crate
3334
{
35+
// Need to do this check here because cfg runs before feature_gates
36+
check_for_gated_stmt_expr_attributes(&krate, feature_gated_cfgs);
37+
3438
let krate = process_cfg_attr(diagnostic, krate, feature_gated_cfgs);
3539
let config = krate.config.clone();
3640
strip_items(diagnostic,
3741
krate,
38-
|attrs| in_cfg(diagnostic, &config, attrs, feature_gated_cfgs))
42+
|attrs| {
43+
let mut diag = CfgDiagReal {
44+
diag: diagnostic,
45+
feature_gated_cfgs: feature_gated_cfgs,
46+
};
47+
in_cfg(&config, attrs, &mut diag)
48+
})
3949
}
4050

4151
impl<'a, F> fold::Folder for Context<'a, F> where F: FnMut(&[ast::Attribute]) -> bool {
42-
fn fold_mod(&mut self, module: ast::Mod) -> ast::Mod {
43-
fold_mod(self, module)
44-
}
4552
fn fold_foreign_mod(&mut self, foreign_mod: ast::ForeignMod) -> ast::ForeignMod {
4653
fold_foreign_mod(self, foreign_mod)
4754
}
@@ -87,19 +94,6 @@ pub fn strip_items<'a, F>(diagnostic: &'a SpanHandler,
8794
ctxt.fold_crate(krate)
8895
}
8996

90-
fn fold_mod<F>(cx: &mut Context<F>,
91-
ast::Mod {inner, items}: ast::Mod)
92-
-> ast::Mod where
93-
F: FnMut(&[ast::Attribute]) -> bool
94-
{
95-
ast::Mod {
96-
inner: inner,
97-
items: items.into_iter().flat_map(|a| {
98-
cx.fold_item(a).into_iter()
99-
}).collect()
100-
}
101-
}
102-
10397
fn filter_foreign_item<F>(cx: &mut Context<F>,
10498
item: P<ast::ForeignItem>)
10599
-> Option<P<ast::ForeignItem>> where
@@ -271,44 +265,45 @@ fn is_cfg(attr: &ast::Attribute) -> bool {
271265

272266
// Determine if an item should be translated in the current crate
273267
// configuration based on the item's attributes
274-
fn in_cfg(diagnostic: &SpanHandler,
275-
cfg: &[P<ast::MetaItem>],
276-
attrs: &[ast::Attribute],
277-
feature_gated_cfgs: &mut Vec<GatedCfg>) -> bool {
268+
fn in_cfg<T: CfgDiag>(cfg: &[P<ast::MetaItem>],
269+
attrs: &[ast::Attribute],
270+
diag: &mut T) -> bool {
278271
attrs.iter().all(|attr| {
279272
let mis = match attr.node.value.node {
280273
ast::MetaList(_, ref mis) if is_cfg(&attr) => mis,
281274
_ => return true
282275
};
283276

284277
if mis.len() != 1 {
285-
diagnostic.span_err(attr.span, "expected 1 cfg-pattern");
278+
diag.emit_error(|diagnostic| {
279+
diagnostic.span_err(attr.span, "expected 1 cfg-pattern");
280+
});
286281
return true;
287282
}
288283

289-
attr::cfg_matches(diagnostic, cfg, &mis[0],
290-
feature_gated_cfgs)
284+
attr::cfg_matches(cfg, &mis[0], diag)
291285
})
292286
}
293287

294-
struct CfgAttrFolder<'a, 'b> {
295-
diag: &'a SpanHandler,
296-
config: ast::CrateConfig,
297-
feature_gated_cfgs: &'b mut Vec<GatedCfg>
288+
struct CfgAttrFolder<'a, T> {
289+
diag: T,
290+
config: &'a ast::CrateConfig,
298291
}
299292

300293
// Process `#[cfg_attr]`.
301294
fn process_cfg_attr(diagnostic: &SpanHandler, krate: ast::Crate,
302-
feature_gated_cfgs: &mut Vec<GatedCfg>) -> ast::Crate {
295+
feature_gated_cfgs: &mut Vec<GatedCfgAttr>) -> ast::Crate {
303296
let mut fld = CfgAttrFolder {
304-
diag: diagnostic,
305-
config: krate.config.clone(),
306-
feature_gated_cfgs: feature_gated_cfgs,
297+
diag: CfgDiagReal {
298+
diag: diagnostic,
299+
feature_gated_cfgs: feature_gated_cfgs,
300+
},
301+
config: &krate.config.clone(),
307302
};
308303
fld.fold_crate(krate)
309304
}
310305

311-
impl<'a,'b> fold::Folder for CfgAttrFolder<'a,'b> {
306+
impl<'a, T: CfgDiag> fold::Folder for CfgAttrFolder<'a, T> {
312307
fn fold_attribute(&mut self, attr: ast::Attribute) -> Option<ast::Attribute> {
313308
if !attr.check_name("cfg_attr") {
314309
return fold::noop_fold_attribute(attr, self);
@@ -317,20 +312,25 @@ impl<'a,'b> fold::Folder for CfgAttrFolder<'a,'b> {
317312
let attr_list = match attr.meta_item_list() {
318313
Some(attr_list) => attr_list,
319314
None => {
320-
self.diag.span_err(attr.span, "expected `#[cfg_attr(<cfg pattern>, <attr>)]`");
315+
self.diag.emit_error(|diag| {
316+
diag.span_err(attr.span,
317+
"expected `#[cfg_attr(<cfg pattern>, <attr>)]`");
318+
});
321319
return None;
322320
}
323321
};
324322
let (cfg, mi) = match (attr_list.len(), attr_list.get(0), attr_list.get(1)) {
325323
(2, Some(cfg), Some(mi)) => (cfg, mi),
326324
_ => {
327-
self.diag.span_err(attr.span, "expected `#[cfg_attr(<cfg pattern>, <attr>)]`");
325+
self.diag.emit_error(|diag| {
326+
diag.span_err(attr.span,
327+
"expected `#[cfg_attr(<cfg pattern>, <attr>)]`");
328+
});
328329
return None;
329330
}
330331
};
331332

332-
if attr::cfg_matches(self.diag, &self.config[..], &cfg,
333-
self.feature_gated_cfgs) {
333+
if attr::cfg_matches(&self.config[..], &cfg, &mut self.diag) {
334334
Some(respan(mi.span, ast::Attribute_ {
335335
id: attr::mk_attr_id(),
336336
style: attr.node.style,
@@ -347,3 +347,174 @@ impl<'a,'b> fold::Folder for CfgAttrFolder<'a,'b> {
347347
fold::noop_fold_mac(mac, self)
348348
}
349349
}
350+
351+
fn check_for_gated_stmt_expr_attributes(krate: &ast::Crate,
352+
discovered: &mut Vec<GatedCfgAttr>) {
353+
let mut v = StmtExprAttrFeatureVisitor {
354+
config: &krate.config,
355+
discovered: discovered,
356+
};
357+
visit::walk_crate(&mut v, krate);
358+
}
359+
360+
/// To cover this feature, we need to discover all attributes
361+
/// so we need to run before cfg.
362+
struct StmtExprAttrFeatureVisitor<'a, 'b> {
363+
config: &'a ast::CrateConfig,
364+
discovered: &'b mut Vec<GatedCfgAttr>,
365+
}
366+
367+
// Runs the cfg_attr and cfg folders locally in "silent" mode
368+
// to discover attribute use on stmts or expressions ahead of time
369+
impl<'v, 'a, 'b> visit::Visitor<'v> for StmtExprAttrFeatureVisitor<'a, 'b> {
370+
fn visit_stmt(&mut self, s: &'v ast::Stmt) {
371+
// check if there even are any attributes on this node
372+
let stmt_attrs = s.node.attrs();
373+
if stmt_attrs.len() > 0 {
374+
// attributes on items are fine
375+
if let ast::StmtDecl(ref decl, _) = s.node {
376+
if let ast::DeclItem(_) = decl.node {
377+
visit::walk_stmt(self, s);
378+
return;
379+
}
380+
}
381+
382+
// flag the offending attributes
383+
for attr in stmt_attrs {
384+
self.discovered.push(GatedCfgAttr::GatedAttr(attr.span));
385+
}
386+
387+
// if the node does not end up being cfg-d away, walk down
388+
if node_survives_cfg(stmt_attrs, self.config) {
389+
visit::walk_stmt(self, s);
390+
}
391+
} else {
392+
visit::walk_stmt(self, s);
393+
}
394+
}
395+
396+
fn visit_expr(&mut self, ex: &'v ast::Expr) {
397+
// check if there even are any attributes on this node
398+
let expr_attrs = ex.attrs();
399+
if expr_attrs.len() > 0 {
400+
401+
// flag the offending attributes
402+
for attr in expr_attrs {
403+
self.discovered.push(GatedCfgAttr::GatedAttr(attr.span));
404+
}
405+
406+
// if the node does not end up being cfg-d away, walk down
407+
if node_survives_cfg(expr_attrs, self.config) {
408+
visit::walk_expr(self, ex);
409+
}
410+
} else {
411+
visit::walk_expr(self, ex);
412+
}
413+
}
414+
415+
fn visit_foreign_item(&mut self, i: &'v ast::ForeignItem) {
416+
if node_survives_cfg(&i.attrs, self.config) {
417+
visit::walk_foreign_item(self, i);
418+
}
419+
}
420+
421+
fn visit_item(&mut self, i: &'v ast::Item) {
422+
if node_survives_cfg(&i.attrs, self.config) {
423+
visit::walk_item(self, i);
424+
}
425+
}
426+
427+
fn visit_impl_item(&mut self, ii: &'v ast::ImplItem) {
428+
if node_survives_cfg(&ii.attrs, self.config) {
429+
visit::walk_impl_item(self, ii);
430+
}
431+
}
432+
433+
fn visit_trait_item(&mut self, ti: &'v ast::TraitItem) {
434+
if node_survives_cfg(&ti.attrs, self.config) {
435+
visit::walk_trait_item(self, ti);
436+
}
437+
}
438+
439+
fn visit_struct_field(&mut self, s: &'v ast::StructField) {
440+
if node_survives_cfg(&s.node.attrs, self.config) {
441+
visit::walk_struct_field(self, s);
442+
}
443+
}
444+
445+
fn visit_variant(&mut self, v: &'v ast::Variant,
446+
g: &'v ast::Generics, item_id: ast::NodeId) {
447+
if node_survives_cfg(&v.node.attrs, self.config) {
448+
visit::walk_variant(self, v, g, item_id);
449+
}
450+
}
451+
452+
fn visit_arm(&mut self, a: &'v ast::Arm) {
453+
if node_survives_cfg(&a.attrs, self.config) {
454+
visit::walk_arm(self, a);
455+
}
456+
}
457+
458+
// This visitor runs pre expansion, so we need to prevent
459+
// the default panic here
460+
fn visit_mac(&mut self, mac: &'v ast::Mac) {
461+
visit::walk_mac(self, mac)
462+
}
463+
}
464+
465+
pub trait CfgDiag {
466+
fn emit_error<F>(&mut self, f: F) where F: FnMut(&SpanHandler);
467+
fn flag_gated<F>(&mut self, f: F) where F: FnMut(&mut Vec<GatedCfgAttr>);
468+
}
469+
470+
pub struct CfgDiagReal<'a, 'b> {
471+
pub diag: &'a SpanHandler,
472+
pub feature_gated_cfgs: &'b mut Vec<GatedCfgAttr>,
473+
}
474+
475+
impl<'a, 'b> CfgDiag for CfgDiagReal<'a, 'b> {
476+
fn emit_error<F>(&mut self, mut f: F) where F: FnMut(&SpanHandler) {
477+
f(self.diag)
478+
}
479+
fn flag_gated<F>(&mut self, mut f: F) where F: FnMut(&mut Vec<GatedCfgAttr>) {
480+
f(self.feature_gated_cfgs)
481+
}
482+
}
483+
484+
struct CfgDiagSilent {
485+
error: bool,
486+
}
487+
488+
impl CfgDiag for CfgDiagSilent {
489+
fn emit_error<F>(&mut self, _: F) where F: FnMut(&SpanHandler) {
490+
self.error = true;
491+
}
492+
fn flag_gated<F>(&mut self, _: F) where F: FnMut(&mut Vec<GatedCfgAttr>) {}
493+
}
494+
495+
fn node_survives_cfg(attrs: &[ast::Attribute],
496+
config: &ast::CrateConfig) -> bool {
497+
let mut survives_cfg = true;
498+
499+
for attr in attrs {
500+
let mut fld = CfgAttrFolder {
501+
diag: CfgDiagSilent { error: false },
502+
config: config,
503+
};
504+
let attr = fld.fold_attribute(attr.clone());
505+
506+
// In case of error we can just return true,
507+
// since the actual cfg folders will end compilation anyway.
508+
509+
if fld.diag.error { return true; }
510+
511+
survives_cfg &= attr.map(|attr| {
512+
let mut diag = CfgDiagSilent { error: false };
513+
let r = in_cfg(config, &[attr], &mut diag);
514+
if diag.error { return true; }
515+
r
516+
}).unwrap_or(true)
517+
}
518+
519+
survives_cfg
520+
}

0 commit comments

Comments
 (0)