Skip to content

Commit a48c29d

Browse files
committed
Auto merge of #27992 - wthrowe:dead-main-2, r=alexcrichton
* Suppresses warnings that main is unused when testing (#12327) * Makes `--test` work with explicit `#[start]` (#11766) * Fixes some cases where the normal main would not be disabled by `--test`, resulting in compilation failures.
2 parents 685332c + 8320a3a commit a48c29d

File tree

9 files changed

+224
-67
lines changed

9 files changed

+224
-67
lines changed

src/librustc/middle/entry.rs

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,19 @@
1111

1212
use ast_map;
1313
use session::{config, Session};
14-
use syntax::ast::{Name, NodeId, Item, ItemFn};
14+
use syntax;
15+
use syntax::ast::{NodeId, Item};
1516
use syntax::attr;
1617
use syntax::codemap::Span;
17-
use syntax::parse::token;
18+
use syntax::entry::EntryPointType;
1819
use syntax::visit;
1920
use syntax::visit::Visitor;
2021

21-
struct EntryContext<'a, 'ast: 'a> {
22+
struct EntryContext<'a> {
2223
session: &'a Session,
2324

24-
ast_map: &'a ast_map::Map<'ast>,
25-
26-
// The interned Name for "main".
27-
main_name: Name,
25+
// The current depth in the ast
26+
depth: usize,
2827

2928
// The top-level function called 'main'
3029
main_fn: Option<(NodeId, Span)>,
@@ -40,9 +39,11 @@ struct EntryContext<'a, 'ast: 'a> {
4039
non_main_fns: Vec<(NodeId, Span)> ,
4140
}
4241

43-
impl<'a, 'ast, 'v> Visitor<'v> for EntryContext<'a, 'ast> {
42+
impl<'a, 'v> Visitor<'v> for EntryContext<'a> {
4443
fn visit_item(&mut self, item: &Item) {
44+
self.depth += 1;
4545
find_item(item, self);
46+
self.depth -= 1;
4647
}
4748
}
4849

@@ -63,8 +64,7 @@ pub fn find_entry_point(session: &Session, ast_map: &ast_map::Map) {
6364

6465
let mut ctxt = EntryContext {
6566
session: session,
66-
main_name: token::intern("main"),
67-
ast_map: ast_map,
67+
depth: 0,
6868
main_fn: None,
6969
attr_main_fn: None,
7070
start_fn: None,
@@ -77,44 +77,35 @@ pub fn find_entry_point(session: &Session, ast_map: &ast_map::Map) {
7777
}
7878

7979
fn find_item(item: &Item, ctxt: &mut EntryContext) {
80-
match item.node {
81-
ItemFn(..) => {
82-
if item.ident.name == ctxt.main_name {
83-
ctxt.ast_map.with_path(item.id, |path| {
84-
if path.count() == 1 {
85-
// This is a top-level function so can be 'main'
86-
if ctxt.main_fn.is_none() {
87-
ctxt.main_fn = Some((item.id, item.span));
88-
} else {
89-
span_err!(ctxt.session, item.span, E0136,
90-
"multiple 'main' functions");
91-
}
92-
} else {
93-
// This isn't main
94-
ctxt.non_main_fns.push((item.id, item.span));
95-
}
96-
});
80+
match syntax::entry::entry_point_type(item, ctxt.depth) {
81+
EntryPointType::MainNamed => {
82+
if ctxt.main_fn.is_none() {
83+
ctxt.main_fn = Some((item.id, item.span));
84+
} else {
85+
span_err!(ctxt.session, item.span, E0136,
86+
"multiple 'main' functions");
9787
}
98-
99-
if attr::contains_name(&item.attrs, "main") {
100-
if ctxt.attr_main_fn.is_none() {
101-
ctxt.attr_main_fn = Some((item.id, item.span));
102-
} else {
103-
span_err!(ctxt.session, item.span, E0137,
104-
"multiple functions with a #[main] attribute");
105-
}
88+
},
89+
EntryPointType::OtherMain => {
90+
ctxt.non_main_fns.push((item.id, item.span));
91+
},
92+
EntryPointType::MainAttr => {
93+
if ctxt.attr_main_fn.is_none() {
94+
ctxt.attr_main_fn = Some((item.id, item.span));
95+
} else {
96+
span_err!(ctxt.session, item.span, E0137,
97+
"multiple functions with a #[main] attribute");
10698
}
107-
108-
if attr::contains_name(&item.attrs, "start") {
109-
if ctxt.start_fn.is_none() {
110-
ctxt.start_fn = Some((item.id, item.span));
111-
} else {
112-
span_err!(ctxt.session, item.span, E0138,
113-
"multiple 'start' functions");
114-
}
99+
},
100+
EntryPointType::Start => {
101+
if ctxt.start_fn.is_none() {
102+
ctxt.start_fn = Some((item.id, item.span));
103+
} else {
104+
span_err!(ctxt.session, item.span, E0138,
105+
"multiple 'start' functions");
115106
}
116-
}
117-
_ => ()
107+
},
108+
EntryPointType::None => ()
118109
}
119110

120111
visit::walk_item(ctxt, item);

src/libsyntax/entry.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2012-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+
use attr;
12+
use ast::{Item, ItemFn};
13+
14+
pub enum EntryPointType {
15+
None,
16+
MainNamed,
17+
MainAttr,
18+
Start,
19+
OtherMain, // Not an entry point, but some other function named main
20+
}
21+
22+
pub fn entry_point_type(item: &Item, depth: usize) -> EntryPointType {
23+
match item.node {
24+
ItemFn(..) => {
25+
if attr::contains_name(&item.attrs, "start") {
26+
EntryPointType::Start
27+
} else if attr::contains_name(&item.attrs, "main") {
28+
EntryPointType::MainAttr
29+
} else if item.ident.name == "main" {
30+
if depth == 1 {
31+
// This is a top-level function so can be 'main'
32+
EntryPointType::MainNamed
33+
} else {
34+
EntryPointType::OtherMain
35+
}
36+
} else {
37+
EntryPointType::None
38+
}
39+
}
40+
_ => EntryPointType::None,
41+
}
42+
}

src/libsyntax/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ pub mod attr;
9090
pub mod codemap;
9191
pub mod config;
9292
pub mod diagnostic;
93+
pub mod entry;
9394
pub mod feature_gate;
9495
pub mod fold;
9596
pub mod owned_slice;

src/libsyntax/test.rs

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![allow(unused_imports)]
1515
use self::HasTestSignature::*;
1616

17+
use std::iter;
1718
use std::slice;
1819
use std::mem;
1920
use std::vec;
@@ -24,6 +25,7 @@ use codemap::{DUMMY_SP, Span, ExpnInfo, NameAndSpan, MacroAttribute};
2425
use codemap;
2526
use diagnostic;
2627
use config;
28+
use entry::{self, EntryPointType};
2729
use ext::base::ExtCtxt;
2830
use ext::build::AstBuilder;
2931
use ext::expand::ExpansionConfig;
@@ -173,28 +175,6 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
173175
let tests = mem::replace(&mut self.tests, tests);
174176
let tested_submods = mem::replace(&mut self.tested_submods, tested_submods);
175177

176-
// Remove any #[main] from the AST so it doesn't clash with
177-
// the one we're going to add. Only if compiling an executable.
178-
179-
mod_folded.items = mem::replace(&mut mod_folded.items, vec![]).move_map(|item| {
180-
item.map(|ast::Item {id, ident, attrs, node, vis, span}| {
181-
ast::Item {
182-
id: id,
183-
ident: ident,
184-
attrs: attrs.into_iter().filter_map(|attr| {
185-
if !attr.check_name("main") {
186-
Some(attr)
187-
} else {
188-
None
189-
}
190-
}).collect(),
191-
node: node,
192-
vis: vis,
193-
span: span
194-
}
195-
})
196-
});
197-
198178
if !tests.is_empty() || !tested_submods.is_empty() {
199179
let (it, sym) = mk_reexport_mod(&mut self.cx, tests, tested_submods);
200180
mod_folded.items.push(it);
@@ -211,6 +191,55 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
211191
}
212192
}
213193

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] or #[start] from the AST so it doesn't
206+
// clash with 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()
225+
.filter(|attr| {
226+
!attr.check_name("main") && !attr.check_name("start")
227+
})
228+
.chain(iter::once(allow_dead_code))
229+
.collect(),
230+
node: node,
231+
vis: vis,
232+
span: span
233+
}
234+
}),
235+
EntryPointType::None |
236+
EntryPointType::OtherMain => folded,
237+
};
238+
239+
SmallVector::one(folded)
240+
}
241+
}
242+
214243
fn mk_reexport_mod(cx: &mut TestCtxt, tests: Vec<ast::Ident>,
215244
tested_submods: Vec<(ast::Ident, ast::Ident)>) -> (P<ast::Item>, ast::Ident) {
216245
let super_ = token::str_to_ident("super");
@@ -246,6 +275,10 @@ fn generate_test_harness(sess: &ParseSess,
246275
krate: ast::Crate,
247276
cfg: &ast::CrateConfig,
248277
sd: &diagnostic::SpanHandler) -> ast::Crate {
278+
// Remove the entry points
279+
let mut cleaner = EntryPointCleaner { depth: 0 };
280+
let krate = cleaner.fold_crate(krate);
281+
249282
let mut feature_gated_cfgs = vec![];
250283
let mut cx: TestCtxt = TestCtxt {
251284
sess: sess,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
#![deny(dead_code)]
14+
15+
fn dead() {} //~ error: function is never used: `dead`
16+
17+
fn main() {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
#![deny(dead_code)]
16+
17+
#[main]
18+
fn foo() { panic!(); }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
#![deny(dead_code)]
14+
15+
fn main() { panic!(); }
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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(start)]
14+
15+
#[start]
16+
fn start(_: isize, _: *const *const u8) -> isize { panic!(); }

0 commit comments

Comments
 (0)