]> git.lizzy.rs Git - rust.git/commitdiff
Introduce query `static_mutability`
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Fri, 19 Apr 2019 20:32:26 +0000 (23:32 +0300)
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Sun, 21 Apr 2019 11:16:44 +0000 (14:16 +0300)
src/librustc/query/mod.rs
src/librustc/ty/util.rs
src/librustc_metadata/cstore_impl.rs
src/librustc_metadata/decoder.rs
src/librustc_typeck/collect.rs

index bc9aaf870ceed2b576f7b196b96c6aadc874d190..8c1e345cdaec5c94e6b64f91a39b430c660eef5c 100644 (file)
         /// 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> {
index ccead14e76b23ea68da5e34d49f32d69ce5cdca8..d808387e269fd594fbe7e21da16bb245de22a787 100644 (file)
@@ -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};
@@ -615,32 +614,7 @@ pub fn empty_substs_for_def_id(self, item_def_id: DefId) -> SubstsRef<'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
-            }
-        }
+        self.static_mutability(def_id)
     }
 
     /// Expands the given impl trait type, stopping if the type is recursive.
index 10a99e40c419cfa36e3fff52b873c76b6b775f36..1a1b933ccf311683a958838cc42500b7c3ecf6b1 100644 (file)
@@ -137,6 +137,7 @@ fn into_args(self) -> (DefId, DefId) { (self.0.as_def_id(), self.1) }
     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 => {
index 5dade8d9438937bc62f562981e661f1779aa1807..3ac91ae718328be3b8e46d959321082033a7c411 100644 (file)
@@ -1163,6 +1163,16 @@ pub fn is_foreign_item(&self, id: DefIndex) -> bool {
         }
     }
 
+    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>)
index d601c962fc6afd22c753cb6ab049dcba0b7c9793..5c0c2fece3dd5a102976235886bcc5685b44202e 100644 (file)
@@ -78,6 +78,7 @@ pub fn provide(providers: &mut Providers<'_>) {
         impl_trait_ref,
         impl_polarity,
         is_foreign_item,
+        static_mutability,
         codegen_fn_attrs,
         collect_mod_item_types,
         ..*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(mutbl),
+        Some(Node::ForeignItem( &hir::ForeignItem {
+            node: hir::ForeignItemKind::Static(_, mutbl), ..
+        })) => Some(if mutbl { hir::MutMutable } else { hir::MutImmutable }),
+        Some(_) => None,
+        _ => bug!("static_mutability applied to non-local def-id {:?}", def_id),
+    }
+}
+
 fn from_target_feature(
     tcx: TyCtxt<'_, '_, '_>,
     id: DefId,