Skip to content

Process cfg_attr right before stripping cfg #22116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion src/libsyntax/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use attr::AttrMetaMethods;
use diagnostic::SpanHandler;
use fold::Folder;
use {ast, fold, attr};
use codemap::Spanned;
use codemap::{Spanned, respan};
use ptr::P;

use util::small_vector::SmallVector;
Expand All @@ -26,6 +26,7 @@ struct Context<F> where F: FnMut(&[ast::Attribute]) -> bool {
// Support conditional compilation by transforming the AST, stripping out
// any items that do not belong in the current configuration
pub fn strip_unconfigured_items(diagnostic: &SpanHandler, krate: ast::Crate) -> ast::Crate {
let krate = process_cfg_attr(diagnostic, krate);
let config = krate.config.clone();
strip_items(krate, |attrs| in_cfg(diagnostic, &config, attrs))
}
Expand Down Expand Up @@ -281,3 +282,49 @@ fn in_cfg(diagnostic: &SpanHandler, cfg: &[P<ast::MetaItem>], attrs: &[ast::Attr
attr::cfg_matches(diagnostic, cfg, &*mis[0])
})
}

struct CfgAttrFolder<'a> {
diag: &'a SpanHandler,
config: ast::CrateConfig,
}

// Process `#[cfg_attr]`.
fn process_cfg_attr(diagnostic: &SpanHandler, krate: ast::Crate) -> ast::Crate {
let mut fld = CfgAttrFolder {
diag: diagnostic,
config: krate.config.clone(),
};
fld.fold_crate(krate)
}

impl<'a> fold::Folder for CfgAttrFolder<'a> {
fn fold_attribute(&mut self, attr: ast::Attribute) -> Option<ast::Attribute> {
if !attr.check_name("cfg_attr") {
return fold::noop_fold_attribute(attr, self);
}

let (cfg, mi) = match attr.meta_item_list() {
Some([ref cfg, ref mi]) => (cfg, mi),
_ => {
self.diag.span_err(attr.span, "expected `#[cfg_attr(<cfg pattern>, <attr>)]`");
return None;
}
};

if attr::cfg_matches(self.diag, &self.config[], &cfg) {
Some(respan(mi.span, ast::Attribute_ {
id: attr::mk_attr_id(),
style: attr.node.style,
value: mi.clone(),
is_sugared_doc: false,
}))
} else {
None
}
}

// Need the ability to run pre-expansion.
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
fold::noop_fold_mac(mac, self)
}
}
2 changes: 0 additions & 2 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,6 @@ fn initial_syntax_expander_table(ecfg: &expand::ExpansionConfig) -> SyntaxEnv {
syntax_expanders.insert(intern("cfg"),
builtin_normal_expander(
ext::cfg::expand_cfg));
syntax_expanders.insert(intern("cfg_attr"),
Modifier(box ext::cfg_attr::expand));
syntax_expanders.insert(intern("trace_macros"),
builtin_normal_expander(
ext::trace_macros::expand_trace_macros));
Expand Down
34 changes: 0 additions & 34 deletions src/libsyntax/ext/cfg_attr.rs

This file was deleted.

4 changes: 2 additions & 2 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ fn expand_arm(arm: ast::Arm, fld: &mut MacroExpander) -> ast::Arm {
arm.guard.map(|g| fld.fold_expr(rename_fld.fold_expr(g)));
let rewritten_body = fld.fold_expr(rename_fld.fold_expr(arm.body));
ast::Arm {
attrs: arm.attrs.move_map(|x| fld.fold_attribute(x)),
attrs: fold::fold_attrs(arm.attrs, fld),
pats: rewritten_pats,
guard: rewritten_guard,
body: rewritten_body,
Expand Down Expand Up @@ -1273,7 +1273,7 @@ fn expand_method(m: P<ast::Method>, fld: &mut MacroExpander) -> SmallVector<P<as
let (rewritten_fn_decl, rewritten_body)
= expand_and_rename_fn_decl_and_block(decl, body, fld);
SmallVector::one(P(ast::Method {
attrs: m.attrs.move_map(|a| fld.fold_attribute(a)),
attrs: fold::fold_attrs(m.attrs, fld),
id: id,
span: fld.new_span(m.span),
node: ast::MethDecl(fld.fold_ident(ident),
Expand Down
32 changes: 18 additions & 14 deletions src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ pub trait Folder : Sized {
noop_fold_lifetime_def(l, self)
}

fn fold_attribute(&mut self, at: Attribute) -> Attribute {
fn fold_attribute(&mut self, at: Attribute) -> Option<Attribute> {
noop_fold_attribute(at, self)
}

Expand Down Expand Up @@ -373,9 +373,13 @@ pub fn noop_fold_view_path<T: Folder>(view_path: P<ViewPath>, fld: &mut T) -> P<
})
}

pub fn fold_attrs<T: Folder>(attrs: Vec<Attribute>, fld: &mut T) -> Vec<Attribute> {
attrs.into_iter().flat_map(|x| fld.fold_attribute(x).into_iter()).collect()
}

pub fn noop_fold_arm<T: Folder>(Arm {attrs, pats, guard, body}: Arm, fld: &mut T) -> Arm {
Arm {
attrs: attrs.move_map(|x| fld.fold_attribute(x)),
attrs: fold_attrs(attrs, fld),
pats: pats.move_map(|x| fld.fold_pat(x)),
guard: guard.map(|x| fld.fold_expr(x)),
body: fld.fold_expr(body),
Expand Down Expand Up @@ -475,7 +479,7 @@ pub fn noop_fold_variant<T: Folder>(v: P<Variant>, fld: &mut T) -> P<Variant> {
node: Variant_ {
id: fld.new_id(id),
name: name,
attrs: attrs.move_map(|x| fld.fold_attribute(x)),
attrs: fold_attrs(attrs, fld),
kind: match kind {
TupleVariantKind(variant_args) => {
TupleVariantKind(variant_args.move_map(|x|
Expand Down Expand Up @@ -553,17 +557,17 @@ pub fn noop_fold_local<T: Folder>(l: P<Local>, fld: &mut T) -> P<Local> {
})
}

pub fn noop_fold_attribute<T: Folder>(at: Attribute, fld: &mut T) -> Attribute {
pub fn noop_fold_attribute<T: Folder>(at: Attribute, fld: &mut T) -> Option<Attribute> {
let Spanned {node: Attribute_ {id, style, value, is_sugared_doc}, span} = at;
Spanned {
Some(Spanned {
node: Attribute_ {
id: id,
style: style,
value: fld.fold_meta_item(value),
is_sugared_doc: is_sugared_doc
},
span: fld.new_span(span)
}
})
}

pub fn noop_fold_explicit_self_underscore<T: Folder>(es: ExplicitSelf_, fld: &mut T)
Expand Down Expand Up @@ -843,8 +847,8 @@ pub fn noop_fold_typedef<T>(t: Typedef, folder: &mut T)
where T: Folder {
let new_id = folder.new_id(t.id);
let new_span = folder.new_span(t.span);
let new_attrs = t.attrs.iter().map(|attr| {
folder.fold_attribute((*attr).clone())
let new_attrs = t.attrs.iter().flat_map(|attr| {
folder.fold_attribute((*attr).clone()).into_iter()
}).collect();
let new_ident = folder.fold_ident(t.ident);
let new_type = folder.fold_ty(t.typ);
Expand All @@ -864,7 +868,7 @@ pub fn noop_fold_associated_type<T>(at: AssociatedType, folder: &mut T)
{
let new_attrs = at.attrs
.iter()
.map(|attr| folder.fold_attribute((*attr).clone()))
.flat_map(|attr| folder.fold_attribute((*attr).clone()).into_iter())
.collect();
let new_param = folder.fold_ty_param(at.ty_param);
ast::AssociatedType {
Expand Down Expand Up @@ -906,7 +910,7 @@ pub fn noop_fold_struct_field<T: Folder>(f: StructField, fld: &mut T) -> StructF
id: fld.new_id(id),
kind: kind,
ty: fld.fold_ty(ty),
attrs: attrs.move_map(|a| fld.fold_attribute(a))
attrs: fold_attrs(attrs, fld),
},
span: fld.new_span(span)
}
Expand Down Expand Up @@ -1069,7 +1073,7 @@ pub fn noop_fold_type_method<T: Folder>(m: TypeMethod, fld: &mut T) -> TypeMetho
TypeMethod {
id: fld.new_id(id),
ident: fld.fold_ident(ident),
attrs: attrs.move_map(|a| fld.fold_attribute(a)),
attrs: fold_attrs(attrs, fld),
unsafety: unsafety,
abi: abi,
decl: fld.fold_fn_decl(decl),
Expand Down Expand Up @@ -1151,7 +1155,7 @@ pub fn noop_fold_item_simple<T: Folder>(Item {id, ident, attrs, node, vis, span}
Item {
id: id,
ident: folder.fold_ident(ident),
attrs: attrs.move_map(|e| folder.fold_attribute(e)),
attrs: fold_attrs(attrs, folder),
node: node,
vis: vis,
span: folder.new_span(span)
Expand All @@ -1162,7 +1166,7 @@ pub fn noop_fold_foreign_item<T: Folder>(ni: P<ForeignItem>, folder: &mut T) ->
ni.map(|ForeignItem {id, ident, attrs, node, span, vis}| ForeignItem {
id: folder.new_id(id),
ident: folder.fold_ident(ident),
attrs: attrs.move_map(|x| folder.fold_attribute(x)),
attrs: fold_attrs(attrs, folder),
node: match node {
ForeignItemFn(fdec, generics) => {
ForeignItemFn(folder.fold_fn_decl(fdec), folder.fold_generics(generics))
Expand All @@ -1181,7 +1185,7 @@ pub fn noop_fold_foreign_item<T: Folder>(ni: P<ForeignItem>, folder: &mut T) ->
pub fn noop_fold_method<T: Folder>(m: P<Method>, folder: &mut T) -> SmallVector<P<Method>> {
SmallVector::one(m.map(|Method {id, attrs, node, span}| Method {
id: folder.new_id(id),
attrs: attrs.move_map(|a| folder.fold_attribute(a)),
attrs: fold_attrs(attrs, folder),
node: match node {
MethDecl(ident,
generics,
Expand Down
1 change: 0 additions & 1 deletion src/libsyntax/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ pub mod ext {
pub mod base;
pub mod build;
pub mod cfg;
pub mod cfg_attr;
pub mod concat;
pub mod concat_idents;
pub mod deriving;
Expand Down
18 changes: 18 additions & 0 deletions src/test/compile-fail/cfg-attr-cfg-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2015 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.
//
// error-pattern: main function not found
// compile-flags: --cfg foo

// main is conditionally compiled, but the conditional compilation
// is conditional too!

#[cfg_attr(foo, cfg(bar))]
fn main() { }
17 changes: 17 additions & 0 deletions src/test/compile-fail/cfg-attr-crate-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2015 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.
//
// compile-flags: --cfg broken

// https://github.com/rust-lang/rust/issues/21833#issuecomment-72353044

#![cfg_attr(broken, no_std)] //~ ERROR no_std is experimental

fn main() { }
15 changes: 15 additions & 0 deletions src/test/run-pass/cfg-attr-cfg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2015 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.

// main is conditionally compiled, but the conditional compilation
// is conditional too!

#[cfg_attr(foo, cfg(bar))]
fn main() { }
15 changes: 15 additions & 0 deletions src/test/run-pass/cfg-attr-crate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2015 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.

// https://github.com/rust-lang/rust/issues/21833#issuecomment-72353044

#![cfg_attr(not_used, no_std)]

fn main() { }