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 https://github.com/rust-lang/rust/pull/60110 r? @eddyb
This commit is contained in:
commit
31a75a1728
34 changed files with 93 additions and 96 deletions
|
@ -73,7 +73,7 @@ pub enum Def<Id = hir::HirId> {
|
||||||
Fn(DefId),
|
Fn(DefId),
|
||||||
Const(DefId),
|
Const(DefId),
|
||||||
ConstParam(DefId),
|
ConstParam(DefId),
|
||||||
Static(DefId, bool /* is_mutbl */),
|
Static(DefId),
|
||||||
/// `DefId` refers to the struct or enum variant's constructor.
|
/// `DefId` refers to the struct or enum variant's constructor.
|
||||||
Ctor(DefId, CtorOf, CtorKind),
|
Ctor(DefId, CtorOf, CtorKind),
|
||||||
SelfCtor(DefId /* impl */), // `DefId` refers to the impl
|
SelfCtor(DefId /* impl */), // `DefId` refers to the impl
|
||||||
|
@ -291,7 +291,7 @@ impl<Id> Def<Id> {
|
||||||
/// Return `Some(..)` with the `DefId` of this `Def` if it has a id, else `None`.
|
/// Return `Some(..)` with the `DefId` of this `Def` if it has a id, else `None`.
|
||||||
pub fn opt_def_id(&self) -> Option<DefId> {
|
pub fn opt_def_id(&self) -> Option<DefId> {
|
||||||
match *self {
|
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::Variant(id) | Def::Ctor(id, ..) | Def::Enum(id) |
|
||||||
Def::TyAlias(id) | Def::TraitAlias(id) |
|
Def::TyAlias(id) | Def::TraitAlias(id) |
|
||||||
Def::AssociatedTy(id) | Def::TyParam(id) | Def::ConstParam(id) | Def::Struct(id) |
|
Def::AssociatedTy(id) | Def::TyParam(id) | Def::ConstParam(id) | Def::Struct(id) |
|
||||||
|
@ -379,7 +379,7 @@ impl<Id> Def<Id> {
|
||||||
match self {
|
match self {
|
||||||
Def::Fn(id) => Def::Fn(id),
|
Def::Fn(id) => Def::Fn(id),
|
||||||
Def::Mod(id) => Def::Mod(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::Enum(id) => Def::Enum(id),
|
||||||
Def::Variant(id) => Def::Variant(id),
|
Def::Variant(id) => Def::Variant(id),
|
||||||
Def::Ctor(a, b, c) => Def::Ctor(a, b, c),
|
Def::Ctor(a, b, c) => Def::Ctor(a, b, c),
|
||||||
|
|
|
@ -3742,7 +3742,7 @@ impl<'a> LoweringContext<'a> {
|
||||||
}
|
}
|
||||||
ForeignItemKind::Static(ref t, m) => {
|
ForeignItemKind::Static(ref t, m) => {
|
||||||
hir::ForeignItemKind::Static(
|
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::Ty => hir::ForeignItemKind::Type,
|
||||||
ForeignItemKind::Macro(_) => panic!("shouldn't exist here"),
|
ForeignItemKind::Macro(_) => panic!("shouldn't exist here"),
|
||||||
|
|
|
@ -322,7 +322,7 @@ impl<'hir> Map<'hir> {
|
||||||
let def_id = || self.local_def_id_from_hir_id(item.hir_id);
|
let def_id = || self.local_def_id_from_hir_id(item.hir_id);
|
||||||
|
|
||||||
match item.node {
|
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::Const(..) => Some(Def::Const(def_id())),
|
||||||
ItemKind::Fn(..) => Some(Def::Fn(def_id())),
|
ItemKind::Fn(..) => Some(Def::Fn(def_id())),
|
||||||
ItemKind::Mod(..) => Some(Def::Mod(def_id())),
|
ItemKind::Mod(..) => Some(Def::Mod(def_id())),
|
||||||
|
@ -344,7 +344,7 @@ impl<'hir> Map<'hir> {
|
||||||
let def_id = self.local_def_id_from_hir_id(item.hir_id);
|
let def_id = self.local_def_id_from_hir_id(item.hir_id);
|
||||||
match item.node {
|
match item.node {
|
||||||
ForeignItemKind::Fn(..) => Some(Def::Fn(def_id)),
|
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)),
|
ForeignItemKind::Type => Some(Def::ForeignTy(def_id)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2405,9 +2405,8 @@ pub struct ForeignItem {
|
||||||
pub enum ForeignItemKind {
|
pub enum ForeignItemKind {
|
||||||
/// A foreign function.
|
/// A foreign function.
|
||||||
Fn(P<FnDecl>, HirVec<Ident>, Generics),
|
Fn(P<FnDecl>, HirVec<Ident>, Generics),
|
||||||
/// A foreign static item (`static ext: u8`), with optional mutability
|
/// A foreign static item (`static ext: u8`).
|
||||||
/// (the boolean is true when mutable).
|
Static(P<Ty>, Mutability),
|
||||||
Static(P<Ty>, bool),
|
|
||||||
/// A foreign type.
|
/// A foreign type.
|
||||||
Type,
|
Type,
|
||||||
}
|
}
|
||||||
|
|
|
@ -466,7 +466,7 @@ impl<'a> State<'a> {
|
||||||
}
|
}
|
||||||
hir::ForeignItemKind::Static(ref t, m) => {
|
hir::ForeignItemKind::Static(ref t, m) => {
|
||||||
self.head(visibility_qualified(&item.vis, "static"))?;
|
self.head(visibility_qualified(&item.vis, "static"))?;
|
||||||
if m {
|
if m == hir::MutMutable {
|
||||||
self.word_space("mut")?;
|
self.word_space("mut")?;
|
||||||
}
|
}
|
||||||
self.print_ident(item.ident)?;
|
self.print_ident(item.ident)?;
|
||||||
|
|
|
@ -705,7 +705,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
|
||||||
Ok(self.cat_rvalue_node(hir_id, span, expr_ty))
|
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
|
// `#[thread_local]` statics may not outlive the current function, but
|
||||||
// they also cannot be moved out of.
|
// they also cannot be moved out of.
|
||||||
let is_thread_local = self.tcx.get_attrs(def_id)[..]
|
let is_thread_local = self.tcx.get_attrs(def_id)[..]
|
||||||
|
@ -723,7 +723,10 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
|
||||||
hir_id,
|
hir_id,
|
||||||
span,
|
span,
|
||||||
cat,
|
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,
|
ty:expr_ty,
|
||||||
note: NoteNone
|
note: NoteNone
|
||||||
})
|
})
|
||||||
|
|
|
@ -238,6 +238,9 @@ rustc_queries! {
|
||||||
/// True if this is a foreign item (i.e., linked via `extern { ... }`).
|
/// True if this is a foreign item (i.e., linked via `extern { ... }`).
|
||||||
query is_foreign_item(_: DefId) -> bool {}
|
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`
|
/// Get a map with the variance of every item; use `item_variance`
|
||||||
/// instead.
|
/// instead.
|
||||||
query crate_variances(_: CrateNum) -> Lrc<ty::CrateVariancesMap> {
|
query crate_variances(_: CrateNum) -> Lrc<ty::CrateVariancesMap> {
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
//! Miscellaneous type-system utilities that are too small to deserve their own modules.
|
//! 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::def_id::DefId;
|
||||||
use crate::hir::map::DefPathData;
|
use crate::hir::map::DefPathData;
|
||||||
use crate::hir::{self, Node};
|
|
||||||
use crate::mir::interpret::{sign_extend, truncate};
|
use crate::mir::interpret::{sign_extend, truncate};
|
||||||
use crate::ich::NodeIdHashingMode;
|
use crate::ich::NodeIdHashingMode;
|
||||||
use crate::traits::{self, ObligationCause};
|
use crate::traits::{self, ObligationCause};
|
||||||
|
@ -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.
|
/// Returns `true` if the node pointed to by `def_id` is a `static` item.
|
||||||
pub fn is_static(&self, def_id: DefId) -> Option<hir::Mutability> {
|
pub fn is_static(&self, def_id: DefId) -> bool {
|
||||||
if let Some(node) = self.hir().get_if_local(def_id) {
|
self.static_mutability(def_id).is_some()
|
||||||
match node {
|
}
|
||||||
Node::Item(&hir::Item {
|
|
||||||
node: hir::ItemKind::Static(_, mutbl, _), ..
|
/// Returns `true` if the node pointed to by `def_id` is a mutable `static` item.
|
||||||
}) => Some(mutbl),
|
pub fn is_mutable_static(&self, def_id: DefId) -> bool {
|
||||||
Node::ForeignItem(&hir::ForeignItem {
|
self.static_mutability(def_id) == Some(hir::MutMutable)
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Expands the given impl trait type, stopping if the type is recursive.
|
/// 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)
|
self.get_fn(fn_instance)
|
||||||
}
|
}
|
||||||
Some(AllocKind::Static(def_id)) => {
|
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)
|
self.get_static(def_id)
|
||||||
}
|
}
|
||||||
None => bug!("missing allocation {:?}", ptr.alloc_id),
|
None => bug!("missing allocation {:?}", ptr.alloc_id),
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
use rustc::hir;
|
use rustc::hir;
|
||||||
use rustc::hir::def::Def;
|
|
||||||
use rustc::mir::mono::{Linkage, Visibility};
|
use rustc::mir::mono::{Linkage, Visibility};
|
||||||
use rustc::ty::layout::HasTyCtxt;
|
use rustc::ty::layout::HasTyCtxt;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
@ -19,17 +18,7 @@ pub trait MonoItemExt<'a, 'tcx: 'a>: fmt::Debug + BaseMonoItemExt<'a, 'tcx> {
|
||||||
|
|
||||||
match *self.as_mono_item() {
|
match *self.as_mono_item() {
|
||||||
MonoItem::Static(def_id) => {
|
MonoItem::Static(def_id) => {
|
||||||
let tcx = cx.tcx();
|
cx.codegen_static(def_id, cx.tcx().is_mutable_static(def_id));
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
MonoItem::GlobalAsm(hir_id) => {
|
MonoItem::GlobalAsm(hir_id) => {
|
||||||
let item = cx.tcx().hir().expect_item_by_hir_id(hir_id);
|
let item = cx.tcx().hir().expect_item_by_hir_id(hir_id);
|
||||||
|
|
|
@ -1171,8 +1171,7 @@ declare_lint_pass!(
|
||||||
|
|
||||||
fn check_const(cx: &LateContext<'_, '_>, body_id: hir::BodyId) {
|
fn check_const(cx: &LateContext<'_, '_>, body_id: hir::BodyId) {
|
||||||
let def_id = cx.tcx.hir().body_owner_def_id(body_id);
|
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 cx.tcx.is_static(def_id) {
|
||||||
let param_env = if is_static {
|
|
||||||
// Use the same param_env as `codegen_static_initializer`, to reuse the cache.
|
// Use the same param_env as `codegen_static_initializer`, to reuse the cache.
|
||||||
ty::ParamEnv::reveal_all()
|
ty::ParamEnv::reveal_all()
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -137,6 +137,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
|
||||||
inherent_impls => { Lrc::new(cdata.get_inherent_implementations_for_type(def_id.index)) }
|
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_const_fn_raw => { cdata.is_const_fn_raw(def_id.index) }
|
||||||
is_foreign_item => { cdata.is_foreign_item(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) }
|
describe_def => { cdata.get_def(def_id.index) }
|
||||||
def_span => { cdata.get_span(def_id.index, &tcx.sess) }
|
def_span => { cdata.get_span(def_id.index, &tcx.sess) }
|
||||||
lookup_stability => {
|
lookup_stability => {
|
||||||
|
|
|
@ -404,9 +404,9 @@ impl<'tcx> EntryKind<'tcx> {
|
||||||
EntryKind::Const(..) => Def::Const(did),
|
EntryKind::Const(..) => Def::Const(did),
|
||||||
EntryKind::AssociatedConst(..) => Def::AssociatedConst(did),
|
EntryKind::AssociatedConst(..) => Def::AssociatedConst(did),
|
||||||
EntryKind::ImmStatic |
|
EntryKind::ImmStatic |
|
||||||
EntryKind::ForeignImmStatic => Def::Static(did, false),
|
|
||||||
EntryKind::MutStatic |
|
EntryKind::MutStatic |
|
||||||
EntryKind::ForeignMutStatic => Def::Static(did, true),
|
EntryKind::ForeignImmStatic |
|
||||||
|
EntryKind::ForeignMutStatic => Def::Static(did),
|
||||||
EntryKind::Struct(_, _) => Def::Struct(did),
|
EntryKind::Struct(_, _) => Def::Struct(did),
|
||||||
EntryKind::Union(_, _) => Def::Union(did),
|
EntryKind::Union(_, _) => Def::Union(did),
|
||||||
EntryKind::Fn(_) |
|
EntryKind::Fn(_) |
|
||||||
|
@ -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,
|
pub fn fn_sig(&self,
|
||||||
id: DefIndex,
|
id: DefIndex,
|
||||||
tcx: TyCtxt<'a, 'tcx, 'tcx>)
|
tcx: TyCtxt<'a, 'tcx, 'tcx>)
|
||||||
|
|
|
@ -1647,8 +1647,8 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
|
||||||
};
|
};
|
||||||
EntryKind::ForeignFn(self.lazy(&data))
|
EntryKind::ForeignFn(self.lazy(&data))
|
||||||
}
|
}
|
||||||
hir::ForeignItemKind::Static(_, true) => EntryKind::ForeignMutStatic,
|
hir::ForeignItemKind::Static(_, hir::MutMutable) => EntryKind::ForeignMutStatic,
|
||||||
hir::ForeignItemKind::Static(_, false) => EntryKind::ForeignImmStatic,
|
hir::ForeignItemKind::Static(_, hir::MutImmutable) => EntryKind::ForeignImmStatic,
|
||||||
hir::ForeignItemKind::Type => EntryKind::ForeignType,
|
hir::ForeignItemKind::Type => EntryKind::ForeignType,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -2117,7 +2117,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
|
||||||
is_local_mutation_allowed,
|
is_local_mutation_allowed,
|
||||||
}),
|
}),
|
||||||
Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Static(def_id), .. })) => {
|
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)
|
Err(place)
|
||||||
} else {
|
} else {
|
||||||
Ok(RootPlace {
|
Ok(RootPlace {
|
||||||
|
|
|
@ -1321,7 +1321,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
|
||||||
..
|
..
|
||||||
}) = self.borrowck_context
|
}) = self.borrowck_context
|
||||||
{
|
{
|
||||||
if tcx.is_static(*def_id).is_some() {
|
if tcx.is_static(*def_id) {
|
||||||
ConstraintCategory::UseAsStatic
|
ConstraintCategory::UseAsStatic
|
||||||
} else {
|
} else {
|
||||||
ConstraintCategory::UseAsConst
|
ConstraintCategory::UseAsConst
|
||||||
|
@ -1626,7 +1626,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
|
||||||
..
|
..
|
||||||
}) = self.borrowck_context
|
}) = self.borrowck_context
|
||||||
{
|
{
|
||||||
if tcx.is_static(*def_id).is_some() {
|
if tcx.is_static(*def_id) {
|
||||||
ConstraintCategory::UseAsStatic
|
ConstraintCategory::UseAsStatic
|
||||||
} else {
|
} else {
|
||||||
ConstraintCategory::UseAsConst
|
ConstraintCategory::UseAsConst
|
||||||
|
|
|
@ -52,7 +52,7 @@ impl<'tcx> PlaceExt<'tcx> for Place<'tcx> {
|
||||||
Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Promoted(_), .. })) =>
|
Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Promoted(_), .. })) =>
|
||||||
false,
|
false,
|
||||||
Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Static(def_id), .. })) => {
|
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 {
|
Place::Projection(proj) => match proj.elem {
|
||||||
ProjectionElem::Field(..)
|
ProjectionElem::Field(..)
|
||||||
|
|
|
@ -321,7 +321,7 @@ fn place_base_conflict<'a, 'gcx: 'tcx, 'tcx>(
|
||||||
if def_id_1 != def_id_2 {
|
if def_id_1 != def_id_2 {
|
||||||
debug!("place_element_conflict: DISJOINT-STATIC");
|
debug!("place_element_conflict: DISJOINT-STATIC");
|
||||||
Overlap::Disjoint
|
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.
|
// We ignore mutable statics - they can only be unsafe code.
|
||||||
debug!("place_element_conflict: IGNORE-STATIC-MUT");
|
debug!("place_element_conflict: IGNORE-STATIC-MUT");
|
||||||
Overlap::Disjoint
|
Overlap::Disjoint
|
||||||
|
|
|
@ -6,8 +6,8 @@ use std::borrow::{Borrow, Cow};
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
use std::collections::hash_map::Entry;
|
use std::collections::hash_map::Entry;
|
||||||
|
|
||||||
use rustc::hir::{self, def_id::DefId};
|
|
||||||
use rustc::hir::def::Def;
|
use rustc::hir::def::Def;
|
||||||
|
use rustc::hir::def_id::DefId;
|
||||||
use rustc::mir::interpret::{ConstEvalErr, ErrorHandled};
|
use rustc::mir::interpret::{ConstEvalErr, ErrorHandled};
|
||||||
use rustc::mir;
|
use rustc::mir;
|
||||||
use rustc::ty::{self, TyCtxt, query::TyCtxtAt};
|
use rustc::ty::{self, TyCtxt, query::TyCtxtAt};
|
||||||
|
@ -158,9 +158,8 @@ fn eval_body_using_ecx<'mir, 'tcx>(
|
||||||
ecx.run()?;
|
ecx.run()?;
|
||||||
|
|
||||||
// Intern the result
|
// Intern the result
|
||||||
let internally_mutable = !layout.ty.is_freeze(tcx, param_env, mir.span);
|
let mutability = if tcx.is_mutable_static(cid.instance.def_id()) ||
|
||||||
let is_static = tcx.is_static(cid.instance.def_id());
|
!layout.ty.is_freeze(tcx, param_env, mir.span) {
|
||||||
let mutability = if is_static == Some(hir::Mutability::MutMutable) || internally_mutable {
|
|
||||||
Mutability::Mutable
|
Mutability::Mutable
|
||||||
} else {
|
} else {
|
||||||
Mutability::Immutable
|
Mutability::Immutable
|
||||||
|
@ -533,7 +532,7 @@ fn validate_and_turn_into_const<'a, 'tcx>(
|
||||||
}
|
}
|
||||||
// Now that we validated, turn this into a proper constant.
|
// Now that we validated, turn this into a proper constant.
|
||||||
let def_id = cid.instance.def.def_id();
|
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))
|
Ok(mplace_to_const(&ecx, mplace))
|
||||||
} else {
|
} else {
|
||||||
Ok(op_to_const(&ecx, mplace.into()))
|
Ok(op_to_const(&ecx, mplace.into()))
|
||||||
|
@ -628,7 +627,7 @@ pub fn const_eval_raw_provider<'a, 'tcx>(
|
||||||
}).map_err(|error| {
|
}).map_err(|error| {
|
||||||
let err = error_to_const_error(&ecx, error);
|
let err = error_to_const_error(&ecx, error);
|
||||||
// errors in statics are always emitted as fatal errors
|
// 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`
|
// Ensure that if the above error was either `TooGeneric` or `Reported`
|
||||||
// an error must be reported.
|
// an error must be reported.
|
||||||
let reported_err = tcx.sess.track_errors(|| {
|
let reported_err = tcx.sess.track_errors(|| {
|
||||||
|
|
|
@ -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),
|
Def::Local(..) | Def::Upvar(..) => convert_var(cx, expr, def),
|
||||||
|
|
||||||
|
|
|
@ -634,7 +634,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tc
|
||||||
&self,
|
&self,
|
||||||
gid: GlobalId<'tcx>,
|
gid: GlobalId<'tcx>,
|
||||||
) -> EvalResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
|
) -> 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()
|
ty::ParamEnv::reveal_all()
|
||||||
} else {
|
} else {
|
||||||
self.param_env
|
self.param_env
|
||||||
|
|
|
@ -342,7 +342,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
|
||||||
// full query anyway
|
// full query anyway
|
||||||
tcx.const_eval_raw(ty::ParamEnv::reveal_all().and(gid)).map_err(|err| {
|
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
|
// 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 {
|
match err {
|
||||||
ErrorHandled::Reported => InterpError::ReferencedConstant.into(),
|
ErrorHandled::Reported => InterpError::ReferencedConstant.into(),
|
||||||
ErrorHandled::TooGeneric => InterpError::TooGeneric.into(),
|
ErrorHandled::TooGeneric => InterpError::TooGeneric.into(),
|
||||||
|
|
|
@ -306,7 +306,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
|
||||||
&Place::Base(
|
&Place::Base(
|
||||||
PlaceBase::Static(box Static { kind: StaticKind::Static(def_id), .. })
|
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",
|
self.require_unsafe("use of mutable static",
|
||||||
"mutable statics can be mutated by multiple threads: aliasing violations \
|
"mutable statics can be mutated by multiple threads: aliasing violations \
|
||||||
or data races will cause undefined behavior",
|
or data races will cause undefined behavior",
|
||||||
|
|
|
@ -592,8 +592,8 @@ fn write_mir_sig(
|
||||||
match (descr, src.promoted) {
|
match (descr, src.promoted) {
|
||||||
(_, Some(i)) => write!(w, "{:?} in ", i)?,
|
(_, Some(i)) => write!(w, "{:?} in ", i)?,
|
||||||
(Some(Def::Const(_)), _) | (Some(Def::AssociatedConst(_)), _) => write!(w, "const ")?,
|
(Some(Def::Const(_)), _) | (Some(Def::AssociatedConst(_)), _) => write!(w, "const ")?,
|
||||||
(Some(Def::Static(_, /*is_mutbl*/false)), _) => write!(w, "static ")?,
|
(Some(Def::Static(def_id)), _) =>
|
||||||
(Some(Def::Static(_, /*is_mutbl*/true)), _) => write!(w, "static mut ")?,
|
write!(w, "static {}", if tcx.is_mutable_static(def_id) { "mut " } else { "" })?,
|
||||||
(_, _) if is_function => write!(w, "fn ")?,
|
(_, _) if is_function => write!(w, "fn ")?,
|
||||||
(None, _) => {}, // things like anon const, not an item
|
(None, _) => {}, // things like anon const, not an item
|
||||||
_ => bug!("Unexpected def description {:?}", descr),
|
_ => bug!("Unexpected def description {:?}", descr),
|
||||||
|
|
|
@ -329,7 +329,7 @@ fn check_expr_kind<'a, 'tcx>(
|
||||||
// are inherently promotable with the exception
|
// are inherently promotable with the exception
|
||||||
// of "#[thread_local]" statics, which may not
|
// of "#[thread_local]" statics, which may not
|
||||||
// outlive the current function
|
// outlive the current function
|
||||||
Def::Static(did, _) => {
|
Def::Static(did) => {
|
||||||
|
|
||||||
if v.in_static {
|
if v.in_static {
|
||||||
for attr in &v.tcx.get_attrs(did)[..] {
|
for attr in &v.tcx.get_attrs(did)[..] {
|
||||||
|
|
|
@ -28,7 +28,7 @@ use syntax::ast::{Name, Ident};
|
||||||
use syntax::attr;
|
use syntax::attr;
|
||||||
|
|
||||||
use syntax::ast::{self, Block, ForeignItem, ForeignItemKind, Item, ItemKind, NodeId};
|
use syntax::ast::{self, Block, ForeignItem, ForeignItemKind, Item, ItemKind, NodeId};
|
||||||
use syntax::ast::{MetaItemKind, Mutability, StmtKind, TraitItem, TraitItemKind, Variant};
|
use syntax::ast::{MetaItemKind, StmtKind, TraitItem, TraitItemKind, Variant};
|
||||||
use syntax::ext::base::{MacroKind, SyntaxExtension};
|
use syntax::ext::base::{MacroKind, SyntaxExtension};
|
||||||
use syntax::ext::base::Determinacy::Undetermined;
|
use syntax::ext::base::Determinacy::Undetermined;
|
||||||
use syntax::ext::hygiene::Mark;
|
use syntax::ext::hygiene::Mark;
|
||||||
|
@ -442,9 +442,8 @@ impl<'a> Resolver<'a> {
|
||||||
ItemKind::ForeignMod(..) => {}
|
ItemKind::ForeignMod(..) => {}
|
||||||
|
|
||||||
// These items live in the value namespace.
|
// These items live in the value namespace.
|
||||||
ItemKind::Static(_, m, _) => {
|
ItemKind::Static(..) => {
|
||||||
let mutbl = m == Mutability::Mutable;
|
let def = Def::Static(self.definitions.local_def_id(item.id));
|
||||||
let def = Def::Static(self.definitions.local_def_id(item.id), mutbl);
|
|
||||||
self.define(parent, ident, ValueNS, (def, vis, sp, expansion));
|
self.define(parent, ident, ValueNS, (def, vis, sp, expansion));
|
||||||
}
|
}
|
||||||
ItemKind::Const(..) => {
|
ItemKind::Const(..) => {
|
||||||
|
@ -616,8 +615,8 @@ impl<'a> Resolver<'a> {
|
||||||
ForeignItemKind::Fn(..) => {
|
ForeignItemKind::Fn(..) => {
|
||||||
(Def::Fn(self.definitions.local_def_id(item.id)), ValueNS)
|
(Def::Fn(self.definitions.local_def_id(item.id)), ValueNS)
|
||||||
}
|
}
|
||||||
ForeignItemKind::Static(_, m) => {
|
ForeignItemKind::Static(..) => {
|
||||||
(Def::Static(self.definitions.local_def_id(item.id), m), ValueNS)
|
(Def::Static(self.definitions.local_def_id(item.id)), ValueNS)
|
||||||
}
|
}
|
||||||
ForeignItemKind::Ty => {
|
ForeignItemKind::Ty => {
|
||||||
(Def::ForeignTy(self.definitions.local_def_id(item.id)), TypeNS)
|
(Def::ForeignTy(self.definitions.local_def_id(item.id)), TypeNS)
|
||||||
|
|
|
@ -798,7 +798,7 @@ impl Sig for ast::ForeignItem {
|
||||||
}
|
}
|
||||||
ast::ForeignItemKind::Static(ref ty, m) => {
|
ast::ForeignItemKind::Static(ref ty, m) => {
|
||||||
let mut text = "static ".to_owned();
|
let mut text = "static ".to_owned();
|
||||||
if m {
|
if m == ast::Mutability::Mutable {
|
||||||
text.push_str("mut ");
|
text.push_str("mut ");
|
||||||
}
|
}
|
||||||
let name = self.ident.to_string();
|
let name = self.ident.to_string();
|
||||||
|
|
|
@ -1665,7 +1665,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
|
||||||
Def::Fn(def_id) |
|
Def::Fn(def_id) |
|
||||||
Def::Const(def_id) |
|
Def::Const(def_id) |
|
||||||
Def::ConstParam(def_id) |
|
Def::ConstParam(def_id) |
|
||||||
Def::Static(def_id, _) => {
|
Def::Static(def_id) => {
|
||||||
path_segs.push(PathSeg(def_id, last));
|
path_segs.push(PathSeg(def_id, last));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -78,6 +78,7 @@ pub fn provide(providers: &mut Providers<'_>) {
|
||||||
impl_trait_ref,
|
impl_trait_ref,
|
||||||
impl_polarity,
|
impl_polarity,
|
||||||
is_foreign_item,
|
is_foreign_item,
|
||||||
|
static_mutability,
|
||||||
codegen_fn_attrs,
|
codegen_fn_attrs,
|
||||||
collect_mod_item_types,
|
collect_mod_item_types,
|
||||||
..*providers
|
..*providers
|
||||||
|
@ -2361,6 +2362,22 @@ fn is_foreign_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> bool
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn static_mutability<'a, 'tcx>(
|
||||||
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
||||||
|
def_id: DefId,
|
||||||
|
) -> Option<hir::Mutability> {
|
||||||
|
match tcx.hir().get_if_local(def_id) {
|
||||||
|
Some(Node::Item(&hir::Item {
|
||||||
|
node: hir::ItemKind::Static(_, mutbl, _), ..
|
||||||
|
})) |
|
||||||
|
Some(Node::ForeignItem( &hir::ForeignItem {
|
||||||
|
node: hir::ForeignItemKind::Static(_, mutbl), ..
|
||||||
|
})) => Some(mutbl),
|
||||||
|
Some(_) => None,
|
||||||
|
_ => bug!("static_mutability applied to non-local def-id {:?}", def_id),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn from_target_feature(
|
fn from_target_feature(
|
||||||
tcx: TyCtxt<'_, '_, '_>,
|
tcx: TyCtxt<'_, '_, '_>,
|
||||||
id: DefId,
|
id: DefId,
|
||||||
|
|
|
@ -93,9 +93,9 @@ pub fn try_inline(
|
||||||
record_extern_fqn(cx, did, clean::TypeKind::Module);
|
record_extern_fqn(cx, did, clean::TypeKind::Module);
|
||||||
clean::ModuleItem(build_module(cx, did, visited))
|
clean::ModuleItem(build_module(cx, did, visited))
|
||||||
}
|
}
|
||||||
Def::Static(did, mtbl) => {
|
Def::Static(did) => {
|
||||||
record_extern_fqn(cx, did, clean::TypeKind::Static);
|
record_extern_fqn(cx, did, clean::TypeKind::Static);
|
||||||
clean::StaticItem(build_static(cx, did, mtbl))
|
clean::StaticItem(build_static(cx, did, cx.tcx.is_mutable_static(did)))
|
||||||
}
|
}
|
||||||
Def::Const(did) => {
|
Def::Const(did) => {
|
||||||
record_extern_fqn(cx, did, clean::TypeKind::Const);
|
record_extern_fqn(cx, did, clean::TypeKind::Const);
|
||||||
|
|
|
@ -4055,7 +4055,7 @@ impl Clean<Item> for hir::ForeignItem {
|
||||||
hir::ForeignItemKind::Static(ref ty, mutbl) => {
|
hir::ForeignItemKind::Static(ref ty, mutbl) => {
|
||||||
ForeignStaticItem(Static {
|
ForeignStaticItem(Static {
|
||||||
type_: ty.clean(cx),
|
type_: ty.clean(cx),
|
||||||
mutability: if mutbl {Mutable} else {Immutable},
|
mutability: mutbl.clean(cx),
|
||||||
expr: String::new(),
|
expr: String::new(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -4204,7 +4204,7 @@ pub fn register_def(cx: &DocContext<'_>, def: Def) -> DefId {
|
||||||
Def::Mod(i) => (i, TypeKind::Module),
|
Def::Mod(i) => (i, TypeKind::Module),
|
||||||
Def::ForeignTy(i) => (i, TypeKind::Foreign),
|
Def::ForeignTy(i) => (i, TypeKind::Foreign),
|
||||||
Def::Const(i) => (i, TypeKind::Const),
|
Def::Const(i) => (i, TypeKind::Const),
|
||||||
Def::Static(i, _) => (i, TypeKind::Static),
|
Def::Static(i) => (i, TypeKind::Static),
|
||||||
Def::Variant(i) => (cx.tcx.parent(i).expect("cannot get parent def id"),
|
Def::Variant(i) => (cx.tcx.parent(i).expect("cannot get parent def id"),
|
||||||
TypeKind::Enum),
|
TypeKind::Enum),
|
||||||
Def::Macro(i, mac_kind) => match mac_kind {
|
Def::Macro(i, mac_kind) => match mac_kind {
|
||||||
|
|
|
@ -2340,9 +2340,8 @@ pub struct ForeignItem {
|
||||||
pub enum ForeignItemKind {
|
pub enum ForeignItemKind {
|
||||||
/// A foreign function.
|
/// A foreign function.
|
||||||
Fn(P<FnDecl>, Generics),
|
Fn(P<FnDecl>, Generics),
|
||||||
/// A foreign static item (`static ext: u8`), with optional mutability.
|
/// A foreign static item (`static ext: u8`).
|
||||||
/// (The boolean is `true` for mutable items).
|
Static(P<Ty>, Mutability),
|
||||||
Static(P<Ty>, bool),
|
|
||||||
/// A foreign type.
|
/// A foreign type.
|
||||||
Ty,
|
Ty,
|
||||||
/// A macro invocation.
|
/// A macro invocation.
|
||||||
|
|
|
@ -7683,7 +7683,7 @@ impl<'a> Parser<'a> {
|
||||||
/// Assumes that the `static` keyword is already parsed.
|
/// Assumes that the `static` keyword is already parsed.
|
||||||
fn parse_item_foreign_static(&mut self, vis: ast::Visibility, lo: Span, attrs: Vec<Attribute>)
|
fn parse_item_foreign_static(&mut self, vis: ast::Visibility, lo: Span, attrs: Vec<Attribute>)
|
||||||
-> PResult<'a, ForeignItem> {
|
-> PResult<'a, ForeignItem> {
|
||||||
let mutbl = self.eat_keyword(keywords::Mut);
|
let mutbl = self.parse_mutability();
|
||||||
let ident = self.parse_ident()?;
|
let ident = self.parse_ident()?;
|
||||||
self.expect(&token::Colon)?;
|
self.expect(&token::Colon)?;
|
||||||
let ty = self.parse_ty()?;
|
let ty = self.parse_ty()?;
|
||||||
|
|
|
@ -1142,7 +1142,7 @@ impl<'a> State<'a> {
|
||||||
}
|
}
|
||||||
ast::ForeignItemKind::Static(ref t, m) => {
|
ast::ForeignItemKind::Static(ref t, m) => {
|
||||||
self.head(visibility_qualified(&item.vis, "static"))?;
|
self.head(visibility_qualified(&item.vis, "static"))?;
|
||||||
if m {
|
if m == ast::Mutability::Mutable {
|
||||||
self.word_space("mut")?;
|
self.word_space("mut")?;
|
||||||
}
|
}
|
||||||
self.print_ident(item.ident)?;
|
self.print_ident(item.ident)?;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue