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