From d23d633eb843078551f382fc729e5d20f62b9c87 Mon Sep 17 00:00:00 2001 From: Jakub Bukaj Date: Fri, 31 Oct 2014 00:19:01 +0100 Subject: [PATCH] Constants used in range patterns should not be considered unused --- src/librustc/middle/dead.rs | 18 ++++++++++-------- src/test/run-pass/issue-18464.rs | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 src/test/run-pass/issue-18464.rs diff --git a/src/librustc/middle/dead.rs b/src/librustc/middle/dead.rs index 80cef763d24e7..ec09706f97e80 100644 --- a/src/librustc/middle/dead.rs +++ b/src/librustc/middle/dead.rs @@ -51,7 +51,7 @@ struct MarkSymbolVisitor<'a, 'tcx: 'a> { tcx: &'a ty::ctxt<'tcx>, live_symbols: Box>, struct_has_extern_repr: bool, - ignore_paths: bool + ignore_non_const_paths: bool } impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> { @@ -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 } } @@ -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); @@ -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); } diff --git a/src/test/run-pass/issue-18464.rs b/src/test/run-pass/issue-18464.rs new file mode 100644 index 0000000000000..dff86bc1b4527 --- /dev/null +++ b/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 or the MIT license +// , 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 => (), + _ => () + }; +}