Skip to content

Commit c75daeb

Browse files
committed
Do not recurse inside lookup_deprecation_entry query.
1 parent 9293136 commit c75daeb

File tree

7 files changed

+52
-36
lines changed

7 files changed

+52
-36
lines changed

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_middle::arena::ArenaAllocatable;
1111
use rustc_middle::bug;
1212
use rustc_middle::metadata::ModChild;
1313
use rustc_middle::middle::exported_symbols::ExportedSymbol;
14-
use rustc_middle::middle::stability::DeprecationEntry;
14+
use rustc_middle::middle::stability::RawDeprecationEntry;
1515
use rustc_middle::query::{ExternProviders, LocalCrate};
1616
use rustc_middle::ty::fast_reject::SimplifiedType;
1717
use rustc_middle::ty::{self, TyCtxt};
@@ -103,10 +103,10 @@ impl<'a, 'tcx, T: Copy + Decodable<DecodeContext<'a, 'tcx>>>
103103
}
104104
}
105105

106-
impl ProcessQueryValue<'_, Option<DeprecationEntry>> for Option<Deprecation> {
106+
impl ProcessQueryValue<'_, RawDeprecationEntry> for Option<Deprecation> {
107107
#[inline(always)]
108-
fn process_decoded(self, _tcx: TyCtxt<'_>, _err: impl Fn() -> !) -> Option<DeprecationEntry> {
109-
self.map(DeprecationEntry::external)
108+
fn process_decoded(self, _tcx: TyCtxt<'_>, _err: impl Fn() -> !) -> RawDeprecationEntry {
109+
RawDeprecationEntry::external(self)
110110
}
111111
}
112112

@@ -268,7 +268,7 @@ provide! { tcx, def_id, other, cdata,
268268
lookup_stability => { table }
269269
lookup_const_stability => { table }
270270
lookup_default_body_stability => { table }
271-
lookup_deprecation_entry => { table }
271+
lookup_deprecation_entry_raw => { table }
272272
params_in_repr => { table }
273273
unused_generic_params => { table_direct }
274274
def_kind => { cdata.def_kind(def_id.index) }

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1794,7 +1794,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
17941794
#[instrument(level = "debug", skip(self))]
17951795
fn encode_deprecation(&mut self, def_id: DefId) {
17961796
if let Some(depr) = self.tcx.lookup_deprecation(def_id) {
1797-
record!(self.tables.lookup_deprecation_entry[def_id] <- depr);
1797+
record!(self.tables.lookup_deprecation_entry_raw[def_id] <- depr);
17981798
}
17991799
}
18001800

compiler/rustc_metadata/src/rmeta/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ define_tables! {
418418
lookup_stability: Table<DefIndex, LazyValue<attr::Stability>>,
419419
lookup_const_stability: Table<DefIndex, LazyValue<attr::ConstStability>>,
420420
lookup_default_body_stability: Table<DefIndex, LazyValue<attr::DefaultBodyStability>>,
421-
lookup_deprecation_entry: Table<DefIndex, LazyValue<attr::Deprecation>>,
421+
lookup_deprecation_entry_raw: Table<DefIndex, LazyValue<attr::Deprecation>>,
422422
explicit_predicates_of: Table<DefIndex, LazyValue<ty::GenericPredicates<'static>>>,
423423
generics_of: Table<DefIndex, LazyValue<ty::Generics>>,
424424
type_of: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, Ty<'static>>>>,

compiler/rustc_middle/src/middle/stability.rs

+29-8
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ pub enum StabilityLevel {
3232
Stable,
3333
}
3434

35-
/// An entry in the `depr_map`.
35+
#[derive(Copy, Clone, HashStable, Debug, Encodable, Decodable)]
36+
pub enum RawDeprecationEntry {
37+
Explicit(DeprecationEntry),
38+
Inherited,
39+
None,
40+
}
41+
3642
#[derive(Copy, Clone, HashStable, Debug, Encodable, Decodable)]
3743
pub struct DeprecationEntry {
3844
/// The metadata of the attribute associated with this entry.
@@ -42,16 +48,21 @@ pub struct DeprecationEntry {
4248
origin: Option<LocalDefId>,
4349
}
4450

45-
impl DeprecationEntry {
46-
pub fn local(attr: Deprecation, def_id: LocalDefId) -> DeprecationEntry {
47-
DeprecationEntry { attr, origin: Some(def_id) }
51+
impl RawDeprecationEntry {
52+
pub fn local(attr: Deprecation, def_id: LocalDefId) -> RawDeprecationEntry {
53+
RawDeprecationEntry::Explicit(DeprecationEntry { attr, origin: Some(def_id) })
4854
}
4955

50-
pub fn external(attr: Deprecation) -> DeprecationEntry {
51-
DeprecationEntry { attr, origin: None }
56+
pub fn external(attr: Option<Deprecation>) -> RawDeprecationEntry {
57+
match attr {
58+
Some(attr) => RawDeprecationEntry::Explicit(DeprecationEntry { attr, origin: None }),
59+
None => RawDeprecationEntry::None,
60+
}
5261
}
62+
}
5363

54-
pub fn same_origin(&self, other: &DeprecationEntry) -> bool {
64+
impl DeprecationEntry {
65+
fn same_origin(&self, other: &DeprecationEntry) -> bool {
5566
match (self.origin, other.origin) {
5667
(Some(o1), Some(o2)) => o1 == o2,
5768
_ => false,
@@ -617,7 +628,17 @@ impl<'tcx> TyCtxt<'tcx> {
617628
is_allowed
618629
}
619630

631+
pub fn lookup_deprecation_entry(self, mut id: DefId) -> Option<DeprecationEntry> {
632+
loop {
633+
match self.lookup_deprecation_entry_raw(id) {
634+
RawDeprecationEntry::Explicit(depr) => return Some(depr),
635+
RawDeprecationEntry::None => return None,
636+
RawDeprecationEntry::Inherited => id = self.opt_parent(id)?,
637+
}
638+
}
639+
}
640+
620641
pub fn lookup_deprecation(self, id: DefId) -> Option<Deprecation> {
621-
self.lookup_deprecation_entry(id).map(|depr| depr.attr)
642+
self.lookup_deprecation_entry(id).map(|d| d.attr)
622643
}
623644
}

compiler/rustc_middle/src/query/erase.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ trivial! {
244244
Option<rustc_hir::def::DefKind>,
245245
Option<rustc_hir::CoroutineKind>,
246246
Option<rustc_hir::HirId>,
247-
Option<rustc_middle::middle::stability::DeprecationEntry>,
248247
Option<rustc_middle::ty::AsyncDestructor>,
249248
Option<rustc_middle::ty::Destructor>,
250249
Option<rustc_middle::ty::ImplTraitInTraitData>,
@@ -263,7 +262,6 @@ trivial! {
263262
rustc_ast::expand::allocator::AllocatorKind,
264263
rustc_attr::ConstStability,
265264
rustc_attr::DefaultBodyStability,
266-
rustc_attr::Deprecation,
267265
rustc_attr::Stability,
268266
rustc_data_structures::svh::Svh,
269267
rustc_errors::ErrorGuaranteed,
@@ -287,7 +285,7 @@ trivial! {
287285
rustc_middle::middle::exported_symbols::SymbolExportInfo,
288286
rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault,
289287
rustc_middle::middle::resolve_bound_vars::ResolvedArg,
290-
rustc_middle::middle::stability::DeprecationEntry,
288+
rustc_middle::middle::stability::RawDeprecationEntry,
291289
rustc_middle::mir::ConstQualifs,
292290
rustc_middle::mir::interpret::AllocId,
293291
rustc_middle::mir::interpret::CtfeProvenance,

compiler/rustc_middle/src/query/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use crate::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
5454
use crate::middle::lib_features::LibFeatures;
5555
use crate::middle::privacy::EffectiveVisibilities;
5656
use crate::middle::resolve_bound_vars::{ObjectLifetimeDefault, ResolveBoundVars, ResolvedArg};
57-
use crate::middle::stability::{self, DeprecationEntry};
57+
use crate::middle::stability::{self, RawDeprecationEntry};
5858
use crate::mir::interpret::{
5959
EvalStaticInitializerRawResult, EvalToAllocationRawResult, EvalToConstValueResult,
6060
EvalToValTreeResult, GlobalId, LitToConstError, LitToConstInput,
@@ -1223,7 +1223,7 @@ rustc_queries! {
12231223
desc { |tcx| "computing should_inherit_track_caller of `{}`", tcx.def_path_str(def_id) }
12241224
}
12251225

1226-
query lookup_deprecation_entry(def_id: DefId) -> Option<DeprecationEntry> {
1226+
query lookup_deprecation_entry_raw(def_id: DefId) -> RawDeprecationEntry {
12271227
desc { |tcx| "checking whether `{}` is deprecated", tcx.def_path_str(def_id) }
12281228
cache_on_disk_if { def_id.is_local() }
12291229
separate_provide_extern

compiler/rustc_passes/src/stability.rs

+13-16
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ use rustc_hir::{FieldDef, Item, ItemKind, TraitRef, Ty, TyKind, Variant};
1919
use rustc_middle::hir::nested_filter;
2020
use rustc_middle::middle::lib_features::{FeatureStability, LibFeatures};
2121
use rustc_middle::middle::privacy::EffectiveVisibilities;
22-
use rustc_middle::middle::stability::{AllowUnstable, DeprecationEntry, Index};
22+
use rustc_middle::middle::stability::{AllowUnstable, Index, RawDeprecationEntry};
2323
use rustc_middle::query::Providers;
2424
use rustc_middle::ty::TyCtxt;
2525
use rustc_session::lint;
2626
use rustc_session::lint::builtin::{INEFFECTIVE_UNSTABLE_TRAIT_IMPL, USELESS_DEPRECATED};
2727
use rustc_span::Span;
2828
use rustc_span::symbol::{Symbol, sym};
2929
use rustc_target::spec::abi::Abi;
30-
use tracing::{debug, info};
30+
use tracing::debug;
3131

3232
use crate::errors;
3333

@@ -128,19 +128,16 @@ fn annotation_kind(tcx: TyCtxt<'_>, def_id: LocalDefId) -> AnnotationKind {
128128
}
129129
}
130130

131-
fn lookup_deprecation_entry(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<DeprecationEntry> {
131+
fn lookup_deprecation_entry_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> RawDeprecationEntry {
132132
let attrs = tcx.hir().attrs(tcx.local_def_id_to_hir_id(def_id));
133133

134134
let depr = attr::find_deprecation(&tcx.sess, tcx.features(), attrs);
135135
let Some((depr, span)) = &depr else {
136-
if inherit_deprecation(tcx.def_kind(def_id)).yes() {
137-
let parent_id = tcx.opt_local_parent(def_id)?;
138-
let parent_depr = tcx.lookup_deprecation_entry(parent_id)?;
139-
info!("tagging child {:?} as deprecated from parent", def_id);
140-
return Some(parent_depr);
141-
}
142-
143-
return None;
136+
return if inherit_deprecation(tcx.def_kind(def_id)).yes() {
137+
RawDeprecationEntry::Inherited
138+
} else {
139+
RawDeprecationEntry::None
140+
};
144141
};
145142

146143
let kind = annotation_kind(tcx, def_id);
@@ -155,7 +152,7 @@ fn lookup_deprecation_entry(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<Depre
155152
}
156153

157154
// `Deprecation` is just two pointers, no need to intern it
158-
Some(DeprecationEntry::local(*depr, def_id))
155+
RawDeprecationEntry::local(*depr, def_id)
159156
}
160157

161158
/// A private tree-walker for producing an `Index`.
@@ -184,7 +181,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
184181
) where
185182
F: FnOnce(&mut Self),
186183
{
187-
let depr = self.tcx.lookup_deprecation_entry(def_id);
184+
let depr = self.tcx.lookup_deprecation(def_id.to_def_id());
188185
let is_deprecated = depr.is_some();
189186

190187
if !self.tcx.features().staged_api {
@@ -237,7 +234,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
237234
}
238235
}
239236

240-
if stab.is_none() && depr.map_or(false, |d| d.attr.is_since_rustc_version()) {
237+
if stab.is_none() && depr.map_or(false, |d| d.is_since_rustc_version()) {
241238
if let Some(attr) = attrs.iter().find(|attr| attr.has_name(sym::deprecated)) {
242239
self.tcx.dcx().emit_err(errors::DeprecatedAttribute { span: attr.span });
243240
}
@@ -263,7 +260,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
263260
// Check if deprecated_since < stable_since. If it is,
264261
// this is *almost surely* an accident.
265262
if let Some(depr) = depr
266-
&& let DeprecatedSince::RustcVersion(dep_since) = depr.attr.since
263+
&& let DeprecatedSince::RustcVersion(dep_since) = depr.since
267264
&& let attr::Stable { since: stab_since, .. } = stab.level
268265
{
269266
match stab_since {
@@ -707,7 +704,7 @@ pub(crate) fn provide(providers: &mut Providers) {
707704
lookup_stability: |tcx, id| tcx.stability().local_stability(id),
708705
lookup_const_stability: |tcx, id| tcx.stability().local_const_stability(id),
709706
lookup_default_body_stability: |tcx, id| tcx.stability().local_default_body_stability(id),
710-
lookup_deprecation_entry,
707+
lookup_deprecation_entry_raw,
711708
..*providers
712709
};
713710
}

0 commit comments

Comments
 (0)