Skip to content

Commit 963e8a4

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 d06b8e1 commit 963e8a4

File tree

5 files changed

+74
-11
lines changed

5 files changed

+74
-11
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-
let (file, line) = FILE_LINE;
46+
static _FILE_LINE: (&'static str, uint) = (file!(), line!());
47+
let (file, line) = _FILE_LINE;
4848
::std::rt::begin_unwind($msg, file, line)
4949
});
5050
($fmt:expr, $($arg:tt)*) => ({
@@ -59,12 +59,17 @@ macro_rules! fail(
5959
// because it's just a tiny wrapper. Small wins (156K to 149K in size)
6060
// were seen when forcing this to be inlined, and that number just goes
6161
// up with the number of calls to fail!()
62+
//
63+
// The leading _'s are to avoid dead code warnings if this is
64+
// used inside a dead function. Just `#[allow(dead_code)]` is
65+
// insufficient, since the user may have
66+
// `#[forbid(dead_code)]` and which cannot be overridden.
6267
#[inline(always)]
63-
fn run_fmt(fmt: &::std::fmt::Arguments) -> ! {
64-
static FILE_LINE: (&'static str, uint) = (file!(), line!());
65-
::std::rt::begin_unwind_fmt(fmt, &FILE_LINE)
68+
fn _run_fmt(fmt: &::std::fmt::Arguments) -> ! {
69+
static _FILE_LINE: (&'static str, uint) = (file!(), line!());
70+
::std::rt::begin_unwind_fmt(fmt, &_FILE_LINE)
6671
}
67-
format_args!(run_fmt, $fmt, $($arg)*)
72+
format_args!(_run_fmt, $fmt, $($arg)*)
6873
});
6974
)
7075

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, syntax)] 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() {}

0 commit comments

Comments
 (0)