Skip to content

Commit d3f981b

Browse files
committed
Move is_const_fn to under TyCtxt
1 parent cdeba02 commit d3f981b

File tree

6 files changed

+26
-26
lines changed

6 files changed

+26
-26
lines changed

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

+1-18
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,6 @@ use rustc_middle::ty::TyCtxt;
66
use rustc_span::symbol::Symbol;
77
use rustc_target::spec::abi::Abi;
88

9-
/// Whether the `def_id` counts as const fn in your current crate, considering all active
10-
/// feature gates
11-
pub fn is_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
12-
tcx.is_const_fn_raw(def_id)
13-
&& match is_unstable_const_fn(tcx, def_id) {
14-
Some(feature_name) => {
15-
// has a `rustc_const_unstable` attribute, check whether the user enabled the
16-
// corresponding feature gate.
17-
tcx.features().declared_lib_features.iter().any(|&(sym, _)| sym == feature_name)
18-
}
19-
// functions without const stability are either stable user written
20-
// const fn or the user is using feature gates and we thus don't
21-
// care what they do
22-
None => true,
23-
}
24-
}
25-
269
/// Whether the `def_id` is an unstable const fn and what feature gate is necessary to enable it
2710
pub fn is_unstable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Symbol> {
2811
if tcx.is_const_fn_raw(def_id) {
@@ -77,7 +60,7 @@ fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
7760
}
7861

7962
fn is_promotable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
80-
is_const_fn(tcx, def_id)
63+
tcx.is_const_fn(def_id)
8164
&& match tcx.lookup_const_stability(def_id) {
8265
Some(stab) => {
8366
if cfg!(debug_assertions) && stab.promotable {

compiler/rustc_const_eval/src/transform/promote_consts.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use rustc_index::vec::{Idx, IndexVec};
2626
use std::cell::Cell;
2727
use std::{cmp, iter, mem};
2828

29-
use crate::const_eval::{is_const_fn, is_unstable_const_fn};
3029
use crate::transform::check_consts::{is_lang_panic_fn, qualifs, ConstCx};
3130
use crate::transform::MirPass;
3231

@@ -656,8 +655,7 @@ impl<'tcx> Validator<'_, 'tcx> {
656655

657656
let is_const_fn = match *fn_ty.kind() {
658657
ty::FnDef(def_id, _) => {
659-
is_const_fn(self.tcx, def_id)
660-
|| is_unstable_const_fn(self.tcx, def_id).is_some()
658+
self.tcx.is_const_fn_raw(def_id)
661659
|| is_lang_panic_fn(self.tcx, def_id)
662660
}
663661
_ => false,
@@ -1079,7 +1077,7 @@ pub fn is_const_fn_in_array_repeat_expression<'tcx>(
10791077
if let ty::FnDef(def_id, _) = *literal.ty().kind() {
10801078
if let Some((destination_place, _)) = destination {
10811079
if destination_place == place {
1082-
if is_const_fn(ccx.tcx, def_id) {
1080+
if ccx.tcx.is_const_fn(def_id) {
10831081
return true;
10841082
}
10851083
}

compiler/rustc_middle/src/ty/context.rs

+20
Original file line numberDiff line numberDiff line change
@@ -2741,6 +2741,26 @@ impl<'tcx> TyCtxt<'tcx> {
27412741
pub fn lifetime_scope(self, id: HirId) -> Option<LifetimeScopeForPath> {
27422742
self.lifetime_scope_map(id.owner).and_then(|mut map| map.remove(&id.local_id))
27432743
}
2744+
2745+
/// Whether the `def_id` counts as const fn in the current crate, considering all active
2746+
/// feature gates
2747+
pub fn is_const_fn(self, def_id: DefId) -> bool {
2748+
if self.is_const_fn_raw(def_id) {
2749+
match self.lookup_const_stability(def_id) {
2750+
Some(stability) if stability.level.is_unstable() => {
2751+
// has a `rustc_const_unstable` attribute, check whether the user enabled the
2752+
// corresponding feature gate.
2753+
self.features().declared_lib_features.iter().any(|&(sym, _)| sym == stability.feature)
2754+
}
2755+
// functions without const stability are either stable user written
2756+
// const fn or the user is using feature gates and we thus don't
2757+
// care what they do
2758+
_ => true,
2759+
}
2760+
} else {
2761+
false
2762+
}
2763+
}
27442764
}
27452765

27462766
impl TyCtxtAt<'tcx> {

src/librustdoc/clean/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ crate mod utils;
1111

1212
use rustc_ast as ast;
1313
use rustc_attr as attr;
14-
use rustc_const_eval::const_eval::{is_const_fn, is_unstable_const_fn};
14+
use rustc_const_eval::const_eval::is_unstable_const_fn;
1515
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1616
use rustc_hir as hir;
1717
use rustc_hir::def::{CtorKind, DefKind, Res};
@@ -787,7 +787,7 @@ fn clean_fn_or_proc_macro(
787787
let mut func = (sig, generics, body_id).clean(cx);
788788
let def_id = item.def_id.to_def_id();
789789
func.header.constness =
790-
if is_const_fn(cx.tcx, def_id) && is_unstable_const_fn(cx.tcx, def_id).is_none() {
790+
if cx.tcx.is_const_fn(def_id) && is_unstable_const_fn(cx.tcx, def_id).is_none() {
791791
hir::Constness::Const
792792
} else {
793793
hir::Constness::NotConst

src/tools/clippy/clippy_utils/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
extern crate rustc_ast;
1919
extern crate rustc_ast_pretty;
2020
extern crate rustc_attr;
21-
extern crate rustc_const_eval;
2221
extern crate rustc_data_structures;
2322
extern crate rustc_errors;
2423
extern crate rustc_hir;

src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ fn check_terminator(
364364
}
365365

366366
fn is_const_fn(tcx: TyCtxt<'_>, def_id: DefId, msrv: Option<&RustcVersion>) -> bool {
367-
rustc_const_eval::const_eval::is_const_fn(tcx, def_id)
367+
tcx.is_const_fn(def_id)
368368
&& tcx.lookup_const_stability(def_id).map_or(true, |const_stab| {
369369
if let rustc_attr::StabilityLevel::Stable { since } = const_stab.level {
370370
// Checking MSRV is manually necessary because `rustc` has no such concept. This entire

0 commit comments

Comments
 (0)