Skip to content

Commit 6c6c56e

Browse files
Auto merge of #144873 - cjgillot:implications, r=<try>
Implement `stability_implications` without a visitor.
2 parents f34ba77 + 36f2045 commit 6c6c56e

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
@@ -268,93 +268,51 @@ fn lookup_const_stability(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<ConstSt
268268
None
269269
}
270270

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

287-
if let Some(stability) = self.tcx.lookup_stability(def_id)
274+
let mut register_implication = |def_id| {
275+
if let Some(stability) = tcx.lookup_stability(def_id)
288276
&& let StabilityLevel::Unstable { implied_by: Some(implied_by), .. } = stability.level
289277
{
290-
self.implications.insert(implied_by, stability.feature);
278+
implications.insert(implied_by, stability.feature);
291279
}
292280

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

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

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

360318
struct MissingStabilityAnnotations<'tcx> {
@@ -566,13 +524,6 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
566524
}
567525
}
568526

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

0 commit comments

Comments
 (0)