Skip to content

Commit ddc1d7f

Browse files
committed
Strip unconfigured items during macro expansion
1 parent a1ef856 commit ddc1d7f

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

src/librustc_driver/driver.rs

-9
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

+12-7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ pub struct StripUnconfigured<'a> {
3232
}
3333

3434
impl<'a> StripUnconfigured<'a> {
35+
pub fn new(config: &'a ast::CrateConfig,
36+
diagnostic: &'a Handler,
37+
feature_gated_cfgs: &'a mut Vec<GatedCfgAttr>)
38+
-> Self {
39+
StripUnconfigured {
40+
config: config,
41+
diag: CfgDiagReal { diag: diagnostic, feature_gated_cfgs: feature_gated_cfgs },
42+
}
43+
}
44+
3545
// Determine if an item should be translated in the current crate
3646
// configuration based on the item's attributes
3747
fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool {
@@ -118,13 +128,8 @@ pub fn strip_unconfigured_items(diagnostic: &Handler, krate: ast::Crate,
118128
feature_gated_cfgs: &mut Vec<GatedCfgAttr>)
119129
-> ast::Crate
120130
{
121-
StripUnconfigured {
122-
config: &krate.config.clone(),
123-
diag: CfgDiagReal {
124-
diag: diagnostic,
125-
feature_gated_cfgs: feature_gated_cfgs,
126-
},
127-
}.fold_crate(krate)
131+
let config = &krate.config.clone();
132+
StripUnconfigured::new(config, diagnostic, feature_gated_cfgs).fold_crate(krate)
128133
}
129134

130135
impl<T> fold::Folder for T where T: CfgFolder {

src/libsyntax/ext/expand.rs

+32-10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use attr;
1919
use attr::{AttrMetaMethods, WithAttrs};
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;
@@ -44,9 +45,10 @@ fn check_attributes(attrs: &[ast::Attribute], fld: &MacroExpander) {
4445
}
4546
}
4647

47-
pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
48+
pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander, is_optional: bool)
49+
-> Option<P<ast::Expr>> {
4850
let expr_span = e.span;
49-
return e.and_then(|ast::Expr {id, node, span, attrs}| match node {
51+
return e.and_then(|ast::Expr {id, node, span, attrs}| Some(match node {
5052

5153
// expr_mac should really be expr_ext or something; it's the
5254
// entry-point for all syntax extensions.
@@ -61,17 +63,20 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
6163
let expanded_expr = match expand_mac_invoc(mac, span,
6264
|r| r.make_expr(),
6365
mark_expr, fld) {
64-
Some(expr) => expr,
66+
Some(expr) => match is_optional {
67+
true => fld.strip_unconfigured().fold_opt_expr(expr),
68+
false => Some(fld.strip_unconfigured().fold_expr(expr)),
69+
},
6570
None => {
66-
return DummyResult::raw_expr(span);
71+
return Some(DummyResult::raw_expr(span));
6772
}
6873
};
6974

7075
// Keep going, outside-in.
71-
let fully_expanded = fld.fold_expr(expanded_expr);
76+
let fully_expanded = expanded_expr.map(|expr| fld.fold_expr(expr));
7277
fld.cx.bt_pop();
7378

74-
fully_expanded
79+
return fully_expanded;
7580
}
7681

7782
ast::ExprKind::InPlace(placer, value_expr) => {
@@ -178,7 +183,7 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
178183
attrs: attrs
179184
}, fld))
180185
}
181-
});
186+
}));
182187
}
183188

184189
/// Expand a (not-ident-style) macro invocation. Returns the result
@@ -487,6 +492,9 @@ pub fn expand_item_mac(it: P<ast::Item>,
487492
let expn_id = fld.cx.backtrace();
488493
items.into_iter()
489494
.map(|i| mark_item(i, fm, expn_id))
495+
.flat_map(|i| fld.strip_unconfigured().fold_item(i))
496+
.collect::<SmallVector<_>>()
497+
.into_iter()
490498
.flat_map(|i| fld.fold_item(i).into_iter())
491499
.collect()
492500
}
@@ -533,6 +541,8 @@ fn expand_stmt(stmt: Stmt, fld: &mut MacroExpander) -> SmallVector<Stmt> {
533541
Some(stmts) => {
534542
// Keep going, outside-in.
535543
let new_items = stmts.into_iter().flat_map(|s| {
544+
fld.strip_unconfigured().fold_stmt(s).into_iter()
545+
}).collect::<SmallVector<_>>().into_iter().flat_map(|s| {
536546
fld.fold_stmt(s).into_iter()
537547
}).collect();
538548
fld.cx.bt_pop();
@@ -791,7 +801,7 @@ fn expand_pat(p: P<ast::Pat>, fld: &mut MacroExpander) -> P<ast::Pat> {
791801
mac_span,
792802
&marked_before[..]).make_pat();
793803
let expanded = match pat {
794-
Some(e) => e,
804+
Some(e) => fld.strip_unconfigured().fold_pat(e),
795805
None => {
796806
fld.cx.span_err(
797807
pth.span,
@@ -1089,6 +1099,8 @@ fn expand_impl_item(ii: ast::ImplItem, fld: &mut MacroExpander)
10891099
Some(impl_items) => {
10901100
// expand again if necessary
10911101
let new_items = impl_items.into_iter().flat_map(|ii| {
1102+
fld.strip_unconfigured().fold_impl_item(ii)
1103+
}).collect::<SmallVector<_>>().into_iter().flat_map(|ii| {
10921104
expand_impl_item(ii, fld).into_iter()
10931105
}).collect();
10941106
fld.cx.bt_pop();
@@ -1143,7 +1155,7 @@ pub fn expand_type(t: P<ast::Ty>, fld: &mut MacroExpander) -> P<ast::Ty> {
11431155
|r| r.make_ty(),
11441156
mark_ty,
11451157
fld) {
1146-
Some(ty) => ty,
1158+
Some(ty) => fld.strip_unconfigured().fold_ty(ty),
11471159
None => {
11481160
return DummyResult::raw_ty(t.span);
11491161
}
@@ -1184,6 +1196,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
11841196
pub fn new(cx: &'a mut ExtCtxt<'b>) -> MacroExpander<'a, 'b> {
11851197
MacroExpander { cx: cx }
11861198
}
1199+
1200+
fn strip_unconfigured(&mut self) -> StripUnconfigured {
1201+
StripUnconfigured::new(&self.cx.cfg,
1202+
&self.cx.parse_sess.span_diagnostic,
1203+
self.cx.feature_gated_cfgs)
1204+
}
11871205
}
11881206

11891207
impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
@@ -1193,7 +1211,11 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
11931211
}
11941212

11951213
fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
1196-
expand_expr(expr, self)
1214+
expand_expr(expr, self, false).unwrap()
1215+
}
1216+
1217+
fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
1218+
expand_expr(expr, self, true)
11971219
}
11981220

11991221
fn fold_pat(&mut self, pat: P<ast::Pat>) -> P<ast::Pat> {

0 commit comments

Comments
 (0)