about summary refs log tree commit diff
diff options
context:
space:
mode:
authorHuon Wilson <dbau.pp+github@gmail.com>2014-08-02 22:41:40 +1000
committerHuon Wilson <dbau.pp+github@gmail.com>2014-08-11 18:26:31 +1000
commit07aadc2e8b1923b28595393922a816e46d3903f4 (patch)
tree33dd57a9cf350d949971b1957c7a6eace5166f33
parentf3d88c320d4b08e5534f2193462e6a20e27b1d85 (diff)
downloadrust-07aadc2e8b1923b28595393922a816e46d3903f4.tar.gz
rust-07aadc2e8b1923b28595393922a816e46d3903f4.zip
core/std: squash dead_code warnings from fail! invocations.
The fail macro defines some function/static items internally, which got
a dead_code warning when `fail!()` is used inside a dead function. This
is ugly and unnecessarily reveals implementation details, so the
warnings can be squashed.

Fixes #16192.
-rw-r--r--src/libcore/macros.rs13
-rw-r--r--src/librustc/middle/dead.rs1
-rw-r--r--src/libstd/macros.rs17
-rw-r--r--src/test/compile-fail/fail-no-dead-code-core.rs29
-rw-r--r--src/test/compile-fail/fail-no-dead-code.rs25
-rw-r--r--src/test/run-pass/dead-code-leading-underscore.rs2
6 files changed, 75 insertions, 12 deletions
diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs
index 5373008612b..c80d1ed3451 100644
--- a/src/libcore/macros.rs
+++ b/src/libcore/macros.rs
@@ -31,12 +31,17 @@ macro_rules! fail(
         // because it's just a tiny wrapper. Small wins (156K to 149K in size)
         // were seen when forcing this to be inlined, and that number just goes
         // up with the number of calls to fail!()
+        //
+        // The leading _'s are to avoid dead code warnings if this is
+        // used inside a dead function. Just `#[allow(dead_code)]` is
+        // insufficient, since the user may have
+        // `#[forbid(dead_code)]` and which cannot be overridden.
         #[inline(always)]
-        fn run_fmt(fmt: &::std::fmt::Arguments) -> ! {
-            static FILE_LINE: (&'static str, uint) = (file!(), line!());
-            ::core::failure::begin_unwind(fmt, &FILE_LINE)
+        fn _run_fmt(fmt: &::std::fmt::Arguments) -> ! {
+            static _FILE_LINE: (&'static str, uint) = (file!(), line!());
+            ::core::failure::begin_unwind(fmt, &_FILE_LINE)
         }
-        format_args!(run_fmt, $fmt, $($arg)*)
+        format_args!(_run_fmt, $fmt, $($arg)*)
     });
 )
 
diff --git a/src/librustc/middle/dead.rs b/src/librustc/middle/dead.rs
index 23ba26ad3bf..86fe6dc0ddc 100644
--- a/src/librustc/middle/dead.rs
+++ b/src/librustc/middle/dead.rs
@@ -26,7 +26,6 @@ use syntax::ast_util::{local_def, is_local, PostExpansionMethod};
 use syntax::attr::AttrMetaMethods;
 use syntax::attr;
 use syntax::codemap;
-use syntax::parse::token;
 use syntax::visit::Visitor;
 use syntax::visit;
 
diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs
index f2d7fb0cea6..85e614ab47e 100644
--- a/src/libstd/macros.rs
+++ b/src/libstd/macros.rs
@@ -43,8 +43,8 @@ macro_rules! fail(
     });
     ($msg:expr) => ({
         // static requires less code at runtime, more constant data
-        static FILE_LINE: (&'static str, uint) = (file!(), line!());
-        ::std::rt::begin_unwind($msg, &FILE_LINE)
+        static _FILE_LINE: (&'static str, uint) = (file!(), line!());
+        ::std::rt::begin_unwind($msg, &_FILE_LINE)
     });
     ($fmt:expr, $($arg:tt)*) => ({
         // a closure can't have return type !, so we need a full
@@ -58,12 +58,17 @@ macro_rules! fail(
         // because it's just a tiny wrapper. Small wins (156K to 149K in size)
         // were seen when forcing this to be inlined, and that number just goes
         // up with the number of calls to fail!()
+        //
+        // The leading _'s are to avoid dead code warnings if this is
+        // used inside a dead function. Just `#[allow(dead_code)]` is
+        // insufficient, since the user may have
+        // `#[forbid(dead_code)]` and which cannot be overridden.
         #[inline(always)]
-        fn run_fmt(fmt: &::std::fmt::Arguments) -> ! {
-            static FILE_LINE: (&'static str, uint) = (file!(), line!());
-            ::std::rt::begin_unwind_fmt(fmt, &FILE_LINE)
+        fn _run_fmt(fmt: &::std::fmt::Arguments) -> ! {
+            static _FILE_LINE: (&'static str, uint) = (file!(), line!());
+            ::std::rt::begin_unwind_fmt(fmt, &_FILE_LINE)
         }
-        format_args!(run_fmt, $fmt, $($arg)*)
+        format_args!(_run_fmt, $fmt, $($arg)*)
     });
 )
 
diff --git a/src/test/compile-fail/fail-no-dead-code-core.rs b/src/test/compile-fail/fail-no-dead-code-core.rs
new file mode 100644
index 00000000000..b2b04edc787
--- /dev/null
+++ b/src/test/compile-fail/fail-no-dead-code-core.rs
@@ -0,0 +1,29 @@
+// Copyright 2014 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.
+
+#![feature(phase)]
+#![deny(dead_code)]
+#![allow(unreachable_code)]
+
+#[phase(link, plugin)] extern crate core;
+
+
+fn foo() { //~ ERROR code is never used
+
+    // none of these should have any dead_code exposed to the user
+    fail!();
+
+    fail!("foo");
+
+    fail!("bar {}", "baz")
+}
+
+
+fn main() {}
diff --git a/src/test/compile-fail/fail-no-dead-code.rs b/src/test/compile-fail/fail-no-dead-code.rs
new file mode 100644
index 00000000000..da59722c3ff
--- /dev/null
+++ b/src/test/compile-fail/fail-no-dead-code.rs
@@ -0,0 +1,25 @@
+// Copyright 2014 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.
+
+#![deny(dead_code)]
+#![allow(unreachable_code)]
+
+fn foo() { //~ ERROR code is never used
+
+    // none of these should have any dead_code exposed to the user
+    fail!();
+
+    fail!("foo");
+
+    fail!("bar {}", "baz")
+}
+
+
+fn main() {}
diff --git a/src/test/run-pass/dead-code-leading-underscore.rs b/src/test/run-pass/dead-code-leading-underscore.rs
index b678b77095f..b588ea9cfd0 100644
--- a/src/test/run-pass/dead-code-leading-underscore.rs
+++ b/src/test/run-pass/dead-code-leading-underscore.rs
@@ -35,4 +35,4 @@ extern {
     fn _abort() -> !;
 }
 
-fn main() {}
+pub fn main() {}