Skip to content

Commit

Permalink
Auto merge of #60124 - petrochenkov:stanomut, r=eddyb
Browse files Browse the repository at this point in the history
Remove mutability from `Def::Static`

Querify `TyCtxt::is_static`.
Use `Mutability` instead of bool in foreign statics in AST/HIR.

cc #60110
r? @eddyb
  • Loading branch information
bors committed Apr 21, 2019
2 parents 06a271a + 4eb94b4 commit 31a75a1
Show file tree
Hide file tree
Showing 34 changed files with 93 additions and 96 deletions.
6 changes: 3 additions & 3 deletions src/librustc/hir/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub enum Def<Id = hir::HirId> {
Fn(DefId),
Const(DefId),
ConstParam(DefId),
Static(DefId, bool /* is_mutbl */),
Static(DefId),
/// `DefId` refers to the struct or enum variant's constructor.
Ctor(DefId, CtorOf, CtorKind),
SelfCtor(DefId /* impl */), // `DefId` refers to the impl
Expand Down Expand Up @@ -291,7 +291,7 @@ impl<Id> Def<Id> {
/// Return `Some(..)` with the `DefId` of this `Def` if it has a id, else `None`.
pub fn opt_def_id(&self) -> Option<DefId> {
match *self {
Def::Fn(id) | Def::Mod(id) | Def::Static(id, _) |
Def::Fn(id) | Def::Mod(id) | Def::Static(id) |
Def::Variant(id) | Def::Ctor(id, ..) | Def::Enum(id) |
Def::TyAlias(id) | Def::TraitAlias(id) |
Def::AssociatedTy(id) | Def::TyParam(id) | Def::ConstParam(id) | Def::Struct(id) |
Expand Down Expand Up @@ -379,7 +379,7 @@ impl<Id> Def<Id> {
match self {
Def::Fn(id) => Def::Fn(id),
Def::Mod(id) => Def::Mod(id),
Def::Static(id, is_mutbl) => Def::Static(id, is_mutbl),
Def::Static(id) => Def::Static(id),
Def::Enum(id) => Def::Enum(id),
Def::Variant(id) => Def::Variant(id),
Def::Ctor(a, b, c) => Def::Ctor(a, b, c),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3742,7 +3742,7 @@ impl<'a> LoweringContext<'a> {
}
ForeignItemKind::Static(ref t, m) => {
hir::ForeignItemKind::Static(
self.lower_ty(t, ImplTraitContext::disallowed()), m)
self.lower_ty(t, ImplTraitContext::disallowed()), self.lower_mutability(m))
}
ForeignItemKind::Ty => hir::ForeignItemKind::Type,
ForeignItemKind::Macro(_) => panic!("shouldn't exist here"),
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ impl<'hir> Map<'hir> {
let def_id = || self.local_def_id_from_hir_id(item.hir_id);

match item.node {
ItemKind::Static(_, m, _) => Some(Def::Static(def_id(), m == MutMutable)),
ItemKind::Static(..) => Some(Def::Static(def_id())),
ItemKind::Const(..) => Some(Def::Const(def_id())),
ItemKind::Fn(..) => Some(Def::Fn(def_id())),
ItemKind::Mod(..) => Some(Def::Mod(def_id())),
Expand All @@ -344,7 +344,7 @@ impl<'hir> Map<'hir> {
let def_id = self.local_def_id_from_hir_id(item.hir_id);
match item.node {
ForeignItemKind::Fn(..) => Some(Def::Fn(def_id)),
ForeignItemKind::Static(_, m) => Some(Def::Static(def_id, m)),
ForeignItemKind::Static(..) => Some(Def::Static(def_id)),
ForeignItemKind::Type => Some(Def::ForeignTy(def_id)),
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2405,9 +2405,8 @@ pub struct ForeignItem {
pub enum ForeignItemKind {
/// A foreign function.
Fn(P<FnDecl>, HirVec<Ident>, Generics),
/// A foreign static item (`static ext: u8`), with optional mutability
/// (the boolean is true when mutable).
Static(P<Ty>, bool),
/// A foreign static item (`static ext: u8`).
Static(P<Ty>, Mutability),
/// A foreign type.
Type,
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ impl<'a> State<'a> {
}
hir::ForeignItemKind::Static(ref t, m) => {
self.head(visibility_qualified(&item.vis, "static"))?;
if m {
if m == hir::MutMutable {
self.word_space("mut")?;
}
self.print_ident(item.ident)?;
Expand Down
7 changes: 5 additions & 2 deletions src/librustc/middle/mem_categorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
Ok(self.cat_rvalue_node(hir_id, span, expr_ty))
}

Def::Static(def_id, mutbl) => {
Def::Static(def_id) => {
// `#[thread_local]` statics may not outlive the current function, but
// they also cannot be moved out of.
let is_thread_local = self.tcx.get_attrs(def_id)[..]
Expand All @@ -723,7 +723,10 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
hir_id,
span,
cat,
mutbl: if mutbl { McDeclared } else { McImmutable},
mutbl: match self.tcx.static_mutability(def_id).unwrap() {
hir::MutImmutable => McImmutable,
hir::MutMutable => McDeclared,
},
ty:expr_ty,
note: NoteNone
})
Expand Down
3 changes: 3 additions & 0 deletions src/librustc/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ rustc_queries! {
/// True if this is a foreign item (i.e., linked via `extern { ... }`).
query is_foreign_item(_: DefId) -> bool {}

/// Returns `Some(mutability)` if the node pointed to by `def_id` is a static item.
query static_mutability(_: DefId) -> Option<hir::Mutability> {}

/// Get a map with the variance of every item; use `item_variance`
/// instead.
query crate_variances(_: CrateNum) -> Lrc<ty::CrateVariancesMap> {
Expand Down
39 changes: 9 additions & 30 deletions src/librustc/ty/util.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//! Miscellaneous type-system utilities that are too small to deserve their own modules.

use crate::hir::def::Def;
use crate::hir;
use crate::hir::def_id::DefId;
use crate::hir::map::DefPathData;
use crate::hir::{self, Node};
use crate::mir::interpret::{sign_extend, truncate};
use crate::ich::NodeIdHashingMode;
use crate::traits::{self, ObligationCause};
Expand Down Expand Up @@ -613,34 +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> {
if let Some(node) = self.hir().get_if_local(def_id) {
match node {
Node::Item(&hir::Item {
node: hir::ItemKind::Static(_, mutbl, _), ..
}) => Some(mutbl),
Node::ForeignItem(&hir::ForeignItem {
node: hir::ForeignItemKind::Static(_, is_mutbl), ..
}) =>
Some(if is_mutbl {
hir::Mutability::MutMutable
} else {
hir::Mutability::MutImmutable
}),
_ => None
}
} else {
match self.describe_def(def_id) {
Some(Def::Static(_, is_mutbl)) =>
Some(if is_mutbl {
hir::Mutability::MutMutable
} else {
hir::Mutability::MutImmutable
}),
_ => None
}
}
/// 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.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_llvm/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
13 changes: 1 addition & 12 deletions src/librustc_codegen_ssa/mono_item.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use rustc::hir;
use rustc::hir::def::Def;
use rustc::mir::mono::{Linkage, Visibility};
use rustc::ty::layout::HasTyCtxt;
use std::fmt;
Expand All @@ -19,17 +18,7 @@ pub trait MonoItemExt<'a, 'tcx: 'a>: fmt::Debug + BaseMonoItemExt<'a, 'tcx> {

match *self.as_mono_item() {
MonoItem::Static(def_id) => {
let tcx = cx.tcx();
let is_mutable = match tcx.describe_def(def_id) {
Some(Def::Static(_, is_mutable)) => is_mutable,
Some(other) => {
bug!("Expected Def::Static, found {:?}", other)
}
None => {
bug!("Expected Def::Static for {:?}, found nothing", def_id)
}
};
cx.codegen_static(def_id, is_mutable);
cx.codegen_static(def_id, cx.tcx().is_mutable_static(def_id));
}
MonoItem::GlobalAsm(hir_id) => {
let item = cx.tcx().hir().expect_item_by_hir_id(hir_id);
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
inherent_impls => { Lrc::new(cdata.get_inherent_implementations_for_type(def_id.index)) }
is_const_fn_raw => { cdata.is_const_fn_raw(def_id.index) }
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
static_mutability => { cdata.static_mutability(def_id.index) }
describe_def => { cdata.get_def(def_id.index) }
def_span => { cdata.get_span(def_id.index, &tcx.sess) }
lookup_stability => {
Expand Down
14 changes: 12 additions & 2 deletions src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,9 @@ impl<'tcx> EntryKind<'tcx> {
EntryKind::Const(..) => Def::Const(did),
EntryKind::AssociatedConst(..) => Def::AssociatedConst(did),
EntryKind::ImmStatic |
EntryKind::ForeignImmStatic => Def::Static(did, false),
EntryKind::MutStatic |
EntryKind::ForeignMutStatic => Def::Static(did, true),
EntryKind::ForeignImmStatic |
EntryKind::ForeignMutStatic => Def::Static(did),
EntryKind::Struct(_, _) => Def::Struct(did),
EntryKind::Union(_, _) => Def::Union(did),
EntryKind::Fn(_) |
Expand Down Expand Up @@ -1163,6 +1163,16 @@ impl<'a, 'tcx> CrateMetadata {
}
}

crate fn static_mutability(&self, id: DefIndex) -> Option<hir::Mutability> {
match self.entry(id).kind {
EntryKind::ImmStatic |
EntryKind::ForeignImmStatic => Some(hir::MutImmutable),
EntryKind::MutStatic |
EntryKind::ForeignMutStatic => Some(hir::MutMutable),
_ => None,
}
}

pub fn fn_sig(&self,
id: DefIndex,
tcx: TyCtxt<'a, 'tcx, 'tcx>)
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1647,8 +1647,8 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
};
EntryKind::ForeignFn(self.lazy(&data))
}
hir::ForeignItemKind::Static(_, true) => EntryKind::ForeignMutStatic,
hir::ForeignItemKind::Static(_, false) => EntryKind::ForeignImmStatic,
hir::ForeignItemKind::Static(_, hir::MutMutable) => EntryKind::ForeignMutStatic,
hir::ForeignItemKind::Static(_, hir::MutImmutable) => EntryKind::ForeignImmStatic,
hir::ForeignItemKind::Type => EntryKind::ForeignType,
};

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/borrow_check/nll/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/place_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(..)
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/places_conflict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions src/librustc_mir/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()))
Expand Down Expand Up @@ -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(|| {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/hair/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
}
}

Def::Static(node_id, _) => ExprKind::StaticRef { id: node_id },
Def::Static(id) => ExprKind::StaticRef { id },

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

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/interpret/eval_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/interpret/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/transform/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/util/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,8 @@ fn write_mir_sig(
match (descr, src.promoted) {
(_, Some(i)) => write!(w, "{:?} in ", i)?,
(Some(Def::Const(_)), _) | (Some(Def::AssociatedConst(_)), _) => write!(w, "const ")?,
(Some(Def::Static(_, /*is_mutbl*/false)), _) => write!(w, "static ")?,
(Some(Def::Static(_, /*is_mutbl*/true)), _) => write!(w, "static mut ")?,
(Some(Def::Static(def_id)), _) =>
write!(w, "static {}", if tcx.is_mutable_static(def_id) { "mut " } else { "" })?,
(_, _) if is_function => write!(w, "fn ")?,
(None, _) => {}, // things like anon const, not an item
_ => bug!("Unexpected def description {:?}", descr),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_passes/rvalue_promotion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ fn check_expr_kind<'a, 'tcx>(
// are inherently promotable with the exception
// of "#[thread_local]" statics, which may not
// outlive the current function
Def::Static(did, _) => {
Def::Static(did) => {

if v.in_static {
for attr in &v.tcx.get_attrs(did)[..] {
Expand Down
Loading

0 comments on commit 31a75a1

Please sign in to comment.