diff options
| author | bors <bors@rust-lang.org> | 2022-03-30 22:09:56 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-03-30 22:09:56 +0000 |
| commit | a40c595695bff3bfb373a8a3355ae4bd4ea64608 (patch) | |
| tree | 8b2f8edf18e6557da3fdd81eec8726397af7a9eb /compiler/rustc_middle | |
| parent | c5cf08d37b85f953b132951e868df5b924250fdc (diff) | |
| parent | 21a554caf680b74ee6270d3a61b0336b643c5456 (diff) | |
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.
Diffstat (limited to 'compiler/rustc_middle')
| -rw-r--r-- | compiler/rustc_middle/src/hir/map/mod.rs | 39 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/mir/pretty.rs | 5 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/query/mod.rs | 6 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/mod.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/print/pretty.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/util.rs | 9 |
6 files changed, 30 insertions, 33 deletions
diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 8afa6e70e41..f9d24b68634 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -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 } diff --git a/compiler/rustc_middle/src/mir/pretty.rs b/compiler/rustc_middle/src/mir/pretty.rs index 50d9c8e98b9..b2dd67fb16d 100644 --- a/compiler/rustc_middle/src/mir/pretty.rs +++ b/compiler/rustc_middle/src/mir/pretty.rs @@ -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), diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 6d7e7ef0cb0..bf45269e06f 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -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) } diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index dfc405b1195..7b12ec50b0d 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -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 diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index fecc5d805fc..826000dcecf 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -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)) } _ => { diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index 1b07dc6eb37..7d9a917d04c 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -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) } |
