Skip to content

Commit 51e6df6

Browse files
committed
rustc: Allow the test runner to run unexported tests
1 parent d2218d9 commit 51e6df6

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

src/comp/front/test.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,12 @@ fn mk_test_module(cx: test_ctxt) -> @ast::item {
187187
let mainfn = mk_main(cx);
188188
let testmod: ast::_mod = {view_items: [], items: [mainfn, testsfn]};
189189
let item_ = ast::item_mod(testmod);
190+
// This attribute tells resolve to let us call unexported functions
191+
let resolve_unexported_attr =
192+
attr::mk_attr(attr::mk_word_item("!resolve_unexported"));
190193
let item: ast::item =
191194
{ident: "__test",
192-
attrs: [],
195+
attrs: [resolve_unexported_attr],
193196
id: cx.sess.next_node_id(),
194197
node: item_,
195198
span: dummy_sp()};

src/comp/middle/resolve.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
import syntax::{ast, ast_util, codemap};
33
import syntax::ast::*;
44
import ast::{ident, fn_ident, def, def_id, node_id};
5-
import syntax::ast_util::{local_def, def_id_of_def, is_exported};
5+
import syntax::ast_util::{local_def, def_id_of_def};
66

7+
import front::attr;
78
import metadata::{csearch, cstore};
89
import driver::session::session;
910
import util::common::*;
@@ -143,6 +144,7 @@ type env =
143144
mutable reported: [{ident: str, sc: scope}],
144145
mutable ignored_imports: [node_id],
145146
mutable current_tp: option::t<uint>,
147+
mutable resolve_unexported: bool,
146148
sess: session};
147149

148150

@@ -170,6 +172,7 @@ fn resolve_crate(sess: session, amap: ast_map::map, crate: @ast::crate) ->
170172
mutable reported: [],
171173
mutable ignored_imports: [],
172174
mutable current_tp: none,
175+
mutable resolve_unexported: false,
173176
sess: sess};
174177
map_crate(e, crate);
175178
resolve_imports(*e);
@@ -231,7 +234,7 @@ fn map_crate(e: @env, c: @ast::crate) {
231234
path
232235
}
233236
fn index_i(e: @env, i: @ast::item, sc: scopes, v: vt<scopes>) {
234-
visit_item_with_scope(i, sc, v);
237+
visit_item_with_scope(e, i, sc, v);
235238
alt i.node {
236239
ast::item_mod(md) {
237240
e.mod_map.insert(i.id,
@@ -257,7 +260,7 @@ fn map_crate(e: @env, c: @ast::crate) {
257260
let v_link_glob =
258261
@{visit_view_item: bind link_glob(e, _, _, _),
259262
visit_block: visit_block_with_scope,
260-
visit_item: visit_item_with_scope
263+
visit_item: bind visit_item_with_scope(e, _, _, _)
261264
with *visit::default_visitor::<scopes>()};
262265
visit::visit_crate(*c, cons(scope_crate, @nil),
263266
visit::mk_vt(v_link_glob));
@@ -331,7 +334,7 @@ fn resolve_names(e: @env, c: @ast::crate) {
331334
e.used_imports.track = true;
332335
let v =
333336
@{visit_native_item: visit_native_item_with_scope,
334-
visit_item: visit_item_with_scope,
337+
visit_item: bind visit_item_with_scope(e, _, _, _),
335338
visit_block: visit_block_with_scope,
336339
visit_decl: visit_decl_with_scope,
337340
visit_arm: visit_arm_with_scope,
@@ -413,7 +416,17 @@ fn resolve_names(e: @env, c: @ast::crate) {
413416

414417

415418
// Visit helper functions
416-
fn visit_item_with_scope(i: @ast::item, sc: scopes, v: vt<scopes>) {
419+
fn visit_item_with_scope(e: @env, i: @ast::item, sc: scopes, v: vt<scopes>) {
420+
421+
// Some magic here. Items with the !resolve_unexported attribute
422+
// cause us to consider every name to be exported when resolving their
423+
// contents. This is used to allow the test runner to run unexported
424+
// tests.
425+
let old_resolve_unexported = e.resolve_unexported;
426+
e.resolve_unexported |=
427+
attr::contains_name(attr::attr_metas(i.attrs),
428+
"!resolve_unexported");
429+
417430
let sc = cons(scope_item(i), @sc);
418431
alt i.node {
419432
ast::item_impl(tps, ifce, sty, methods) {
@@ -436,6 +449,8 @@ fn visit_item_with_scope(i: @ast::item, sc: scopes, v: vt<scopes>) {
436449
}
437450
_ { visit::visit_item(i, sc, v); }
438451
}
452+
453+
e.resolve_unexported = old_resolve_unexported;
439454
}
440455

441456
fn visit_native_item_with_scope(ni: @ast::native_item, sc: scopes,
@@ -1187,10 +1202,14 @@ fn lookup_in_local_native_mod(e: env, node_id: node_id, sp: span, id: ident,
11871202
ret lookup_in_local_mod(e, node_id, sp, id, ns, inside);
11881203
}
11891204

1205+
fn is_exported(e: env, i: ident, m: _mod) -> bool {
1206+
ast_util::is_exported(i, m) || e.resolve_unexported
1207+
}
1208+
11901209
fn lookup_in_local_mod(e: env, node_id: node_id, sp: span, id: ident,
11911210
ns: namespace, dr: dir) -> option::t<def> {
11921211
let info = e.mod_map.get(node_id);
1193-
if dr == outside && !is_exported(id, option::get(info.m)) {
1212+
if dr == outside && !is_exported(e, id, option::get(info.m)) {
11941213
// if we're in a native mod, then dr==inside, so info.m is some _mod
11951214
ret none::<def>; // name is not visible
11961215
}
@@ -1792,7 +1811,10 @@ fn find_impls_in_item(e: env, i: @ast::item, &impls: [@_impl],
17921811
alt i.node {
17931812
ast::item_impl(_, ifce, _, mthds) {
17941813
if alt name { some(n) { n == i.ident } _ { true } } &&
1795-
alt ck_exports { some(m) { is_exported(i.ident, m) } _ { true } } {
1814+
alt ck_exports {
1815+
some(m) { is_exported(e, i.ident, m) }
1816+
_ { true }
1817+
} {
17961818
impls += [@{did: local_def(i.id),
17971819
iface_did: alt ifce {
17981820
some(@{node: ast::ty_path(_, id), _}) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// error-pattern:runned an unexported test
2+
// compile-flags:--test
3+
4+
use std;
5+
6+
mod m {
7+
export exported;
8+
9+
fn exported() { }
10+
11+
#[test]
12+
fn unexported() { fail "runned an unexported test"; }
13+
}

0 commit comments

Comments
 (0)