Skip to content

Commit 1aa34e0

Browse files
committed
Strip unconfigured items during macro expansion
1 parent 25c7333 commit 1aa34e0

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

src/librustc_driver/driver.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -720,16 +720,7 @@ pub fn phase_2_configure_and_expand(sess: &Session,
720720
ret
721721
});
722722

723-
// JBC: make CFG processing part of expansion to avoid this problem:
724-
725-
// strip again, in case expansion added anything with a #[cfg].
726723
krate = sess.track_errors(|| {
727-
let krate = time(time_passes, "configuration 2", || {
728-
syntax::config::strip_unconfigured_items(sess.diagnostic(),
729-
krate,
730-
&mut feature_gated_cfgs)
731-
});
732-
733724
time(time_passes, "gated configuration checking", || {
734725
let features = sess.features.borrow();
735726
feature_gated_cfgs.sort();

src/libsyntax/config.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ pub struct StripUnconfigured<'a> {
3838
}
3939

4040
impl<'a> StripUnconfigured<'a> {
41+
pub fn new(config: &'a ast::CrateConfig,
42+
diagnostic: &'a Handler,
43+
feature_gated_cfgs: &'a mut Vec<GatedCfgAttr>)
44+
-> Self {
45+
StripUnconfigured {
46+
config: config,
47+
diag: CfgDiagReal { diag: diagnostic, feature_gated_cfgs: feature_gated_cfgs },
48+
}
49+
}
50+
4151
fn process_cfg_attr(&mut self, attr: ast::Attribute) -> Option<ast::Attribute> {
4252
if !attr.check_name("cfg_attr") {
4353
return Some(attr);
@@ -121,13 +131,8 @@ pub fn strip_unconfigured_items(diagnostic: &Handler, krate: ast::Crate,
121131
feature_gated_cfgs: &mut Vec<GatedCfgAttr>)
122132
-> ast::Crate
123133
{
124-
StripUnconfigured {
125-
config: &krate.config.clone(),
126-
diag: CfgDiagReal {
127-
diag: diagnostic,
128-
feature_gated_cfgs: feature_gated_cfgs,
129-
},
130-
}.fold_crate(krate)
134+
let config = &krate.config.clone();
135+
StripUnconfigured::new(config, diagnostic, feature_gated_cfgs).fold_crate(krate)
131136
}
132137

133138
impl<T: CfgFolder> fold::Folder for T {

src/libsyntax/ext/expand.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use attr;
1919
use attr::{AttrMetaMethods, WithAttrs, ThinAttributesExt};
2020
use codemap;
2121
use codemap::{Span, Spanned, ExpnInfo, ExpnId, NameAndSpan, MacroBang, MacroAttribute};
22+
use config::StripUnconfigured;
2223
use ext::base::*;
2324
use feature_gate::{self, Features};
2425
use fold;
@@ -76,6 +77,17 @@ impl_macro_generable! {
7677
"statement", .make_stmts, lift .fold_stmt, |_span| SmallVector::zero();
7778
}
7879

80+
impl MacroGenerable for Option<P<ast::Expr>> {
81+
fn kind_name() -> &'static str { "expression" }
82+
fn dummy(_span: Span) -> Self { None }
83+
fn make_with<'a>(result: Box<MacResult + 'a>) -> Option<Self> {
84+
result.make_expr().map(Some)
85+
}
86+
fn fold_with<F: Folder>(self, folder: &mut F) -> Self {
87+
self.and_then(|expr| folder.fold_opt_expr(expr))
88+
}
89+
}
90+
7991
pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
8092
return e.and_then(|ast::Expr {id, node, span, attrs}| match node {
8193

@@ -322,7 +334,8 @@ fn expand_mac_invoc<T>(mac: ast::Mac, ident: Option<Ident>, attrs: Vec<ast::Attr
322334
};
323335

324336
let marked = expanded.fold_with(&mut Marker { mark: mark, expn_id: Some(fld.cx.backtrace()) });
325-
let fully_expanded = marked.fold_with(fld);
337+
let configured = marked.fold_with(&mut fld.strip_unconfigured());
338+
let fully_expanded = configured.fold_with(fld);
326339
fld.cx.bt_pop();
327340
fully_expanded
328341
}
@@ -987,6 +1000,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
9871000
pub fn new(cx: &'a mut ExtCtxt<'b>) -> MacroExpander<'a, 'b> {
9881001
MacroExpander { cx: cx }
9891002
}
1003+
1004+
fn strip_unconfigured(&mut self) -> StripUnconfigured {
1005+
StripUnconfigured::new(&self.cx.cfg,
1006+
&self.cx.parse_sess.span_diagnostic,
1007+
self.cx.feature_gated_cfgs)
1008+
}
9901009
}
9911010

9921011
impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
@@ -999,6 +1018,19 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
9991018
expand_expr(expr, self)
10001019
}
10011020

1021+
fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
1022+
match expr.node {
1023+
ast::ExprKind::Mac(_) => {}
1024+
_ => return Some(expand_expr(expr, self)),
1025+
}
1026+
1027+
expr.and_then(|ast::Expr {node, span, attrs, ..}| match node {
1028+
ast::ExprKind::Mac(mac) =>
1029+
expand_mac_invoc(mac, None, attrs.into_attr_vec(), span, self),
1030+
_ => unreachable!(),
1031+
})
1032+
}
1033+
10021034
fn fold_pat(&mut self, pat: P<ast::Pat>) -> P<ast::Pat> {
10031035
expand_pat(pat, self)
10041036
}

0 commit comments

Comments
 (0)