2023-02-08 13:25:38 +00:00
|
|
|
use rustc_attr as attr;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
2022-03-13 11:12:50 +01:00
|
|
|
use rustc_hir::def::DefKind;
|
2021-10-20 20:59:15 +02:00
|
|
|
use rustc_hir::def_id::{DefId, LocalDefId};
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::ty::query::Providers;
|
2023-02-22 19:51:17 +04:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::symbol::Symbol;
|
2018-08-31 13:50:14 +02:00
|
|
|
|
2023-02-08 13:25:38 +00:00
|
|
|
/// Whether the `def_id` is an unstable const fn and what feature gate(s) are necessary to enable
|
|
|
|
/// it.
|
|
|
|
pub fn is_unstable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<(Symbol, Option<Symbol>)> {
|
2020-01-01 18:06:00 +01:00
|
|
|
if tcx.is_const_fn_raw(def_id) {
|
|
|
|
let const_stab = tcx.lookup_const_stability(def_id)?;
|
2023-02-08 13:25:38 +00:00
|
|
|
match const_stab.level {
|
|
|
|
attr::StabilityLevel::Unstable { implied_by, .. } => {
|
|
|
|
Some((const_stab.feature, implied_by))
|
|
|
|
}
|
|
|
|
attr::StabilityLevel::Stable { .. } => None,
|
|
|
|
}
|
2020-01-01 18:06:00 +01:00
|
|
|
} else {
|
|
|
|
None
|
2018-08-31 13:50:14 +02:00
|
|
|
}
|
2020-01-01 18:06:00 +01:00
|
|
|
}
|
2018-08-31 13:50:14 +02:00
|
|
|
|
2021-10-20 20:59:15 +02:00
|
|
|
pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
|
2022-04-25 22:08:45 +03:00
|
|
|
let parent_id = tcx.local_parent(def_id);
|
2023-02-12 18:26:47 +00:00
|
|
|
matches!(tcx.def_kind(parent_id), DefKind::Impl { .. })
|
|
|
|
&& tcx.constness(parent_id) == hir::Constness::Const
|
2020-02-18 15:45:14 -08:00
|
|
|
}
|
2019-06-14 16:55:36 +03:00
|
|
|
|
2022-06-15 20:54:43 +10:00
|
|
|
/// Checks whether an item is considered to be `const`. If it is a constructor, it is const. If
|
|
|
|
/// it is a trait impl/function, return if it has a `const` modifier. If it is an intrinsic,
|
|
|
|
/// report whether said intrinsic has a `rustc_const_{un,}stable` attribute. Otherwise, return
|
|
|
|
/// `Constness::NotConst`.
|
2023-03-13 18:54:05 +00:00
|
|
|
fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
|
2022-10-19 18:34:01 +00:00
|
|
|
let node = tcx.hir().get_by_def_id(def_id);
|
2022-10-09 06:54:00 +00:00
|
|
|
|
2022-10-19 18:34:01 +00:00
|
|
|
match node {
|
|
|
|
hir::Node::Ctor(_) => hir::Constness::Const,
|
|
|
|
hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(impl_), .. }) => impl_.constness,
|
2022-03-13 11:12:50 +01:00
|
|
|
hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) => {
|
|
|
|
// Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other
|
|
|
|
// foreign items cannot be evaluated at compile-time.
|
2022-05-13 13:50:21 +00:00
|
|
|
let is_const = if tcx.is_intrinsic(def_id) {
|
2022-03-13 11:12:50 +01:00
|
|
|
tcx.lookup_const_stability(def_id).is_some()
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
if is_const { hir::Constness::Const } else { hir::Constness::NotConst }
|
2018-08-31 13:50:14 +02:00
|
|
|
}
|
2022-12-21 16:32:16 +00:00
|
|
|
hir::Node::Expr(e) if let hir::ExprKind::Closure(c) = e.kind => c.constness,
|
2022-10-19 18:34:01 +00:00
|
|
|
_ => {
|
|
|
|
if let Some(fn_kind) = node.fn_kind() {
|
|
|
|
if fn_kind.constness() == hir::Constness::Const {
|
|
|
|
return hir::Constness::Const;
|
|
|
|
}
|
2020-02-18 15:45:14 -08:00
|
|
|
|
2022-10-19 18:34:01 +00:00
|
|
|
// If the function itself is not annotated with `const`, it may still be a `const fn`
|
|
|
|
// if it resides in a const trait impl.
|
|
|
|
let is_const = is_parent_const_impl_raw(tcx, def_id);
|
|
|
|
if is_const { hir::Constness::Const } else { hir::Constness::NotConst }
|
|
|
|
} else {
|
|
|
|
hir::Constness::NotConst
|
2022-03-13 11:12:50 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-31 13:50:14 +02:00
|
|
|
}
|
2020-02-18 15:45:14 -08:00
|
|
|
}
|
2018-08-31 13:50:14 +02:00
|
|
|
|
2020-02-18 15:45:14 -08:00
|
|
|
fn is_promotable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
|
2021-09-15 10:03:03 +00:00
|
|
|
tcx.is_const_fn(def_id)
|
2020-02-18 15:45:14 -08:00
|
|
|
&& match tcx.lookup_const_stability(def_id) {
|
|
|
|
Some(stab) => {
|
|
|
|
if cfg!(debug_assertions) && stab.promotable {
|
2023-01-18 16:52:47 -07:00
|
|
|
let sig = tcx.fn_sig(def_id);
|
2020-02-18 15:45:14 -08:00
|
|
|
assert_eq!(
|
2023-01-18 15:43:20 -07:00
|
|
|
sig.skip_binder().unsafety(),
|
2020-02-18 15:45:14 -08:00
|
|
|
hir::Unsafety::Normal,
|
|
|
|
"don't mark const unsafe fns as promotable",
|
|
|
|
// https://github.com/rust-lang/rust/pull/53851#issuecomment-418760682
|
|
|
|
);
|
2018-08-31 13:50:14 +02:00
|
|
|
}
|
2020-02-18 15:45:14 -08:00
|
|
|
stab.promotable
|
2019-12-24 17:38:22 -05:00
|
|
|
}
|
2020-02-18 15:45:14 -08:00
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
}
|
2018-08-31 13:50:14 +02:00
|
|
|
|
2020-07-05 23:00:14 +03:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
2022-06-15 20:54:43 +10:00
|
|
|
*providers = Providers { constness, is_promotable_const_fn, ..*providers };
|
2018-08-31 13:50:14 +02:00
|
|
|
}
|