Skip to content

Commit 31a75a1

Browse files
committed
Auto merge of #60124 - petrochenkov:stanomut, r=eddyb
Remove mutability from `Def::Static` Querify `TyCtxt::is_static`. Use `Mutability` instead of bool in foreign statics in AST/HIR. cc #60110 r? @eddyb
2 parents 06a271a + 4eb94b4 commit 31a75a1

File tree

34 files changed

+93
-96
lines changed

34 files changed

+93
-96
lines changed

src/librustc/hir/def.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub enum Def<Id = hir::HirId> {
7373
Fn(DefId),
7474
Const(DefId),
7575
ConstParam(DefId),
76-
Static(DefId, bool /* is_mutbl */),
76+
Static(DefId),
7777
/// `DefId` refers to the struct or enum variant's constructor.
7878
Ctor(DefId, CtorOf, CtorKind),
7979
SelfCtor(DefId /* impl */), // `DefId` refers to the impl
@@ -291,7 +291,7 @@ impl<Id> Def<Id> {
291291
/// Return `Some(..)` with the `DefId` of this `Def` if it has a id, else `None`.
292292
pub fn opt_def_id(&self) -> Option<DefId> {
293293
match *self {
294-
Def::Fn(id) | Def::Mod(id) | Def::Static(id, _) |
294+
Def::Fn(id) | Def::Mod(id) | Def::Static(id) |
295295
Def::Variant(id) | Def::Ctor(id, ..) | Def::Enum(id) |
296296
Def::TyAlias(id) | Def::TraitAlias(id) |
297297
Def::AssociatedTy(id) | Def::TyParam(id) | Def::ConstParam(id) | Def::Struct(id) |
@@ -379,7 +379,7 @@ impl<Id> Def<Id> {
379379
match self {
380380
Def::Fn(id) => Def::Fn(id),
381381
Def::Mod(id) => Def::Mod(id),
382-
Def::Static(id, is_mutbl) => Def::Static(id, is_mutbl),
382+
Def::Static(id) => Def::Static(id),
383383
Def::Enum(id) => Def::Enum(id),
384384
Def::Variant(id) => Def::Variant(id),
385385
Def::Ctor(a, b, c) => Def::Ctor(a, b, c),

src/librustc/hir/lowering.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3742,7 +3742,7 @@ impl<'a> LoweringContext<'a> {
37423742
}
37433743
ForeignItemKind::Static(ref t, m) => {
37443744
hir::ForeignItemKind::Static(
3745-
self.lower_ty(t, ImplTraitContext::disallowed()), m)
3745+
self.lower_ty(t, ImplTraitContext::disallowed()), self.lower_mutability(m))
37463746
}
37473747
ForeignItemKind::Ty => hir::ForeignItemKind::Type,
37483748
ForeignItemKind::Macro(_) => panic!("shouldn't exist here"),

src/librustc/hir/map/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ impl<'hir> Map<'hir> {
322322
let def_id = || self.local_def_id_from_hir_id(item.hir_id);
323323

324324
match item.node {
325-
ItemKind::Static(_, m, _) => Some(Def::Static(def_id(), m == MutMutable)),
325+
ItemKind::Static(..) => Some(Def::Static(def_id())),
326326
ItemKind::Const(..) => Some(Def::Const(def_id())),
327327
ItemKind::Fn(..) => Some(Def::Fn(def_id())),
328328
ItemKind::Mod(..) => Some(Def::Mod(def_id())),
@@ -344,7 +344,7 @@ impl<'hir> Map<'hir> {
344344
let def_id = self.local_def_id_from_hir_id(item.hir_id);
345345
match item.node {
346346
ForeignItemKind::Fn(..) => Some(Def::Fn(def_id)),
347-
ForeignItemKind::Static(_, m) => Some(Def::Static(def_id, m)),
347+
ForeignItemKind::Static(..) => Some(Def::Static(def_id)),
348348
ForeignItemKind::Type => Some(Def::ForeignTy(def_id)),
349349
}
350350
}

src/librustc/hir/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -2405,9 +2405,8 @@ pub struct ForeignItem {
24052405
pub enum ForeignItemKind {
24062406
/// A foreign function.
24072407
Fn(P<FnDecl>, HirVec<Ident>, Generics),
2408-
/// A foreign static item (`static ext: u8`), with optional mutability
2409-
/// (the boolean is true when mutable).
2410-
Static(P<Ty>, bool),
2408+
/// A foreign static item (`static ext: u8`).
2409+
Static(P<Ty>, Mutability),
24112410
/// A foreign type.
24122411
Type,
24132412
}

src/librustc/hir/print.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ impl<'a> State<'a> {
466466
}
467467
hir::ForeignItemKind::Static(ref t, m) => {
468468
self.head(visibility_qualified(&item.vis, "static"))?;
469-
if m {
469+
if m == hir::MutMutable {
470470
self.word_space("mut")?;
471471
}
472472
self.print_ident(item.ident)?;

src/librustc/middle/mem_categorization.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
705705
Ok(self.cat_rvalue_node(hir_id, span, expr_ty))
706706
}
707707

708-
Def::Static(def_id, mutbl) => {
708+
Def::Static(def_id) => {
709709
// `#[thread_local]` statics may not outlive the current function, but
710710
// they also cannot be moved out of.
711711
let is_thread_local = self.tcx.get_attrs(def_id)[..]
@@ -723,7 +723,10 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
723723
hir_id,
724724
span,
725725
cat,
726-
mutbl: if mutbl { McDeclared } else { McImmutable},
726+
mutbl: match self.tcx.static_mutability(def_id).unwrap() {
727+
hir::MutImmutable => McImmutable,
728+
hir::MutMutable => McDeclared,
729+
},
727730
ty:expr_ty,
728731
note: NoteNone
729732
})

src/librustc/query/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ rustc_queries! {
238238
/// True if this is a foreign item (i.e., linked via `extern { ... }`).
239239
query is_foreign_item(_: DefId) -> bool {}
240240

241+
/// Returns `Some(mutability)` if the node pointed to by `def_id` is a static item.
242+
query static_mutability(_: DefId) -> Option<hir::Mutability> {}
243+
241244
/// Get a map with the variance of every item; use `item_variance`
242245
/// instead.
243246
query crate_variances(_: CrateNum) -> Lrc<ty::CrateVariancesMap> {

src/librustc/ty/util.rs

+9-30
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//! Miscellaneous type-system utilities that are too small to deserve their own modules.
22
3-
use crate::hir::def::Def;
3+
use crate::hir;
44
use crate::hir::def_id::DefId;
55
use crate::hir::map::DefPathData;
6-
use crate::hir::{self, Node};
76
use crate::mir::interpret::{sign_extend, truncate};
87
use crate::ich::NodeIdHashingMode;
98
use crate::traits::{self, ObligationCause};
@@ -613,34 +612,14 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
613612
})
614613
}
615614

616-
/// Returns `true` if the node pointed to by `def_id` is a static item, and its mutability.
617-
pub fn is_static(&self, def_id: DefId) -> Option<hir::Mutability> {
618-
if let Some(node) = self.hir().get_if_local(def_id) {
619-
match node {
620-
Node::Item(&hir::Item {
621-
node: hir::ItemKind::Static(_, mutbl, _), ..
622-
}) => Some(mutbl),
623-
Node::ForeignItem(&hir::ForeignItem {
624-
node: hir::ForeignItemKind::Static(_, is_mutbl), ..
625-
}) =>
626-
Some(if is_mutbl {
627-
hir::Mutability::MutMutable
628-
} else {
629-
hir::Mutability::MutImmutable
630-
}),
631-
_ => None
632-
}
633-
} else {
634-
match self.describe_def(def_id) {
635-
Some(Def::Static(_, is_mutbl)) =>
636-
Some(if is_mutbl {
637-
hir::Mutability::MutMutable
638-
} else {
639-
hir::Mutability::MutImmutable
640-
}),
641-
_ => None
642-
}
643-
}
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)
644623
}
645624

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

src/librustc_codegen_llvm/common.rs

+1-1
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_codegen_ssa/mono_item.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use rustc::hir;
2-
use rustc::hir::def::Def;
32
use rustc::mir::mono::{Linkage, Visibility};
43
use rustc::ty::layout::HasTyCtxt;
54
use std::fmt;
@@ -19,17 +18,7 @@ pub trait MonoItemExt<'a, 'tcx: 'a>: fmt::Debug + BaseMonoItemExt<'a, 'tcx> {
1918

2019
match *self.as_mono_item() {
2120
MonoItem::Static(def_id) => {
22-
let tcx = cx.tcx();
23-
let is_mutable = match tcx.describe_def(def_id) {
24-
Some(Def::Static(_, is_mutable)) => is_mutable,
25-
Some(other) => {
26-
bug!("Expected Def::Static, found {:?}", other)
27-
}
28-
None => {
29-
bug!("Expected Def::Static for {:?}, found nothing", def_id)
30-
}
31-
};
32-
cx.codegen_static(def_id, is_mutable);
21+
cx.codegen_static(def_id, cx.tcx().is_mutable_static(def_id));
3322
}
3423
MonoItem::GlobalAsm(hir_id) => {
3524
let item = cx.tcx().hir().expect_item_by_hir_id(hir_id);

src/librustc_lint/builtin.rs

+1-2
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_metadata/cstore_impl.rs

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
137137
inherent_impls => { Lrc::new(cdata.get_inherent_implementations_for_type(def_id.index)) }
138138
is_const_fn_raw => { cdata.is_const_fn_raw(def_id.index) }
139139
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
140+
static_mutability => { cdata.static_mutability(def_id.index) }
140141
describe_def => { cdata.get_def(def_id.index) }
141142
def_span => { cdata.get_span(def_id.index, &tcx.sess) }
142143
lookup_stability => {

src/librustc_metadata/decoder.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,9 @@ impl<'tcx> EntryKind<'tcx> {
404404
EntryKind::Const(..) => Def::Const(did),
405405
EntryKind::AssociatedConst(..) => Def::AssociatedConst(did),
406406
EntryKind::ImmStatic |
407-
EntryKind::ForeignImmStatic => Def::Static(did, false),
408407
EntryKind::MutStatic |
409-
EntryKind::ForeignMutStatic => Def::Static(did, true),
408+
EntryKind::ForeignImmStatic |
409+
EntryKind::ForeignMutStatic => Def::Static(did),
410410
EntryKind::Struct(_, _) => Def::Struct(did),
411411
EntryKind::Union(_, _) => Def::Union(did),
412412
EntryKind::Fn(_) |
@@ -1163,6 +1163,16 @@ impl<'a, 'tcx> CrateMetadata {
11631163
}
11641164
}
11651165

1166+
crate fn static_mutability(&self, id: DefIndex) -> Option<hir::Mutability> {
1167+
match self.entry(id).kind {
1168+
EntryKind::ImmStatic |
1169+
EntryKind::ForeignImmStatic => Some(hir::MutImmutable),
1170+
EntryKind::MutStatic |
1171+
EntryKind::ForeignMutStatic => Some(hir::MutMutable),
1172+
_ => None,
1173+
}
1174+
}
1175+
11661176
pub fn fn_sig(&self,
11671177
id: DefIndex,
11681178
tcx: TyCtxt<'a, 'tcx, 'tcx>)

src/librustc_metadata/encoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1647,8 +1647,8 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
16471647
};
16481648
EntryKind::ForeignFn(self.lazy(&data))
16491649
}
1650-
hir::ForeignItemKind::Static(_, true) => EntryKind::ForeignMutStatic,
1651-
hir::ForeignItemKind::Static(_, false) => EntryKind::ForeignImmStatic,
1650+
hir::ForeignItemKind::Static(_, hir::MutMutable) => EntryKind::ForeignMutStatic,
1651+
hir::ForeignItemKind::Static(_, hir::MutImmutable) => EntryKind::ForeignImmStatic,
16521652
hir::ForeignItemKind::Type => EntryKind::ForeignType,
16531653
};
16541654

src/librustc_mir/borrow_check/mod.rs

+1-1
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

+2-2
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

+1-1
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

+1-1
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

+5-6
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/hair/cx/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
960960
}
961961
}
962962

963-
Def::Static(node_id, _) => ExprKind::StaticRef { id: node_id },
963+
Def::Static(id) => ExprKind::StaticRef { id },
964964

965965
Def::Local(..) | Def::Upvar(..) => convert_var(cx, expr, def),
966966

src/librustc_mir/interpret/eval_context.rs

+1-1
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

+1-1
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

+1-1
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",

src/librustc_mir/util/pretty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,8 @@ fn write_mir_sig(
592592
match (descr, src.promoted) {
593593
(_, Some(i)) => write!(w, "{:?} in ", i)?,
594594
(Some(Def::Const(_)), _) | (Some(Def::AssociatedConst(_)), _) => write!(w, "const ")?,
595-
(Some(Def::Static(_, /*is_mutbl*/false)), _) => write!(w, "static ")?,
596-
(Some(Def::Static(_, /*is_mutbl*/true)), _) => write!(w, "static mut ")?,
595+
(Some(Def::Static(def_id)), _) =>
596+
write!(w, "static {}", if tcx.is_mutable_static(def_id) { "mut " } else { "" })?,
597597
(_, _) if is_function => write!(w, "fn ")?,
598598
(None, _) => {}, // things like anon const, not an item
599599
_ => bug!("Unexpected def description {:?}", descr),

src/librustc_passes/rvalue_promotion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ fn check_expr_kind<'a, 'tcx>(
329329
// are inherently promotable with the exception
330330
// of "#[thread_local]" statics, which may not
331331
// outlive the current function
332-
Def::Static(did, _) => {
332+
Def::Static(did) => {
333333

334334
if v.in_static {
335335
for attr in &v.tcx.get_attrs(did)[..] {

0 commit comments

Comments
 (0)