Skip to content

Commit 6c187cc

Browse files
committed
Change return type of TyCtxt::is_static to bool
Add `TyCtxt::is_mutable_static`
1 parent 286a469 commit 6c187cc

File tree

11 files changed

+23
-20
lines changed

11 files changed

+23
-20
lines changed

src/librustc/ty/util.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,9 +612,14 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
612612
})
613613
}
614614

615-
/// Returns `true` if the node pointed to by `def_id` is a static item, and its mutability.
616-
pub fn is_static(&self, def_id: DefId) -> Option<hir::Mutability> {
617-
self.static_mutability(def_id)
615+
/// Returns `true` if the node pointed to by `def_id` is a `static` item.
616+
pub fn is_static(&self, def_id: DefId) -> bool {
617+
self.static_mutability(def_id).is_some()
618+
}
619+
620+
/// Returns `true` if the node pointed to by `def_id` is a mutable `static` item.
621+
pub fn is_mutable_static(&self, def_id: DefId) -> bool {
622+
self.static_mutability(def_id) == Some(hir::MutMutable)
618623
}
619624

620625
/// Expands the given impl trait type, stopping if the type is recursive.

src/librustc_codegen_llvm/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
322322
self.get_fn(fn_instance)
323323
}
324324
Some(AllocKind::Static(def_id)) => {
325-
assert!(self.tcx.is_static(def_id).is_some());
325+
assert!(self.tcx.is_static(def_id));
326326
self.get_static(def_id)
327327
}
328328
None => bug!("missing allocation {:?}", ptr.alloc_id),

src/librustc_lint/builtin.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,8 +1171,7 @@ declare_lint_pass!(
11711171

11721172
fn check_const(cx: &LateContext<'_, '_>, body_id: hir::BodyId) {
11731173
let def_id = cx.tcx.hir().body_owner_def_id(body_id);
1174-
let is_static = cx.tcx.is_static(def_id).is_some();
1175-
let param_env = if is_static {
1174+
let param_env = if cx.tcx.is_static(def_id) {
11761175
// Use the same param_env as `codegen_static_initializer`, to reuse the cache.
11771176
ty::ParamEnv::reveal_all()
11781177
} else {

src/librustc_mir/borrow_check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2117,7 +2117,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
21172117
is_local_mutation_allowed,
21182118
}),
21192119
Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Static(def_id), .. })) => {
2120-
if self.infcx.tcx.is_static(def_id) != Some(hir::Mutability::MutMutable) {
2120+
if !self.infcx.tcx.is_mutable_static(def_id) {
21212121
Err(place)
21222122
} else {
21232123
Ok(RootPlace {

src/librustc_mir/borrow_check/nll/type_check/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
13211321
..
13221322
}) = self.borrowck_context
13231323
{
1324-
if tcx.is_static(*def_id).is_some() {
1324+
if tcx.is_static(*def_id) {
13251325
ConstraintCategory::UseAsStatic
13261326
} else {
13271327
ConstraintCategory::UseAsConst
@@ -1626,7 +1626,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
16261626
..
16271627
}) = self.borrowck_context
16281628
{
1629-
if tcx.is_static(*def_id).is_some() {
1629+
if tcx.is_static(*def_id) {
16301630
ConstraintCategory::UseAsStatic
16311631
} else {
16321632
ConstraintCategory::UseAsConst

src/librustc_mir/borrow_check/place_ext.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl<'tcx> PlaceExt<'tcx> for Place<'tcx> {
5252
Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Promoted(_), .. })) =>
5353
false,
5454
Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Static(def_id), .. })) => {
55-
tcx.is_static(*def_id) == Some(hir::Mutability::MutMutable)
55+
tcx.is_mutable_static(*def_id)
5656
}
5757
Place::Projection(proj) => match proj.elem {
5858
ProjectionElem::Field(..)

src/librustc_mir/borrow_check/places_conflict.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ fn place_base_conflict<'a, 'gcx: 'tcx, 'tcx>(
321321
if def_id_1 != def_id_2 {
322322
debug!("place_element_conflict: DISJOINT-STATIC");
323323
Overlap::Disjoint
324-
} else if tcx.is_static(*def_id_1) == Some(hir::Mutability::MutMutable) {
324+
} else if tcx.is_mutable_static(*def_id_1) {
325325
// We ignore mutable statics - they can only be unsafe code.
326326
debug!("place_element_conflict: IGNORE-STATIC-MUT");
327327
Overlap::Disjoint

src/librustc_mir/const_eval.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use std::borrow::{Borrow, Cow};
66
use std::hash::Hash;
77
use std::collections::hash_map::Entry;
88

9-
use rustc::hir::{self, def_id::DefId};
109
use rustc::hir::def::Def;
10+
use rustc::hir::def_id::DefId;
1111
use rustc::mir::interpret::{ConstEvalErr, ErrorHandled};
1212
use rustc::mir;
1313
use rustc::ty::{self, TyCtxt, query::TyCtxtAt};
@@ -158,9 +158,8 @@ fn eval_body_using_ecx<'mir, 'tcx>(
158158
ecx.run()?;
159159

160160
// Intern the result
161-
let internally_mutable = !layout.ty.is_freeze(tcx, param_env, mir.span);
162-
let is_static = tcx.is_static(cid.instance.def_id());
163-
let mutability = if is_static == Some(hir::Mutability::MutMutable) || internally_mutable {
161+
let mutability = if tcx.is_mutable_static(cid.instance.def_id()) ||
162+
!layout.ty.is_freeze(tcx, param_env, mir.span) {
164163
Mutability::Mutable
165164
} else {
166165
Mutability::Immutable
@@ -533,7 +532,7 @@ fn validate_and_turn_into_const<'a, 'tcx>(
533532
}
534533
// Now that we validated, turn this into a proper constant.
535534
let def_id = cid.instance.def.def_id();
536-
if tcx.is_static(def_id).is_some() || cid.promoted.is_some() {
535+
if tcx.is_static(def_id) || cid.promoted.is_some() {
537536
Ok(mplace_to_const(&ecx, mplace))
538537
} else {
539538
Ok(op_to_const(&ecx, mplace.into()))
@@ -628,7 +627,7 @@ pub fn const_eval_raw_provider<'a, 'tcx>(
628627
}).map_err(|error| {
629628
let err = error_to_const_error(&ecx, error);
630629
// errors in statics are always emitted as fatal errors
631-
if tcx.is_static(def_id).is_some() {
630+
if tcx.is_static(def_id) {
632631
// Ensure that if the above error was either `TooGeneric` or `Reported`
633632
// an error must be reported.
634633
let reported_err = tcx.sess.track_errors(|| {

src/librustc_mir/interpret/eval_context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tc
634634
&self,
635635
gid: GlobalId<'tcx>,
636636
) -> EvalResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
637-
let param_env = if self.tcx.is_static(gid.instance.def_id()).is_some() {
637+
let param_env = if self.tcx.is_static(gid.instance.def_id()) {
638638
ty::ParamEnv::reveal_all()
639639
} else {
640640
self.param_env

src/librustc_mir/interpret/memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
342342
// full query anyway
343343
tcx.const_eval_raw(ty::ParamEnv::reveal_all().and(gid)).map_err(|err| {
344344
// no need to report anything, the const_eval call takes care of that for statics
345-
assert!(tcx.is_static(def_id).is_some());
345+
assert!(tcx.is_static(def_id));
346346
match err {
347347
ErrorHandled::Reported => InterpError::ReferencedConstant.into(),
348348
ErrorHandled::TooGeneric => InterpError::TooGeneric.into(),

src/librustc_mir/transform/check_unsafety.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
306306
&Place::Base(
307307
PlaceBase::Static(box Static { kind: StaticKind::Static(def_id), .. })
308308
) => {
309-
if self.tcx.is_static(def_id) == Some(hir::Mutability::MutMutable) {
309+
if self.tcx.is_mutable_static(def_id) {
310310
self.require_unsafe("use of mutable static",
311311
"mutable statics can be mutated by multiple threads: aliasing violations \
312312
or data races will cause undefined behavior",

0 commit comments

Comments
 (0)