Skip to content

Commit 8e7609b

Browse files
Propagate rustc_const_unstable to children
1 parent 0a5abca commit 8e7609b

File tree

1 file changed

+54
-11
lines changed

1 file changed

+54
-11
lines changed

src/librustc_passes/stability.rs

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc::session::parse::feature_err;
99
use rustc::session::Session;
1010
use rustc::ty::query::Providers;
1111
use rustc::ty::TyCtxt;
12-
use rustc_attr::{self as attr, Stability};
12+
use rustc_attr::{self as attr, ConstStability, Stability};
1313
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1414
use rustc_errors::struct_span_err;
1515
use rustc_hir as hir;
@@ -41,6 +41,7 @@ struct Annotator<'a, 'tcx> {
4141
tcx: TyCtxt<'tcx>,
4242
index: &'a mut Index<'tcx>,
4343
parent_stab: Option<&'tcx Stability>,
44+
parent_const_stab: Option<&'tcx ConstStability>,
4445
parent_depr: Option<DeprecationEntry>,
4546
in_trait_impl: bool,
4647
}
@@ -64,6 +65,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
6465
}
6566

6667
// This crate explicitly wants staged API.
68+
6769
debug!("annotate(id = {:?}, attrs = {:?})", hir_id, attrs);
6870
if let Some(..) = attr::find_deprecation(&self.tcx.sess.parse_sess, attrs, item_sp) {
6971
self.tcx.sess.span_err(
@@ -72,13 +74,25 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
7274
use `#[rustc_deprecated]` instead",
7375
);
7476
}
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| {
7881
let const_stab = self.tcx.intern_const_stability(const_stab);
7982
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+
}
8093
}
81-
if let Some(mut stab) = stab {
94+
95+
let stab = stab.map(|mut stab| {
8296
// Error if prohibited, or can't inherit anything from a container.
8397
if kind == AnnotationKind::Prohibited
8498
|| (kind == AnnotationKind::Container
@@ -137,18 +151,46 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
137151
}
138152

139153
self.index.stab_map.insert(hir_id, stab);
154+
stab
155+
});
140156

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);
146159
if let Some(stab) = self.parent_stab {
147160
if stab.level.is_unstable() {
148161
self.index.stab_map.insert(hir_id, stab);
149162
}
150163
}
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;
152194
}
153195
}
154196

@@ -388,6 +430,7 @@ fn new_index(tcx: TyCtxt<'tcx>) -> Index<'tcx> {
388430
tcx,
389431
index: &mut index,
390432
parent_stab: None,
433+
parent_const_stab: None,
391434
parent_depr: None,
392435
in_trait_impl: false,
393436
};

0 commit comments

Comments
 (0)