Skip to content

Commit 413328b

Browse files
committed
auto merge of #15964 : huonw/rust/gensym-test, r=alexcrichton
This requires avoiding `quote_...!` for constructing the parts of the __test module, since that stringifies and reinterns the idents, losing the special gensym'd nature of them. (#15962.)
2 parents 39bafb0 + edc9191 commit 413328b

File tree

10 files changed

+184
-115
lines changed

10 files changed

+184
-115
lines changed

src/librustc/front/test.rs

+107-67
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ struct TestCtxt<'a> {
5151
ext_cx: ExtCtxt<'a>,
5252
testfns: Vec<Test>,
5353
reexport_mod_ident: ast::Ident,
54+
reexport_test_harness_main: Option<InternedString>,
5455
is_test_crate: bool,
5556
config: ast::CrateConfig,
5657
}
@@ -64,8 +65,16 @@ pub fn modify_for_testing(sess: &Session,
6465
// command line options.
6566
let should_test = attr::contains_name(krate.config.as_slice(), "test");
6667

68+
// Check for #[reexport_test_harness_main = "some_name"] which
69+
// creates a `use some_name = __test::main;`. This needs to be
70+
// unconditional, so that the attribute is still marked as used in
71+
// non-test builds.
72+
let reexport_test_harness_main =
73+
attr::first_attr_value_str_by_name(krate.attrs.as_slice(),
74+
"reexport_test_harness_main");
75+
6776
if should_test {
68-
generate_test_harness(sess, krate)
77+
generate_test_harness(sess, reexport_test_harness_main, krate)
6978
} else {
7079
strip_test_functions(krate)
7180
}
@@ -79,14 +88,17 @@ struct TestHarnessGenerator<'a> {
7988

8089
impl<'a> fold::Folder for TestHarnessGenerator<'a> {
8190
fn fold_crate(&mut self, c: ast::Crate) -> ast::Crate {
82-
let folded = fold::noop_fold_crate(c, self);
91+
let mut folded = fold::noop_fold_crate(c, self);
8392

8493
// Add a special __test module to the crate that will contain code
8594
// generated for the test harness
86-
ast::Crate {
87-
module: add_test_module(&self.cx, &folded.module),
88-
.. folded
95+
let (mod_, reexport) = mk_test_module(&self.cx, &self.cx.reexport_test_harness_main);
96+
folded.module.items.push(mod_);
97+
match reexport {
98+
Some(re) => folded.module.view_items.push(re),
99+
None => {}
89100
}
101+
folded
90102
}
91103

92104
fn fold_item(&mut self, i: Gc<ast::Item>) -> SmallVector<Gc<ast::Item>> {
@@ -196,7 +208,9 @@ fn mk_reexport_mod(cx: &mut TestCtxt, tests: Vec<ast::Ident>,
196208
}
197209
}
198210

199-
fn generate_test_harness(sess: &Session, krate: ast::Crate) -> ast::Crate {
211+
fn generate_test_harness(sess: &Session,
212+
reexport_test_harness_main: Option<InternedString>,
213+
krate: ast::Crate) -> ast::Crate {
200214
let mut cx: TestCtxt = TestCtxt {
201215
sess: sess,
202216
ext_cx: ExtCtxt::new(&sess.parse_sess, sess.opts.cfg.clone(),
@@ -206,7 +220,8 @@ fn generate_test_harness(sess: &Session, krate: ast::Crate) -> ast::Crate {
206220
}),
207221
path: Vec::new(),
208222
testfns: Vec::new(),
209-
reexport_mod_ident: token::str_to_ident("__test_reexports"),
223+
reexport_mod_ident: token::gensym_ident("__test_reexports"),
224+
reexport_test_harness_main: reexport_test_harness_main,
210225
is_test_crate: is_test_crate(&krate),
211226
config: krate.config.clone(),
212227
};
@@ -314,14 +329,6 @@ fn should_fail(i: Gc<ast::Item>) -> bool {
314329
attr::contains_name(i.attrs.as_slice(), "should_fail")
315330
}
316331

317-
fn add_test_module(cx: &TestCtxt, m: &ast::Mod) -> ast::Mod {
318-
let testmod = mk_test_module(cx);
319-
ast::Mod {
320-
items: m.items.clone().append_one(testmod),
321-
..(*m).clone()
322-
}
323-
}
324-
325332
/*
326333
327334
We're going to be building a module that looks more or less like:
@@ -359,7 +366,8 @@ fn mk_std(cx: &TestCtxt) -> ast::ViewItem {
359366
}
360367
}
361368

362-
fn mk_test_module(cx: &TestCtxt) -> Gc<ast::Item> {
369+
fn mk_test_module(cx: &TestCtxt, reexport_test_harness_main: &Option<InternedString>)
370+
-> (Gc<ast::Item>, Option<ast::ViewItem>) {
363371
// Link to test crate
364372
let view_items = vec!(mk_std(cx));
365373

@@ -383,18 +391,35 @@ fn mk_test_module(cx: &TestCtxt) -> Gc<ast::Item> {
383391
};
384392
let item_ = ast::ItemMod(testmod);
385393

394+
let mod_ident = token::gensym_ident("__test");
386395
let item = ast::Item {
387-
ident: token::str_to_ident("__test"),
396+
ident: mod_ident,
388397
attrs: Vec::new(),
389398
id: ast::DUMMY_NODE_ID,
390399
node: item_,
391400
vis: ast::Public,
392401
span: DUMMY_SP,
393-
};
402+
};
403+
let reexport = reexport_test_harness_main.as_ref().map(|s| {
404+
// building `use <ident> = __test::main`
405+
let reexport_ident = token::str_to_ident(s.get());
406+
407+
let use_path =
408+
nospan(ast::ViewPathSimple(reexport_ident,
409+
path_node(vec![mod_ident, token::str_to_ident("main")]),
410+
ast::DUMMY_NODE_ID));
411+
412+
ast::ViewItem {
413+
node: ast::ViewItemUse(box(GC) use_path),
414+
attrs: vec![],
415+
vis: ast::Inherited,
416+
span: DUMMY_SP
417+
}
418+
});
394419

395420
debug!("Synthetic test module:\n{}\n", pprust::item_to_string(&item));
396421

397-
box(GC) item
422+
(box(GC) item, reexport)
398423
}
399424

400425
fn nospan<T>(t: T) -> codemap::Spanned<T> {
@@ -417,11 +442,27 @@ fn mk_tests(cx: &TestCtxt) -> Gc<ast::Item> {
417442
// The vector of test_descs for this crate
418443
let test_descs = mk_test_descs(cx);
419444

420-
(quote_item!(&cx.ext_cx,
421-
pub static TESTS : &'static [self::test::TestDescAndFn] =
422-
$test_descs
423-
;
424-
)).unwrap()
445+
// FIXME #15962: should be using quote_item, but that stringifies
446+
// __test_reexports, causing it to be reinterned, losing the
447+
// gensym information.
448+
let sp = DUMMY_SP;
449+
let ecx = &cx.ext_cx;
450+
let struct_type = ecx.ty_path(ecx.path(sp, vec![ecx.ident_of("self"),
451+
ecx.ident_of("test"),
452+
ecx.ident_of("TestDescAndFn")]),
453+
None);
454+
let static_lt = ecx.lifetime(sp, token::special_idents::static_lifetime.name);
455+
// &'static [self::test::TestDescAndFn]
456+
let static_type = ecx.ty_rptr(sp,
457+
ecx.ty(sp, ast::TyVec(struct_type)),
458+
Some(static_lt),
459+
ast::MutImmutable);
460+
// static TESTS: $static_type = &[...];
461+
ecx.item_static(sp,
462+
ecx.ident_of("TESTS"),
463+
static_type,
464+
ast::MutImmutable,
465+
test_descs)
425466
}
426467

427468
fn is_test_crate(krate: &ast::Crate) -> bool {
@@ -448,59 +489,58 @@ fn mk_test_descs(cx: &TestCtxt) -> Gc<ast::Expr> {
448489
}
449490

450491
fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> Gc<ast::Expr> {
492+
// FIXME #15962: should be using quote_expr, but that stringifies
493+
// __test_reexports, causing it to be reinterned, losing the
494+
// gensym information.
495+
451496
let span = test.span;
452497
let path = test.path.clone();
498+
let ecx = &cx.ext_cx;
499+
let self_id = ecx.ident_of("self");
500+
let test_id = ecx.ident_of("test");
501+
502+
// creates self::test::$name
503+
let test_path = |name| {
504+
ecx.path(span, vec![self_id, test_id, ecx.ident_of(name)])
505+
};
506+
// creates $name: $expr
507+
let field = |name, expr| ecx.field_imm(span, ecx.ident_of(name), expr);
453508

454509
debug!("encoding {}", ast_util::path_name_i(path.as_slice()));
455510

456-
let name_lit: ast::Lit =
457-
nospan(ast::LitStr(token::intern_and_get_ident(
458-
ast_util::path_name_i(path.as_slice()).as_slice()),
459-
ast::CookedStr));
511+
// path to the #[test] function: "foo::bar::baz"
512+
let path_string = ast_util::path_name_i(path.as_slice());
513+
let name_expr = ecx.expr_str(span, token::intern_and_get_ident(path_string.as_slice()));
460514

461-
let name_expr = box(GC) ast::Expr {
462-
id: ast::DUMMY_NODE_ID,
463-
node: ast::ExprLit(box(GC) name_lit),
464-
span: span
465-
};
515+
// self::test::StaticTestName($name_expr)
516+
let name_expr = ecx.expr_call(span,
517+
ecx.expr_path(test_path("StaticTestName")),
518+
vec![name_expr]);
466519

467-
let mut visible_path = vec![cx.reexport_mod_ident.clone()];
468-
visible_path.extend(path.move_iter());
469-
let fn_path = cx.ext_cx.path_global(DUMMY_SP, visible_path);
520+
let ignore_expr = ecx.expr_bool(span, test.ignore);
521+
let fail_expr = ecx.expr_bool(span, test.should_fail);
470522

471-
let fn_expr = box(GC) ast::Expr {
472-
id: ast::DUMMY_NODE_ID,
473-
node: ast::ExprPath(fn_path),
474-
span: span,
475-
};
523+
// self::test::TestDesc { ... }
524+
let desc_expr = ecx.expr_struct(
525+
span,
526+
test_path("TestDesc"),
527+
vec![field("name", name_expr),
528+
field("ignore", ignore_expr),
529+
field("should_fail", fail_expr)]);
476530

477-
let t_expr = if test.bench {
478-
quote_expr!(&cx.ext_cx, self::test::StaticBenchFn($fn_expr) )
479-
} else {
480-
quote_expr!(&cx.ext_cx, self::test::StaticTestFn($fn_expr) )
481-
};
482531

483-
let ignore_expr = if test.ignore {
484-
quote_expr!(&cx.ext_cx, true )
485-
} else {
486-
quote_expr!(&cx.ext_cx, false )
487-
};
532+
let mut visible_path = vec![cx.reexport_mod_ident.clone()];
533+
visible_path.extend(path.move_iter());
488534

489-
let fail_expr = if test.should_fail {
490-
quote_expr!(&cx.ext_cx, true )
491-
} else {
492-
quote_expr!(&cx.ext_cx, false )
493-
};
535+
let fn_expr = ecx.expr_path(ecx.path_global(span, visible_path));
494536

495-
let e = quote_expr!(&cx.ext_cx,
496-
self::test::TestDescAndFn {
497-
desc: self::test::TestDesc {
498-
name: self::test::StaticTestName($name_expr),
499-
ignore: $ignore_expr,
500-
should_fail: $fail_expr
501-
},
502-
testfn: $t_expr,
503-
}
504-
);
505-
e
537+
let variant_name = if test.bench { "StaticBenchFn" } else { "StaticTestFn" };
538+
// self::test::$variant_name($fn_expr)
539+
let testfn_expr = ecx.expr_call(span, ecx.expr_path(test_path(variant_name)), vec![fn_expr]);
540+
541+
// self::test::TestDescAndFn { ... }
542+
ecx.expr_struct(span,
543+
test_path("TestDescAndFn"),
544+
vec![field("desc", desc_expr),
545+
field("testfn", testfn_expr)])
506546
}

src/librustuv/lib.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ via `close` and `delete` methods.
4848
#![deny(unused_result, unused_must_use)]
4949
#![allow(visible_private_types)]
5050

51+
#![reexport_test_harness_main = "test_main"]
52+
5153
#[cfg(test)] extern crate green;
5254
#[cfg(test)] extern crate debug;
5355
#[cfg(test)] extern crate realrustuv = "rustuv";
@@ -76,13 +78,9 @@ pub use self::timer::TimerWatcher;
7678
pub use self::tty::TtyWatcher;
7779

7880
// Run tests with libgreen instead of libnative.
79-
//
80-
// FIXME: This egregiously hacks around starting the test runner in a different
81-
// threading mode than the default by reaching into the auto-generated
82-
// '__test' module.
8381
#[cfg(test)] #[start]
8482
fn start(argc: int, argv: *const *const u8) -> int {
85-
green::start(argc, argv, event_loop, __test::main)
83+
green::start(argc, argv, event_loop, test_main)
8684
}
8785

8886
mod macros;

src/libstd/lib.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@
114114
#![allow(deprecated)]
115115
#![deny(missing_doc)]
116116

117+
#![reexport_test_harness_main = "test_main"]
118+
117119
// When testing libstd, bring in libuv as the I/O backend so tests can print
118120
// things and all of the std::io tests have an I/O interface to run on top
119121
// of
@@ -186,13 +188,9 @@ pub use unicode::char;
186188
pub use core_sync::comm;
187189

188190
// Run tests with libgreen instead of libnative.
189-
//
190-
// FIXME: This egregiously hacks around starting the test runner in a different
191-
// threading mode than the default by reaching into the auto-generated
192-
// '__test' module.
193191
#[cfg(test)] #[start]
194192
fn start(argc: int, argv: *const *const u8) -> int {
195-
green::start(argc, argv, rustuv::event_loop, __test::main)
193+
green::start(argc, argv, rustuv::event_loop, test_main)
196194
}
197195

198196
/* Exported macros */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags:--test
12+
13+
// the `--test` harness creates modules with these textual names, but
14+
// they should be inaccessible from normal code.
15+
use x = __test; //~ ERROR unresolved import `__test`
16+
use y = __test_reexports; //~ ERROR unresolved import `__test_reexports`
17+
18+
#[test]
19+
fn baz() {}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-include ../tools.mk
2+
3+
all:
4+
# check that #[ignore(cfg(...))] does the right thing.
5+
$(RUSTC) --test test-ignore-cfg.rs --cfg ignorecfg
6+
$(call RUN,test-ignore-cfg) | grep 'shouldnotignore ... ok'
7+
$(call RUN,test-ignore-cfg) | grep 'shouldignore ... ignored'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[test]
12+
#[ignore(cfg(ignorecfg))]
13+
fn shouldignore() {
14+
}
15+
16+
#[test]
17+
#[ignore(cfg(noignorecfg))]
18+
fn shouldnotignore() {
19+
}

src/test/run-pass/core-run-destroy.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
// instead of in std.
1717

1818
#![feature(macro_rules)]
19+
#![reexport_test_harness_main = "test_main"]
20+
1921
extern crate libc;
2022

2123
extern crate native;
@@ -55,7 +57,7 @@ macro_rules! iotest (
5557

5658
#[cfg(test)] #[start]
5759
fn start(argc: int, argv: *const *const u8) -> int {
58-
green::start(argc, argv, rustuv::event_loop, __test::main)
60+
green::start(argc, argv, rustuv::event_loop, test_main)
5961
}
6062

6163
iotest!(fn test_destroy_once() {

0 commit comments

Comments
 (0)