Skip to content

Commit 0d83403

Browse files
committedAug 4, 2015
syntax: Don't assume std exists for tests
This commit removes the injection of `std::env::args()` from `--test` expanded code, relying on the test runner itself to call this funciton. This is more hygienic because we can't assume that `std` exists at the top layer all the time, and it meaks the injected test module entirely self contained.
1 parent 5cccf3c commit 0d83403

File tree

7 files changed

+23
-33
lines changed

7 files changed

+23
-33
lines changed
 

‎src/libstd/lib.rs

-8
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,3 @@ pub mod __rand {
414414
// the rustdoc documentation for primitive types. Using `include!`
415415
// because rustdoc only looks for these modules at the crate level.
416416
include!("primitive_docs.rs");
417-
418-
// The expansion of --test has a few references to `::std::$foo` so this module
419-
// is necessary to get things to compile.
420-
#[cfg(test)]
421-
mod std {
422-
pub use option;
423-
pub use realstd::env;
424-
}

‎src/libstd/process.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ mod tests {
815815
#[cfg(target_os="android")]
816816
#[test]
817817
fn test_inherit_env() {
818-
use std::env;
818+
use env;
819819

820820
let mut result = env_cmd().output().unwrap();
821821
let output = String::from_utf8(result.stdout).unwrap();

‎src/libstd/sys/unix/thread.rs

+1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ impl Drop for Thread {
166166
not(target_os = "netbsd"),
167167
not(target_os = "openbsd")))]
168168
pub mod guard {
169+
#[cfg(stage0)]
169170
use prelude::v1::*;
170171

171172
pub unsafe fn current() -> Option<usize> { None }

‎src/libstd/sys/windows/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#[cfg(stage0)]
1211
use prelude::v1::*;
1312

1413
use alloc::boxed::FnBox;
@@ -87,6 +86,7 @@ impl Thread {
8786
}
8887

8988
pub mod guard {
89+
#[cfg(stage0)]
9090
use prelude::v1::*;
9191

9292
pub unsafe fn current() -> Option<usize> { None }

‎src/libstd/thread/local.rs

+1
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ mod imp {
329329
// Due to rust-lang/rust#18804, make sure this is not generic!
330330
#[cfg(target_os = "linux")]
331331
unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern fn(*mut u8)) {
332+
use prelude::v1::*;
332333
use mem;
333334
use libc;
334335
use sys_common::thread_local as os;

‎src/libsyntax/test.rs

+9-14
Original file line numberDiff line numberDiff line change
@@ -450,18 +450,11 @@ fn mk_main(cx: &mut TestCtxt) -> P<ast::Item> {
450450
// test::test_main_static
451451
let test_main_path = ecx.path(sp, vec![token::str_to_ident("test"),
452452
token::str_to_ident("test_main_static")]);
453-
// ::std::env::args
454-
let os_args_path = ecx.path_global(sp, vec![token::str_to_ident("std"),
455-
token::str_to_ident("env"),
456-
token::str_to_ident("args")]);
457-
// ::std::env::args()
458-
let os_args_path_expr = ecx.expr_path(os_args_path);
459-
let call_os_args = ecx.expr_call(sp, os_args_path_expr, vec![]);
460453
// test::test_main_static(...)
461454
let test_main_path_expr = ecx.expr_path(test_main_path);
462455
let tests_ident_expr = ecx.expr_ident(sp, token::str_to_ident("TESTS"));
463456
let call_test_main = ecx.expr_call(sp, test_main_path_expr,
464-
vec![call_os_args, tests_ident_expr]);
457+
vec![tests_ident_expr]);
465458
let call_test_main = ecx.stmt_expr(call_test_main);
466459
// #![main]
467460
let main_meta = ecx.meta_word(sp, token::intern_and_get_ident("main"));
@@ -634,12 +627,14 @@ fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> P<ast::Expr> {
634627
let fail_expr = match test.should_panic {
635628
ShouldPanic::No => ecx.expr_path(should_panic_path("No")),
636629
ShouldPanic::Yes(ref msg) => {
637-
let path = should_panic_path("Yes");
638-
let arg = match *msg {
639-
Some(ref msg) => ecx.expr_some(span, ecx.expr_str(span, msg.clone())),
640-
None => ecx.expr_none(span),
641-
};
642-
ecx.expr_call(span, ecx.expr_path(path), vec![arg])
630+
match *msg {
631+
Some(ref msg) => {
632+
let msg = ecx.expr_str(span, msg.clone());
633+
let path = should_panic_path("YesWithMessage");
634+
ecx.expr_call(span, ecx.expr_path(path), vec![msg])
635+
}
636+
None => ecx.expr_path(should_panic_path("Yes")),
637+
}
643638
}
644639
};
645640

‎src/libtest/lib.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ pub struct Bencher {
197197
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
198198
pub enum ShouldPanic {
199199
No,
200-
Yes(Option<&'static str>)
200+
Yes,
201+
YesWithMessage(&'static str)
201202
}
202203

203204
// The definition of a single test. A test runner will run a list of
@@ -262,8 +263,8 @@ pub fn test_main(args: &[String], tests: Vec<TestDescAndFn> ) {
262263
// a Vec<TestDescAndFn> is used in order to effect ownership-transfer
263264
// semantics into parallel test runners, which in turn requires a Vec<>
264265
// rather than a &[].
265-
pub fn test_main_static(args: env::Args, tests: &[TestDescAndFn]) {
266-
let args = args.collect::<Vec<_>>();
266+
pub fn test_main_static(tests: &[TestDescAndFn]) {
267+
let args = env::args().collect::<Vec<_>>();
267268
let owned_tests = tests.iter().map(|t| {
268269
match t.testfn {
269270
StaticTestFn(f) => TestDescAndFn { testfn: StaticTestFn(f), desc: t.desc.clone() },
@@ -1027,8 +1028,8 @@ pub fn run_test(opts: &TestOpts,
10271028
fn calc_result(desc: &TestDesc, task_result: Result<(), Box<Any+Send>>) -> TestResult {
10281029
match (&desc.should_panic, task_result) {
10291030
(&ShouldPanic::No, Ok(())) |
1030-
(&ShouldPanic::Yes(None), Err(_)) => TrOk,
1031-
(&ShouldPanic::Yes(Some(msg)), Err(ref err))
1031+
(&ShouldPanic::Yes, Err(_)) => TrOk,
1032+
(&ShouldPanic::YesWithMessage(msg), Err(ref err))
10321033
if err.downcast_ref::<String>()
10331034
.map(|e| &**e)
10341035
.or_else(|| err.downcast_ref::<&'static str>().map(|e| *e))
@@ -1276,7 +1277,7 @@ mod tests {
12761277
desc: TestDesc {
12771278
name: StaticTestName("whatever"),
12781279
ignore: false,
1279-
should_panic: ShouldPanic::Yes(None)
1280+
should_panic: ShouldPanic::Yes,
12801281
},
12811282
testfn: DynTestFn(Box::new(move|| f())),
12821283
};
@@ -1293,7 +1294,7 @@ mod tests {
12931294
desc: TestDesc {
12941295
name: StaticTestName("whatever"),
12951296
ignore: false,
1296-
should_panic: ShouldPanic::Yes(Some("error message"))
1297+
should_panic: ShouldPanic::YesWithMessage("error message"),
12971298
},
12981299
testfn: DynTestFn(Box::new(move|| f())),
12991300
};
@@ -1310,7 +1311,7 @@ mod tests {
13101311
desc: TestDesc {
13111312
name: StaticTestName("whatever"),
13121313
ignore: false,
1313-
should_panic: ShouldPanic::Yes(Some("foobar"))
1314+
should_panic: ShouldPanic::YesWithMessage("foobar"),
13141315
},
13151316
testfn: DynTestFn(Box::new(move|| f())),
13161317
};
@@ -1327,7 +1328,7 @@ mod tests {
13271328
desc: TestDesc {
13281329
name: StaticTestName("whatever"),
13291330
ignore: false,
1330-
should_panic: ShouldPanic::Yes(None)
1331+
should_panic: ShouldPanic::Yes,
13311332
},
13321333
testfn: DynTestFn(Box::new(move|| f())),
13331334
};

0 commit comments

Comments
 (0)