Skip to content

Commit 0112e7b

Browse files
committed
Move main removal to its own pass in --test mode
This handles the case where the #[main] function is buried deeper in the ast than we search for #[test] functions. I'm not sure why one would want to do that, but since it works in standard compilation it should also work for tests.
1 parent 15d6837 commit 0112e7b

File tree

2 files changed

+80
-39
lines changed

2 files changed

+80
-39
lines changed

src/libsyntax/test.rs

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -175,45 +175,6 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
175175
let tests = mem::replace(&mut self.tests, tests);
176176
let tested_submods = mem::replace(&mut self.tested_submods, tested_submods);
177177

178-
// Remove any #[main] from the AST so it doesn't clash with
179-
// the one we're going to add. Only if compiling an executable.
180-
181-
mod_folded.items = mem::replace(&mut mod_folded.items, vec![]).move_map(|item| {
182-
match entry::entry_point_type(&item, self.cx.path.len() + 1) {
183-
EntryPointType::MainNamed |
184-
EntryPointType::MainAttr |
185-
EntryPointType::Start =>
186-
item.map(|ast::Item {id, ident, attrs, node, vis, span}| {
187-
let allow_str = InternedString::new("allow");
188-
let dead_code_str = InternedString::new("dead_code");
189-
let allow_dead_code_item =
190-
attr::mk_list_item(allow_str,
191-
vec![attr::mk_word_item(dead_code_str)]);
192-
let allow_dead_code = attr::mk_attr_outer(attr::mk_attr_id(),
193-
allow_dead_code_item);
194-
195-
ast::Item {
196-
id: id,
197-
ident: ident,
198-
attrs: attrs.into_iter().filter_map(|attr| {
199-
if !attr.check_name("main") {
200-
Some(attr)
201-
} else {
202-
None
203-
}
204-
})
205-
.chain(iter::once(allow_dead_code))
206-
.collect(),
207-
node: node,
208-
vis: vis,
209-
span: span
210-
}
211-
}),
212-
EntryPointType::None |
213-
EntryPointType::OtherMain => item,
214-
}
215-
});
216-
217178
if !tests.is_empty() || !tested_submods.is_empty() {
218179
let (it, sym) = mk_reexport_mod(&mut self.cx, tests, tested_submods);
219180
mod_folded.items.push(it);
@@ -230,6 +191,58 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
230191
}
231192
}
232193

194+
struct EntryPointCleaner {
195+
// Current depth in the ast
196+
depth: usize,
197+
}
198+
199+
impl fold::Folder for EntryPointCleaner {
200+
fn fold_item(&mut self, i: P<ast::Item>) -> SmallVector<P<ast::Item>> {
201+
self.depth += 1;
202+
let folded = fold::noop_fold_item(i, self).expect_one("noop did something");
203+
self.depth -= 1;
204+
205+
// Remove any #[main] from the AST so it doesn't clash with
206+
// the one we're going to add, but mark it as
207+
// #[allow(dead_code)] to avoid printing warnings.
208+
let folded = match entry::entry_point_type(&*folded, self.depth) {
209+
EntryPointType::MainNamed |
210+
EntryPointType::MainAttr |
211+
EntryPointType::Start =>
212+
folded.map(|ast::Item {id, ident, attrs, node, vis, span}| {
213+
let allow_str = InternedString::new("allow");
214+
let dead_code_str = InternedString::new("dead_code");
215+
let allow_dead_code_item =
216+
attr::mk_list_item(allow_str,
217+
vec![attr::mk_word_item(dead_code_str)]);
218+
let allow_dead_code = attr::mk_attr_outer(attr::mk_attr_id(),
219+
allow_dead_code_item);
220+
221+
ast::Item {
222+
id: id,
223+
ident: ident,
224+
attrs: attrs.into_iter().filter_map(|attr| {
225+
if !attr.check_name("main") {
226+
Some(attr)
227+
} else {
228+
None
229+
}
230+
})
231+
.chain(iter::once(allow_dead_code))
232+
.collect(),
233+
node: node,
234+
vis: vis,
235+
span: span
236+
}
237+
}),
238+
EntryPointType::None |
239+
EntryPointType::OtherMain => folded,
240+
};
241+
242+
SmallVector::one(folded)
243+
}
244+
}
245+
233246
fn mk_reexport_mod(cx: &mut TestCtxt, tests: Vec<ast::Ident>,
234247
tested_submods: Vec<(ast::Ident, ast::Ident)>) -> (P<ast::Item>, ast::Ident) {
235248
let super_ = token::str_to_ident("super");
@@ -265,6 +278,10 @@ fn generate_test_harness(sess: &ParseSess,
265278
krate: ast::Crate,
266279
cfg: &ast::CrateConfig,
267280
sd: &diagnostic::SpanHandler) -> ast::Crate {
281+
// Remove the entry points
282+
let mut cleaner = EntryPointCleaner { depth: 0 };
283+
let krate = cleaner.fold_crate(krate);
284+
268285
let mut feature_gated_cfgs = vec![];
269286
let mut cx: TestCtxt = TestCtxt {
270287
sess: sess,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2015 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+
#![feature(main)]
14+
15+
#![allow(dead_code)]
16+
17+
mod a {
18+
fn b() {
19+
|| {
20+
#[main]
21+
fn c() { panic!(); }
22+
};
23+
}
24+
}

0 commit comments

Comments
 (0)