diff --git a/src/librustc/ty/util.rs b/src/librustc/ty/util.rs index d808387e269fd..67507f7b5d116 100644 --- a/src/librustc/ty/util.rs +++ b/src/librustc/ty/util.rs @@ -612,9 +612,14 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { }) } - /// Returns `true` if the node pointed to by `def_id` is a static item, and its mutability. - pub fn is_static(&self, def_id: DefId) -> Option { - self.static_mutability(def_id) + /// Returns `true` if the node pointed to by `def_id` is a `static` item. + pub fn is_static(&self, def_id: DefId) -> bool { + self.static_mutability(def_id).is_some() + } + + /// Returns `true` if the node pointed to by `def_id` is a mutable `static` item. + pub fn is_mutable_static(&self, def_id: DefId) -> bool { + self.static_mutability(def_id) == Some(hir::MutMutable) } /// Expands the given impl trait type, stopping if the type is recursive. diff --git a/src/librustc_codegen_llvm/common.rs b/src/librustc_codegen_llvm/common.rs index 9554e54e4142a..b9fd9629e6ff1 100644 --- a/src/librustc_codegen_llvm/common.rs +++ b/src/librustc_codegen_llvm/common.rs @@ -322,7 +322,7 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> { self.get_fn(fn_instance) } Some(AllocKind::Static(def_id)) => { - assert!(self.tcx.is_static(def_id).is_some()); + assert!(self.tcx.is_static(def_id)); self.get_static(def_id) } None => bug!("missing allocation {:?}", ptr.alloc_id), diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index f289b0b48fbdf..5fde4331d4702 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -1171,8 +1171,7 @@ declare_lint_pass!( fn check_const(cx: &LateContext<'_, '_>, body_id: hir::BodyId) { let def_id = cx.tcx.hir().body_owner_def_id(body_id); - let is_static = cx.tcx.is_static(def_id).is_some(); - let param_env = if is_static { + let param_env = if cx.tcx.is_static(def_id) { // Use the same param_env as `codegen_static_initializer`, to reuse the cache. ty::ParamEnv::reveal_all() } else { diff --git a/src/librustc_mir/borrow_check/mod.rs b/src/librustc_mir/borrow_check/mod.rs index 0b2c90b916045..43ed85d4ac541 100644 --- a/src/librustc_mir/borrow_check/mod.rs +++ b/src/librustc_mir/borrow_check/mod.rs @@ -2117,7 +2117,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { is_local_mutation_allowed, }), Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Static(def_id), .. })) => { - if self.infcx.tcx.is_static(def_id) != Some(hir::Mutability::MutMutable) { + if !self.infcx.tcx.is_mutable_static(def_id) { Err(place) } else { Ok(RootPlace { diff --git a/src/librustc_mir/borrow_check/nll/type_check/mod.rs b/src/librustc_mir/borrow_check/nll/type_check/mod.rs index d123f3e405936..ce8f1852551f2 100644 --- a/src/librustc_mir/borrow_check/nll/type_check/mod.rs +++ b/src/librustc_mir/borrow_check/nll/type_check/mod.rs @@ -1321,7 +1321,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { .. }) = self.borrowck_context { - if tcx.is_static(*def_id).is_some() { + if tcx.is_static(*def_id) { ConstraintCategory::UseAsStatic } else { ConstraintCategory::UseAsConst @@ -1626,7 +1626,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { .. }) = self.borrowck_context { - if tcx.is_static(*def_id).is_some() { + if tcx.is_static(*def_id) { ConstraintCategory::UseAsStatic } else { ConstraintCategory::UseAsConst diff --git a/src/librustc_mir/borrow_check/place_ext.rs b/src/librustc_mir/borrow_check/place_ext.rs index 8269b7b95f490..913884a821837 100644 --- a/src/librustc_mir/borrow_check/place_ext.rs +++ b/src/librustc_mir/borrow_check/place_ext.rs @@ -52,7 +52,7 @@ impl<'tcx> PlaceExt<'tcx> for Place<'tcx> { Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Promoted(_), .. })) => false, Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Static(def_id), .. })) => { - tcx.is_static(*def_id) == Some(hir::Mutability::MutMutable) + tcx.is_mutable_static(*def_id) } Place::Projection(proj) => match proj.elem { ProjectionElem::Field(..) diff --git a/src/librustc_mir/borrow_check/places_conflict.rs b/src/librustc_mir/borrow_check/places_conflict.rs index cebf05ad9b99d..1bf606109dc76 100644 --- a/src/librustc_mir/borrow_check/places_conflict.rs +++ b/src/librustc_mir/borrow_check/places_conflict.rs @@ -321,7 +321,7 @@ fn place_base_conflict<'a, 'gcx: 'tcx, 'tcx>( if def_id_1 != def_id_2 { debug!("place_element_conflict: DISJOINT-STATIC"); Overlap::Disjoint - } else if tcx.is_static(*def_id_1) == Some(hir::Mutability::MutMutable) { + } else if tcx.is_mutable_static(*def_id_1) { // We ignore mutable statics - they can only be unsafe code. debug!("place_element_conflict: IGNORE-STATIC-MUT"); Overlap::Disjoint diff --git a/src/librustc_mir/const_eval.rs b/src/librustc_mir/const_eval.rs index 09c50d4f81f49..b65f2ba2601e4 100644 --- a/src/librustc_mir/const_eval.rs +++ b/src/librustc_mir/const_eval.rs @@ -6,8 +6,8 @@ use std::borrow::{Borrow, Cow}; use std::hash::Hash; use std::collections::hash_map::Entry; -use rustc::hir::{self, def_id::DefId}; use rustc::hir::def::Def; +use rustc::hir::def_id::DefId; use rustc::mir::interpret::{ConstEvalErr, ErrorHandled}; use rustc::mir; use rustc::ty::{self, TyCtxt, query::TyCtxtAt}; @@ -158,9 +158,8 @@ fn eval_body_using_ecx<'mir, 'tcx>( ecx.run()?; // Intern the result - let internally_mutable = !layout.ty.is_freeze(tcx, param_env, mir.span); - let is_static = tcx.is_static(cid.instance.def_id()); - let mutability = if is_static == Some(hir::Mutability::MutMutable) || internally_mutable { + let mutability = if tcx.is_mutable_static(cid.instance.def_id()) || + !layout.ty.is_freeze(tcx, param_env, mir.span) { Mutability::Mutable } else { Mutability::Immutable @@ -533,7 +532,7 @@ fn validate_and_turn_into_const<'a, 'tcx>( } // Now that we validated, turn this into a proper constant. let def_id = cid.instance.def.def_id(); - if tcx.is_static(def_id).is_some() || cid.promoted.is_some() { + if tcx.is_static(def_id) || cid.promoted.is_some() { Ok(mplace_to_const(&ecx, mplace)) } else { Ok(op_to_const(&ecx, mplace.into())) @@ -628,7 +627,7 @@ pub fn const_eval_raw_provider<'a, 'tcx>( }).map_err(|error| { let err = error_to_const_error(&ecx, error); // errors in statics are always emitted as fatal errors - if tcx.is_static(def_id).is_some() { + if tcx.is_static(def_id) { // Ensure that if the above error was either `TooGeneric` or `Reported` // an error must be reported. let reported_err = tcx.sess.track_errors(|| { diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs index 32f7ecd97b2ef..4ae8bfe854d78 100644 --- a/src/librustc_mir/interpret/eval_context.rs +++ b/src/librustc_mir/interpret/eval_context.rs @@ -634,7 +634,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tc &self, gid: GlobalId<'tcx>, ) -> EvalResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> { - let param_env = if self.tcx.is_static(gid.instance.def_id()).is_some() { + let param_env = if self.tcx.is_static(gid.instance.def_id()) { ty::ParamEnv::reveal_all() } else { self.param_env diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs index 117bd15399cde..9674822b47a3d 100644 --- a/src/librustc_mir/interpret/memory.rs +++ b/src/librustc_mir/interpret/memory.rs @@ -342,7 +342,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { // full query anyway tcx.const_eval_raw(ty::ParamEnv::reveal_all().and(gid)).map_err(|err| { // no need to report anything, the const_eval call takes care of that for statics - assert!(tcx.is_static(def_id).is_some()); + assert!(tcx.is_static(def_id)); match err { ErrorHandled::Reported => InterpError::ReferencedConstant.into(), ErrorHandled::TooGeneric => InterpError::TooGeneric.into(), diff --git a/src/librustc_mir/transform/check_unsafety.rs b/src/librustc_mir/transform/check_unsafety.rs index c9cccb2b03ac4..87c02b7f01da3 100644 --- a/src/librustc_mir/transform/check_unsafety.rs +++ b/src/librustc_mir/transform/check_unsafety.rs @@ -306,7 +306,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> { &Place::Base( PlaceBase::Static(box Static { kind: StaticKind::Static(def_id), .. }) ) => { - if self.tcx.is_static(def_id) == Some(hir::Mutability::MutMutable) { + if self.tcx.is_mutable_static(def_id) { self.require_unsafe("use of mutable static", "mutable statics can be mutated by multiple threads: aliasing violations \ or data races will cause undefined behavior",