Skip to content

Commit 0bb77bd

Browse files
committed
mir-borrowck: Move is_static_mut() to ty/utils.rs
1 parent 8efbf7a commit 0bb77bd

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

src/librustc/ty/util.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
//! misc. type-system utilities too small to deserve their own file
1212
13+
use hir::def::Def;
1314
use hir::def_id::{DefId, LOCAL_CRATE};
14-
use hir::map::DefPathData;
15+
use hir::map::{DefPathData, Node};
1516
use hir;
1617
use ich::NodeIdHashingMode;
1718
use middle::const_val::ConstVal;
@@ -648,6 +649,26 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
648649
_ => bug!(),
649650
}
650651
}
652+
653+
/// Check if the node pointed to by def_id is a mutable static item
654+
pub fn is_static_mut(&self, def_id: DefId) -> bool {
655+
if let Some(node) = self.hir.get_if_local(def_id) {
656+
match node {
657+
Node::NodeItem(&hir::Item {
658+
node: hir::ItemStatic(_, hir::MutMutable, _), ..
659+
}) => true,
660+
Node::NodeForeignItem(&hir::ForeignItem {
661+
node: hir::ForeignItemStatic(_, mutbl), ..
662+
}) => mutbl,
663+
_ => false
664+
}
665+
} else {
666+
match self.describe_def(def_id) {
667+
Some(Def::Static(_, mutbl)) => mutbl,
668+
_ => false
669+
}
670+
}
671+
}
651672
}
652673

653674
pub struct TypeIdHasher<'a, 'gcx: 'a+'tcx, 'tcx: 'a, W> {

src/librustc_mir/transform/check_unsafety.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ use rustc_data_structures::indexed_vec::IndexVec;
1414
use rustc::ty::maps::Providers;
1515
use rustc::ty::{self, TyCtxt};
1616
use rustc::hir;
17-
use rustc::hir::def::Def;
1817
use rustc::hir::def_id::DefId;
19-
use rustc::hir::map::{DefPathData, Node};
18+
use rustc::hir::map::DefPathData;
2019
use rustc::lint::builtin::{SAFE_EXTERN_STATICS, UNUSED_UNSAFE};
2120
use rustc::mir::*;
2221
use rustc::mir::visit::{LvalueContext, Visitor};
@@ -189,7 +188,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
189188
// locals are safe
190189
}
191190
&Lvalue::Static(box Static { def_id, ty: _ }) => {
192-
if self.is_static_mut(def_id) {
191+
if self.tcx.is_static_mut(def_id) {
193192
self.require_unsafe("use of mutable static");
194193
} else if self.tcx.is_foreign_item(def_id) {
195194
let source_info = self.source_info;
@@ -208,24 +207,6 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
208207
}
209208

210209
impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
211-
fn is_static_mut(&self, def_id: DefId) -> bool {
212-
if let Some(node) = self.tcx.hir.get_if_local(def_id) {
213-
match node {
214-
Node::NodeItem(&hir::Item {
215-
node: hir::ItemStatic(_, hir::MutMutable, _), ..
216-
}) => true,
217-
Node::NodeForeignItem(&hir::ForeignItem {
218-
node: hir::ForeignItemStatic(_, mutbl), ..
219-
}) => mutbl,
220-
_ => false
221-
}
222-
} else {
223-
match self.tcx.describe_def(def_id) {
224-
Some(Def::Static(_, mutbl)) => mutbl,
225-
_ => false
226-
}
227-
}
228-
}
229210
fn require_unsafe(&mut self,
230211
description: &'static str)
231212
{

0 commit comments

Comments
 (0)