Skip to content

Commit 07aadc2

Browse files
committed
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.
1 parent f3d88c3 commit 07aadc2

File tree

6 files changed

+75
-12
lines changed

6 files changed

+75
-12
lines changed

src/libcore/macros.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,17 @@ macro_rules! fail(
3131
// because it's just a tiny wrapper. Small wins (156K to 149K in size)
3232
// were seen when forcing this to be inlined, and that number just goes
3333
// up with the number of calls to fail!()
34+
//
35+
// The leading _'s are to avoid dead code warnings if this is
36+
// used inside a dead function. Just `#[allow(dead_code)]` is
37+
// insufficient, since the user may have
38+
// `#[forbid(dead_code)]` and which cannot be overridden.
3439
#[inline(always)]
35-
fn run_fmt(fmt: &::std::fmt::Arguments) -> ! {
36-
static FILE_LINE: (&'static str, uint) = (file!(), line!());
37-
::core::failure::begin_unwind(fmt, &FILE_LINE)
40+
fn _run_fmt(fmt: &::std::fmt::Arguments) -> ! {
41+
static _FILE_LINE: (&'static str, uint) = (file!(), line!());
42+
::core::failure::begin_unwind(fmt, &_FILE_LINE)
3843
}
39-
format_args!(run_fmt, $fmt, $($arg)*)
44+
format_args!(_run_fmt, $fmt, $($arg)*)
4045
});
4146
)
4247

src/librustc/middle/dead.rs

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use syntax::ast_util::{local_def, is_local, PostExpansionMethod};
2626
use syntax::attr::AttrMetaMethods;
2727
use syntax::attr;
2828
use syntax::codemap;
29-
use syntax::parse::token;
3029
use syntax::visit::Visitor;
3130
use syntax::visit;
3231

src/libstd/macros.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ macro_rules! fail(
4343
});
4444
($msg:expr) => ({
4545
// static requires less code at runtime, more constant data
46-
static FILE_LINE: (&'static str, uint) = (file!(), line!());
47-
::std::rt::begin_unwind($msg, &FILE_LINE)
46+
static _FILE_LINE: (&'static str, uint) = (file!(), line!());
47+
::std::rt::begin_unwind($msg, &_FILE_LINE)
4848
});
4949
($fmt:expr, $($arg:tt)*) => ({
5050
// a closure can't have return type !, so we need a full
@@ -58,12 +58,17 @@ macro_rules! fail(
5858
// because it's just a tiny wrapper. Small wins (156K to 149K in size)
5959
// were seen when forcing this to be inlined, and that number just goes
6060
// up with the number of calls to fail!()
61+
//
62+
// The leading _'s are to avoid dead code warnings if this is
63+
// used inside a dead function. Just `#[allow(dead_code)]` is
64+
// insufficient, since the user may have
65+
// `#[forbid(dead_code)]` and which cannot be overridden.
6166
#[inline(always)]
62-
fn run_fmt(fmt: &::std::fmt::Arguments) -> ! {
63-
static FILE_LINE: (&'static str, uint) = (file!(), line!());
64-
::std::rt::begin_unwind_fmt(fmt, &FILE_LINE)
67+
fn _run_fmt(fmt: &::std::fmt::Arguments) -> ! {
68+
static _FILE_LINE: (&'static str, uint) = (file!(), line!());
69+
::std::rt::begin_unwind_fmt(fmt, &_FILE_LINE)
6570
}
66-
format_args!(run_fmt, $fmt, $($arg)*)
71+
format_args!(_run_fmt, $fmt, $($arg)*)
6772
});
6873
)
6974

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
#![feature(phase)]
12+
#![deny(dead_code)]
13+
#![allow(unreachable_code)]
14+
15+
#[phase(link, plugin)] extern crate core;
16+
17+
18+
fn foo() { //~ ERROR code is never used
19+
20+
// none of these should have any dead_code exposed to the user
21+
fail!();
22+
23+
fail!("foo");
24+
25+
fail!("bar {}", "baz")
26+
}
27+
28+
29+
fn main() {}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
#![deny(dead_code)]
12+
#![allow(unreachable_code)]
13+
14+
fn foo() { //~ ERROR code is never used
15+
16+
// none of these should have any dead_code exposed to the user
17+
fail!();
18+
19+
fail!("foo");
20+
21+
fail!("bar {}", "baz")
22+
}
23+
24+
25+
fn main() {}

src/test/run-pass/dead-code-leading-underscore.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ extern {
3535
fn _abort() -> !;
3636
}
3737

38-
fn main() {}
38+
pub fn main() {}

0 commit comments

Comments
 (0)