2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
2021-09-01 08:19:58 +00:00
|
|
|
use rustc_hir::def_id::DefId;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::ty::query::Providers;
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::symbol::Symbol;
|
2019-06-14 16:55:36 +03:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2018-08-31 13:50:14 +02:00
|
|
|
|
2020-01-01 18:06:00 +01:00
|
|
|
/// Whether the `def_id` is an unstable const fn and what feature gate is necessary to enable it
|
|
|
|
pub fn is_unstable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Symbol> {
|
|
|
|
if tcx.is_const_fn_raw(def_id) {
|
|
|
|
let const_stab = tcx.lookup_const_stability(def_id)?;
|
|
|
|
if const_stab.level.is_unstable() { Some(const_stab.feature) } else { None }
|
|
|
|
} 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
|
|
|
|
2020-02-18 15:45:14 -08:00
|
|
|
pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
|
2021-09-01 08:19:58 +00:00
|
|
|
let parent_id = tcx.hir().get_parent_node(hir_id);
|
|
|
|
matches!(
|
|
|
|
tcx.hir().get(parent_id),
|
|
|
|
hir::Node::Item(hir::Item {
|
|
|
|
kind: hir::ItemKind::Impl(hir::Impl { constness: hir::Constness::Const, .. }),
|
|
|
|
..
|
|
|
|
})
|
|
|
|
)
|
2020-02-18 15:45:14 -08:00
|
|
|
}
|
2019-06-14 16:55:36 +03:00
|
|
|
|
2020-02-18 15:45:14 -08:00
|
|
|
/// Checks whether the function has a `const` modifier or, in case it is an intrinsic, whether
|
2020-07-07 11:12:44 -04:00
|
|
|
/// said intrinsic has a `rustc_const_{un,}stable` attribute.
|
2020-02-18 15:45:14 -08:00
|
|
|
fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
|
2020-08-12 12:22:56 +02:00
|
|
|
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
|
2019-06-14 16:55:36 +03:00
|
|
|
|
2020-02-18 15:45:14 -08:00
|
|
|
let node = tcx.hir().get(hir_id);
|
2020-02-03 14:57:45 -08:00
|
|
|
|
2020-04-09 14:56:19 -07:00
|
|
|
if let hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) =
|
|
|
|
node
|
|
|
|
{
|
|
|
|
// Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other
|
|
|
|
// foreign items cannot be evaluated at compile-time.
|
|
|
|
if let Abi::RustIntrinsic | Abi::PlatformIntrinsic = tcx.hir().get_foreign_abi(hir_id) {
|
|
|
|
tcx.lookup_const_stability(def_id).is_some()
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2021-10-19 23:31:51 +02:00
|
|
|
} else if let Some(fn_kind) = node.fn_kind() {
|
|
|
|
if fn_kind.constness() == hir::Constness::Const {
|
2020-02-18 15:45:14 -08:00
|
|
|
return true;
|
2018-08-31 13:50:14 +02:00
|
|
|
}
|
2020-02-18 15:45:14 -08: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.
|
|
|
|
is_parent_const_impl_raw(tcx, hir_id)
|
|
|
|
} else {
|
2021-11-06 01:31:32 +01:00
|
|
|
matches!(node, hir::Node::Ctor(_))
|
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 {
|
|
|
|
let sig = tcx.fn_sig(def_id);
|
|
|
|
assert_eq!(
|
|
|
|
sig.unsafety(),
|
|
|
|
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) {
|
2021-09-01 08:19:58 +00:00
|
|
|
*providers = Providers { is_const_fn_raw, is_promotable_const_fn, ..*providers };
|
2018-08-31 13:50:14 +02:00
|
|
|
}
|