Skip to content
Permalink
Browse files

Change return type of `TyCtxt::is_static` to bool

Add `TyCtxt::is_mutable_static`
  • Loading branch information...
petrochenkov committed Apr 21, 2019
1 parent 286a469 commit 6c187cc0c631f139c8ee43b1a6618664203b63aa
@@ -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<hir::Mutability> {
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.
@@ -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),
@@ -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 {
@@ -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 {
@@ -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
@@ -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(..)
@@ -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
@@ -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(|| {
@@ -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
@@ -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(),
@@ -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",

0 comments on commit 6c187cc

Please sign in to comment.
You can’t perform that action at this time.