Auto merge of #95436 - cjgillot:static-mut, r=oli-obk
Remember mutability in `DefKind::Static`. This allows to compute the `BodyOwnerKind` from `DefKind` only, and removes a direct dependency of some MIR queries onto HIR. As a side effect, it also simplifies metadata, since we don't need 4 flavours of `EntryKind::*Static` any more.
This commit is contained in:
commit
a40c595695
40 changed files with 98 additions and 128 deletions
|
@ -228,7 +228,7 @@ impl<'hir> Map<'hir> {
|
|||
let hir_id = self.local_def_id_to_hir_id(local_def_id);
|
||||
let def_kind = match self.find(hir_id)? {
|
||||
Node::Item(item) => match item.kind {
|
||||
ItemKind::Static(..) => DefKind::Static,
|
||||
ItemKind::Static(_, mt, _) => DefKind::Static(mt),
|
||||
ItemKind::Const(..) => DefKind::Const,
|
||||
ItemKind::Fn(..) => DefKind::Fn,
|
||||
ItemKind::Macro(_, macro_kind) => DefKind::Macro(macro_kind),
|
||||
|
@ -248,7 +248,7 @@ impl<'hir> Map<'hir> {
|
|||
},
|
||||
Node::ForeignItem(item) => match item.kind {
|
||||
ForeignItemKind::Fn(..) => DefKind::Fn,
|
||||
ForeignItemKind::Static(..) => DefKind::Static,
|
||||
ForeignItemKind::Static(_, mt) => DefKind::Static(mt),
|
||||
ForeignItemKind::Type => DefKind::ForeignTy,
|
||||
},
|
||||
Node::TraitItem(item) => match item.kind {
|
||||
|
@ -471,19 +471,15 @@ impl<'hir> Map<'hir> {
|
|||
/// Returns the `BodyOwnerKind` of this `LocalDefId`.
|
||||
///
|
||||
/// Panics if `LocalDefId` does not have an associated body.
|
||||
pub fn body_owner_kind(self, id: HirId) -> BodyOwnerKind {
|
||||
match self.get(id) {
|
||||
Node::Item(&Item { kind: ItemKind::Const(..), .. })
|
||||
| Node::TraitItem(&TraitItem { kind: TraitItemKind::Const(..), .. })
|
||||
| Node::ImplItem(&ImplItem { kind: ImplItemKind::Const(..), .. })
|
||||
| Node::AnonConst(_) => BodyOwnerKind::Const,
|
||||
Node::Ctor(..)
|
||||
| Node::Item(&Item { kind: ItemKind::Fn(..), .. })
|
||||
| Node::TraitItem(&TraitItem { kind: TraitItemKind::Fn(..), .. })
|
||||
| Node::ImplItem(&ImplItem { kind: ImplItemKind::Fn(..), .. }) => BodyOwnerKind::Fn,
|
||||
Node::Item(&Item { kind: ItemKind::Static(_, m, _), .. }) => BodyOwnerKind::Static(m),
|
||||
Node::Expr(&Expr { kind: ExprKind::Closure(..), .. }) => BodyOwnerKind::Closure,
|
||||
node => bug!("{:#?} is not a body node", node),
|
||||
pub fn body_owner_kind(self, def_id: LocalDefId) -> BodyOwnerKind {
|
||||
match self.tcx.def_kind(def_id) {
|
||||
DefKind::Const | DefKind::AssocConst | DefKind::InlineConst | DefKind::AnonConst => {
|
||||
BodyOwnerKind::Const
|
||||
}
|
||||
DefKind::Ctor(..) | DefKind::Fn | DefKind::AssocFn => BodyOwnerKind::Fn,
|
||||
DefKind::Closure | DefKind::Generator => BodyOwnerKind::Closure,
|
||||
DefKind::Static(mt) => BodyOwnerKind::Static(mt),
|
||||
dk => bug!("{:?} is not a body node: {:?}", def_id, dk),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -494,16 +490,17 @@ impl<'hir> Map<'hir> {
|
|||
/// This should only be used for determining the context of a body, a return
|
||||
/// value of `Some` does not always suggest that the owner of the body is `const`,
|
||||
/// just that it has to be checked as if it were.
|
||||
pub fn body_const_context(self, did: LocalDefId) -> Option<ConstContext> {
|
||||
let hir_id = self.local_def_id_to_hir_id(did);
|
||||
let ccx = match self.body_owner_kind(hir_id) {
|
||||
pub fn body_const_context(self, def_id: LocalDefId) -> Option<ConstContext> {
|
||||
let ccx = match self.body_owner_kind(def_id) {
|
||||
BodyOwnerKind::Const => ConstContext::Const,
|
||||
BodyOwnerKind::Static(mt) => ConstContext::Static(mt),
|
||||
|
||||
BodyOwnerKind::Fn if self.tcx.is_constructor(did.to_def_id()) => return None,
|
||||
BodyOwnerKind::Fn if self.tcx.is_const_fn_raw(did.to_def_id()) => ConstContext::ConstFn,
|
||||
BodyOwnerKind::Fn if self.tcx.is_constructor(def_id.to_def_id()) => return None,
|
||||
BodyOwnerKind::Fn if self.tcx.is_const_fn_raw(def_id.to_def_id()) => {
|
||||
ConstContext::ConstFn
|
||||
}
|
||||
BodyOwnerKind::Fn
|
||||
if self.tcx.has_attr(did.to_def_id(), sym::default_method_body_is_const) =>
|
||||
if self.tcx.has_attr(def_id.to_def_id(), sym::default_method_body_is_const) =>
|
||||
{
|
||||
ConstContext::ConstFn
|
||||
}
|
||||
|
|
|
@ -950,9 +950,8 @@ fn write_mir_sig(tcx: TyCtxt<'_>, body: &Body<'_>, w: &mut dyn Write) -> io::Res
|
|||
match (kind, body.source.promoted) {
|
||||
(_, Some(i)) => write!(w, "{:?} in ", i)?,
|
||||
(DefKind::Const | DefKind::AssocConst, _) => write!(w, "const ")?,
|
||||
(DefKind::Static, _) => {
|
||||
write!(w, "static {}", if tcx.is_mutable_static(def_id) { "mut " } else { "" })?
|
||||
}
|
||||
(DefKind::Static(hir::Mutability::Not), _) => write!(w, "static ")?,
|
||||
(DefKind::Static(hir::Mutability::Mut), _) => write!(w, "static mut ")?,
|
||||
(_, _) if is_function => write!(w, "fn ")?,
|
||||
(DefKind::AnonConst | DefKind::InlineConst, _) => {} // things like anon const, not an item
|
||||
_ => bug!("Unexpected def kind {:?}", kind),
|
||||
|
|
|
@ -586,12 +586,6 @@ rustc_queries! {
|
|||
separate_provide_extern
|
||||
}
|
||||
|
||||
/// Returns `Some(mutability)` if the node pointed to by `def_id` is a static item.
|
||||
query static_mutability(def_id: DefId) -> Option<hir::Mutability> {
|
||||
desc { |tcx| "looking up static mutability of `{}`", tcx.def_path_str(def_id) }
|
||||
separate_provide_extern
|
||||
}
|
||||
|
||||
/// Returns `Some(generator_kind)` if the node pointed to by `def_id` is a generator.
|
||||
query generator_kind(def_id: DefId) -> Option<hir::GeneratorKind> {
|
||||
desc { |tcx| "looking up generator kind of `{}`", tcx.def_path_str(def_id) }
|
||||
|
|
|
@ -2147,7 +2147,7 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||
match instance {
|
||||
ty::InstanceDef::Item(def) => match self.def_kind(def.did) {
|
||||
DefKind::Const
|
||||
| DefKind::Static
|
||||
| DefKind::Static(..)
|
||||
| DefKind::AssocConst
|
||||
| DefKind::Ctor(..)
|
||||
| DefKind::AnonConst
|
||||
|
|
|
@ -1188,7 +1188,7 @@ pub trait PrettyPrinter<'tcx>:
|
|||
}
|
||||
ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted: None }) => {
|
||||
match self.tcx().def_kind(def.did) {
|
||||
DefKind::Static | DefKind::Const | DefKind::AssocConst => {
|
||||
DefKind::Static(..) | DefKind::Const | DefKind::AssocConst => {
|
||||
p!(print_value_path(def.did, substs))
|
||||
}
|
||||
_ => {
|
||||
|
|
|
@ -533,8 +533,14 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||
}
|
||||
|
||||
/// Returns `true` if the node pointed to by `def_id` is a `static` item.
|
||||
#[inline]
|
||||
pub fn is_static(self, def_id: DefId) -> bool {
|
||||
self.static_mutability(def_id).is_some()
|
||||
matches!(self.def_kind(def_id), DefKind::Static(_))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn static_mutability(self, def_id: DefId) -> Option<hir::Mutability> {
|
||||
if let DefKind::Static(mt) = self.def_kind(def_id) { Some(mt) } else { None }
|
||||
}
|
||||
|
||||
/// Returns `true` if this is a `static` item with the `#[thread_local]` attribute.
|
||||
|
@ -543,6 +549,7 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||
}
|
||||
|
||||
/// Returns `true` if the node pointed to by `def_id` is a mutable `static` item.
|
||||
#[inline]
|
||||
pub fn is_mutable_static(self, def_id: DefId) -> bool {
|
||||
self.static_mutability(def_id) == Some(hir::Mutability::Mut)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue