From 0bb77bdb5423402b583372abf2f097be6b4e46bc Mon Sep 17 00:00:00 2001 From: Basile Desloges Date: Sun, 12 Nov 2017 12:40:56 +0100 Subject: [PATCH] mir-borrowck: Move `is_static_mut()` to `ty/utils.rs` --- src/librustc/ty/util.rs | 23 +++++++++++++++++++- src/librustc_mir/transform/check_unsafety.rs | 23 ++------------------ 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/librustc/ty/util.rs b/src/librustc/ty/util.rs index d12a973017d..aa07a6b070e 100644 --- a/src/librustc/ty/util.rs +++ b/src/librustc/ty/util.rs @@ -10,8 +10,9 @@ //! misc. type-system utilities too small to deserve their own file +use hir::def::Def; use hir::def_id::{DefId, LOCAL_CRATE}; -use hir::map::DefPathData; +use hir::map::{DefPathData, Node}; use hir; use ich::NodeIdHashingMode; use middle::const_val::ConstVal; @@ -648,6 +649,26 @@ pub fn const_usize(&self, val: u16) -> ConstInt { _ => bug!(), } } + + /// Check if the node pointed to by def_id is a mutable static item + pub fn is_static_mut(&self, def_id: DefId) -> bool { + if let Some(node) = self.hir.get_if_local(def_id) { + match node { + Node::NodeItem(&hir::Item { + node: hir::ItemStatic(_, hir::MutMutable, _), .. + }) => true, + Node::NodeForeignItem(&hir::ForeignItem { + node: hir::ForeignItemStatic(_, mutbl), .. + }) => mutbl, + _ => false + } + } else { + match self.describe_def(def_id) { + Some(Def::Static(_, mutbl)) => mutbl, + _ => false + } + } + } } pub struct TypeIdHasher<'a, 'gcx: 'a+'tcx, 'tcx: 'a, W> { diff --git a/src/librustc_mir/transform/check_unsafety.rs b/src/librustc_mir/transform/check_unsafety.rs index 53a4b2551fd..cbf1b0b0899 100644 --- a/src/librustc_mir/transform/check_unsafety.rs +++ b/src/librustc_mir/transform/check_unsafety.rs @@ -14,9 +14,8 @@ use rustc::ty::maps::Providers; use rustc::ty::{self, TyCtxt}; use rustc::hir; -use rustc::hir::def::Def; use rustc::hir::def_id::DefId; -use rustc::hir::map::{DefPathData, Node}; +use rustc::hir::map::DefPathData; use rustc::lint::builtin::{SAFE_EXTERN_STATICS, UNUSED_UNSAFE}; use rustc::mir::*; use rustc::mir::visit::{LvalueContext, Visitor}; @@ -189,7 +188,7 @@ fn visit_lvalue(&mut self, // locals are safe } &Lvalue::Static(box Static { def_id, ty: _ }) => { - if self.is_static_mut(def_id) { + if self.tcx.is_static_mut(def_id) { self.require_unsafe("use of mutable static"); } else if self.tcx.is_foreign_item(def_id) { let source_info = self.source_info; @@ -208,24 +207,6 @@ fn visit_lvalue(&mut self, } impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> { - fn is_static_mut(&self, def_id: DefId) -> bool { - if let Some(node) = self.tcx.hir.get_if_local(def_id) { - match node { - Node::NodeItem(&hir::Item { - node: hir::ItemStatic(_, hir::MutMutable, _), .. - }) => true, - Node::NodeForeignItem(&hir::ForeignItem { - node: hir::ForeignItemStatic(_, mutbl), .. - }) => mutbl, - _ => false - } - } else { - match self.tcx.describe_def(def_id) { - Some(Def::Static(_, mutbl)) => mutbl, - _ => false - } - } - } fn require_unsafe(&mut self, description: &'static str) {