Skip to content

Commit 7fac4f4

Browse files
committed
Querify lookup_deprecation_entry.
1 parent f748e9e commit 7fac4f4

File tree

2 files changed

+39
-45
lines changed

2 files changed

+39
-45
lines changed

compiler/rustc_middle/src/middle/stability.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ pub struct Index {
7373
pub stab_map: LocalDefIdMap<Stability>,
7474
pub const_stab_map: LocalDefIdMap<ConstStability>,
7575
pub default_body_stab_map: LocalDefIdMap<DefaultBodyStability>,
76-
pub depr_map: LocalDefIdMap<DeprecationEntry>,
7776
/// Mapping from feature name to feature name based on the `implied_by` field of `#[unstable]`
7877
/// attributes. If a `#[unstable(feature = "implier", implied_by = "impliee")]` attribute
7978
/// exists, then this map will have a `impliee -> implier` entry.
@@ -101,10 +100,6 @@ impl Index {
101100
pub fn local_default_body_stability(&self, def_id: LocalDefId) -> Option<DefaultBodyStability> {
102101
self.default_body_stab_map.get(&def_id).copied()
103102
}
104-
105-
pub fn local_deprecation_entry(&self, def_id: LocalDefId) -> Option<DeprecationEntry> {
106-
self.depr_map.get(&def_id).cloned()
107-
}
108103
}
109104

110105
pub fn report_unstable(

compiler/rustc_passes/src/stability.rs

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,40 @@ impl InheritStability {
8989
}
9090
}
9191

92+
fn inherit_deprecation(def_kind: DefKind) -> InheritDeprecation {
93+
match def_kind {
94+
DefKind::LifetimeParam | DefKind::TyParam | DefKind::ConstParam => InheritDeprecation::No,
95+
_ => InheritDeprecation::Yes,
96+
}
97+
}
98+
99+
fn lookup_deprecation_entry(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<DeprecationEntry> {
100+
let attrs = tcx.hir_attrs(tcx.local_def_id_to_hir_id(def_id));
101+
let depr = attrs::find_attr!(attrs,
102+
AttributeKind::Deprecation { deprecation, span: _ } => *deprecation
103+
);
104+
105+
let Some(depr) = depr else {
106+
if inherit_deprecation(tcx.def_kind(def_id)).yes() {
107+
let parent_id = tcx.opt_local_parent(def_id)?;
108+
let parent_depr = tcx.lookup_deprecation_entry(parent_id)?;
109+
info!("tagging child {:?} as deprecated from parent", def_id);
110+
return Some(parent_depr);
111+
}
112+
113+
return None;
114+
};
115+
116+
// `Deprecation` is just two pointers, no need to intern it
117+
Some(DeprecationEntry::local(depr, def_id))
118+
}
119+
92120
/// A private tree-walker for producing an `Index`.
93121
struct Annotator<'a, 'tcx> {
94122
tcx: TyCtxt<'tcx>,
95123
index: &'a mut Index,
96124
parent_stab: Option<Stability>,
97125
parent_const_stab: Option<ConstStability>,
98-
parent_depr: Option<DeprecationEntry>,
99126
in_trait_impl: bool,
100127
}
101128

@@ -119,24 +146,9 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
119146
let attrs = self.tcx.hir_attrs(self.tcx.local_def_id_to_hir_id(def_id));
120147
debug!("annotate(id = {:?}, attrs = {:?})", def_id, attrs);
121148

122-
let depr = attrs::find_attr!(attrs, AttributeKind::Deprecation{deprecation, span} => (*deprecation, *span));
149+
let depr = self.tcx.lookup_deprecation_entry(def_id);
123150
let const_stability_indirect = find_attr!(attrs, AttributeKind::ConstStabilityIndirect);
124151

125-
let mut is_deprecated = false;
126-
if let Some((depr, _)) = &depr {
127-
is_deprecated = true;
128-
129-
// `Deprecation` is just two pointers, no need to intern it
130-
let depr_entry = DeprecationEntry::local(*depr, def_id);
131-
self.index.depr_map.insert(def_id, depr_entry);
132-
} else if let Some(parent_depr) = self.parent_depr {
133-
if inherit_deprecation.yes() {
134-
is_deprecated = true;
135-
info!("tagging child {:?} as deprecated from parent", def_id);
136-
self.index.depr_map.insert(def_id, parent_depr);
137-
}
138-
}
139-
140152
if !self.tcx.features().staged_api() {
141153
// Propagate unstability. This can happen even for non-staged-api crates in case
142154
// -Zforce-unstable-if-unmarked is set.
@@ -152,12 +164,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
152164
}
153165
}
154166

155-
self.recurse_with_stability_attrs(
156-
depr.map(|(d, _)| DeprecationEntry::local(d, def_id)),
157-
None,
158-
None,
159-
visit_children,
160-
);
167+
self.recurse_with_stability_attrs(None, None, visit_children);
161168
return;
162169
}
163170

@@ -166,11 +173,14 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
166173
let body_stab =
167174
attrs::find_attr!(attrs, AttributeKind::BodyStability { stability, .. } => *stability);
168175

169-
if let Some((depr, span)) = &depr
170-
&& depr.is_since_rustc_version()
176+
if let Some(depr) = &depr
177+
&& depr.attr.is_since_rustc_version()
171178
&& stab.is_none()
179+
&& let Some(depr_span) = attrs::find_attr!(attrs,
180+
AttributeKind::Deprecation { span, .. } => *span
181+
)
172182
{
173-
self.tcx.dcx().emit_err(errors::DeprecatedAttribute { span: *span });
183+
self.tcx.dcx().emit_err(errors::DeprecatedAttribute { span: depr_span });
174184
}
175185

176186
if let Some(body_stab) = body_stab {
@@ -183,7 +193,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
183193
let stab = stab.map(|(stab, span)| {
184194
// Error if prohibited, or can't inherit anything from a container.
185195
if kind == AnnotationKind::Prohibited
186-
|| (kind == AnnotationKind::Container && stab.level.is_stable() && is_deprecated)
196+
|| (kind == AnnotationKind::Container && stab.level.is_stable() && depr.is_some())
187197
{
188198
self.tcx.dcx().emit_err(errors::UselessStability { span, item_sp });
189199
}
@@ -195,7 +205,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
195205
if let (
196206
&Some(DeprecatedSince::RustcVersion(dep_since)),
197207
&attrs::StabilityLevel::Stable { since: stab_since, .. },
198-
) = (&depr.as_ref().map(|(d, _)| d.since), &stab.level)
208+
) = (&depr.as_ref().map(|d| d.attr.since), &stab.level)
199209
{
200210
match stab_since {
201211
StableSince::Current => {
@@ -343,7 +353,6 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
343353
}
344354

345355
self.recurse_with_stability_attrs(
346-
depr.map(|(d, _)| DeprecationEntry::local(d, def_id)),
347356
stab,
348357
inherit_const_stability.yes().then_some(const_stab).flatten(),
349358
visit_children,
@@ -352,19 +361,14 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
352361

353362
fn recurse_with_stability_attrs(
354363
&mut self,
355-
depr: Option<DeprecationEntry>,
356364
stab: Option<Stability>,
357365
const_stab: Option<ConstStability>,
358366
f: impl FnOnce(&mut Self),
359367
) {
360368
// These will be `Some` if this item changes the corresponding stability attribute.
361-
let mut replaced_parent_depr = None;
362369
let mut replaced_parent_stab = None;
363370
let mut replaced_parent_const_stab = None;
364371

365-
if let Some(depr) = depr {
366-
replaced_parent_depr = Some(replace(&mut self.parent_depr, Some(depr)));
367-
}
368372
if let Some(stab) = stab {
369373
replaced_parent_stab = Some(replace(&mut self.parent_stab, Some(stab)));
370374
}
@@ -375,9 +379,6 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
375379

376380
f(self);
377381

378-
if let Some(orig_parent_depr) = replaced_parent_depr {
379-
self.parent_depr = orig_parent_depr;
380-
}
381382
if let Some(orig_parent_stab) = replaced_parent_stab {
382383
self.parent_stab = orig_parent_stab;
383384
}
@@ -679,7 +680,6 @@ fn stability_index(tcx: TyCtxt<'_>, (): ()) -> Index {
679680
stab_map: Default::default(),
680681
const_stab_map: Default::default(),
681682
default_body_stab_map: Default::default(),
682-
depr_map: Default::default(),
683683
implications: Default::default(),
684684
};
685685

@@ -689,7 +689,6 @@ fn stability_index(tcx: TyCtxt<'_>, (): ()) -> Index {
689689
index: &mut index,
690690
parent_stab: None,
691691
parent_const_stab: None,
692-
parent_depr: None,
693692
in_trait_impl: false,
694693
};
695694

@@ -741,7 +740,7 @@ pub(crate) fn provide(providers: &mut Providers) {
741740
lookup_stability: |tcx, id| tcx.stability().local_stability(id),
742741
lookup_const_stability: |tcx, id| tcx.stability().local_const_stability(id),
743742
lookup_default_body_stability: |tcx, id| tcx.stability().local_default_body_stability(id),
744-
lookup_deprecation_entry: |tcx, id| tcx.stability().local_deprecation_entry(id),
743+
lookup_deprecation_entry,
745744
..*providers
746745
};
747746
}

0 commit comments

Comments
 (0)