Skip to content

Commit

Permalink
Constants used in range patterns should not be considered unused
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Bukaj committed Oct 31, 2014
1 parent 52c3fe9 commit d23d633
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/librustc/middle/dead.rs
Expand Up @@ -51,7 +51,7 @@ struct MarkSymbolVisitor<'a, 'tcx: 'a> {
tcx: &'a ty::ctxt<'tcx>,
live_symbols: Box<HashSet<ast::NodeId>>,
struct_has_extern_repr: bool,
ignore_paths: bool
ignore_non_const_paths: bool
}

impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
Expand All @@ -62,7 +62,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
tcx: tcx,
live_symbols: box HashSet::new(),
struct_has_extern_repr: false,
ignore_paths: false
ignore_non_const_paths: false
}
}

Expand All @@ -76,6 +76,10 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
fn lookup_and_handle_definition(&mut self, id: &ast::NodeId) {
self.tcx.def_map.borrow().find(id).map(|def| {
match def {
&def::DefConst(_) => {
self.check_def_id(def.def_id())
}
_ if self.ignore_non_const_paths => (),
&def::DefPrimTy(_) => (),
&def::DefVariant(enum_id, variant_id, _) => {
self.check_def_id(enum_id);
Expand Down Expand Up @@ -283,21 +287,19 @@ impl<'a, 'tcx, 'v> Visitor<'v> for MarkSymbolVisitor<'a, 'tcx> {
self.handle_field_pattern_match(pat, fields.as_slice());
}
_ if pat_util::pat_is_const(def_map, pat) => {
// it might be the only use of a static:
// it might be the only use of a const
self.lookup_and_handle_definition(&pat.id)
}
_ => ()
}

self.ignore_paths = true;
self.ignore_non_const_paths = true;
visit::walk_pat(self, pat);
self.ignore_paths = false;
self.ignore_non_const_paths = false;
}

fn visit_path(&mut self, path: &ast::Path, id: ast::NodeId) {
if !self.ignore_paths {
self.lookup_and_handle_definition(&id);
}
self.lookup_and_handle_definition(&id);
visit::walk_path(self, path);
}

Expand Down
21 changes: 21 additions & 0 deletions src/test/run-pass/issue-18464.rs
@@ -0,0 +1,21 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![deny(dead_code)]

const LOW_RANGE: char = '0';
const HIGH_RANGE: char = '9';

fn main() {
match '5' {
LOW_RANGE...HIGH_RANGE => (),
_ => ()
};
}

8 comments on commit d23d633

@bors
Copy link
Contributor

@bors bors commented on d23d633 Nov 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 d23d633 Nov 2, 2014

Choose a reason for hiding this comment

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

merging jakub-/rust/issue-18464 = d23d633 into auto

@bors
Copy link
Contributor

@bors bors commented on d23d633 Nov 2, 2014

Choose a reason for hiding this comment

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

jakub-/rust/issue-18464 = d23d633 merged ok, testing candidate = 3d8b29c

@bors
Copy link
Contributor

@bors bors commented on d23d633 Nov 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 d23d633 Nov 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 d23d633 Nov 2, 2014

Choose a reason for hiding this comment

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

merging jakub-/rust/issue-18464 = d23d633 into auto

@bors
Copy link
Contributor

@bors bors commented on d23d633 Nov 2, 2014

Choose a reason for hiding this comment

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

jakub-/rust/issue-18464 = d23d633 merged ok, testing candidate = 2707adf1

@bors
Copy link
Contributor

@bors bors commented on d23d633 Nov 2, 2014

Choose a reason for hiding this comment

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

Please sign in to comment.