Skip to content

Commit 36f2045

Browse files
committed
Implement stability_implications without a visitor.
1 parent 498ae9f commit 36f2045

File tree

2 files changed

+72
-80
lines changed

2 files changed

+72
-80
lines changed

compiler/rustc_hir/src/def.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,11 @@ impl DefKind {
305305
matches!(self, DefKind::Mod | DefKind::Enum | DefKind::Trait)
306306
}
307307

308+
#[inline]
309+
pub fn is_adt(self) -> bool {
310+
matches!(self, DefKind::Struct | DefKind::Union | DefKind::Enum)
311+
}
312+
308313
#[inline]
309314
pub fn is_fn_like(self) -> bool {
310315
matches!(
@@ -313,6 +318,42 @@ impl DefKind {
313318
)
314319
}
315320

321+
pub fn has_generics(self) -> bool {
322+
match self {
323+
DefKind::AnonConst
324+
| DefKind::AssocConst
325+
| DefKind::AssocFn
326+
| DefKind::AssocTy
327+
| DefKind::Closure
328+
| DefKind::Const
329+
| DefKind::Ctor(..)
330+
| DefKind::Enum
331+
| DefKind::Field
332+
| DefKind::Fn
333+
| DefKind::ForeignTy
334+
| DefKind::Impl { .. }
335+
| DefKind::InlineConst
336+
| DefKind::OpaqueTy
337+
| DefKind::Static { .. }
338+
| DefKind::Struct
339+
| DefKind::SyntheticCoroutineBody
340+
| DefKind::Trait
341+
| DefKind::TraitAlias
342+
| DefKind::TyAlias
343+
| DefKind::Union
344+
| DefKind::Variant => true,
345+
DefKind::ConstParam
346+
| DefKind::ExternCrate
347+
| DefKind::ForeignMod
348+
| DefKind::GlobalAsm
349+
| DefKind::LifetimeParam
350+
| DefKind::Macro(_)
351+
| DefKind::Mod
352+
| DefKind::TyParam
353+
| DefKind::Use => false,
354+
}
355+
}
356+
316357
/// Whether `query get_codegen_attrs` should be used with this definition.
317358
pub fn has_codegen_attrs(self) -> bool {
318359
match self {

compiler/rustc_passes/src/stability.rs

Lines changed: 31 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -267,93 +267,51 @@ fn lookup_const_stability(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<ConstSt
267267
None
268268
}
269269

270-
/// A private tree-walker for producing an `Index`.
271-
struct Annotator<'tcx> {
272-
tcx: TyCtxt<'tcx>,
273-
implications: UnordMap<Symbol, Symbol>,
274-
}
275-
276-
impl<'tcx> Annotator<'tcx> {
277-
/// Determine the stability for a node based on its attributes and inherited stability. The
278-
/// stability is recorded in the index and used as the parent. If the node is a function,
279-
/// `fn_sig` is its signature.
280-
#[instrument(level = "trace", skip(self))]
281-
fn annotate(&mut self, def_id: LocalDefId) {
282-
if !self.tcx.features().staged_api() {
283-
return;
284-
}
270+
fn stability_implications(tcx: TyCtxt<'_>, LocalCrate: LocalCrate) -> UnordMap<Symbol, Symbol> {
271+
let mut implications = UnordMap::default();
285272

286-
if let Some(stability) = self.tcx.lookup_stability(def_id)
273+
let mut register_implication = |def_id| {
274+
if let Some(stability) = tcx.lookup_stability(def_id)
287275
&& let StabilityLevel::Unstable { implied_by: Some(implied_by), .. } = stability.level
288276
{
289-
self.implications.insert(implied_by, stability.feature);
277+
implications.insert(implied_by, stability.feature);
290278
}
291279

292-
if let Some(stability) = self.tcx.lookup_const_stability(def_id)
280+
if let Some(stability) = tcx.lookup_const_stability(def_id)
293281
&& let StabilityLevel::Unstable { implied_by: Some(implied_by), .. } = stability.level
294282
{
295-
self.implications.insert(implied_by, stability.feature);
283+
implications.insert(implied_by, stability.feature);
296284
}
297-
}
298-
}
299-
300-
impl<'tcx> Visitor<'tcx> for Annotator<'tcx> {
301-
/// Because stability levels are scoped lexically, we want to walk
302-
/// nested items in the context of the outer item, so enable
303-
/// deep-walking.
304-
type NestedFilter = nested_filter::All;
305-
306-
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
307-
self.tcx
308-
}
285+
};
309286

310-
fn visit_item(&mut self, i: &'tcx Item<'tcx>) {
311-
match i.kind {
312-
hir::ItemKind::Struct(_, _, ref sd) => {
313-
if let Some(ctor_def_id) = sd.ctor_def_id() {
314-
self.annotate(ctor_def_id);
287+
if tcx.features().staged_api() {
288+
register_implication(CRATE_DEF_ID);
289+
for def_id in tcx.hir_crate_items(()).definitions() {
290+
register_implication(def_id);
291+
let def_kind = tcx.def_kind(def_id);
292+
if def_kind.is_adt() {
293+
let adt = tcx.adt_def(def_id);
294+
for variant in adt.variants() {
295+
if variant.def_id != def_id.to_def_id() {
296+
register_implication(variant.def_id.expect_local());
297+
}
298+
for field in &variant.fields {
299+
register_implication(field.did.expect_local());
300+
}
301+
if let Some(ctor_def_id) = variant.ctor_def_id() {
302+
register_implication(ctor_def_id.expect_local())
303+
}
304+
}
305+
}
306+
if def_kind.has_generics() {
307+
for param in tcx.generics_of(def_id).own_params.iter() {
308+
register_implication(param.def_id.expect_local())
315309
}
316310
}
317-
_ => {}
318-
}
319-
320-
self.annotate(i.owner_id.def_id);
321-
intravisit::walk_item(self, i)
322-
}
323-
324-
fn visit_trait_item(&mut self, ti: &'tcx hir::TraitItem<'tcx>) {
325-
self.annotate(ti.owner_id.def_id);
326-
intravisit::walk_trait_item(self, ti);
327-
}
328-
329-
fn visit_impl_item(&mut self, ii: &'tcx hir::ImplItem<'tcx>) {
330-
self.annotate(ii.owner_id.def_id);
331-
intravisit::walk_impl_item(self, ii);
332-
}
333-
334-
fn visit_variant(&mut self, var: &'tcx Variant<'tcx>) {
335-
self.annotate(var.def_id);
336-
if let Some(ctor_def_id) = var.data.ctor_def_id() {
337-
self.annotate(ctor_def_id);
338311
}
339-
340-
intravisit::walk_variant(self, var)
341312
}
342313

343-
fn visit_field_def(&mut self, s: &'tcx FieldDef<'tcx>) {
344-
self.annotate(s.def_id);
345-
intravisit::walk_field_def(self, s);
346-
}
347-
348-
fn visit_foreign_item(&mut self, i: &'tcx hir::ForeignItem<'tcx>) {
349-
self.annotate(i.owner_id.def_id);
350-
intravisit::walk_foreign_item(self, i);
351-
}
352-
353-
fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam<'tcx>) {
354-
self.annotate(p.def_id);
355-
intravisit::walk_generic_param(self, p);
356-
}
314+
implications
357315
}
358316

359317
struct MissingStabilityAnnotations<'tcx> {
@@ -565,13 +523,6 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
565523
}
566524
}
567525

568-
fn stability_implications(tcx: TyCtxt<'_>, LocalCrate: LocalCrate) -> UnordMap<Symbol, Symbol> {
569-
let mut annotator = Annotator { tcx, implications: Default::default() };
570-
annotator.annotate(CRATE_DEF_ID);
571-
tcx.hir_walk_toplevel_module(&mut annotator);
572-
annotator.implications
573-
}
574-
575526
/// Cross-references the feature names of unstable APIs with enabled
576527
/// features and possibly prints errors.
577528
fn check_mod_unstable_api_usage(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {

0 commit comments

Comments
 (0)