|
1 | | -use rustc_hir as hir; |
2 | | -use rustc_hir::def::DefKind; |
3 | 1 | use rustc_hir::def_id::{DefId, LocalDefId}; |
| 2 | +use rustc_hir::{ |
| 3 | + Constness, ExprKind, ForeignItemKind, ImplItem, ImplItemImplKind, ImplItemKind, Item, ItemKind, |
| 4 | + Node, TraitItem, TraitItemKind, VariantData, |
| 5 | +}; |
4 | 6 | use rustc_middle::query::Providers; |
5 | 7 | use rustc_middle::ty::TyCtxt; |
6 | 8 |
|
7 | | -fn parent_impl_or_trait_constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness { |
8 | | - let parent_id = tcx.local_parent(def_id); |
9 | | - match tcx.def_kind(parent_id) { |
10 | | - DefKind::Impl { of_trait: true } => tcx.impl_trait_header(parent_id).constness, |
11 | | - DefKind::Impl { of_trait: false } => tcx.constness(parent_id), |
12 | | - DefKind::Trait => { |
13 | | - if tcx.is_const_trait(parent_id.into()) { |
14 | | - hir::Constness::Const |
15 | | - } else { |
16 | | - hir::Constness::NotConst |
17 | | - } |
18 | | - } |
19 | | - _ => hir::Constness::NotConst, |
20 | | - } |
21 | | -} |
22 | | - |
23 | | -/// Checks whether a function-like definition is considered to be `const`. |
24 | | -fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness { |
| 9 | +/// Checks whether a function-like definition is considered to be `const`. Also stores constness of inherent impls. |
| 10 | +fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Constness { |
25 | 11 | let node = tcx.hir_node_by_def_id(def_id); |
26 | 12 |
|
27 | 13 | match node { |
28 | | - hir::Node::Ctor(hir::VariantData::Tuple(..)) => hir::Constness::Const, |
29 | | - hir::Node::ForeignItem(item) if let hir::ForeignItemKind::Fn(..) = item.kind => { |
| 14 | + Node::Ctor(VariantData::Tuple(..)) => Constness::Const, |
| 15 | + Node::ForeignItem(item) if let ForeignItemKind::Fn(..) = item.kind => { |
30 | 16 | // Foreign functions cannot be evaluated at compile-time. |
31 | | - hir::Constness::NotConst |
| 17 | + Constness::NotConst |
32 | 18 | } |
33 | | - hir::Node::Expr(e) if let hir::ExprKind::Closure(c) = e.kind => c.constness, |
34 | | - hir::Node::Item(i) if let hir::ItemKind::Impl(impl_) = i.kind => impl_.constness, |
35 | | - _ => { |
36 | | - if let Some(fn_kind) = node.fn_kind() { |
37 | | - if fn_kind.constness() == hir::Constness::Const { |
38 | | - return hir::Constness::Const; |
39 | | - } |
40 | | - |
41 | | - // If the function itself is not annotated with `const`, it may still be a `const fn` |
42 | | - // if it resides in a const trait impl. |
43 | | - parent_impl_or_trait_constness(tcx, def_id) |
44 | | - } else { |
45 | | - tcx.dcx().span_bug( |
46 | | - tcx.def_span(def_id), |
47 | | - format!("should not be requesting the constness of items that can't be const: {node:#?}: {:?}", tcx.def_kind(def_id)) |
48 | | - ) |
| 19 | + Node::Expr(e) if let ExprKind::Closure(c) = e.kind => c.constness, |
| 20 | + // FIXME(fee1-dead): extract this one out and rename this query to `fn_constness` so we don't need `is_const_fn` anymore. |
| 21 | + Node::Item(i) if let ItemKind::Impl(impl_) = i.kind => impl_.constness, |
| 22 | + Node::Item(Item { kind: ItemKind::Fn { sig, .. }, .. }) => sig.header.constness, |
| 23 | + Node::ImplItem(ImplItem { |
| 24 | + impl_kind: ImplItemImplKind::Trait { .. }, |
| 25 | + kind: ImplItemKind::Fn(..), |
| 26 | + .. |
| 27 | + }) => tcx.impl_trait_header(tcx.local_parent(def_id)).constness, |
| 28 | + Node::ImplItem(ImplItem { |
| 29 | + impl_kind: ImplItemImplKind::Inherent { .. }, |
| 30 | + kind: ImplItemKind::Fn(sig, _), |
| 31 | + .. |
| 32 | + }) => { |
| 33 | + match sig.header.constness { |
| 34 | + Constness::Const => Constness::Const, |
| 35 | + // inherent impl could be const |
| 36 | + Constness::NotConst => tcx.constness(tcx.local_parent(def_id)), |
49 | 37 | } |
50 | 38 | } |
| 39 | + Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(..), .. }) => tcx.trait_def(tcx.local_parent(def_id)).constness, |
| 40 | + _ => { |
| 41 | + tcx.dcx().span_bug( |
| 42 | + tcx.def_span(def_id), |
| 43 | + format!("should not be requesting the constness of items that can't be const: {node:#?}: {:?}", tcx.def_kind(def_id)) |
| 44 | + ) |
| 45 | + } |
51 | 46 | } |
52 | 47 | } |
53 | 48 |
|
|
0 commit comments