X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibsyntax%2Fconfig.rs;h=0ca110c5b1ed2cf1a490e7f3f9f782b91a8a5c53;hb=46750d0409a9c3ba5214aa20ccb0c9bdbf09ea7e;hp=366806bc19b4962882552eb27725679acb9d1177;hpb=1114fcd945f6e979660053aeed561bcfb5da669e;p=rust.git diff --git a/src/libsyntax/config.rs b/src/libsyntax/config.rs index 366806bc19b..0ca110c5b1e 100644 --- a/src/libsyntax/config.rs +++ b/src/libsyntax/config.rs @@ -10,6 +10,7 @@ use attr::AttrMetaMethods; use diagnostic::SpanHandler; +use feature_gate::GatedCfg; use fold::Folder; use {ast, fold, attr}; use codemap::{Spanned, respan}; @@ -25,10 +26,13 @@ struct Context 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); +pub fn strip_unconfigured_items(diagnostic: &SpanHandler, krate: ast::Crate, + feature_gated_cfgs: &mut Vec) + -> ast::Crate +{ + let krate = process_cfg_attr(diagnostic, krate, feature_gated_cfgs); let config = krate.config.clone(); - strip_items(krate, |attrs| in_cfg(diagnostic, &config, attrs)) + strip_items(krate, |attrs| in_cfg(diagnostic, &config, attrs, feature_gated_cfgs)) } impl fold::Folder for Context where F: FnMut(&[ast::Attribute]) -> bool { @@ -136,21 +140,14 @@ fn fold_item_underscore(cx: &mut Context, item: ast::Item_) -> ast::Item_ if !(cx.in_cfg)(&v.node.attrs) { None } else { - Some(v.map(|Spanned {node: ast::Variant_ {id, name, attrs, kind, - disr_expr, vis}, span}| { + Some(v.map(|Spanned {node: ast::Variant_ {name, attrs, data, + disr_expr}, span}| { Spanned { node: ast::Variant_ { - id: id, name: name, attrs: attrs, - kind: match kind { - ast::TupleVariantKind(..) => kind, - ast::StructVariantKind(def) => { - ast::StructVariantKind(fold_struct(cx, def)) - } - }, + data: fold_struct(cx, data), disr_expr: disr_expr, - vis: vis }, span: span } @@ -167,15 +164,22 @@ fn fold_item_underscore(cx: &mut Context, item: ast::Item_) -> ast::Item_ fold::noop_fold_item_underscore(item, cx) } -fn fold_struct(cx: &mut Context, def: P) -> P where +fn fold_struct(cx: &mut Context, def: P) -> P where F: FnMut(&[ast::Attribute]) -> bool { - def.map(|ast::StructDef { fields, ctor_id }| { - ast::StructDef { - fields: fields.into_iter().filter(|m| { - (cx.in_cfg)(&m.node.attrs) - }).collect(), - ctor_id: ctor_id, + def.map(|vdata| { + match vdata { + ast::VariantData::Struct(fields, id) => { + ast::VariantData::Struct(fields.into_iter().filter(|m| { + (cx.in_cfg)(&m.node.attrs) + }).collect(), id) + } + ast::VariantData::Tuple(fields, id) => { + ast::VariantData::Tuple(fields.into_iter().filter(|m| { + (cx.in_cfg)(&m.node.attrs) + }).collect(), id) + } + ast::VariantData::Unit(id) => ast::VariantData::Unit(id) } }) } @@ -222,10 +226,10 @@ fn fold_expr(cx: &mut Context, expr: P) -> P where fold::noop_fold_expr(ast::Expr { id: id, node: match node { - ast::ExprMatch(m, arms, source) => { + ast::ExprMatch(m, arms) => { ast::ExprMatch(m, arms.into_iter() .filter(|a| (cx.in_cfg)(&a.attrs)) - .collect(), source) + .collect()) } _ => node }, @@ -248,7 +252,8 @@ fn foreign_item_in_cfg(cx: &mut Context, item: &ast::ForeignItem) -> bool // Determine if an item should be translated in the current crate // configuration based on the item's attributes -fn in_cfg(diagnostic: &SpanHandler, cfg: &[P], attrs: &[ast::Attribute]) -> bool { +fn in_cfg(diagnostic: &SpanHandler, cfg: &[P], attrs: &[ast::Attribute], + feature_gated_cfgs: &mut Vec) -> bool { attrs.iter().all(|attr| { let mis = match attr.node.value.node { ast::MetaList(_, ref mis) if attr.check_name("cfg") => mis, @@ -260,25 +265,29 @@ fn in_cfg(diagnostic: &SpanHandler, cfg: &[P], attrs: &[ast::Attr return true; } - attr::cfg_matches(diagnostic, cfg, &*mis[0]) + attr::cfg_matches(diagnostic, cfg, &*mis[0], + feature_gated_cfgs) }) } -struct CfgAttrFolder<'a> { +struct CfgAttrFolder<'a, 'b> { diag: &'a SpanHandler, config: ast::CrateConfig, + feature_gated_cfgs: &'b mut Vec } // Process `#[cfg_attr]`. -fn process_cfg_attr(diagnostic: &SpanHandler, krate: ast::Crate) -> ast::Crate { +fn process_cfg_attr(diagnostic: &SpanHandler, krate: ast::Crate, + feature_gated_cfgs: &mut Vec) -> ast::Crate { let mut fld = CfgAttrFolder { diag: diagnostic, config: krate.config.clone(), + feature_gated_cfgs: feature_gated_cfgs, }; fld.fold_crate(krate) } -impl<'a> fold::Folder for CfgAttrFolder<'a> { +impl<'a,'b> fold::Folder for CfgAttrFolder<'a,'b> { fn fold_attribute(&mut self, attr: ast::Attribute) -> Option { if !attr.check_name("cfg_attr") { return fold::noop_fold_attribute(attr, self); @@ -299,7 +308,8 @@ fn fold_attribute(&mut self, attr: ast::Attribute) -> Option { } }; - if attr::cfg_matches(self.diag, &self.config[..], &cfg) { + if attr::cfg_matches(self.diag, &self.config[..], &cfg, + self.feature_gated_cfgs) { Some(respan(mi.span, ast::Attribute_ { id: attr::mk_attr_id(), style: attr.node.style,