Skip to content

Commit 8f08b3a

Browse files
committed
Auto merge of #143845 - cjgillot:stability-query, r=jieyouxu
Split-up stability_index query This PR aims to move deprecation and stability processing away from the monolithic `stability_index` query, and directly implement `lookup_{deprecation,stability,body_stability,const_stability}` queries. The basic idea is to: - move per-attribute sanity checks into `check_attr.rs`; - move attribute compatibility checks into the `MissingStabilityAnnotations` visitor; - progressively dismantle the `Annotator` visitor and the `stability_index` query. The first commit contains functional change, and now warns when `#[automatically_derived]` is applied on a non-trait impl block. The other commits should not change visible behaviour. Perf in #143845 (comment) shows small but consistent improvement, except for unused-warnings case. That case being a stress test, I'm leaning towards accepting the regression. This PR changes `check_attr`, so has a high conflict rate on that file. This should not cause issues for review.
2 parents 8231065 + 7662731 commit 8f08b3a

36 files changed

+767
-926
lines changed

compiler/rustc_attr_data_structures/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_ast::token::CommentKind;
2424
use rustc_ast::{AttrStyle, IntTy, UintTy};
2525
use rustc_ast_pretty::pp::Printer;
2626
use rustc_span::hygiene::Transparency;
27-
use rustc_span::{Span, Symbol};
27+
use rustc_span::{ErrorGuaranteed, Span, Symbol};
2828
pub use stability::*;
2929
use thin_vec::ThinVec;
3030
pub use version::*;
@@ -170,7 +170,7 @@ macro_rules! print_tup {
170170
}
171171

172172
print_tup!(A B C D E F G H);
173-
print_skip!(Span, ());
173+
print_skip!(Span, (), ErrorGuaranteed);
174174
print_disp!(u16, bool, NonZero<u32>);
175175
print_debug!(Symbol, UintTy, IntTy, Align, AttrStyle, CommentKind, Transparency);
176176

compiler/rustc_attr_data_structures/src/stability.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::num::NonZero;
22

33
use rustc_macros::{Decodable, Encodable, HashStable_Generic, PrintAttribute};
4-
use rustc_span::{Symbol, sym};
4+
use rustc_span::{ErrorGuaranteed, Symbol, sym};
55

66
use crate::{PrintAttribute, RustcVersion};
77

@@ -153,7 +153,7 @@ pub enum StableSince {
153153
/// Stabilized in the upcoming version, whatever number that is.
154154
Current,
155155
/// Failed to parse a stabilization version.
156-
Err,
156+
Err(ErrorGuaranteed),
157157
}
158158

159159
impl StabilityLevel {

compiler/rustc_attr_parsing/src/attributes/stability.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,12 @@ pub(crate) fn parse_stability<S: Stage>(
292292
} else if let Some(version) = parse_version(since) {
293293
StableSince::Version(version)
294294
} else {
295-
cx.emit_err(session_diagnostics::InvalidSince { span: cx.attr_span });
296-
StableSince::Err
295+
let err = cx.emit_err(session_diagnostics::InvalidSince { span: cx.attr_span });
296+
StableSince::Err(err)
297297
}
298298
} else {
299-
cx.emit_err(session_diagnostics::MissingSince { span: cx.attr_span });
300-
StableSince::Err
299+
let err = cx.emit_err(session_diagnostics::MissingSince { span: cx.attr_span });
300+
StableSince::Err(err)
301301
};
302302

303303
match feature {

compiler/rustc_hir/src/target.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub enum Target {
4141
Union,
4242
Trait,
4343
TraitAlias,
44-
Impl,
44+
Impl { of_trait: bool },
4545
Expression,
4646
Statement,
4747
Arm,
@@ -51,7 +51,7 @@ pub enum Target {
5151
ForeignFn,
5252
ForeignStatic,
5353
ForeignTy,
54-
GenericParam(GenericParamKind),
54+
GenericParam { kind: GenericParamKind, has_default: bool },
5555
MacroDef,
5656
Param,
5757
PatField,
@@ -86,14 +86,14 @@ impl Target {
8686
| Target::Union
8787
| Target::Trait
8888
| Target::TraitAlias
89-
| Target::Impl
89+
| Target::Impl { .. }
9090
| Target::Expression
9191
| Target::Statement
9292
| Target::Arm
9393
| Target::ForeignFn
9494
| Target::ForeignStatic
9595
| Target::ForeignTy
96-
| Target::GenericParam(_)
96+
| Target::GenericParam { .. }
9797
| Target::MacroDef
9898
| Target::Param
9999
| Target::PatField
@@ -119,7 +119,7 @@ impl Target {
119119
ItemKind::Union(..) => Target::Union,
120120
ItemKind::Trait(..) => Target::Trait,
121121
ItemKind::TraitAlias(..) => Target::TraitAlias,
122-
ItemKind::Impl { .. } => Target::Impl,
122+
ItemKind::Impl(imp_) => Target::Impl { of_trait: imp_.of_trait.is_some() },
123123
}
124124
}
125125

@@ -141,7 +141,7 @@ impl Target {
141141
DefKind::Union => Target::Union,
142142
DefKind::Trait => Target::Trait,
143143
DefKind::TraitAlias => Target::TraitAlias,
144-
DefKind::Impl { .. } => Target::Impl,
144+
DefKind::Impl { of_trait } => Target::Impl { of_trait },
145145
_ => panic!("impossible case reached"),
146146
}
147147
}
@@ -169,11 +169,17 @@ impl Target {
169169

170170
pub fn from_generic_param(generic_param: &hir::GenericParam<'_>) -> Target {
171171
match generic_param.kind {
172-
hir::GenericParamKind::Type { .. } => Target::GenericParam(GenericParamKind::Type),
172+
hir::GenericParamKind::Type { default, .. } => Target::GenericParam {
173+
kind: GenericParamKind::Type,
174+
has_default: default.is_some(),
175+
},
173176
hir::GenericParamKind::Lifetime { .. } => {
174-
Target::GenericParam(GenericParamKind::Lifetime)
177+
Target::GenericParam { kind: GenericParamKind::Lifetime, has_default: false }
175178
}
176-
hir::GenericParamKind::Const { .. } => Target::GenericParam(GenericParamKind::Const),
179+
hir::GenericParamKind::Const { default, .. } => Target::GenericParam {
180+
kind: GenericParamKind::Const,
181+
has_default: default.is_some(),
182+
},
177183
}
178184
}
179185

@@ -196,7 +202,8 @@ impl Target {
196202
Target::Union => "union",
197203
Target::Trait => "trait",
198204
Target::TraitAlias => "trait alias",
199-
Target::Impl => "implementation block",
205+
Target::Impl { of_trait: false } => "inherent implementation block",
206+
Target::Impl { of_trait: true } => "trait implementation block",
200207
Target::Expression => "expression",
201208
Target::Statement => "statement",
202209
Target::Arm => "match arm",
@@ -210,7 +217,7 @@ impl Target {
210217
Target::ForeignFn => "foreign function",
211218
Target::ForeignStatic => "foreign static item",
212219
Target::ForeignTy => "foreign type",
213-
Target::GenericParam(kind) => match kind {
220+
Target::GenericParam { kind, has_default: _ } => match kind {
214221
GenericParamKind::Type => "type parameter",
215222
GenericParamKind::Lifetime => "lifetime parameter",
216223
GenericParamKind::Const => "const parameter",

compiler/rustc_interface/src/passes.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,18 +1054,12 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
10541054
tcx.ensure_ok().check_mod_unstable_api_usage(module);
10551055
});
10561056
},
1057-
{
1058-
sess.time("unused_lib_feature_checking", || {
1059-
rustc_passes::stability::check_unused_or_stable_features(tcx)
1060-
});
1061-
},
10621057
{
10631058
// We force these queries to run,
10641059
// since they might not otherwise get called.
10651060
// This marks the corresponding crate-level attributes
10661061
// as used, and ensures that their values are valid.
10671062
tcx.ensure_ok().limits(());
1068-
tcx.ensure_ok().stability_index(());
10691063
}
10701064
);
10711065
});

compiler/rustc_middle/src/middle/stability.rs

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ use rustc_ast::NodeId;
77
use rustc_attr_data_structures::{
88
self as attrs, ConstStability, DefaultBodyStability, DeprecatedSince, Deprecation, Stability,
99
};
10-
use rustc_data_structures::unord::UnordMap;
1110
use rustc_errors::{Applicability, Diag, EmissionGuarantee};
1211
use rustc_feature::GateIssue;
13-
use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdMap};
12+
use rustc_hir::def_id::{DefId, LocalDefId};
1413
use rustc_hir::{self as hir, HirId};
1514
use rustc_macros::{Decodable, Encodable, HashStable, Subdiagnostic};
1615
use rustc_session::Session;
@@ -65,48 +64,6 @@ impl DeprecationEntry {
6564
}
6665
}
6766

68-
/// A stability index, giving the stability level for items and methods.
69-
#[derive(HashStable, Debug)]
70-
pub struct Index {
71-
/// This is mostly a cache, except the stabilities of local items
72-
/// are filled by the annotator.
73-
pub stab_map: LocalDefIdMap<Stability>,
74-
pub const_stab_map: LocalDefIdMap<ConstStability>,
75-
pub default_body_stab_map: LocalDefIdMap<DefaultBodyStability>,
76-
pub depr_map: LocalDefIdMap<DeprecationEntry>,
77-
/// Mapping from feature name to feature name based on the `implied_by` field of `#[unstable]`
78-
/// attributes. If a `#[unstable(feature = "implier", implied_by = "impliee")]` attribute
79-
/// exists, then this map will have a `impliee -> implier` entry.
80-
///
81-
/// This mapping is necessary unless both the `#[stable]` and `#[unstable]` attributes should
82-
/// specify their implications (both `implies` and `implied_by`). If only one of the two
83-
/// attributes do (as in the current implementation, `implied_by` in `#[unstable]`), then this
84-
/// mapping is necessary for diagnostics. When a "unnecessary feature attribute" error is
85-
/// reported, only the `#[stable]` attribute information is available, so the map is necessary
86-
/// to know that the feature implies another feature. If it were reversed, and the `#[stable]`
87-
/// attribute had an `implies` meta item, then a map would be necessary when avoiding a "use of
88-
/// unstable feature" error for a feature that was implied.
89-
pub implications: UnordMap<Symbol, Symbol>,
90-
}
91-
92-
impl Index {
93-
pub fn local_stability(&self, def_id: LocalDefId) -> Option<Stability> {
94-
self.stab_map.get(&def_id).copied()
95-
}
96-
97-
pub fn local_const_stability(&self, def_id: LocalDefId) -> Option<ConstStability> {
98-
self.const_stab_map.get(&def_id).copied()
99-
}
100-
101-
pub fn local_default_body_stability(&self, def_id: LocalDefId) -> Option<DefaultBodyStability> {
102-
self.default_body_stab_map.get(&def_id).copied()
103-
}
104-
105-
pub fn local_deprecation_entry(&self, def_id: LocalDefId) -> Option<DeprecationEntry> {
106-
self.depr_map.get(&def_id).cloned()
107-
}
108-
}
109-
11067
pub fn report_unstable(
11168
sess: &Session,
11269
feature: Symbol,

compiler/rustc_middle/src/query/mod.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ use crate::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
112112
use crate::middle::lib_features::LibFeatures;
113113
use crate::middle::privacy::EffectiveVisibilities;
114114
use crate::middle::resolve_bound_vars::{ObjectLifetimeDefault, ResolveBoundVars, ResolvedArg};
115-
use crate::middle::stability::{self, DeprecationEntry};
115+
use crate::middle::stability::DeprecationEntry;
116116
use crate::mir::interpret::{
117117
EvalStaticInitializerRawResult, EvalToAllocationRawResult, EvalToConstValueResult,
118118
EvalToValTreeResult, GlobalId, LitToConstInput,
@@ -2171,6 +2171,18 @@ rustc_queries! {
21712171
separate_provide_extern
21722172
arena_cache
21732173
}
2174+
/// Mapping from feature name to feature name based on the `implied_by` field of `#[unstable]`
2175+
/// attributes. If a `#[unstable(feature = "implier", implied_by = "impliee")]` attribute
2176+
/// exists, then this map will have a `impliee -> implier` entry.
2177+
///
2178+
/// This mapping is necessary unless both the `#[stable]` and `#[unstable]` attributes should
2179+
/// specify their implications (both `implies` and `implied_by`). If only one of the two
2180+
/// attributes do (as in the current implementation, `implied_by` in `#[unstable]`), then this
2181+
/// mapping is necessary for diagnostics. When a "unnecessary feature attribute" error is
2182+
/// reported, only the `#[stable]` attribute information is available, so the map is necessary
2183+
/// to know that the feature implies another feature. If it were reversed, and the `#[stable]`
2184+
/// attribute had an `implies` meta item, then a map would be necessary when avoiding a "use of
2185+
/// unstable feature" error for a feature that was implied.
21742186
query stability_implications(_: CrateNum) -> &'tcx UnordMap<Symbol, Symbol> {
21752187
arena_cache
21762188
desc { "calculating the implications between `#[unstable]` features defined in a crate" }
@@ -2277,11 +2289,6 @@ rustc_queries! {
22772289
desc { "fetching potentially unused trait imports" }
22782290
}
22792291

2280-
query stability_index(_: ()) -> &'tcx stability::Index {
2281-
arena_cache
2282-
eval_always
2283-
desc { "calculating the stability index for the local crate" }
2284-
}
22852292
/// All available crates in the graph, including those that should not be user-facing
22862293
/// (such as private crates).
22872294
query crates(_: ()) -> &'tcx [CrateNum] {

compiler/rustc_middle/src/ty/context.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use crate::infer::canonical::{CanonicalParamEnvCache, CanonicalVarKind, Canonica
6565
use crate::lint::lint_level;
6666
use crate::metadata::ModChild;
6767
use crate::middle::codegen_fn_attrs::{CodegenFnAttrs, TargetFeature};
68-
use crate::middle::{resolve_bound_vars, stability};
68+
use crate::middle::resolve_bound_vars;
6969
use crate::mir::interpret::{self, Allocation, ConstAllocation};
7070
use crate::mir::{Body, Local, Place, PlaceElem, ProjectionKind, Promoted};
7171
use crate::query::plumbing::QuerySystem;
@@ -1807,10 +1807,6 @@ impl<'tcx> TyCtxt<'tcx> {
18071807
)
18081808
}
18091809

1810-
pub fn stability(self) -> &'tcx stability::Index {
1811-
self.stability_index(())
1812-
}
1813-
18141810
pub fn features(self) -> &'tcx rustc_feature::Features {
18151811
self.features_query(())
18161812
}

compiler/rustc_passes/messages.ftl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,8 @@ passes_only_has_effect_on =
560560
`#[{$attr_name}]` only has an effect on {$target_name ->
561561
[function] functions
562562
[module] modules
563-
[implementation_block] implementation blocks
563+
[trait_implementation_block] trait implementation blocks
564+
[inherent_implementation_block] inherent implementation blocks
564565
*[unspecified] (unspecified--this is a compiler bug)
565566
}
566567

0 commit comments

Comments
 (0)