Skip to content

Commit 3d3a663

Browse files
committed
auto merge of #10870 : ktt3ja/rust/issue-10865, r=alexcrichton
Fix #10865 and #10939.
2 parents f73c9c9 + a67b886 commit 3d3a663

File tree

7 files changed

+97
-48
lines changed

7 files changed

+97
-48
lines changed

src/etc/combine-tests.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,15 @@ def scrub(b):
4141

4242
c = open("tmp/run_pass_stage2.rc", "w")
4343
i = 0
44-
c.write("// AUTO-GENERATED FILE: DO NOT EDIT\n")
45-
c.write("#[pkgid=\"run_pass_stage2#0.1\"];\n")
46-
c.write("#[link(name=\"run_pass_stage2\", vers=\"0.1\")];\n")
47-
c.write("#[feature(globs, macro_rules, struct_variant, managed_boxes)];\n")
48-
c.write("#[allow(attribute_usage)];\n")
44+
c.write(
45+
"""
46+
// AUTO-GENERATED FILE: DO NOT EDIT
47+
#[pkgid=\"run_pass_stage2#0.1\"];
48+
#[link(name=\"run_pass_stage2\", vers=\"0.1\")];
49+
#[feature(globs, macro_rules, struct_variant, managed_boxes)];
50+
#[allow(warnings)];
51+
"""
52+
)
4953
for t in stage2_tests:
5054
p = os.path.join(run_pass, t)
5155
p = p.replace("\\", "\\\\")
@@ -56,15 +60,19 @@ def scrub(b):
5660

5761

5862
d = open("tmp/run_pass_stage2_driver.rs", "w")
59-
d.write("// AUTO-GENERATED FILE: DO NOT EDIT\n")
60-
d.write("#[feature(globs, managed_boxes)];\n")
61-
d.write("extern mod extra;\n")
62-
d.write("extern mod run_pass_stage2;\n")
63-
d.write("use run_pass_stage2::*;\n")
64-
d.write("use std::io;\n")
65-
d.write("use std::io::Writer;\n")
66-
d.write("fn main() {\n")
67-
d.write(" let mut out = io::stdout();\n")
63+
d.write(
64+
"""
65+
// AUTO-GENERATED FILE: DO NOT EDIT
66+
#[feature(globs, managed_boxes)];
67+
extern mod extra;
68+
extern mod run_pass_stage2;
69+
use run_pass_stage2::*;
70+
use std::io;
71+
use std::io::Writer;
72+
fn main() {
73+
let mut out = io::stdout();
74+
"""
75+
)
6876
i = 0
6977
for t in stage2_tests:
7078
p = os.path.join("test", "run-pass", t)

src/librustc/middle/dead.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ fn should_explore(tcx: ty::ctxt, def_id: ast::DefId) -> bool {
3737
match tcx.items.find(&def_id.node) {
3838
Some(&ast_map::node_item(..))
3939
| Some(&ast_map::node_method(..))
40+
| Some(&ast_map::node_foreign_item(..))
4041
| Some(&ast_map::node_trait_method(..)) => true,
4142
_ => false
4243
}
@@ -61,11 +62,10 @@ impl MarkSymbolVisitor {
6162
}
6263
}
6364

64-
fn lookup_and_handle_definition(&mut self, id: &ast::NodeId,
65-
span: codemap::Span) {
65+
fn lookup_and_handle_definition(&mut self, id: &ast::NodeId) {
6666
let def = match self.tcx.def_map.find(id) {
6767
Some(&def) => def,
68-
None => self.tcx.sess.span_bug(span, "def ID not in def map?!"),
68+
None => return
6969
};
7070
let def_id = match def {
7171
ast::DefVariant(enum_id, _, _) => Some(enum_id),
@@ -107,8 +107,7 @@ impl MarkSymbolVisitor {
107107
match item.node {
108108
ast::item_fn(..)
109109
| ast::item_ty(..)
110-
| ast::item_static(..)
111-
| ast::item_foreign_mod(_) => {
110+
| ast::item_static(..) => {
112111
visit::walk_item(self, item, ());
113112
}
114113
_ => ()
@@ -120,6 +119,9 @@ impl MarkSymbolVisitor {
120119
ast_map::node_method(method, _, _) => {
121120
visit::walk_block(self, method.body, ());
122121
}
122+
ast_map::node_foreign_item(foreign_item, _, _, _) => {
123+
visit::walk_foreign_item(self, foreign_item, ());
124+
}
123125
_ => ()
124126
}
125127
}
@@ -129,9 +131,6 @@ impl Visitor<()> for MarkSymbolVisitor {
129131

130132
fn visit_expr(&mut self, expr: @ast::Expr, _: ()) {
131133
match expr.node {
132-
ast::ExprPath(_) | ast::ExprStruct(..) => {
133-
self.lookup_and_handle_definition(&expr.id, expr.span);
134-
}
135134
ast::ExprMethodCall(..) => {
136135
match self.method_map.find(&expr.id) {
137136
Some(&typeck::method_map_entry {
@@ -160,12 +159,16 @@ impl Visitor<()> for MarkSymbolVisitor {
160159
fn visit_ty(&mut self, typ: &ast::Ty, _: ()) {
161160
match typ.node {
162161
ast::ty_path(_, _, ref id) => {
163-
self.lookup_and_handle_definition(id, typ.span);
162+
self.lookup_and_handle_definition(id);
164163
}
165164
_ => visit::walk_ty(self, typ, ()),
166165
}
167166
}
168167

168+
fn visit_path(&mut self, _: &ast::Path, id: ast::NodeId, _: ()) {
169+
self.lookup_and_handle_definition(&id);
170+
}
171+
169172
fn visit_item(&mut self, _item: @ast::item, _: ()) {
170173
// Do not recurse into items. These items will be added to the
171174
// worklist and recursed into manually if necessary.
@@ -299,19 +302,31 @@ impl DeadVisitor {
299302
}
300303
false
301304
}
305+
306+
fn warn_dead_code(&mut self, id: ast::NodeId,
307+
span: codemap::Span, ident: &ast::Ident) {
308+
self.tcx.sess.add_lint(dead_code, id, span,
309+
format!("code is never used: `{}`",
310+
token::ident_to_str(ident)));
311+
}
302312
}
303313

304314
impl Visitor<()> for DeadVisitor {
305315
fn visit_item(&mut self, item: @ast::item, _: ()) {
306316
let ctor_id = get_struct_ctor_id(item);
307317
if !self.symbol_is_live(item.id, ctor_id) && should_warn(item) {
308-
self.tcx.sess.add_lint(dead_code, item.id, item.span,
309-
format!("code is never used: `{}`",
310-
token::ident_to_str(&item.ident)));
318+
self.warn_dead_code(item.id, item.span, &item.ident);
311319
}
312320
visit::walk_item(self, item, ());
313321
}
314322

323+
fn visit_foreign_item(&mut self, fi: @ast::foreign_item, _: ()) {
324+
if !self.symbol_is_live(fi.id, None) {
325+
self.warn_dead_code(fi.id, fi.span, &fi.ident);
326+
}
327+
visit::walk_foreign_item(self, fi, ());
328+
}
329+
315330
fn visit_fn(&mut self, fk: &visit::fn_kind,
316331
_: &ast::fn_decl, block: ast::P<ast::Block>,
317332
span: codemap::Span, id: ast::NodeId, _: ()) {
@@ -320,10 +335,7 @@ impl Visitor<()> for DeadVisitor {
320335
visit::fk_method(..) => {
321336
let ident = visit::name_of_fn(fk);
322337
if !self.symbol_is_live(id, None) {
323-
self.tcx.sess
324-
.add_lint(dead_code, id, span,
325-
format!("code is never used: `{}`",
326-
token::ident_to_str(&ident)));
338+
self.warn_dead_code(id, span, &ident);
327339
}
328340
}
329341
_ => ()

src/librustuv/uvll.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@
2929

3030
#[allow(non_camel_case_types)]; // C types
3131

32-
use std::libc::{size_t, c_int, c_uint, c_void, c_char, uintptr_t, c_double};
32+
use std::libc::{size_t, c_int, c_uint, c_void, c_char, c_double};
3333
use std::libc::ssize_t;
3434
use std::libc::{malloc, free};
3535
use std::libc;
3636
use std::vec;
3737

38+
#[cfg(test)]
39+
use std::libc::uintptr_t;
40+
3841
pub use self::errors::*;
3942

4043
pub static OK: c_int = 0;
@@ -541,7 +544,9 @@ extern {
541544
pub fn rust_is_ipv4_sockaddr(addr: *sockaddr) -> c_int;
542545
pub fn rust_is_ipv6_sockaddr(addr: *sockaddr) -> c_int;
543546

547+
#[cfg(test)]
544548
fn rust_uv_handle_type_max() -> uintptr_t;
549+
#[cfg(test)]
545550
fn rust_uv_req_type_max() -> uintptr_t;
546551
fn rust_uv_get_udp_handle_from_send_req(req: *uv_udp_send_t) -> *uv_udp_t;
547552

src/libstd/num/cmath.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#[allow(missing_doc)];
1212
#[allow(non_uppercase_statics)];
13+
#[allow(dead_code)];
1314

1415
// function names are almost identical to C's libmath, a few have been
1516
// renamed, grep for "rename:"

src/test/compile-fail/lint-dead-code-1.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,7 @@ pub static pub_static: int = 0;
2626
static priv_static: int = 0; //~ ERROR: code is never used
2727
static used_static: int = 0;
2828
pub static used_static2: int = used_static;
29-
30-
pub fn pub_fn() {
31-
used_fn();
32-
let used_struct1 = UsedStruct1 { x: 1 };
33-
let used_struct2 = UsedStruct2(1);
34-
let used_struct3 = UsedStruct3;
35-
let e = foo3;
36-
SemiUsedStruct::la_la_la();
37-
38-
}
39-
fn priv_fn() { //~ ERROR: code is never used
40-
let unused_struct = PrivStruct;
41-
}
42-
fn used_fn() {}
29+
static USED_STATIC: int = 0;
4330

4431
pub type typ = ~UsedStruct4;
4532
pub struct PubStruct();
@@ -59,6 +46,25 @@ pub enum pub_enum { foo1, bar1 }
5946
enum priv_enum { foo2, bar2 } //~ ERROR: code is never used
6047
enum used_enum { foo3, bar3 }
6148

49+
pub fn pub_fn() {
50+
used_fn();
51+
let used_struct1 = UsedStruct1 { x: 1 };
52+
let used_struct2 = UsedStruct2(1);
53+
let used_struct3 = UsedStruct3;
54+
let e = foo3;
55+
SemiUsedStruct::la_la_la();
56+
57+
let i = 1;
58+
match i {
59+
USED_STATIC => (),
60+
_ => ()
61+
}
62+
}
63+
fn priv_fn() { //~ ERROR: code is never used
64+
let unused_struct = PrivStruct;
65+
}
66+
fn used_fn() {}
67+
6268
fn foo() { //~ ERROR: code is never used
6369
bar();
6470
let unused_enum = foo2;

src/test/compile-fail/lint-dead-code-3.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,27 @@ fn bar2() {
4040
pub fn pub_fn() {
4141
let foo2_struct = Foo2;
4242
foo2_struct.foo2();
43+
44+
blah::baz();
4345
}
4446

45-
// not warned because it's used in the parameter of `free` below
46-
enum c_void {}
47+
mod blah {
48+
use std::libc::size_t;
49+
// not warned because it's used in the parameter of `free` and return of
50+
// `malloc` below, which are also used.
51+
enum c_void {}
52+
53+
extern {
54+
fn free(p: *c_void);
55+
fn malloc(size: size_t) -> *c_void;
56+
}
57+
58+
pub fn baz() {
59+
unsafe { free(malloc(4)); }
60+
}
61+
}
4762

63+
enum c_void {} //~ ERROR: code is never used
4864
extern {
49-
fn free(p: *c_void);
65+
fn free(p: *c_void); //~ ERROR: code is never used
5066
}

src/test/compile-fail/warn-foreign-int-types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
#[forbid(ctypes)];
12+
#[allow(dead_code)];
1213

1314
mod xx {
1415
extern {

0 commit comments

Comments
 (0)