@@ -9,7 +9,7 @@ use rustc::session::parse::feature_err;
9
9
use rustc:: session:: Session ;
10
10
use rustc:: ty:: query:: Providers ;
11
11
use rustc:: ty:: TyCtxt ;
12
- use rustc_attr:: { self as attr, Stability } ;
12
+ use rustc_attr:: { self as attr, ConstStability , Stability } ;
13
13
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
14
14
use rustc_errors:: struct_span_err;
15
15
use rustc_hir as hir;
@@ -41,6 +41,7 @@ struct Annotator<'a, 'tcx> {
41
41
tcx : TyCtxt < ' tcx > ,
42
42
index : & ' a mut Index < ' tcx > ,
43
43
parent_stab : Option < & ' tcx Stability > ,
44
+ parent_const_stab : Option < & ' tcx ConstStability > ,
44
45
parent_depr : Option < DeprecationEntry > ,
45
46
in_trait_impl : bool ,
46
47
}
@@ -64,6 +65,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
64
65
}
65
66
66
67
// This crate explicitly wants staged API.
68
+
67
69
debug ! ( "annotate(id = {:?}, attrs = {:?})" , hir_id, attrs) ;
68
70
if let Some ( ..) = attr:: find_deprecation ( & self . tcx . sess . parse_sess , attrs, item_sp) {
69
71
self . tcx . sess . span_err (
@@ -72,13 +74,25 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
72
74
use `#[rustc_deprecated]` instead",
73
75
) ;
74
76
}
75
- let ( stab, const_stab) =
76
- attr:: find_stability ( & self . tcx . sess . parse_sess , attrs, item_sp) ;
77
- if let Some ( const_stab) = const_stab {
77
+
78
+ let ( stab, const_stab) = attr:: find_stability ( & self . tcx . sess . parse_sess , attrs, item_sp) ;
79
+
80
+ let const_stab = const_stab. map ( |const_stab| {
78
81
let const_stab = self . tcx . intern_const_stability ( const_stab) ;
79
82
self . index . const_stab_map . insert ( hir_id, const_stab) ;
83
+ const_stab
84
+ } ) ;
85
+
86
+ if const_stab. is_none ( ) {
87
+ debug ! ( "annotate: const_stab not found, parent = {:?}" , self . parent_const_stab) ;
88
+ if let Some ( parent) = self . parent_const_stab {
89
+ if parent. level . is_unstable ( ) {
90
+ self . index . const_stab_map . insert ( hir_id, parent) ;
91
+ }
92
+ }
80
93
}
81
- if let Some ( mut stab) = stab {
94
+
95
+ let stab = stab. map ( |mut stab| {
82
96
// Error if prohibited, or can't inherit anything from a container.
83
97
if kind == AnnotationKind :: Prohibited
84
98
|| ( kind == AnnotationKind :: Container
@@ -137,18 +151,46 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
137
151
}
138
152
139
153
self . index . stab_map . insert ( hir_id, stab) ;
154
+ stab
155
+ } ) ;
140
156
141
- let orig_parent_stab = replace ( & mut self . parent_stab , Some ( stab) ) ;
142
- visit_children ( self ) ;
143
- self . parent_stab = orig_parent_stab;
144
- } else {
145
- debug ! ( "annotate: not found, parent = {:?}" , self . parent_stab) ;
157
+ if stab. is_none ( ) {
158
+ debug ! ( "annotate: stab not found, parent = {:?}" , self . parent_stab) ;
146
159
if let Some ( stab) = self . parent_stab {
147
160
if stab. level . is_unstable ( ) {
148
161
self . index . stab_map . insert ( hir_id, stab) ;
149
162
}
150
163
}
151
- visit_children ( self ) ;
164
+ }
165
+
166
+ self . recurse_with_stability_attrs ( stab, const_stab, visit_children) ;
167
+ }
168
+
169
+ fn recurse_with_stability_attrs (
170
+ & mut self ,
171
+ stab : Option < & ' tcx Stability > ,
172
+ const_stab : Option < & ' tcx ConstStability > ,
173
+ f : impl FnOnce ( & mut Self ) ,
174
+ ) {
175
+ // These will be `Some` if this item changes the corresponding stability attribute.
176
+ let mut replaced_parent_stab = None ;
177
+ let mut replaced_parent_const_stab = None ;
178
+
179
+ if let Some ( stab) = stab {
180
+ replaced_parent_stab = Some ( replace ( & mut self . parent_stab , Some ( stab) ) ) ;
181
+ }
182
+ if let Some ( const_stab) = const_stab {
183
+ replaced_parent_const_stab =
184
+ Some ( replace ( & mut self . parent_const_stab , Some ( const_stab) ) ) ;
185
+ }
186
+
187
+ f ( self ) ;
188
+
189
+ if let Some ( orig_parent_stab) = replaced_parent_stab {
190
+ self . parent_stab = orig_parent_stab;
191
+ }
192
+ if let Some ( orig_parent_const_stab) = replaced_parent_const_stab {
193
+ self . parent_const_stab = orig_parent_const_stab;
152
194
}
153
195
}
154
196
@@ -388,6 +430,7 @@ fn new_index(tcx: TyCtxt<'tcx>) -> Index<'tcx> {
388
430
tcx,
389
431
index : & mut index,
390
432
parent_stab : None ,
433
+ parent_const_stab : None ,
391
434
parent_depr : None ,
392
435
in_trait_impl : false ,
393
436
} ;
0 commit comments