Skip to content

Commit a38ff54

Browse files
committed
Fix dead code check for associated const impls.
1 parent 43e07bf commit a38ff54

File tree

2 files changed

+54
-22
lines changed

2 files changed

+54
-22
lines changed

src/librustc/middle/dead.rs

+32-22
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ impl<'v> Visitor<'v> for LifeSeeder {
346346
ast::ItemTrait(_, _, _, ref trait_items) => {
347347
for trait_item in trait_items {
348348
match trait_item.node {
349+
ast::ConstTraitItem(_, Some(_)) |
349350
ast::MethodTraitItem(_, Some(_)) => {
350351
if has_allow_dead_code_or_lang_attr(&trait_item.attrs) {
351352
self.worklist.push(trait_item.id);
@@ -358,7 +359,7 @@ impl<'v> Visitor<'v> for LifeSeeder {
358359
ast::ItemImpl(_, _, _, ref opt_trait, _, ref impl_items) => {
359360
for impl_item in impl_items {
360361
match impl_item.node {
361-
ast::ConstImplItem(..) => {}
362+
ast::ConstImplItem(..) |
362363
ast::MethodImplItem(..) => {
363364
if opt_trait.is_some() ||
364365
has_allow_dead_code_or_lang_attr(&impl_item.attrs) {
@@ -400,7 +401,7 @@ fn create_and_seed_worklist(tcx: &ty::ctxt,
400401
None => ()
401402
}
402403

403-
// Seed implemented trait methods
404+
// Seed implemented trait items
404405
let mut life_seeder = LifeSeeder {
405406
worklist: worklist
406407
};
@@ -481,7 +482,7 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
481482
|ctor| self.live_symbols.contains(&ctor)) {
482483
return true;
483484
}
484-
// If it's a type whose methods are live, then it's live, too.
485+
// If it's a type whose items are live, then it's live, too.
485486
// This is done to handle the case where, for example, the static
486487
// method of a private type is used, but the type itself is never
487488
// called directly.
@@ -546,21 +547,6 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
546547
visit::walk_foreign_item(self, fi);
547548
}
548549

549-
fn visit_fn(&mut self, fk: visit::FnKind<'v>,
550-
_: &'v ast::FnDecl, block: &'v ast::Block,
551-
span: codemap::Span, id: ast::NodeId) {
552-
// Have to warn method here because methods are not ast::Item
553-
match fk {
554-
visit::FkMethod(name, _) => {
555-
if !self.symbol_is_live(id, None) {
556-
self.warn_dead_code(id, span, name, "method");
557-
}
558-
}
559-
_ => ()
560-
}
561-
visit::walk_block(self, block);
562-
}
563-
564550
fn visit_struct_field(&mut self, field: &ast::StructField) {
565551
if self.should_warn_about_field(&field.node) {
566552
self.warn_dead_code(field.node.id, field.span,
@@ -570,13 +556,37 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
570556
visit::walk_struct_field(self, field);
571557
}
572558

573-
// Overwrite so that we don't warn the trait method itself.
574-
fn visit_trait_item(&mut self, trait_method: &ast::TraitItem) {
575-
match trait_method.node {
576-
ast::ConstTraitItem(_, _) => {}
559+
fn visit_impl_item(&mut self, impl_item: &ast::ImplItem) {
560+
match impl_item.node {
561+
ast::ConstImplItem(_, ref expr) => {
562+
if !self.symbol_is_live(impl_item.id, None) {
563+
self.warn_dead_code(impl_item.id, impl_item.span,
564+
impl_item.ident, "associated const");
565+
}
566+
visit::walk_expr(self, expr)
567+
}
568+
ast::MethodImplItem(_, ref body) => {
569+
if !self.symbol_is_live(impl_item.id, None) {
570+
self.warn_dead_code(impl_item.id, impl_item.span,
571+
impl_item.ident, "method");
572+
}
573+
visit::walk_block(self, body)
574+
}
575+
ast::TypeImplItem(..) |
576+
ast::MacImplItem(..) => {}
577+
}
578+
}
579+
580+
// Overwrite so that we don't warn the trait item itself.
581+
fn visit_trait_item(&mut self, trait_item: &ast::TraitItem) {
582+
match trait_item.node {
583+
ast::ConstTraitItem(_, Some(ref expr)) => {
584+
visit::walk_expr(self, expr)
585+
}
577586
ast::MethodTraitItem(_, Some(ref body)) => {
578587
visit::walk_block(self, body)
579588
}
589+
ast::ConstTraitItem(_, None) |
580590
ast::MethodTraitItem(_, None) |
581591
ast::TypeTraitItem(..) => {}
582592
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2015 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+
13+
struct MyFoo;
14+
15+
impl MyFoo {
16+
const BAR: u32 = 1;
17+
//~^ ERROR associated const is never used: `BAR`
18+
}
19+
20+
fn main() {
21+
let _: MyFoo = MyFoo;
22+
}

0 commit comments

Comments
 (0)