Skip to content

Commit 62ff11f

Browse files
Add is_const_impl_raw query
1 parent e0e5d82 commit 62ff11f

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/librustc/query/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,14 @@ rustc_queries! {
279279
desc { |tcx| "checking if item is const fn: `{}`", tcx.def_path_str(key) }
280280
}
281281

282+
/// Returns `true` if this is a const `impl`. **Do not call this function manually.**
283+
///
284+
/// This query caches the base data for the `is_const_impl` helper function, which also
285+
/// takes into account stability attributes (e.g., `#[rustc_const_unstable]`).
286+
query is_const_impl_raw(key: DefId) -> bool {
287+
desc { |tcx| "checking if item is const impl: `{}`", tcx.def_path_str(key) }
288+
}
289+
282290
query asyncness(key: DefId) -> hir::IsAsync {
283291
desc { |tcx| "checking if the function is async: `{}`", tcx.def_path_str(key) }
284292
}

src/librustc_mir/const_eval/fn_queries.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc::ty::query::Providers;
33
use rustc::ty::TyCtxt;
44
use rustc_attr as attr;
55
use rustc_hir as hir;
6-
use rustc_hir::def_id::DefId;
6+
use rustc_hir::def_id::{DefId, LocalDefId};
77
use rustc_span::symbol::Symbol;
88
use rustc_target::spec::abi::Abi;
99

@@ -119,6 +119,19 @@ pub fn provide(providers: &mut Providers<'_>) {
119119
}
120120
}
121121

122+
/// Checks whether the given item is an `impl` that has a `const` modifier.
123+
fn is_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
124+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
125+
let node = tcx.hir().get(hir_id);
126+
matches!(
127+
node,
128+
hir::Node::Item(hir::Item {
129+
kind: hir::ItemKind::Impl { constness: hir::Constness::Const, .. },
130+
..
131+
})
132+
)
133+
}
134+
122135
fn is_promotable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
123136
is_const_fn(tcx, def_id)
124137
&& match tcx.lookup_const_stability(def_id) {
@@ -148,6 +161,7 @@ pub fn provide(providers: &mut Providers<'_>) {
148161

149162
*providers = Providers {
150163
is_const_fn_raw,
164+
is_const_impl_raw: |tcx, def_id| is_const_impl_raw(tcx, LocalDefId::from_def_id(def_id)),
151165
is_promotable_const_fn,
152166
const_fn_is_allowed_fn_ptr,
153167
..*providers

0 commit comments

Comments
 (0)