about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan MacKenzie <ecstaticmorse@gmail.com>2020-02-03 12:21:27 -0800
committerDylan MacKenzie <ecstaticmorse@gmail.com>2020-02-18 19:27:58 -0800
commit62ff11f1a4d27a61ea60d309ed8fe738c07ce2ef (patch)
treee7fe97acae5f9f0630d68a8d2a224b769039fea7
parente0e5d82e1677c82d209b214bbfc2cc5705c2336a (diff)
downloadrust-62ff11f1a4d27a61ea60d309ed8fe738c07ce2ef.tar.gz
rust-62ff11f1a4d27a61ea60d309ed8fe738c07ce2ef.zip
Add `is_const_impl_raw` query
-rw-r--r--src/librustc/query/mod.rs8
-rw-r--r--src/librustc_mir/const_eval/fn_queries.rs16
2 files changed, 23 insertions, 1 deletions
diff --git a/src/librustc/query/mod.rs b/src/librustc/query/mod.rs
index 45ab3fc0b85..3cee10f1356 100644
--- a/src/librustc/query/mod.rs
+++ b/src/librustc/query/mod.rs
@@ -279,6 +279,14 @@ rustc_queries! {
             desc { |tcx| "checking if item is const fn: `{}`", tcx.def_path_str(key) }
         }
 
+        /// Returns `true` if this is a const `impl`. **Do not call this function manually.**
+        ///
+        /// This query caches the base data for the `is_const_impl` helper function, which also
+        /// takes into account stability attributes (e.g., `#[rustc_const_unstable]`).
+        query is_const_impl_raw(key: DefId) -> bool {
+            desc { |tcx| "checking if item is const impl: `{}`", tcx.def_path_str(key) }
+        }
+
         query asyncness(key: DefId) -> hir::IsAsync {
             desc { |tcx| "checking if the function is async: `{}`", tcx.def_path_str(key) }
         }
diff --git a/src/librustc_mir/const_eval/fn_queries.rs b/src/librustc_mir/const_eval/fn_queries.rs
index 4144bbc41d2..5706d435f7e 100644
--- a/src/librustc_mir/const_eval/fn_queries.rs
+++ b/src/librustc_mir/const_eval/fn_queries.rs
@@ -3,7 +3,7 @@ use rustc::ty::query::Providers;
 use rustc::ty::TyCtxt;
 use rustc_attr as attr;
 use rustc_hir as hir;
-use rustc_hir::def_id::DefId;
+use rustc_hir::def_id::{DefId, LocalDefId};
 use rustc_span::symbol::Symbol;
 use rustc_target::spec::abi::Abi;
 
@@ -119,6 +119,19 @@ pub fn provide(providers: &mut Providers<'_>) {
         }
     }
 
+    /// Checks whether the given item is an `impl` that has a `const` modifier.
+    fn is_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
+        let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
+        let node = tcx.hir().get(hir_id);
+        matches!(
+            node,
+            hir::Node::Item(hir::Item {
+                kind: hir::ItemKind::Impl { constness: hir::Constness::Const, .. },
+                ..
+            })
+        )
+    }
+
     fn is_promotable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
         is_const_fn(tcx, def_id)
             && match tcx.lookup_const_stability(def_id) {
@@ -148,6 +161,7 @@ pub fn provide(providers: &mut Providers<'_>) {
 
     *providers = Providers {
         is_const_fn_raw,
+        is_const_impl_raw: |tcx, def_id| is_const_impl_raw(tcx, LocalDefId::from_def_id(def_id)),
         is_promotable_const_fn,
         const_fn_is_allowed_fn_ptr,
         ..*providers