Skip to content

Commit 9f9deff

Browse files
nikomatsakisbrson
authored andcommitted
make a good error msg if you try to use an unsafe fn for a test
1 parent b1a9d71 commit 9f9deff

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/comp/driver/rustc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn compile_input(sess: session::session, cfg: ast::crate_cfg, input: str,
110110
if sess.get_opts().test {
111111
crate =
112112
time(time_passes, "building test harness",
113-
bind front::test::modify_for_testing(crate));
113+
bind front::test::modify_for_testing(sess, crate));
114114
}
115115
crate =
116116
time(time_passes, "expansion",

src/comp/front/test.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import syntax::ast_util::*;
77
import syntax::fold;
88
import syntax::print::pprust;
99
import syntax::codemap::span;
10+
import driver::session;
1011
import front::attr;
1112

1213
export modify_for_testing;
@@ -16,13 +17,15 @@ type node_id_gen = fn() -> ast::node_id;
1617
type test = {span: span, path: [ast::ident], ignore: bool};
1718

1819
type test_ctxt =
19-
@{next_node_id: node_id_gen,
20+
@{sess: session::session,
21+
next_node_id: node_id_gen,
2022
mutable path: [ast::ident],
2123
mutable testfns: [test]};
2224

2325
// Traverse the crate, collecting all the test functions, eliding any
2426
// existing main functions, and synthesizing a main test harness
25-
fn modify_for_testing(crate: @ast::crate) -> @ast::crate {
27+
fn modify_for_testing(sess: session::session,
28+
crate: @ast::crate) -> @ast::crate {
2629

2730
// FIXME: This hackasaurus assumes that 200000 is a safe number to start
2831
// generating node_ids at (which is totally not the case). pauls is going
@@ -37,7 +40,8 @@ fn modify_for_testing(crate: @ast::crate) -> @ast::crate {
3740
}(next_node_id);
3841

3942
let cx: test_ctxt =
40-
@{next_node_id: next_node_id_fn,
43+
@{sess: sess,
44+
next_node_id: next_node_id_fn,
4145
mutable path: [],
4246
mutable testfns: []};
4347

@@ -90,10 +94,19 @@ fn fold_item(cx: test_ctxt, &&i: @ast::item, fld: fold::ast_fold) ->
9094
log #fmt["current path: %s", ast_util::path_name_i(cx.path)];
9195

9296
if is_test_fn(i) {
93-
log "this is a test function";
94-
let test = {span: i.span, path: cx.path, ignore: is_ignored(i)};
95-
cx.testfns += [test];
96-
log #fmt["have %u test functions", vec::len(cx.testfns)];
97+
alt i.node {
98+
ast::item_fn(f, _) when f.decl.purity == ast::unsafe_fn {
99+
cx.sess.span_fatal(
100+
i.span,
101+
"unsafe functions cannot be used for tests");
102+
}
103+
_ {
104+
log "this is a test function";
105+
let test = {span: i.span, path: cx.path, ignore: is_ignored(i)};
106+
cx.testfns += [test];
107+
log #fmt["have %u test functions", vec::len(cx.testfns)];
108+
}
109+
}
97110
}
98111

99112
let res = fold::noop_fold_item(i, fld);

0 commit comments

Comments
 (0)