]> git.lizzy.rs Git - rust.git/commitdiff
Mark main-like functions allow(dead_code) in tests
authorWilliam Throwe <wtt6@cornell.edu>
Mon, 24 Aug 2015 01:46:10 +0000 (21:46 -0400)
committerWilliam Throwe <wtt6@cornell.edu>
Tue, 25 Aug 2015 00:28:24 +0000 (20:28 -0400)
Fixes #12327.

src/libsyntax/test.rs
src/test/compile-fail/test-warns-dead-code.rs [new file with mode: 0644]
src/test/run-pass/test-main-not-dead-attr.rs [new file with mode: 0644]
src/test/run-pass/test-main-not-dead.rs [new file with mode: 0644]

index 26fb287ce35d1bbfc0f878952d756f0568cd7012..3fbcbd728aabca03bc666d16302b5fa4f4d500e2 100644 (file)
@@ -14,6 +14,7 @@
 #![allow(unused_imports)]
 use self::HasTestSignature::*;
 
+use std::iter;
 use std::slice;
 use std::mem;
 use std::vec;
@@ -24,6 +25,7 @@
 use codemap;
 use diagnostic;
 use config;
+use entry::{self, EntryPointType};
 use ext::base::ExtCtxt;
 use ext::build::AstBuilder;
 use ext::expand::ExpansionConfig;
@@ -177,22 +179,39 @@ fn fold_mod(&mut self, m: ast::Mod) -> ast::Mod {
         // the one we're going to add. Only if compiling an executable.
 
         mod_folded.items = mem::replace(&mut mod_folded.items, vec![]).move_map(|item| {
-            item.map(|ast::Item {id, ident, attrs, node, vis, span}| {
-                ast::Item {
-                    id: id,
-                    ident: ident,
-                    attrs: attrs.into_iter().filter_map(|attr| {
-                        if !attr.check_name("main") {
-                            Some(attr)
-                        } else {
-                            None
+            match entry::entry_point_type(&item, self.cx.path.len() + 1) {
+                EntryPointType::MainNamed |
+                EntryPointType::MainAttr |
+                EntryPointType::Start =>
+                    item.map(|ast::Item {id, ident, attrs, node, vis, span}| {
+                        let allow_str = InternedString::new("allow");
+                        let dead_code_str = InternedString::new("dead_code");
+                        let allow_dead_code_item =
+                            attr::mk_list_item(allow_str,
+                                               vec![attr::mk_word_item(dead_code_str)]);
+                        let allow_dead_code = attr::mk_attr_outer(attr::mk_attr_id(),
+                                                                  allow_dead_code_item);
+
+                        ast::Item {
+                            id: id,
+                            ident: ident,
+                            attrs: attrs.into_iter().filter_map(|attr| {
+                                if !attr.check_name("main") {
+                                    Some(attr)
+                                } else {
+                                    None
+                                }
+                            })
+                                .chain(iter::once(allow_dead_code))
+                                .collect(),
+                            node: node,
+                            vis: vis,
+                            span: span
                         }
-                    }).collect(),
-                    node: node,
-                    vis: vis,
-                    span: span
-                }
-            })
+                    }),
+                EntryPointType::None |
+                EntryPointType::OtherMain => item,
+            }
         });
 
         if !tests.is_empty() || !tested_submods.is_empty() {
diff --git a/src/test/compile-fail/test-warns-dead-code.rs b/src/test/compile-fail/test-warns-dead-code.rs
new file mode 100644 (file)
index 0000000..0e25f1e
--- /dev/null
@@ -0,0 +1,17 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// compile-flags: --test
+
+#![deny(dead_code)]
+
+fn dead() {} //~ error: function is never used: `dead`
+
+fn main() {}
diff --git a/src/test/run-pass/test-main-not-dead-attr.rs b/src/test/run-pass/test-main-not-dead-attr.rs
new file mode 100644 (file)
index 0000000..295559b
--- /dev/null
@@ -0,0 +1,18 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// compile-flags: --test
+
+#![feature(main)]
+
+#![deny(dead_code)]
+
+#[main]
+fn foo() { panic!(); }
diff --git a/src/test/run-pass/test-main-not-dead.rs b/src/test/run-pass/test-main-not-dead.rs
new file mode 100644 (file)
index 0000000..7de3ca7
--- /dev/null
@@ -0,0 +1,15 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// compile-flags: --test
+
+#![deny(dead_code)]
+
+fn main() { panic!(); }