Skip to content

Commit 2e38581

Browse files
committed
auto merge of #16892 : andrew-d/rust/andrew-fix-test-reexports, r=sfackler
Fixes #16597 I'm not 100% sure this is the correct way to handle this - but I wasn't able to find a better way without doing way more refactoring of the code that I was comfortable with. Comments and criticism are appreciated 😄
2 parents 4e5d5ba + 9374d50 commit 2e38581

File tree

3 files changed

+67
-13
lines changed

3 files changed

+67
-13
lines changed

src/librustc/front/test.rs

+32-13
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ struct TestCtxt<'a> {
5050
path: Vec<ast::Ident>,
5151
ext_cx: ExtCtxt<'a>,
5252
testfns: Vec<Test>,
53-
reexport_mod_ident: ast::Ident,
5453
reexport_test_harness_main: Option<InternedString>,
5554
is_test_crate: bool,
5655
config: ast::CrateConfig,
56+
57+
// top-level re-export submodule, filled out after folding is finished
58+
toplevel_reexport: Option<ast::Ident>,
5759
}
5860

5961
// Traverse the crate, collecting all the test functions, eliding any
@@ -83,7 +85,9 @@ pub fn modify_for_testing(sess: &Session,
8385
struct TestHarnessGenerator<'a> {
8486
cx: TestCtxt<'a>,
8587
tests: Vec<ast::Ident>,
86-
tested_submods: Vec<ast::Ident>,
88+
89+
// submodule name, gensym'd identifier for re-exports
90+
tested_submods: Vec<(ast::Ident, ast::Ident)>,
8791
}
8892

8993
impl<'a> fold::Folder for TestHarnessGenerator<'a> {
@@ -168,10 +172,14 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
168172
*i = nomain(*i);
169173
}
170174
if !tests.is_empty() || !tested_submods.is_empty() {
171-
mod_folded.items.push(mk_reexport_mod(&mut self.cx, tests,
172-
tested_submods));
175+
let (it, sym) = mk_reexport_mod(&mut self.cx, tests, tested_submods);
176+
mod_folded.items.push(it);
177+
173178
if !self.cx.path.is_empty() {
174-
self.tested_submods.push(self.cx.path[self.cx.path.len()-1]);
179+
self.tested_submods.push((self.cx.path[self.cx.path.len()-1], sym));
180+
} else {
181+
debug!("pushing nothing, sym: {}", sym);
182+
self.cx.toplevel_reexport = Some(sym);
175183
}
176184
}
177185

@@ -180,16 +188,16 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
180188
}
181189

182190
fn mk_reexport_mod(cx: &mut TestCtxt, tests: Vec<ast::Ident>,
183-
tested_submods: Vec<ast::Ident>) -> Gc<ast::Item> {
191+
tested_submods: Vec<(ast::Ident, ast::Ident)>) -> (Gc<ast::Item>, ast::Ident) {
184192
let mut view_items = Vec::new();
185193
let super_ = token::str_to_ident("super");
186194

187195
view_items.extend(tests.move_iter().map(|r| {
188196
cx.ext_cx.view_use_simple(DUMMY_SP, ast::Public,
189197
cx.ext_cx.path(DUMMY_SP, vec![super_, r]))
190198
}));
191-
view_items.extend(tested_submods.move_iter().map(|r| {
192-
let path = cx.ext_cx.path(DUMMY_SP, vec![super_, r, cx.reexport_mod_ident]);
199+
view_items.extend(tested_submods.move_iter().map(|(r, sym)| {
200+
let path = cx.ext_cx.path(DUMMY_SP, vec![super_, r, sym]);
193201
cx.ext_cx.view_use_simple_(DUMMY_SP, ast::Public, r, path)
194202
}));
195203

@@ -198,14 +206,18 @@ fn mk_reexport_mod(cx: &mut TestCtxt, tests: Vec<ast::Ident>,
198206
view_items: view_items,
199207
items: Vec::new(),
200208
};
201-
box(GC) ast::Item {
202-
ident: cx.reexport_mod_ident.clone(),
209+
210+
let sym = token::gensym_ident("__test_reexports");
211+
let it = box(GC) ast::Item {
212+
ident: sym.clone(),
203213
attrs: Vec::new(),
204214
id: ast::DUMMY_NODE_ID,
205215
node: ast::ItemMod(reexport_mod),
206216
vis: ast::Public,
207217
span: DUMMY_SP,
208-
}
218+
};
219+
220+
(it, sym)
209221
}
210222

211223
fn generate_test_harness(sess: &Session,
@@ -220,10 +232,10 @@ fn generate_test_harness(sess: &Session,
220232
}),
221233
path: Vec::new(),
222234
testfns: Vec::new(),
223-
reexport_mod_ident: token::gensym_ident("__test_reexports"),
224235
reexport_test_harness_main: reexport_test_harness_main,
225236
is_test_crate: is_test_crate(&krate),
226237
config: krate.config.clone(),
238+
toplevel_reexport: None,
227239
};
228240

229241
cx.ext_cx.bt_push(ExpnInfo {
@@ -530,7 +542,14 @@ fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> Gc<ast::Expr> {
530542
field("should_fail", fail_expr)]);
531543

532544

533-
let mut visible_path = vec![cx.reexport_mod_ident.clone()];
545+
let mut visible_path = match cx.toplevel_reexport {
546+
Some(id) => vec![id],
547+
None => {
548+
cx.sess.bug(
549+
"expected to find top-level re-export name, but found None"
550+
);
551+
}
552+
};
534553
visible_path.extend(path.move_iter());
535554

536555
let fn_expr = ecx.expr_path(ecx.path_global(span, visible_path));
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
// This verifies that the test generation doesn't crash when we have
14+
// no tests - for more information, see PR #16892.

src/test/run-pass/issue-16597.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
// ignore-pretty turns out the pretty-printer doesn't handle gensym'd things...
13+
14+
#![feature(globs)]
15+
16+
mod test {
17+
use super::*;
18+
19+
#[test]
20+
fn test(){}
21+
}

0 commit comments

Comments
 (0)