@@ -9,8 +9,6 @@ use rustc_hir::def_id::{DefId, LocalDefId};
9
9
use rustc_middle:: mir;
10
10
use rustc_middle:: ty:: { self , TyCtxt } ;
11
11
12
- use std:: fmt;
13
-
14
12
pub use self :: qualifs:: Qualif ;
15
13
16
14
mod ops;
@@ -25,7 +23,7 @@ pub struct ConstCx<'mir, 'tcx> {
25
23
pub tcx : TyCtxt < ' tcx > ,
26
24
pub def_id : DefId ,
27
25
pub param_env : ty:: ParamEnv < ' tcx > ,
28
- pub const_kind : Option < ConstKind > ,
26
+ pub const_kind : Option < hir :: ConstContext > ,
29
27
}
30
28
31
29
impl ConstCx < ' mir , ' tcx > {
@@ -40,78 +38,18 @@ impl ConstCx<'mir, 'tcx> {
40
38
body : & ' mir mir:: Body < ' tcx > ,
41
39
param_env : ty:: ParamEnv < ' tcx > ,
42
40
) -> Self {
43
- let const_kind = ConstKind :: for_item ( tcx, def_id) ;
44
-
41
+ let const_kind = tcx. hir ( ) . body_const_context ( def_id) ;
45
42
ConstCx { body, tcx, def_id : def_id. to_def_id ( ) , param_env, const_kind }
46
43
}
47
44
48
45
/// Returns the kind of const context this `Item` represents (`const`, `static`, etc.).
49
46
///
50
47
/// Panics if this `Item` is not const.
51
- pub fn const_kind ( & self ) -> ConstKind {
48
+ pub fn const_kind ( & self ) -> hir :: ConstContext {
52
49
self . const_kind . expect ( "`const_kind` must not be called on a non-const fn" )
53
50
}
54
51
}
55
52
56
- /// The kinds of items which require compile-time evaluation.
57
- #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
58
- pub enum ConstKind {
59
- /// A `static` item.
60
- Static ,
61
- /// A `static mut` item.
62
- StaticMut ,
63
- /// A `const fn` item.
64
- ConstFn ,
65
- /// A `const` item or an anonymous constant (e.g. in array lengths).
66
- Const ,
67
- }
68
-
69
- impl ConstKind {
70
- /// Returns the validation mode for the item with the given `DefId`, or `None` if this item
71
- /// does not require validation (e.g. a non-const `fn`).
72
- pub fn for_item ( tcx : TyCtxt < ' tcx > , def_id : LocalDefId ) -> Option < Self > {
73
- use hir:: BodyOwnerKind as HirKind ;
74
-
75
- let hir_id = tcx. hir ( ) . as_local_hir_id ( def_id) ;
76
-
77
- let mode = match tcx. hir ( ) . body_owner_kind ( hir_id) {
78
- HirKind :: Closure => return None ,
79
-
80
- // Note: this is deliberately checking for `is_const_fn_raw`, as the `is_const_fn`
81
- // checks take into account the `rustc_const_unstable` attribute combined with enabled
82
- // feature gates. Otherwise, const qualification would _not check_ whether this
83
- // function body follows the `const fn` rules, as an unstable `const fn` would
84
- // be considered "not const". More details are available in issue #67053.
85
- HirKind :: Fn if tcx. is_const_fn_raw ( def_id) => ConstKind :: ConstFn ,
86
- HirKind :: Fn => return None ,
87
-
88
- HirKind :: Const => ConstKind :: Const ,
89
-
90
- HirKind :: Static ( hir:: Mutability :: Not ) => ConstKind :: Static ,
91
- HirKind :: Static ( hir:: Mutability :: Mut ) => ConstKind :: StaticMut ,
92
- } ;
93
-
94
- Some ( mode)
95
- }
96
-
97
- pub fn is_static ( self ) -> bool {
98
- match self {
99
- ConstKind :: Static | ConstKind :: StaticMut => true ,
100
- ConstKind :: ConstFn | ConstKind :: Const => false ,
101
- }
102
- }
103
- }
104
-
105
- impl fmt:: Display for ConstKind {
106
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
107
- match * self {
108
- ConstKind :: Const => write ! ( f, "constant" ) ,
109
- ConstKind :: Static | ConstKind :: StaticMut => write ! ( f, "static" ) ,
110
- ConstKind :: ConstFn => write ! ( f, "constant function" ) ,
111
- }
112
- }
113
- }
114
-
115
53
/// Returns `true` if this `DefId` points to one of the official `panic` lang items.
116
54
pub fn is_lang_panic_fn ( tcx : TyCtxt < ' tcx > , def_id : DefId ) -> bool {
117
55
Some ( def_id) == tcx. lang_items ( ) . panic_fn ( ) || Some ( def_id) == tcx. lang_items ( ) . begin_panic_fn ( )
0 commit comments