Skip to content

Commit c2c497f

Browse files
committed
rustc: Configure out #[test] functions when not testing
1 parent a2acb05 commit c2c497f

File tree

4 files changed

+62
-25
lines changed

4 files changed

+62
-25
lines changed

src/comp/driver/driver.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,9 @@ fn compile_input(sess: session::session, cfg: ast::crate_cfg, input: str,
146146
crate =
147147
time(time_passes, "configuration",
148148
bind front::config::strip_unconfigured_items(crate));
149-
if sess.get_opts().test {
150-
crate =
151-
time(time_passes, "building test harness",
152-
bind front::test::modify_for_testing(sess, crate));
153-
}
149+
crate =
150+
time(time_passes, "maybe building test harness",
151+
bind front::test::modify_for_testing(sess, crate));
154152
crate =
155153
time(time_passes, "expansion",
156154
bind syntax::ext::expand::expand_crate(sess, crate));

src/comp/front/config.rs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,73 @@ import attr;
44

55
export strip_unconfigured_items;
66
export metas_in_cfg;
7+
export strip_items;
8+
9+
type in_cfg_pred = fn@([ast::attribute]) -> bool;
10+
11+
type ctxt = @{
12+
in_cfg: in_cfg_pred
13+
};
714

815
// Support conditional compilation by transforming the AST, stripping out
916
// any items that do not belong in the current configuration
1017
fn strip_unconfigured_items(crate: @ast::crate) -> @ast::crate {
11-
let cfg = crate.node.config;
18+
strip_items(crate) {|attrs|
19+
in_cfg(crate.node.config, attrs)
20+
}
21+
}
22+
23+
fn strip_items(crate: @ast::crate, in_cfg: in_cfg_pred)
24+
-> @ast::crate {
25+
26+
let ctxt = @{in_cfg: in_cfg};
1227

1328
let precursor =
14-
{fold_mod: bind fold_mod(cfg, _, _),
15-
fold_block: bind fold_block(cfg, _, _),
16-
fold_native_mod: bind fold_native_mod(cfg, _, _)
29+
{fold_mod: bind fold_mod(ctxt, _, _),
30+
fold_block: bind fold_block(ctxt, _, _),
31+
fold_native_mod: bind fold_native_mod(ctxt, _, _)
1732
with *fold::default_ast_fold()};
1833

1934
let fold = fold::make_fold(precursor);
2035
let res = @fold.fold_crate(*crate);
2136
ret res;
2237
}
2338

24-
fn filter_item(cfg: ast::crate_cfg, &&item: @ast::item) ->
39+
fn filter_item(cx: ctxt, &&item: @ast::item) ->
2540
option::t<@ast::item> {
26-
if item_in_cfg(cfg, item) { option::some(item) } else { option::none }
41+
if item_in_cfg(cx, item) { option::some(item) } else { option::none }
2742
}
2843

29-
fn fold_mod(cfg: ast::crate_cfg, m: ast::_mod, fld: fold::ast_fold) ->
44+
fn fold_mod(cx: ctxt, m: ast::_mod, fld: fold::ast_fold) ->
3045
ast::_mod {
31-
let filter = bind filter_item(cfg, _);
46+
let filter = bind filter_item(cx, _);
3247
let filtered_items = vec::filter_map(m.items, filter);
3348
ret {view_items: vec::map(m.view_items, fld.fold_view_item),
3449
items: vec::map(filtered_items, fld.fold_item)};
3550
}
3651

37-
fn filter_native_item(cfg: ast::crate_cfg, &&item: @ast::native_item) ->
52+
fn filter_native_item(cx: ctxt, &&item: @ast::native_item) ->
3853
option::t<@ast::native_item> {
39-
if native_item_in_cfg(cfg, item) {
54+
if native_item_in_cfg(cx, item) {
4055
option::some(item)
4156
} else { option::none }
4257
}
4358

44-
fn fold_native_mod(cfg: ast::crate_cfg, nm: ast::native_mod,
59+
fn fold_native_mod(cx: ctxt, nm: ast::native_mod,
4560
fld: fold::ast_fold) -> ast::native_mod {
46-
let filter = bind filter_native_item(cfg, _);
61+
let filter = bind filter_native_item(cx, _);
4762
let filtered_items = vec::filter_map(nm.items, filter);
4863
ret {view_items: vec::map(nm.view_items, fld.fold_view_item),
4964
items: filtered_items};
5065
}
5166

52-
fn filter_stmt(cfg: ast::crate_cfg, &&stmt: @ast::stmt) ->
67+
fn filter_stmt(cx: ctxt, &&stmt: @ast::stmt) ->
5368
option::t<@ast::stmt> {
5469
alt stmt.node {
5570
ast::stmt_decl(decl, _) {
5671
alt decl.node {
5772
ast::decl_item(item) {
58-
if item_in_cfg(cfg, item) {
73+
if item_in_cfg(cx, item) {
5974
option::some(stmt)
6075
} else { option::none }
6176
}
@@ -66,9 +81,9 @@ fn filter_stmt(cfg: ast::crate_cfg, &&stmt: @ast::stmt) ->
6681
}
6782
}
6883

69-
fn fold_block(cfg: ast::crate_cfg, b: ast::blk_, fld: fold::ast_fold) ->
84+
fn fold_block(cx: ctxt, b: ast::blk_, fld: fold::ast_fold) ->
7085
ast::blk_ {
71-
let filter = bind filter_stmt(cfg, _);
86+
let filter = bind filter_stmt(cx, _);
7287
let filtered_stmts = vec::filter_map(b.stmts, filter);
7388
ret {view_items: b.view_items,
7489
stmts: vec::map(filtered_stmts, fld.fold_stmt),
@@ -77,12 +92,12 @@ fn fold_block(cfg: ast::crate_cfg, b: ast::blk_, fld: fold::ast_fold) ->
7792
rules: b.rules};
7893
}
7994

80-
fn item_in_cfg(cfg: ast::crate_cfg, item: @ast::item) -> bool {
81-
ret in_cfg(cfg, item.attrs);
95+
fn item_in_cfg(cx: ctxt, item: @ast::item) -> bool {
96+
ret cx.in_cfg(item.attrs);
8297
}
8398

84-
fn native_item_in_cfg(cfg: ast::crate_cfg, item: @ast::native_item) -> bool {
85-
ret in_cfg(cfg, item.attrs);
99+
fn native_item_in_cfg(cx: ctxt, item: @ast::native_item) -> bool {
100+
ret cx.in_cfg(item.attrs);
86101
}
87102

88103
// Determine if an item should be translated in the current crate

src/comp/front/test.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ type test_ctxt =
2727
fn modify_for_testing(sess: session::session,
2828
crate: @ast::crate) -> @ast::crate {
2929

30+
if sess.get_opts().test {
31+
generate_test_harness(sess, crate)
32+
} else {
33+
strip_test_functions(crate)
34+
}
35+
}
36+
37+
fn generate_test_harness(sess: session::session,
38+
crate: @ast::crate) -> @ast::crate {
3039
let cx: test_ctxt =
3140
@{sess: sess,
3241
crate: crate,
@@ -43,6 +52,14 @@ fn modify_for_testing(sess: session::session,
4352
ret res;
4453
}
4554

55+
fn strip_test_functions(crate: @ast::crate) -> @ast::crate {
56+
// When not compiling with --test we should not compile the
57+
// #[test] functions
58+
config::strip_items(crate) {|attrs|
59+
!attr::contains_name(attr::attr_metas(attrs), "test")
60+
}
61+
}
62+
4663
fn fold_mod(_cx: test_ctxt, m: ast::_mod, fld: fold::ast_fold) -> ast::_mod {
4764

4865
// Remove any defined main function from the AST so it doesn't clash with

src/test/compile-fail/elided-test.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// error-pattern: main function not found
2+
3+
// Since we're not compiling a test runner this function should be elided
4+
// and the build will fail because main doesn't exist
5+
#[test]
6+
fn main() {
7+
}

0 commit comments

Comments
 (0)