@@ -25,12 +25,10 @@ pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
25
25
/// report whether said intrinsic has a `rustc_const_{un,}stable` attribute. Otherwise, return
26
26
/// `Constness::NotConst`.
27
27
fn constness ( tcx : TyCtxt < ' _ > , def_id : DefId ) -> hir:: Constness {
28
- let def_id = def_id. expect_local ( ) ;
29
- let node = tcx. hir ( ) . get_by_def_id ( def_id) ;
30
-
31
- match node {
28
+ let hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( def_id. expect_local ( ) ) ;
29
+ match tcx. hir ( ) . get ( hir_id) {
32
30
hir:: Node :: Ctor ( _) => hir:: Constness :: Const ,
33
- hir :: Node :: Item ( hir :: Item { kind : hir :: ItemKind :: Impl ( impl_ ) , .. } ) => impl_ . constness ,
31
+
34
32
hir:: Node :: ForeignItem ( hir:: ForeignItem { kind : hir:: ForeignItemKind :: Fn ( ..) , .. } ) => {
35
33
// Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other
36
34
// foreign items cannot be evaluated at compile-time.
@@ -41,20 +39,62 @@ fn constness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Constness {
41
39
} ;
42
40
if is_const { hir:: Constness :: Const } else { hir:: Constness :: NotConst }
43
41
}
44
- _ => {
45
- if let Some ( fn_kind) = node. fn_kind ( ) {
46
- if fn_kind. constness ( ) == hir:: Constness :: Const {
47
- return hir:: Constness :: Const ;
48
- }
49
42
50
- // If the function itself is not annotated with `const`, it may still be a `const fn`
51
- // if it resides in a const trait impl.
52
- let is_const = is_parent_const_impl_raw ( tcx, def_id) ;
53
- if is_const { hir:: Constness :: Const } else { hir:: Constness :: NotConst }
54
- } else {
55
- hir:: Constness :: NotConst
43
+ hir:: Node :: TraitItem ( hir:: TraitItem { kind : hir:: TraitItemKind :: Fn ( ..) , .. } )
44
+ if tcx. is_const_default_method ( def_id) =>
45
+ {
46
+ hir:: Constness :: Const
47
+ }
48
+
49
+ hir:: Node :: Item ( hir:: Item { kind : hir:: ItemKind :: Const ( ..) , .. } )
50
+ | hir:: Node :: Item ( hir:: Item { kind : hir:: ItemKind :: Static ( ..) , .. } )
51
+ | hir:: Node :: TraitItem ( hir:: TraitItem { kind : hir:: TraitItemKind :: Const ( ..) , .. } )
52
+ | hir:: Node :: AnonConst ( _)
53
+ | hir:: Node :: ImplItem ( hir:: ImplItem { kind : hir:: ImplItemKind :: Const ( ..) , .. } )
54
+ | hir:: Node :: ImplItem ( hir:: ImplItem {
55
+ kind :
56
+ hir:: ImplItemKind :: Fn (
57
+ hir:: FnSig {
58
+ header : hir:: FnHeader { constness : hir:: Constness :: Const , .. } ,
59
+ ..
60
+ } ,
61
+ ..,
62
+ ) ,
63
+ ..
64
+ } ) => hir:: Constness :: Const ,
65
+
66
+ hir:: Node :: ImplItem ( hir:: ImplItem {
67
+ kind : hir:: ImplItemKind :: Type ( ..) | hir:: ImplItemKind :: Fn ( ..) ,
68
+ ..
69
+ } ) => {
70
+ let parent_hir_id = tcx. hir ( ) . get_parent_node ( hir_id) ;
71
+ match tcx. hir ( ) . get ( parent_hir_id) {
72
+ hir:: Node :: Item ( hir:: Item {
73
+ kind : hir:: ItemKind :: Impl ( hir:: Impl { constness, .. } ) ,
74
+ ..
75
+ } ) => * constness,
76
+ _ => span_bug ! (
77
+ tcx. def_span( parent_hir_id. owner) ,
78
+ "impl item's parent node is not an impl" ,
79
+ ) ,
56
80
}
57
81
}
82
+
83
+ hir:: Node :: Item ( hir:: Item {
84
+ kind : hir:: ItemKind :: Fn ( hir:: FnSig { header : hir:: FnHeader { constness, .. } , .. } , ..) ,
85
+ ..
86
+ } )
87
+ | hir:: Node :: TraitItem ( hir:: TraitItem {
88
+ kind :
89
+ hir:: TraitItemKind :: Fn ( hir:: FnSig { header : hir:: FnHeader { constness, .. } , .. } , ..) ,
90
+ ..
91
+ } )
92
+ | hir:: Node :: Item ( hir:: Item {
93
+ kind : hir:: ItemKind :: Impl ( hir:: Impl { constness, .. } ) ,
94
+ ..
95
+ } ) => * constness,
96
+
97
+ _ => hir:: Constness :: NotConst ,
58
98
}
59
99
}
60
100
0 commit comments