Skip to content

Commit

Permalink
Mark provided methods in dead-code pass
Browse files Browse the repository at this point in the history
  • Loading branch information
ktt3ja committed Dec 31, 2013
1 parent d459e80 commit 576a851
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 23 deletions.
67 changes: 44 additions & 23 deletions src/librustc/middle/dead.rs
Expand Up @@ -62,6 +62,13 @@ impl MarkSymbolVisitor {
}
}

fn check_def_id(&mut self, def_id: ast::DefId) {
if should_explore(self.tcx, def_id) {
self.worklist.push(def_id.node);
}
self.live_symbols.insert(def_id.node);
}

fn lookup_and_handle_definition(&mut self, id: &ast::NodeId) {
let def_map = self.tcx.def_map.borrow();
let def = match def_map.get().find(id) {
Expand All @@ -74,13 +81,44 @@ impl MarkSymbolVisitor {
_ => Some(def_id_of_def(def)),
};
match def_id {
Some(def_id) => {
if should_explore(self.tcx, def_id) {
self.worklist.push(def_id.node);
Some(def_id) => self.check_def_id(def_id),
None => (),
}
}

fn lookup_and_handle_method(&mut self, id: &ast::NodeId,
span: codemap::Span) {
let method_map = self.method_map.borrow();
match method_map.get().find(id) {
Some(&typeck::method_map_entry { origin, .. }) => {
match origin {
typeck::method_static(def_id) => {
match ty::provided_source(self.tcx, def_id) {
Some(p_did) => self.check_def_id(p_did),
None => self.check_def_id(def_id)
}
}
typeck::method_param(typeck::method_param {
trait_id: trait_id,
method_num: index,
..
})
| typeck::method_object(typeck::method_object {
trait_id: trait_id,
method_num: index,
..
}) => {
let def_id = ty::trait_method(self.tcx,
trait_id, index).def_id;
self.check_def_id(def_id);
}
}
self.live_symbols.insert(def_id.node);
}
None => (),
None => {
self.tcx.sess.span_bug(span,
"method call expression not \
in method map?!")
}
}
}

Expand Down Expand Up @@ -135,24 +173,7 @@ impl Visitor<()> for MarkSymbolVisitor {
fn visit_expr(&mut self, expr: @ast::Expr, _: ()) {
match expr.node {
ast::ExprMethodCall(..) => {
let method_map = self.method_map.borrow();
match method_map.get().find(&expr.id) {
Some(&typeck::method_map_entry {
origin: typeck::method_static(def_id),
..
}) => {
if should_explore(self.tcx, def_id) {
self.worklist.push(def_id.node);
}
self.live_symbols.insert(def_id.node);
}
Some(_) => (),
None => {
self.tcx.sess.span_bug(expr.span,
"method call expression not \
in method map?!")
}
}
self.lookup_and_handle_method(&expr.id, expr.span);
}
_ => ()
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/compile-fail/lint-dead-code-3.rs
Expand Up @@ -64,3 +64,19 @@ enum c_void {} //~ ERROR: code is never used
extern {
fn free(p: *c_void); //~ ERROR: code is never used
}

// Check provided method
mod inner {
pub trait Trait {
fn f(&self) { f(); }
}

impl Trait for int {}

fn f() {}
}

pub fn foo() {
let a = &1 as &inner::Trait;
a.f();
}

5 comments on commit 576a851

@bors
Copy link
Contributor

@bors bors commented on 576a851 Jan 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at ktt3ja@576a851

@bors
Copy link
Contributor

@bors bors commented on 576a851 Jan 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging ktt3ja/rust/issue-11224 = 576a851 into auto

@bors
Copy link
Contributor

@bors bors commented on 576a851 Jan 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ktt3ja/rust/issue-11224 = 576a851 merged ok, testing candidate = 8ba6151

@bors
Copy link
Contributor

@bors bors commented on 576a851 Jan 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 576a851 Jan 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 8ba6151

Please sign in to comment.