1use std::num::NonZero;
5
6use rustc_ast_lowering::stability::extern_abi_stability;
7use rustc_data_structures::fx::FxIndexMap;
8use rustc_data_structures::unord::{ExtendUnord, UnordMap, UnordSet};
9use rustc_feature::{EnabledLangFeature, EnabledLibFeature, UNSTABLE_LANG_FEATURES};
10use rustc_hir::attrs::{AttributeKind, DeprecatedSince};
11use rustc_hir::def::{DefKind, Res};
12use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId, LocalModId};
13use rustc_hir::intravisit::{self, Visitor};
14use rustc_hir::{
15 self as hir, AmbigArg, ConstStability, Constness, DefaultBodyStability, FieldDef, HirId, Item,
16 ItemKind, Path, Stability, StabilityLevel, StableSince, TraitRef, Ty, TyKind, UnstableReason,
17 UsePath, VERSION_PLACEHOLDER, Variant, find_attr,
18};
19use rustc_lint_defs::builtin::{
20 DEPRECATED, DUPLICATE_FEATURES, INEFFECTIVE_UNSTABLE_TRAIT_IMPL, STABLE_FEATURES,
21};
22use rustc_middle::hir::nested_filter;
23use rustc_middle::middle::lib_features::{FeatureStability, LibFeatures};
24use rustc_middle::middle::privacy::EffectiveVisibilities;
25use rustc_middle::middle::stability::{AllowUnstable, DeprecationEntry, EvalResult};
26use rustc_middle::query::{LocalCrate, Providers};
27use rustc_middle::span_bug;
28use rustc_middle::ty::{AssocContainer, TyCtxt};
29use rustc_span::{Span, Symbol, sym};
30use tracing::instrument;
31
32use crate::diagnostics;
33
34#[derive(#[automatically_derived]
impl ::core::marker::StructuralPartialEq for AnnotationKind { }
#[automatically_derived]
impl ::core::cmp::PartialEq for AnnotationKind {
#[inline]
fn eq(&self, other: &AnnotationKind) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq)]
35enum AnnotationKind {
36 Required,
38 Prohibited,
40 DeprecationProhibited,
42 Container,
44}
45
46fn inherit_deprecation(def_kind: DefKind) -> bool {
47 match def_kind {
48 DefKind::LifetimeParam | DefKind::TyParam | DefKind::ConstParam => false,
49 _ => true,
50 }
51}
52
53fn inherit_const_stability(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
54 let def_kind = tcx.def_kind(def_id);
55 match def_kind {
56 DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst => {
57 match tcx.def_kind(tcx.local_parent(def_id)) {
58 DefKind::Trait | DefKind::Impl { .. } => true,
59 _ => false,
60 }
61 }
62 DefKind::Closure => true,
63 _ => false,
64 }
65}
66
67fn annotation_kind(tcx: TyCtxt<'_>, def_id: LocalDefId) -> AnnotationKind {
68 let def_kind = tcx.def_kind(def_id);
69 match def_kind {
70 DefKind::Impl { of_trait: false } | DefKind::ForeignMod => AnnotationKind::Container,
75 DefKind::Impl { of_trait: true } => AnnotationKind::DeprecationProhibited,
76
77 DefKind::TyParam | DefKind::ConstParam => {
79 match &tcx.hir_node_by_def_id(def_id).expect_generic_param().kind {
80 hir::GenericParamKind::Type { default: Some(_), .. }
81 | hir::GenericParamKind::Const { default: Some(_), .. } => {
82 AnnotationKind::Container
83 }
84 _ => AnnotationKind::Prohibited,
85 }
86 }
87
88 DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst => {
90 match tcx.def_kind(tcx.local_parent(def_id)) {
91 DefKind::Impl { of_trait: true } => AnnotationKind::Prohibited,
92 _ => AnnotationKind::Required,
93 }
94 }
95
96 _ => AnnotationKind::Required,
97 }
98}
99
100fn lookup_deprecation_entry(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<DeprecationEntry> {
101 let depr = {
{
'done:
{
for i in ::rustc_attr_ir::HasAttrs::get_attrs(def_id, &tcx) {
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(Deprecated {
deprecation, span: _ }) => {
break 'done Some(*deprecation);
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}find_attr!(tcx, def_id,
102 Deprecated { deprecation, span: _ } => *deprecation
103 );
104
105 let Some(depr) = depr else {
106 if inherit_deprecation(tcx.def_kind(def_id)) {
107 let parent_id = tcx.opt_local_parent(def_id)?;
108 let parent_depr = tcx.lookup_deprecation_entry(parent_id)?;
109 return Some(parent_depr);
110 }
111
112 return None;
113 };
114
115 Some(DeprecationEntry::local(depr, def_id))
117}
118
119fn inherit_stability(def_kind: DefKind) -> bool {
120 match def_kind {
121 DefKind::Field | DefKind::Variant | DefKind::Ctor(..) => true,
122 _ => false,
123 }
124}
125
126const FORCE_UNSTABLE: Stability = Stability {
133 level: StabilityLevel::Unstable {
134 reason: UnstableReason::Default,
135 issue: NonZero::new(27812),
136 implied_by: None,
137 old_name: None,
138 },
139 feature: sym::rustc_private,
140};
141
142{}
#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("lookup_stability",
"rustc_passes::stability", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/4b6d04e706108ccfeafe2547fbe857dfe8972bad/compiler/rustc_passes/src/stability.rs"),
::tracing_core::__macro_support::Option::Some(142u32),
::tracing_core::__macro_support::Option::Some("rustc_passes::stability"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("def_id")
}> =
::tracing::__macro_support::FieldName::new("def_id");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&def_id)
as &dyn ::tracing::field::Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Option<Stability> = loop {};
return __tracing_attr_fake_return;
}
{
if !tcx.features().staged_api() {
if !tcx.sess.opts.unstable_opts.force_unstable_if_unmarked {
return None;
}
let Some(parent) =
tcx.opt_local_parent(def_id) else {
return Some(FORCE_UNSTABLE)
};
if inherit_deprecation(tcx.def_kind(def_id)) {
let parent = tcx.lookup_stability(parent)?;
if parent.is_unstable() { return Some(parent); }
}
return None;
}
let stab =
{
{
'done:
{
for i in ::rustc_attr_ir::HasAttrs::get_attrs(def_id, &tcx)
{
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(Stability {
stability, span: _ }) => {
break 'done Some(*stability);
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
};
if let Some(stab) = stab { return Some(stab); }
if inherit_deprecation(tcx.def_kind(def_id)) {
let Some(parent) =
tcx.opt_local_parent(def_id) else {
return tcx.sess.opts.unstable_opts.force_unstable_if_unmarked.then_some(FORCE_UNSTABLE);
};
let parent = tcx.lookup_stability(parent)?;
if parent.is_unstable() ||
inherit_stability(tcx.def_kind(def_id)) {
return Some(parent);
}
}
None
}
}
}#[instrument(level = "debug", skip(tcx))]
143fn lookup_stability(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<Stability> {
144 if !tcx.features().staged_api() {
147 if !tcx.sess.opts.unstable_opts.force_unstable_if_unmarked {
148 return None;
149 }
150
151 let Some(parent) = tcx.opt_local_parent(def_id) else { return Some(FORCE_UNSTABLE) };
152
153 if inherit_deprecation(tcx.def_kind(def_id)) {
154 let parent = tcx.lookup_stability(parent)?;
155 if parent.is_unstable() {
156 return Some(parent);
157 }
158 }
159
160 return None;
161 }
162
163 let stab = find_attr!(tcx, def_id, Stability { stability, span: _ } => *stability);
165
166 if let Some(stab) = stab {
167 return Some(stab);
168 }
169
170 if inherit_deprecation(tcx.def_kind(def_id)) {
171 let Some(parent) = tcx.opt_local_parent(def_id) else {
172 return tcx
173 .sess
174 .opts
175 .unstable_opts
176 .force_unstable_if_unmarked
177 .then_some(FORCE_UNSTABLE);
178 };
179 let parent = tcx.lookup_stability(parent)?;
180 if parent.is_unstable() || inherit_stability(tcx.def_kind(def_id)) {
181 return Some(parent);
182 }
183 }
184
185 None
186}
187
188{}
#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("lookup_default_body_stability",
"rustc_passes::stability", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/4b6d04e706108ccfeafe2547fbe857dfe8972bad/compiler/rustc_passes/src/stability.rs"),
::tracing_core::__macro_support::Option::Some(188u32),
::tracing_core::__macro_support::Option::Some("rustc_passes::stability"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("def_id")
}> =
::tracing::__macro_support::FieldName::new("def_id");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&def_id)
as &dyn ::tracing::field::Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Option<DefaultBodyStability> =
loop {};
return __tracing_attr_fake_return;
}
{
if !tcx.features().staged_api() { return None; }
{
{
'done:
{
for i in ::rustc_attr_ir::HasAttrs::get_attrs(def_id, &tcx)
{
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(RustcBodyStability {
stability, .. }) => {
break 'done Some(*stability);
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}
}
}
}#[instrument(level = "debug", skip(tcx))]
189fn lookup_default_body_stability(
190 tcx: TyCtxt<'_>,
191 def_id: LocalDefId,
192) -> Option<DefaultBodyStability> {
193 if !tcx.features().staged_api() {
194 return None;
195 }
196
197 find_attr!(tcx, def_id, RustcBodyStability { stability, .. } => *stability)
199}
200
201{}
#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("lookup_const_stability",
"rustc_passes::stability", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/4b6d04e706108ccfeafe2547fbe857dfe8972bad/compiler/rustc_passes/src/stability.rs"),
::tracing_core::__macro_support::Option::Some(201u32),
::tracing_core::__macro_support::Option::Some("rustc_passes::stability"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("def_id")
}> =
::tracing::__macro_support::FieldName::new("def_id");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&def_id)
as &dyn ::tracing::field::Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Option<ConstStability> = loop {};
return __tracing_attr_fake_return;
}
{
if !tcx.features().staged_api() {
if inherit_deprecation(tcx.def_kind(def_id)) {
let parent = tcx.opt_local_parent(def_id)?;
let parent_stab = tcx.lookup_stability(parent)?;
if parent_stab.is_unstable() &&
let Some(fn_sig) = tcx.hir_node_by_def_id(def_id).fn_sig()
&&
#[allow(non_exhaustive_omitted_patterns)] match fn_sig.header.constness
{
Constness::Const { .. } => true,
_ => false,
} {
let const_stable_indirect =
{
{
'done:
{
for i in ::rustc_attr_ir::HasAttrs::get_attrs(def_id, &tcx)
{
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(RustcConstStableIndirect)
=> {
break 'done Some(());
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}.is_some();
return Some(ConstStability::unmarked(const_stable_indirect,
parent_stab));
}
}
return None;
}
let const_stable_indirect =
{
{
'done:
{
for i in ::rustc_attr_ir::HasAttrs::get_attrs(def_id, &tcx)
{
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(RustcConstStableIndirect)
=> {
break 'done Some(());
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}.is_some();
let const_stab =
{
{
'done:
{
for i in ::rustc_attr_ir::HasAttrs::get_attrs(def_id, &tcx)
{
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(RustcConstStability {
stability, span: _ }) => {
break 'done Some(*stability);
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
};
let mut const_stab =
const_stab.map(|const_stab|
ConstStability::from_partial(const_stab,
const_stable_indirect));
if let Some(fn_sig) = tcx.hir_node_by_def_id(def_id).fn_sig() &&
#[allow(non_exhaustive_omitted_patterns)] match fn_sig.header.constness
{
Constness::Const { .. } => true,
_ => false,
} && const_stab.is_none() &&
let Some(inherit_regular_stab) =
tcx.lookup_stability(def_id) &&
inherit_regular_stab.is_unstable() {
const_stab =
Some(ConstStability {
const_stable_indirect: true,
promotable: false,
level: inherit_regular_stab.level,
feature: inherit_regular_stab.feature,
});
}
if let Some(const_stab) = const_stab { return Some(const_stab); }
if inherit_const_stability(tcx, def_id) {
let parent = tcx.opt_local_parent(def_id)?;
let parent = tcx.lookup_const_stability(parent)?;
if parent.is_const_unstable() { return Some(parent); }
}
None
}
}
}#[instrument(level = "debug", skip(tcx))]
202fn lookup_const_stability(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<ConstStability> {
203 if !tcx.features().staged_api() {
204 if inherit_deprecation(tcx.def_kind(def_id)) {
207 let parent = tcx.opt_local_parent(def_id)?;
208 let parent_stab = tcx.lookup_stability(parent)?;
209 if parent_stab.is_unstable()
210 && let Some(fn_sig) = tcx.hir_node_by_def_id(def_id).fn_sig()
211 && matches!(fn_sig.header.constness, Constness::Const { .. })
212 {
213 let const_stable_indirect = find_attr!(tcx, def_id, RustcConstStableIndirect);
214 return Some(ConstStability::unmarked(const_stable_indirect, parent_stab));
215 }
216 }
217
218 return None;
219 }
220
221 let const_stable_indirect = find_attr!(tcx, def_id, RustcConstStableIndirect);
222 let const_stab =
223 find_attr!(tcx, def_id, RustcConstStability { stability, span: _ } => *stability);
224
225 let mut const_stab = const_stab
228 .map(|const_stab| ConstStability::from_partial(const_stab, const_stable_indirect));
229
230 if let Some(fn_sig) = tcx.hir_node_by_def_id(def_id).fn_sig()
233 && matches!(fn_sig.header.constness, Constness::Const { .. })
234 && const_stab.is_none()
235 && let Some(inherit_regular_stab) = tcx.lookup_stability(def_id)
237 && inherit_regular_stab.is_unstable()
238 {
239 const_stab = Some(ConstStability {
240 const_stable_indirect: true,
242 promotable: false,
243 level: inherit_regular_stab.level,
244 feature: inherit_regular_stab.feature,
245 });
246 }
247
248 if let Some(const_stab) = const_stab {
249 return Some(const_stab);
250 }
251
252 if inherit_const_stability(tcx, def_id) {
256 let parent = tcx.opt_local_parent(def_id)?;
257 let parent = tcx.lookup_const_stability(parent)?;
258 if parent.is_const_unstable() {
259 return Some(parent);
260 }
261 }
262
263 None
264}
265
266fn stability_implications(tcx: TyCtxt<'_>, LocalCrate: LocalCrate) -> UnordMap<Symbol, Symbol> {
267 let mut implications = UnordMap::default();
268
269 let mut register_implication = |def_id| {
270 if let Some(stability) = tcx.lookup_stability(def_id)
271 && let StabilityLevel::Unstable { implied_by: Some(implied_by), .. } = stability.level
272 {
273 implications.insert(implied_by, stability.feature);
274 }
275
276 if let Some(stability) = tcx.lookup_const_stability(def_id)
277 && let StabilityLevel::Unstable { implied_by: Some(implied_by), .. } = stability.level
278 {
279 implications.insert(implied_by, stability.feature);
280 }
281 };
282
283 if tcx.features().staged_api() {
284 register_implication(CRATE_DEF_ID);
285 for def_id in tcx.hir_crate_items(()).definitions() {
286 register_implication(def_id);
287 let def_kind = tcx.def_kind(def_id);
288 if def_kind.is_adt() {
289 let adt = tcx.adt_def(def_id);
290 for variant in adt.variants() {
291 if variant.def_id != def_id.to_def_id() {
292 register_implication(variant.def_id.expect_local());
293 }
294 for field in &variant.fields {
295 register_implication(field.did.expect_local());
296 }
297 if let Some(ctor_def_id) = variant.ctor_def_id() {
298 register_implication(ctor_def_id.expect_local())
299 }
300 }
301 }
302 if def_kind.has_generics() {
303 for param in tcx.generics_of(def_id).own_params.iter() {
304 register_implication(param.def_id.expect_local())
305 }
306 }
307 }
308 }
309
310 implications
311}
312
313struct MissingStabilityAnnotations<'tcx> {
314 tcx: TyCtxt<'tcx>,
315 effective_visibilities: &'tcx EffectiveVisibilities,
316}
317
318impl<'tcx> MissingStabilityAnnotations<'tcx> {
319 {}
#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("check_compatible_stability",
"rustc_passes::stability", ::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("/rustc-dev/4b6d04e706108ccfeafe2547fbe857dfe8972bad/compiler/rustc_passes/src/stability.rs"),
::tracing_core::__macro_support::Option::Some(320u32),
::tracing_core::__macro_support::Option::Some("rustc_passes::stability"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("def_id")
}> =
::tracing::__macro_support::FieldName::new("def_id");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&def_id)
as &dyn ::tracing::field::Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{
if !self.tcx.features().staged_api() { return; }
let depr = self.tcx.lookup_deprecation_entry(def_id);
let stab = self.tcx.lookup_stability(def_id);
let const_stab = self.tcx.lookup_const_stability(def_id);
macro_rules! find_attr_span {
($name:ident) =>
{{
let attrs =
self.tcx.hir_attrs(self.tcx.local_def_id_to_hir_id(def_id));
find_attr!(attrs, AttributeKind::$name { span, .. } =>
*span)
}}
}
if stab.is_none() &&
depr.map_or(false, |d| d.attr.is_since_rustc_version()) &&
let Some(span) =
{
let attrs =
self.tcx.hir_attrs(self.tcx.local_def_id_to_hir_id(def_id));
{
'done:
{
for i in attrs {
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(AttributeKind::Deprecated {
span, .. }) => {
break 'done Some(*span);
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
} {
self.tcx.dcx().emit_err(diagnostics::DeprecatedAttribute {
span,
});
}
if let Some(stab) = stab {
let kind = annotation_kind(self.tcx, def_id);
if kind == AnnotationKind::Prohibited ||
(kind == AnnotationKind::Container && stab.level.is_stable()
&& depr.is_some()) {
if let Some(span) =
{
let attrs =
self.tcx.hir_attrs(self.tcx.local_def_id_to_hir_id(def_id));
{
'done:
{
for i in attrs {
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(AttributeKind::Stability {
span, .. }) => {
break 'done Some(*span);
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
} {
let item_sp = self.tcx.def_span(def_id);
self.tcx.dcx().emit_err(diagnostics::UselessStability {
span,
item_sp,
});
}
}
if let Some(depr) = depr &&
let DeprecatedSince::RustcVersion(dep_since) =
depr.attr.since &&
let StabilityLevel::Stable { since: stab_since, .. } =
stab.level &&
let Some(span) =
{
let attrs =
self.tcx.hir_attrs(self.tcx.local_def_id_to_hir_id(def_id));
{
'done:
{
for i in attrs {
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(AttributeKind::Stability {
span, .. }) => {
break 'done Some(*span);
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
} {
let item_sp = self.tcx.def_span(def_id);
match stab_since {
StableSince::Current => {
self.tcx.dcx().emit_err(diagnostics::CannotStabilizeDeprecated {
span,
item_sp,
});
}
StableSince::Version(stab_since) => {
if dep_since < stab_since {
self.tcx.dcx().emit_err(diagnostics::CannotStabilizeDeprecated {
span,
item_sp,
});
}
}
StableSince::Err(_) => {}
}
}
}
let fn_sig = self.tcx.hir_node_by_def_id(def_id).fn_sig();
if let Some(fn_sig) = fn_sig &&
!#[allow(non_exhaustive_omitted_patterns)] match fn_sig.header.constness
{
Constness::Const { .. } => true,
_ => false,
} && const_stab.is_some() &&
{
let attrs =
self.tcx.hir_attrs(self.tcx.local_def_id_to_hir_id(def_id));
{
'done:
{
for i in attrs {
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(AttributeKind::RustcConstStability {
span, .. }) => {
break 'done Some(*span);
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}.is_some() {
self.tcx.dcx().emit_err(diagnostics::MissingConstErr {
fn_sig_span: fn_sig.span,
});
}
if let Some(const_stab) = const_stab && let Some(fn_sig) = fn_sig
&& const_stab.is_const_stable() &&
!stab.is_some_and(|s| s.is_stable()) &&
let Some(path_span) =
{
let attrs =
self.tcx.hir_attrs(self.tcx.local_def_id_to_hir_id(def_id));
{
'done:
{
for i in attrs {
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(AttributeKind::RustcConstStability {
span, .. }) => {
break 'done Some(*span);
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
} {
self.tcx.dcx().emit_err(diagnostics::ConstStableNotStable {
fn_sig_span: fn_sig.span,
path_span,
});
}
if let Some(stab) = &const_stab && stab.is_const_stable() &&
stab.const_stable_indirect &&
let Some(span) =
{
let attrs =
self.tcx.hir_attrs(self.tcx.local_def_id_to_hir_id(def_id));
{
'done:
{
for i in attrs {
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(AttributeKind::RustcConstStability {
span, .. }) => {
break 'done Some(*span);
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
} {
self.tcx.dcx().emit_err(diagnostics::RustcConstStableIndirectPairing {
span,
});
}
}
}
}#[instrument(level = "trace", skip(self))]
321 fn check_compatible_stability(&self, def_id: LocalDefId) {
322 if !self.tcx.features().staged_api() {
323 return;
324 }
325
326 let depr = self.tcx.lookup_deprecation_entry(def_id);
327 let stab = self.tcx.lookup_stability(def_id);
328 let const_stab = self.tcx.lookup_const_stability(def_id);
329
330 macro_rules! find_attr_span {
331 ($name:ident) => {{
332 let attrs = self.tcx.hir_attrs(self.tcx.local_def_id_to_hir_id(def_id));
333 find_attr!(attrs, AttributeKind::$name { span, .. } => *span)
334 }}
335 }
336
337 if stab.is_none()
338 && depr.map_or(false, |d| d.attr.is_since_rustc_version())
339 && let Some(span) = find_attr_span!(Deprecated)
340 {
341 self.tcx.dcx().emit_err(diagnostics::DeprecatedAttribute { span });
342 }
343
344 if let Some(stab) = stab {
345 let kind = annotation_kind(self.tcx, def_id);
347 if kind == AnnotationKind::Prohibited
348 || (kind == AnnotationKind::Container && stab.level.is_stable() && depr.is_some())
349 {
350 if let Some(span) = find_attr_span!(Stability) {
351 let item_sp = self.tcx.def_span(def_id);
352 self.tcx.dcx().emit_err(diagnostics::UselessStability { span, item_sp });
353 }
354 }
355
356 if let Some(depr) = depr
359 && let DeprecatedSince::RustcVersion(dep_since) = depr.attr.since
360 && let StabilityLevel::Stable { since: stab_since, .. } = stab.level
361 && let Some(span) = find_attr_span!(Stability)
362 {
363 let item_sp = self.tcx.def_span(def_id);
364 match stab_since {
365 StableSince::Current => {
366 self.tcx
367 .dcx()
368 .emit_err(diagnostics::CannotStabilizeDeprecated { span, item_sp });
369 }
370 StableSince::Version(stab_since) => {
371 if dep_since < stab_since {
372 self.tcx
373 .dcx()
374 .emit_err(diagnostics::CannotStabilizeDeprecated { span, item_sp });
375 }
376 }
377 StableSince::Err(_) => {
378 }
381 }
382 }
383 }
384
385 let fn_sig = self.tcx.hir_node_by_def_id(def_id).fn_sig();
388 if let Some(fn_sig) = fn_sig
389 && !matches!(fn_sig.header.constness, Constness::Const { .. })
390 && const_stab.is_some()
391 && find_attr_span!(RustcConstStability).is_some()
392 {
393 self.tcx.dcx().emit_err(diagnostics::MissingConstErr { fn_sig_span: fn_sig.span });
394 }
395
396 if let Some(const_stab) = const_stab
398 && let Some(fn_sig) = fn_sig
399 && const_stab.is_const_stable()
400 && !stab.is_some_and(|s| s.is_stable())
401 && let Some(path_span) = find_attr_span!(RustcConstStability)
402 {
403 self.tcx.dcx().emit_err(diagnostics::ConstStableNotStable {
404 fn_sig_span: fn_sig.span,
405 path_span,
406 });
407 }
408
409 if let Some(stab) = &const_stab
410 && stab.is_const_stable()
411 && stab.const_stable_indirect
412 && let Some(span) = find_attr_span!(RustcConstStability)
413 {
414 self.tcx.dcx().emit_err(diagnostics::RustcConstStableIndirectPairing { span });
415 }
416 }
417
418 {}
#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("check_missing_stability",
"rustc_passes::stability", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/4b6d04e706108ccfeafe2547fbe857dfe8972bad/compiler/rustc_passes/src/stability.rs"),
::tracing_core::__macro_support::Option::Some(418u32),
::tracing_core::__macro_support::Option::Some("rustc_passes::stability"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("def_id")
}> =
::tracing::__macro_support::FieldName::new("def_id");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&def_id)
as &dyn ::tracing::field::Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{
let stab = self.tcx.lookup_stability(def_id);
self.tcx.ensure_ok().lookup_const_stability(def_id);
if !self.tcx.sess.is_test_crate() && stab.is_none() &&
self.effective_visibilities.is_reachable(def_id) {
let descr = self.tcx.def_descr(def_id.to_def_id());
let span = self.tcx.def_span(def_id);
self.tcx.dcx().emit_err(diagnostics::MissingStabilityAttr {
span,
descr,
});
}
}
}
}#[instrument(level = "debug", skip(self))]
419 fn check_missing_stability(&self, def_id: LocalDefId) {
420 let stab = self.tcx.lookup_stability(def_id);
421 self.tcx.ensure_ok().lookup_const_stability(def_id);
422 if !self.tcx.sess.is_test_crate()
423 && stab.is_none()
424 && self.effective_visibilities.is_reachable(def_id)
425 {
426 let descr = self.tcx.def_descr(def_id.to_def_id());
427 let span = self.tcx.def_span(def_id);
428 self.tcx.dcx().emit_err(diagnostics::MissingStabilityAttr { span, descr });
429 }
430 }
431
432 fn check_missing_const_stability(&self, def_id: LocalDefId) {
433 let is_const = self.tcx.is_const_fn(def_id.to_def_id())
434 || (self.tcx.def_kind(def_id.to_def_id()) == DefKind::Trait
435 && self.tcx.is_const_trait(def_id.to_def_id()));
436
437 if is_const
439 && self.effective_visibilities.is_reachable(def_id)
440 && self.tcx.lookup_const_stability(def_id).is_none()
441 {
442 let span = self.tcx.def_span(def_id);
443 let descr = self.tcx.def_descr(def_id.to_def_id());
444 self.tcx.dcx().emit_err(diagnostics::MissingConstStabAttr { span, descr });
445 }
446 }
447}
448
449impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
450 type NestedFilter = nested_filter::OnlyBodies;
451
452 fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
453 self.tcx
454 }
455
456 fn visit_item(&mut self, i: &'tcx Item<'tcx>) {
457 self.check_compatible_stability(i.owner_id.def_id);
458
459 if !#[allow(non_exhaustive_omitted_patterns)] match i.kind {
hir::ItemKind::Impl(hir::Impl { of_trait: None, .. }) |
hir::ItemKind::ForeignMod { .. } => true,
_ => false,
}matches!(
464 i.kind,
465 hir::ItemKind::Impl(hir::Impl { of_trait: None, .. })
466 | hir::ItemKind::ForeignMod { .. }
467 ) {
468 self.check_missing_stability(i.owner_id.def_id);
469 }
470
471 self.check_missing_const_stability(i.owner_id.def_id);
473
474 intravisit::walk_item(self, i)
475 }
476
477 fn visit_trait_item(&mut self, ti: &'tcx hir::TraitItem<'tcx>) {
478 self.check_compatible_stability(ti.owner_id.def_id);
479 self.check_missing_stability(ti.owner_id.def_id);
480 intravisit::walk_trait_item(self, ti);
481 }
482
483 fn visit_impl_item(&mut self, ii: &'tcx hir::ImplItem<'tcx>) {
484 self.check_compatible_stability(ii.owner_id.def_id);
485 if let hir::ImplItemImplKind::Inherent { .. } = ii.impl_kind {
486 self.check_missing_stability(ii.owner_id.def_id);
487 self.check_missing_const_stability(ii.owner_id.def_id);
488 }
489 intravisit::walk_impl_item(self, ii);
490 }
491
492 fn visit_variant(&mut self, var: &'tcx Variant<'tcx>) {
493 self.check_compatible_stability(var.def_id);
494 self.check_missing_stability(var.def_id);
495 if let Some(ctor_def_id) = var.data.ctor_def_id() {
496 self.check_missing_stability(ctor_def_id);
497 }
498 intravisit::walk_variant(self, var);
499 }
500
501 fn visit_field_def(&mut self, s: &'tcx FieldDef<'tcx>) {
502 self.check_compatible_stability(s.def_id);
503 self.check_missing_stability(s.def_id);
504 intravisit::walk_field_def(self, s);
505 }
506
507 fn visit_foreign_item(&mut self, i: &'tcx hir::ForeignItem<'tcx>) {
508 self.check_compatible_stability(i.owner_id.def_id);
509 self.check_missing_stability(i.owner_id.def_id);
510 intravisit::walk_foreign_item(self, i);
511 }
512
513 fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam<'tcx>) {
514 self.check_compatible_stability(p.def_id);
515 intravisit::walk_generic_param(self, p);
519 }
520}
521
522fn check_mod_unstable_api_usage(tcx: TyCtxt<'_>, mod_id: LocalModId) {
525 tcx.hir_visit_item_likes_in_module(mod_id, &mut Checker { tcx });
526
527 let is_staged_api =
528 tcx.sess.opts.unstable_opts.force_unstable_if_unmarked || tcx.features().staged_api();
529 if is_staged_api {
530 let effective_visibilities = &tcx.effective_visibilities(());
531 let mut missing = MissingStabilityAnnotations { tcx, effective_visibilities };
532 if mod_id.is_top_level_module() {
533 missing.check_missing_stability(CRATE_DEF_ID);
534 }
535 tcx.hir_visit_item_likes_in_module(mod_id, &mut missing);
536 }
537
538 if mod_id.is_top_level_module() {
539 check_unused_or_stable_features(tcx)
540 }
541}
542
543pub(crate) fn provide(providers: &mut Providers) {
544 *providers = Providers {
545 check_mod_unstable_api_usage,
546 stability_implications,
547 lookup_stability,
548 lookup_const_stability,
549 lookup_default_body_stability,
550 lookup_deprecation_entry,
551 ..*providers
552 };
553}
554
555struct Checker<'tcx> {
556 tcx: TyCtxt<'tcx>,
557}
558
559impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
560 type NestedFilter = nested_filter::OnlyBodies;
561
562 fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
566 self.tcx
567 }
568
569 fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
570 match item.kind {
571 hir::ItemKind::ExternCrate(_, ident) => {
572 if item.span.is_dummy() && ident.name != sym::std {
575 return;
576 }
577
578 let Some(cnum) = self.tcx.extern_mod_stmt_cnum(item.owner_id.def_id) else {
579 return;
580 };
581 let def_id = cnum.as_def_id();
582 self.tcx.check_stability(def_id, Some(item.hir_id()), item.span, None);
583 }
584
585 hir::ItemKind::Impl(hir::Impl {
589 of_trait: Some(of_trait),
590 self_ty,
591 items,
592 constness,
593 ..
594 }) => {
595 let features = self.tcx.features();
596 if features.staged_api() {
597 let attrs = self.tcx.hir_attrs(item.hir_id());
598 let stab = {
'done:
{
for i in attrs {
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(Stability { stability, span
}) => {
break 'done Some((*stability, *span));
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}find_attr!(attrs, Stability{stability, span} => (*stability, *span));
599
600 let const_stab =
602 {
'done:
{
for i in attrs {
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(RustcConstStability {
stability, .. }) => {
break 'done Some(*stability);
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}find_attr!(attrs, RustcConstStability{stability, ..} => *stability);
603
604 let unstable_feature_stab = {
'done:
{
for i in attrs {
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(UnstableFeatureBound(i)) =>
{
break 'done Some(i);
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}find_attr!(attrs, UnstableFeatureBound(i) => i)
605 .map(|i| i.as_slice())
606 .unwrap_or_default();
607
608 if let Some((
624 Stability { level: StabilityLevel::Unstable { .. }, feature },
625 span,
626 )) = stab
627 {
628 let mut c = CheckTraitImplStable { tcx: self.tcx, fully_stable: true };
629 c.visit_ty_unambig(self_ty);
630 c.visit_trait_ref(&of_trait.trait_ref);
631
632 let mut unstable_feature_bound_in_effect = false;
635 for (unstable_bound_feat_name, _) in unstable_feature_stab {
636 if *unstable_bound_feat_name == feature {
637 unstable_feature_bound_in_effect = true;
638 }
639 }
640
641 if of_trait.trait_ref.path.res != Res::Err
644 && c.fully_stable
645 && !unstable_feature_bound_in_effect
646 {
647 self.tcx.emit_node_span_lint(
648 INEFFECTIVE_UNSTABLE_TRAIT_IMPL,
649 item.hir_id(),
650 span,
651 diagnostics::IneffectiveUnstableImpl,
652 );
653 }
654 }
655
656 if features.const_trait_impl()
657 && let hir::Constness::Const { .. } = constness
658 {
659 let stable_or_implied_stable = match const_stab {
660 None => true,
661 Some(stab) if stab.is_const_stable() => {
662 self.tcx.dcx().emit_err(diagnostics::TraitImplConstStable {
666 span: item.span,
667 });
668 true
669 }
670 Some(_) => false,
671 };
672
673 if let Some(trait_id) = of_trait.trait_ref.trait_def_id()
674 && let Some(const_stab) = self.tcx.lookup_const_stability(trait_id)
675 {
676 if const_stab.is_const_stable() != stable_or_implied_stable {
678 let trait_span = self.tcx.def_ident_span(trait_id).unwrap();
679
680 let impl_stability = if stable_or_implied_stable {
681 diagnostics::ImplConstStability::Stable { span: item.span }
682 } else {
683 diagnostics::ImplConstStability::Unstable { span: item.span }
684 };
685 let trait_stability = if const_stab.is_const_stable() {
686 diagnostics::TraitConstStability::Stable { span: trait_span }
687 } else {
688 diagnostics::TraitConstStability::Unstable { span: trait_span }
689 };
690
691 self.tcx.dcx().emit_err(
692 diagnostics::TraitImplConstStabilityMismatch {
693 span: item.span,
694 impl_stability,
695 trait_stability,
696 },
697 );
698 }
699 }
700 }
701 }
702
703 if let hir::Constness::Const { .. } = constness
704 && let Some(def_id) = of_trait.trait_ref.trait_def_id()
705 {
706 self.tcx.check_const_stability(
708 def_id,
709 of_trait.trait_ref.path.span,
710 of_trait.trait_ref.path.span,
711 );
712 }
713
714 for impl_item_ref in items {
715 let impl_item = self.tcx.associated_item(impl_item_ref.owner_id);
716
717 if let AssocContainer::TraitImpl(Ok(def_id)) = impl_item.container {
718 self.tcx.check_stability(
720 def_id,
721 None,
722 self.tcx.def_span(impl_item_ref.owner_id),
723 None,
724 );
725 }
726 }
727 }
728
729 _ => (),
730 }
731 intravisit::walk_item(self, item);
732 }
733
734 fn visit_poly_trait_ref(&mut self, t: &'tcx hir::PolyTraitRef<'tcx>) {
735 match t.modifiers.constness {
736 hir::BoundConstness::Always(span) | hir::BoundConstness::Maybe(span) => {
737 if let Some(def_id) = t.trait_ref.trait_def_id() {
738 self.tcx.check_const_stability(def_id, t.trait_ref.path.span, span);
739 }
740 }
741 hir::BoundConstness::Never => {}
742 }
743 intravisit::walk_poly_trait_ref(self, t);
744 }
745
746 fn visit_use(&mut self, path: &'tcx UsePath<'tcx>, hir_id: HirId) {
747 let res = path.res;
748
749 if let Some(ty_ns_res) = res.type_ns
753 && let Some(value_ns_res) = res.value_ns
754 && let Some(type_ns_did) = ty_ns_res.opt_def_id()
755 && let Some(value_ns_did) = value_ns_res.opt_def_id()
756 && let DefKind::Ctor(.., _) = self.tcx.def_kind(value_ns_did)
757 && self.tcx.parent(value_ns_did) == type_ns_did
758 {
759 let UsePath { segments, res: _, span } = *path;
762 self.visit_path(&Path { segments, res: value_ns_res, span }, hir_id);
763
764 if let Some(res) = res.macro_ns {
767 self.visit_path(&Path { segments, res, span }, hir_id);
768 }
769 } else {
770 intravisit::walk_use(self, path, hir_id)
772 }
773 }
774
775 fn visit_path(&mut self, path: &hir::Path<'tcx>, id: hir::HirId) {
776 if let Some(def_id) = path.res.opt_def_id() {
777 let method_span = path.segments.last().map(|s| s.ident.span);
778 let item_is_allowed = self.tcx.check_stability_allow_unstable(
779 def_id,
780 Some(id),
781 path.span,
782 method_span,
783 if is_unstable_reexport(self.tcx, id) {
784 AllowUnstable::Yes
785 } else {
786 AllowUnstable::No
787 },
788 );
789
790 if item_is_allowed {
791 let is_allowed_through_unstable_modules: Option<(Symbol, Symbol)> =
793 self.tcx.lookup_stability(def_id).and_then(|stab| match stab.level {
794 StabilityLevel::Stable { allowed_through_unstable_modules, .. } => {
795 allowed_through_unstable_modules
796 }
797 _ => None,
798 });
799
800 let parents = path.segments.iter().rev().skip(1);
812 for path_segment in parents {
813 if let Some(def_id) = path_segment.res.opt_def_id() {
814 match is_allowed_through_unstable_modules {
815 None => {
816 self.tcx.check_stability_allow_unstable(
820 def_id,
821 None,
822 path_segment.ident.span,
823 None,
824 if is_unstable_reexport(self.tcx, id) {
825 AllowUnstable::Yes
826 } else {
827 AllowUnstable::No
828 },
829 );
830 }
831 Some((message, suggestion)) => {
832 let eval_result = self.tcx.eval_stability_allow_unstable(
835 def_id,
836 None,
837 path.span,
838 None,
839 if is_unstable_reexport(self.tcx, id) {
840 AllowUnstable::Yes
841 } else {
842 AllowUnstable::No
843 },
844 );
845 let is_allowed = #[allow(non_exhaustive_omitted_patterns)] match eval_result {
EvalResult::Allow => true,
_ => false,
}matches!(eval_result, EvalResult::Allow);
846 if !is_allowed {
847 let [.., intrinsics_module, _intrinsic] = path.segments else {
849 ::rustc_middle::util::bug::span_bug_fmt(path.span,
format_args!("no module for `is_allowed_through_unstable_modules` intrinsic {0:?}",
path))span_bug!(
850 path.span,
851 "no module for `is_allowed_through_unstable_modules` intrinsic {path:?}"
852 )
853 };
854 let diag = diagnostics::RustcAtumSuggestion {
855 message,
856 import_span: path.span,
857 unstable_mod_span: { intrinsics_module.ident.span },
858 module: intrinsics_module.ident,
859 suggestion,
860 };
861 self.tcx.emit_node_span_lint(
862 DEPRECATED,
863 id,
864 method_span.unwrap_or(path.span),
865 diag,
866 );
867 }
868 }
869 }
870 }
871 }
872 }
873 }
874
875 intravisit::walk_path(self, path)
876 }
877}
878
879fn is_unstable_reexport(tcx: TyCtxt<'_>, id: hir::HirId) -> bool {
883 let Some(owner) = id.as_owner() else {
885 return false;
886 };
887 let def_id = owner.def_id;
888
889 let Some(stab) = tcx.lookup_stability(def_id) else {
890 return false;
891 };
892
893 if stab.level.is_stable() {
894 return false;
896 }
897
898 if !#[allow(non_exhaustive_omitted_patterns)] match tcx.hir_expect_item(def_id).kind
{
ItemKind::Use(..) => true,
_ => false,
}matches!(tcx.hir_expect_item(def_id).kind, ItemKind::Use(..)) {
900 return false;
901 }
902
903 true
904}
905
906struct CheckTraitImplStable<'tcx> {
907 tcx: TyCtxt<'tcx>,
908 fully_stable: bool,
909}
910
911impl<'tcx> Visitor<'tcx> for CheckTraitImplStable<'tcx> {
912 fn visit_path(&mut self, path: &hir::Path<'tcx>, _id: hir::HirId) {
913 if let Some(def_id) = path.res.opt_def_id()
914 && let Some(stab) = self.tcx.lookup_stability(def_id)
915 {
916 self.fully_stable &= stab.level.is_stable();
917 }
918 intravisit::walk_path(self, path)
919 }
920
921 fn visit_trait_ref(&mut self, t: &'tcx TraitRef<'tcx>) {
922 if let Res::Def(DefKind::Trait, trait_did) = t.path.res {
923 if let Some(stab) = self.tcx.lookup_stability(trait_did) {
924 self.fully_stable &= stab.level.is_stable();
925 }
926 }
927 intravisit::walk_trait_ref(self, t)
928 }
929
930 fn visit_ty(&mut self, t: &'tcx Ty<'tcx, AmbigArg>) {
931 if let TyKind::FnPtr(function) = t.kind {
932 if extern_abi_stability(function.abi).is_err() {
933 self.fully_stable = false;
934 }
935 }
936 intravisit::walk_ty(self, t)
937 }
938}
939
940pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
945 let _prof_timer = tcx.sess.timer("unused_lib_feature_checking");
946
947 let enabled_lang_features = tcx.features().enabled_lang_features();
948 let mut lang_features = UnordSet::default();
949 for EnabledLangFeature { gate_name, attr_sp, stable_since } in enabled_lang_features {
950 if let Some(version) = stable_since {
951 let _ = tcx.features().enabled(*gate_name);
953
954 unnecessary_stable_feature_lint(tcx, *attr_sp, *gate_name, *version);
956 }
957 if !lang_features.insert(gate_name) {
958 duplicate_feature_lint(tcx, *attr_sp, *gate_name);
960 }
961 }
962
963 let enabled_lib_features = tcx.features().enabled_lib_features();
964 let mut remaining_lib_features = FxIndexMap::default();
965 for EnabledLibFeature { gate_name, attr_sp } in enabled_lib_features {
966 if remaining_lib_features.contains_key(gate_name) {
967 duplicate_feature_lint(tcx, *attr_sp, *gate_name);
969 }
970 remaining_lib_features.insert(*gate_name, *attr_sp);
971 }
972 remaining_lib_features.swap_remove(&sym::libc);
980 remaining_lib_features.swap_remove(&sym::test);
981
982 fn check_features<'tcx>(
1001 tcx: TyCtxt<'tcx>,
1002 remaining_lib_features: &mut FxIndexMap<Symbol, Span>,
1003 remaining_implications: &mut UnordMap<Symbol, Symbol>,
1004 defined_features: &LibFeatures,
1005 all_implications: &UnordMap<Symbol, Symbol>,
1006 ) {
1007 for (feature, stability) in defined_features.to_sorted_vec() {
1008 if let FeatureStability::AcceptedSince(since) = stability
1009 && let Some(span) = remaining_lib_features.get(&feature)
1010 {
1011 let _ = tcx.features().enabled(feature);
1013
1014 if let Some(implies) = all_implications.get(&feature) {
1016 unnecessary_partially_stable_feature_lint(tcx, *span, feature, *implies, since);
1017 } else {
1018 unnecessary_stable_feature_lint(tcx, *span, feature, since);
1019 }
1020 }
1021 remaining_lib_features.swap_remove(&feature);
1023
1024 remaining_implications.remove(&feature);
1029
1030 if let FeatureStability::Unstable { old_name: Some(alias) } = stability
1031 && let Some(span) = remaining_lib_features.swap_remove(&alias)
1032 {
1033 tcx.dcx().emit_err(diagnostics::RenamedFeature { span, feature, alias });
1034 }
1035
1036 if remaining_lib_features.is_empty() && remaining_implications.is_empty() {
1037 break;
1038 }
1039 }
1040 }
1041
1042 let mut remaining_implications = tcx.stability_implications(LOCAL_CRATE).clone();
1044
1045 let local_defined_features = tcx.lib_features(LOCAL_CRATE);
1048 if !remaining_lib_features.is_empty() || !remaining_implications.is_empty() {
1049 let crates = tcx.crates(());
1050
1051 let mut all_implications = remaining_implications.clone();
1055 for &cnum in crates {
1056 all_implications
1057 .extend_unord(tcx.stability_implications(cnum).items().map(|(k, v)| (*k, *v)));
1058 }
1059
1060 check_features(
1061 tcx,
1062 &mut remaining_lib_features,
1063 &mut remaining_implications,
1064 local_defined_features,
1065 &all_implications,
1066 );
1067
1068 for &cnum in crates {
1069 if remaining_lib_features.is_empty() && remaining_implications.is_empty() {
1070 break;
1071 }
1072 check_features(
1073 tcx,
1074 &mut remaining_lib_features,
1075 &mut remaining_implications,
1076 tcx.lib_features(cnum),
1077 &all_implications,
1078 );
1079 }
1080
1081 if !remaining_lib_features.is_empty() {
1082 let lang_features =
1083 UNSTABLE_LANG_FEATURES.iter().map(|feature| feature.name).collect::<Vec<_>>();
1084 let lib_features = crates
1085 .iter()
1086 .flat_map(|&cnum| {
1087 tcx.lib_features(cnum).stability.keys().copied().into_sorted_stable_ord()
1088 })
1089 .collect::<Vec<_>>();
1090
1091 let valid_feature_names = [lang_features, lib_features].concat();
1092
1093 let unstable_removed_features = crates
1095 .iter()
1096 .flat_map(|&cnum| {
1097 {
{
'done:
{
for i in
::rustc_attr_ir::HasAttrs::get_attrs(cnum.as_def_id(), &tcx) {
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(UnstableRemoved(rem_features))
=> {
break 'done Some(rem_features);
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}find_attr!(tcx, cnum.as_def_id(), UnstableRemoved(rem_features) => rem_features)
1098 .into_flat_iter()
1099 })
1100 .collect::<Vec<_>>();
1101
1102 for (feature, span) in remaining_lib_features {
1103 if let Some(removed) =
1104 unstable_removed_features.iter().find(|removed| removed.feature == feature)
1105 {
1106 tcx.dcx().emit_err(diagnostics::FeatureRemoved {
1107 span,
1108 feature,
1109 reason: removed.reason,
1110 link: removed.link,
1111 since: removed.since.to_string(),
1112 });
1113 } else {
1114 let suggestion =
1115 feature.find_similar(&valid_feature_names).map(|(actual_name, _)| {
1116 diagnostics::MisspelledFeature { span, actual_name }
1117 });
1118 tcx.dcx().emit_err(diagnostics::UnknownFeature { span, feature, suggestion });
1119 }
1120 }
1121 }
1122 }
1123
1124 for (&implied_by, &feature) in remaining_implications.to_sorted_stable_ord() {
1125 let local_defined_features = tcx.lib_features(LOCAL_CRATE);
1126 let span = local_defined_features
1127 .stability
1128 .get(&feature)
1129 .expect("feature that implied another does not exist")
1130 .1;
1131 tcx.dcx().emit_err(diagnostics::ImpliedFeatureNotExist { span, feature, implied_by });
1132 }
1133}
1134
1135fn unnecessary_partially_stable_feature_lint(
1136 tcx: TyCtxt<'_>,
1137 span: Span,
1138 feature: Symbol,
1139 implies: Symbol,
1140 since: Symbol,
1141) {
1142 tcx.emit_node_span_lint(
1143 STABLE_FEATURES,
1144 hir::CRATE_HIR_ID,
1145 span,
1146 diagnostics::UnnecessaryPartialStableFeature {
1147 span,
1148 line: tcx.sess.source_map().span_extend_to_line(span),
1149 feature,
1150 since,
1151 implies,
1152 },
1153 );
1154}
1155
1156fn unnecessary_stable_feature_lint(
1157 tcx: TyCtxt<'_>,
1158 span: Span,
1159 feature: Symbol,
1160 mut since: Symbol,
1161) {
1162 if since.as_str() == VERSION_PLACEHOLDER {
1163 since = sym::env_CFG_RELEASE;
1164 }
1165 tcx.emit_node_span_lint(
1166 STABLE_FEATURES,
1167 hir::CRATE_HIR_ID,
1168 span,
1169 diagnostics::UnnecessaryStableFeature { feature, since },
1170 );
1171}
1172
1173fn duplicate_feature_lint(tcx: TyCtxt<'_>, span: Span, feature: Symbol) {
1174 tcx.emit_node_span_lint(
1175 DUPLICATE_FEATURES,
1176 hir::CRATE_HIR_ID,
1177 span,
1178 diagnostics::DuplicateFeature { feature },
1179 );
1180}