Skip to content

Commit 7f9b824

Browse files
authored
Rollup merge of #160459 - obeis:check_deprecated, r=JonathanBrouwer
Use attribute parser for `deprecated` attribute checking Updates #153101 r? @JonathanBrouwer
2 parents 19b78cc + da0ecbb commit 7f9b824

17 files changed

Lines changed: 190 additions & 110 deletions

File tree

compiler/rustc_ast/src/visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
1616
pub use rustc_ast_ir::visit::VisitorResult;
1717
pub use rustc_ast_ir::{try_visit, visit_opt, walk_list, walk_visitable_list};
18+
use rustc_macros::StableHash;
1819
use rustc_span::{Ident, Span, Spanned, Symbol};
1920
use thin_vec::ThinVec;
2021

2122
use crate::ast::*;
2223
use crate::tokenstream::DelimSpan;
2324

24-
#[derive(Copy, Clone, Debug, PartialEq)]
25+
#[derive(Copy, Clone, Debug, PartialEq, Eq, StableHash)]
2526
pub enum AssocCtxt {
2627
Trait,
2728
Impl { of_trait: bool },

compiler/rustc_attr_ir/src/lang_items.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_span::def_id::DefId;
1414
use rustc_span::{Symbol, kw, sym};
1515

1616
use crate::PrintAttribute;
17-
use crate::target::{MethodKind, Target};
17+
use crate::target::{AssocCtxt, MethodKind, Target};
1818

1919
/// All of the lang items, defined or not.
2020
/// Defined lang items can come from the current crate or its dependencies.
@@ -161,8 +161,8 @@ language_item_table! {
161161
MetaSized, sym::meta_sized, meta_sized_trait, Target::Trait, GenericRequirement::Exact(0);
162162
PointeeSized, sym::pointee_sized, pointee_sized_trait, Target::Trait, GenericRequirement::Exact(0);
163163
Unsize, sym::unsize, unsize_trait, Target::Trait, GenericRequirement::Minimum(1);
164-
AlignOf, sym::mem_align_const, align_const, Target::AssocConst, GenericRequirement::Exact(0);
165-
SizeOf, sym::mem_size_const, size_const, Target::AssocConst, GenericRequirement::Exact(0);
164+
AlignOf, sym::mem_align_const, align_const, Target::AssocConst(AssocCtxt::Trait), GenericRequirement::Exact(0);
165+
SizeOf, sym::mem_size_const, size_const, Target::AssocConst(AssocCtxt::Trait), GenericRequirement::Exact(0);
166166
OffsetOf, sym::offset_of, offset_of, Target::Fn, GenericRequirement::Exact(1);
167167
/// Trait injected by `#[derive(PartialEq)]`, (i.e. "Partial EQ").
168168
StructuralPeq, sym::structural_peq, structural_peq_trait, Target::Trait, GenericRequirement::None;
@@ -174,10 +174,10 @@ language_item_table! {
174174
Sync, sym::sync, sync_trait, Target::Trait, GenericRequirement::Exact(0);
175175
DiscriminantKind, sym::discriminant_kind, discriminant_kind_trait, Target::Trait, GenericRequirement::None;
176176
/// The associated item of the `DiscriminantKind` trait.
177-
Discriminant, sym::discriminant_type, discriminant_type, Target::AssocTy, GenericRequirement::None;
177+
Discriminant, sym::discriminant_type, discriminant_type, Target::AssocTy(AssocCtxt::Trait), GenericRequirement::None;
178178

179179
PointeeTrait, sym::pointee_trait, pointee_trait, Target::Trait, GenericRequirement::None;
180-
Metadata, sym::metadata_type, metadata_type, Target::AssocTy, GenericRequirement::None;
180+
Metadata, sym::metadata_type, metadata_type, Target::AssocTy(AssocCtxt::Trait), GenericRequirement::None;
181181
DynMetadata, sym::dyn_metadata, dyn_metadata, Target::Struct, GenericRequirement::None;
182182

183183
NonNull, sym::non_null, non_null_trait, Target::Struct, GenericRequirement::Exact(1);
@@ -241,9 +241,9 @@ language_item_table! {
241241
Deref, sym::deref, deref_trait, Target::Trait, GenericRequirement::Exact(0);
242242
DerefMut, sym::deref_mut, deref_mut_trait, Target::Trait, GenericRequirement::Exact(0);
243243
DerefPure, sym::deref_pure, deref_pure_trait, Target::Trait, GenericRequirement::Exact(0);
244-
DerefTarget, sym::deref_target, deref_target, Target::AssocTy, GenericRequirement::None;
244+
DerefTarget, sym::deref_target, deref_target, Target::AssocTy(AssocCtxt::Trait), GenericRequirement::None;
245245
Receiver, sym::receiver, receiver_trait, Target::Trait, GenericRequirement::None;
246-
ReceiverTarget, sym::receiver_target, receiver_target, Target::AssocTy, GenericRequirement::None;
246+
ReceiverTarget, sym::receiver_target, receiver_target, Target::AssocTy(AssocCtxt::Trait), GenericRequirement::None;
247247
LegacyReceiver, sym::legacy_receiver, legacy_receiver_trait, Target::Trait, GenericRequirement::None;
248248

249249
Fn, kw::Fn, fn_trait, Target::Trait, GenericRequirement::Exact(1);
@@ -254,24 +254,24 @@ language_item_table! {
254254
AsyncFn, sym::async_fn, async_fn_trait, Target::Trait, GenericRequirement::Exact(1);
255255
AsyncFnMut, sym::async_fn_mut, async_fn_mut_trait, Target::Trait, GenericRequirement::Exact(1);
256256
AsyncFnOnce, sym::async_fn_once, async_fn_once_trait, Target::Trait, GenericRequirement::Exact(1);
257-
AsyncFnOnceOutput, sym::async_fn_once_output, async_fn_once_output, Target::AssocTy, GenericRequirement::Exact(1);
258-
CallOnceFuture, sym::call_once_future, call_once_future, Target::AssocTy, GenericRequirement::Exact(1);
259-
CallRefFuture, sym::call_ref_future, call_ref_future, Target::AssocTy, GenericRequirement::Exact(2);
257+
AsyncFnOnceOutput, sym::async_fn_once_output, async_fn_once_output, Target::AssocTy(AssocCtxt::Trait), GenericRequirement::Exact(1);
258+
CallOnceFuture, sym::call_once_future, call_once_future, Target::AssocTy(AssocCtxt::Trait), GenericRequirement::Exact(1);
259+
CallRefFuture, sym::call_ref_future, call_ref_future, Target::AssocTy(AssocCtxt::Trait), GenericRequirement::Exact(2);
260260
AsyncFnKindHelper, sym::async_fn_kind_helper, async_fn_kind_helper, Target::Trait, GenericRequirement::Exact(1);
261-
AsyncFnKindUpvars, sym::async_fn_kind_upvars, async_fn_kind_upvars, Target::AssocTy, GenericRequirement::Exact(5);
261+
AsyncFnKindUpvars, sym::async_fn_kind_upvars, async_fn_kind_upvars, Target::AssocTy(AssocCtxt::Trait), GenericRequirement::Exact(5);
262262

263-
FnOnceOutput, sym::fn_once_output, fn_once_output, Target::AssocTy, GenericRequirement::None;
263+
FnOnceOutput, sym::fn_once_output, fn_once_output, Target::AssocTy(AssocCtxt::Trait), GenericRequirement::None;
264264

265265
Iterator, sym::iterator, iterator_trait, Target::Trait, GenericRequirement::Exact(0);
266266
FusedIterator, sym::fused_iterator, fused_iterator_trait, Target::Trait, GenericRequirement::Exact(0);
267267
Future, sym::future_trait, future_trait, Target::Trait, GenericRequirement::Exact(0);
268-
FutureOutput, sym::future_output, future_output, Target::AssocTy, GenericRequirement::Exact(0);
268+
FutureOutput, sym::future_output, future_output, Target::AssocTy(AssocCtxt::Trait), GenericRequirement::Exact(0);
269269
AsyncIterator, sym::async_iterator, async_iterator_trait, Target::Trait, GenericRequirement::Exact(0);
270270

271271
CoroutineState, sym::coroutine_state, coroutine_state, Target::Enum, GenericRequirement::None;
272272
Coroutine, sym::coroutine, coroutine_trait, Target::Trait, GenericRequirement::Exact(1);
273-
CoroutineReturn, sym::coroutine_return, coroutine_return, Target::AssocTy, GenericRequirement::Exact(1);
274-
CoroutineYield, sym::coroutine_yield, coroutine_yield, Target::AssocTy, GenericRequirement::Exact(1);
273+
CoroutineReturn, sym::coroutine_return, coroutine_return, Target::AssocTy(AssocCtxt::Trait), GenericRequirement::Exact(1);
274+
CoroutineYield, sym::coroutine_yield, coroutine_yield, Target::AssocTy(AssocCtxt::Trait), GenericRequirement::Exact(1);
275275
CoroutineResume, sym::coroutine_resume, coroutine_resume, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
276276

277277
Unpin, sym::unpin, unpin_trait, Target::Trait, GenericRequirement::None;
@@ -389,8 +389,8 @@ language_item_table! {
389389
PollPending, sym::Pending, poll_pending_variant, Target::Variant, GenericRequirement::None;
390390

391391
AsyncGenReady, sym::AsyncGenReady, async_gen_ready, Target::Method(MethodKind::Inherent), GenericRequirement::Exact(1);
392-
AsyncGenPending, sym::AsyncGenPending, async_gen_pending, Target::AssocConst, GenericRequirement::Exact(1);
393-
AsyncGenFinished, sym::AsyncGenFinished, async_gen_finished, Target::AssocConst, GenericRequirement::Exact(1);
392+
AsyncGenPending, sym::AsyncGenPending, async_gen_pending, Target::AssocConst(AssocCtxt::Impl { of_trait: false }), GenericRequirement::Exact(1);
393+
AsyncGenFinished, sym::AsyncGenFinished, async_gen_finished, Target::AssocConst(AssocCtxt::Impl { of_trait: false }), GenericRequirement::Exact(1);
394394

395395
// FIXME(swatinem): the following lang items are used for async lowering and
396396
// should become obsolete eventually.
@@ -426,8 +426,8 @@ language_item_table! {
426426
Range, sym::Range, range_struct, Target::Struct, GenericRequirement::None;
427427
RangeToInclusive, sym::RangeToInclusive, range_to_inclusive_struct, Target::Struct, GenericRequirement::None;
428428
RangeTo, sym::RangeTo, range_to_struct, Target::Struct, GenericRequirement::None;
429-
RangeMax, sym::RangeMax, range_max, Target::AssocConst, GenericRequirement::Exact(0);
430-
RangeMin, sym::RangeMin, range_min, Target::AssocConst, GenericRequirement::Exact(0);
429+
RangeMax, sym::RangeMax, range_max, Target::AssocConst(AssocCtxt::Trait), GenericRequirement::Exact(0);
430+
RangeMin, sym::RangeMin, range_min, Target::AssocConst(AssocCtxt::Trait), GenericRequirement::Exact(0);
431431
RangeSub, sym::RangeSub, range_sub, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::Exact(0);
432432

433433
// `new_range` types that are `Copy + IntoIterator`
@@ -458,9 +458,9 @@ language_item_table! {
458458
// Field representing types.
459459
FieldRepresentingType, sym::field_representing_type, field_representing_type, Target::Struct, GenericRequirement::Exact(3);
460460
Field, sym::field, field, Target::Trait, GenericRequirement::Exact(0);
461-
FieldBase, sym::field_base, field_base, Target::AssocTy, GenericRequirement::Exact(0);
462-
FieldType, sym::field_type, field_type, Target::AssocTy, GenericRequirement::Exact(0);
463-
FieldOffset, sym::field_offset, field_offset, Target::AssocConst, GenericRequirement::Exact(0);
461+
FieldBase, sym::field_base, field_base, Target::AssocTy(AssocCtxt::Trait), GenericRequirement::Exact(0);
462+
FieldType, sym::field_type, field_type, Target::AssocTy(AssocCtxt::Trait), GenericRequirement::Exact(0);
463+
FieldOffset, sym::field_offset, field_offset, Target::AssocConst(AssocCtxt::Trait), GenericRequirement::Exact(0);
464464

465465
// Used to fallback `{float}` to `f32` when `f32: From<{float}>`
466466
From, sym::From, from_trait, Target::Trait, GenericRequirement::Exact(1);

compiler/rustc_attr_ir/src/target.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::fmt::{self, Display};
44

5-
use rustc_ast::visit::AssocCtxt;
5+
pub use rustc_ast::visit::AssocCtxt;
66
use rustc_ast::{AssocItemKind, ForeignItemKind, ast};
77
use rustc_macros::StableHash;
88

@@ -49,9 +49,9 @@ pub enum Target {
4949
Expression,
5050
Statement,
5151
Arm,
52-
AssocConst,
52+
AssocConst(AssocCtxt),
5353
Method(MethodKind),
54-
AssocTy,
54+
AssocTy(AssocCtxt),
5555
ForeignFn,
5656
ForeignStatic,
5757
ForeignTy,
@@ -81,7 +81,7 @@ rustc_error_messages::into_diag_arg_using_display!(Target);
8181
impl Target {
8282
pub fn is_associated_item(self) -> bool {
8383
match self {
84-
Target::AssocConst | Target::AssocTy | Target::Method(_) => true,
84+
Target::AssocConst(_) | Target::AssocTy(_) | Target::Method(_) => true,
8585
Target::ExternCrate
8686
| Target::Use
8787
| Target::Static
@@ -157,7 +157,7 @@ impl Target {
157157

158158
pub fn from_assoc_item_kind(kind: &ast::AssocItemKind, assoc_ctxt: AssocCtxt) -> Target {
159159
match kind {
160-
AssocItemKind::Const(_) => Target::AssocConst,
160+
AssocItemKind::Const(_) => Target::AssocConst(assoc_ctxt),
161161
AssocItemKind::Fn(f) => Target::Method(match assoc_ctxt {
162162
AssocCtxt::Trait => MethodKind::Trait { body: f.body.is_some() },
163163
AssocCtxt::Impl { of_trait, .. } => {
@@ -168,7 +168,7 @@ impl Target {
168168
}
169169
}
170170
}),
171-
AssocItemKind::Type(_) => Target::AssocTy,
171+
AssocItemKind::Type(_) => Target::AssocTy(assoc_ctxt),
172172
AssocItemKind::Delegation(_) => Target::Delegation { mac: false },
173173
AssocItemKind::DelegationMac(_) => Target::Delegation { mac: true },
174174
AssocItemKind::MacCall(_) => Target::MacroCall,
@@ -210,14 +210,14 @@ impl Target {
210210
Target::Expression => "expression",
211211
Target::Statement => "statement",
212212
Target::Arm => "match arm",
213-
Target::AssocConst => "associated const",
213+
Target::AssocConst(_) => "associated const",
214214
Target::Method(kind) => match kind {
215215
MethodKind::Inherent => "inherent method",
216216
MethodKind::Trait { body: false } => "required trait method",
217217
MethodKind::Trait { body: true } => "provided trait method",
218218
MethodKind::TraitImpl => "trait method in an impl block",
219219
},
220-
Target::AssocTy => "associated type",
220+
Target::AssocTy(_) => "associated type",
221221
Target::ForeignFn => "foreign function",
222222
Target::ForeignStatic => "foreign static item",
223223
Target::ForeignTy => "foreign type",
@@ -265,14 +265,14 @@ impl Target {
265265
Target::Expression => "expressions",
266266
Target::Statement => "statements",
267267
Target::Arm => "match arms",
268-
Target::AssocConst => "associated consts",
268+
Target::AssocConst(_) => "associated consts",
269269
Target::Method(kind) => match kind {
270270
MethodKind::Inherent => "inherent methods",
271271
MethodKind::Trait { body: false } => "required trait methods",
272272
MethodKind::Trait { body: true } => "provided trait methods",
273273
MethodKind::TraitImpl => "trait methods in impl blocks",
274274
},
275-
Target::AssocTy => "associated types",
275+
Target::AssocTy(_) => "associated types",
276276
Target::ForeignFn => "foreign functions",
277277
Target::ForeignStatic => "foreign statics",
278278
Target::ForeignTy => "foreign types",

compiler/rustc_attr_parsing/src/attributes/deprecation.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
use rustc_ast::LitKind;
22
use rustc_attr_ir::{DeprecatedSince, Deprecation, RustcVersion, VERSION_PLACEHOLDER};
33
use rustc_feature::AttributeStability;
4+
use rustc_lint_defs::builtin::UNUSED_ATTRIBUTES;
45

56
use super::prelude::*;
67
use super::util::parse_version;
7-
use crate::diagnostics::{DeprecatedItemSuggestion, InvalidSince, MissingNote, MissingSince};
8+
use crate::diagnostics::{
9+
DeprecatedAnnotationHasNoEffect, DeprecatedItemSuggestion, InvalidSince, MissingNote,
10+
MissingSince,
11+
};
12+
use crate::target_checking::Policy::AllowSilent;
813

914
fn get(
1015
cx: &mut AcceptContext<'_, '_>,
@@ -48,8 +53,12 @@ impl SingleAttributeParser for DeprecatedParser {
4853
Allow(Target::ForeignTy),
4954
Allow(Target::Field),
5055
Allow(Target::Trait),
51-
Allow(Target::AssocTy),
52-
Allow(Target::AssocConst),
56+
Allow(Target::AssocConst(AssocCtxt::Impl { of_trait: false })),
57+
Allow(Target::AssocConst(AssocCtxt::Trait)),
58+
AllowSilent(Target::AssocConst(AssocCtxt::Impl { of_trait: true })),
59+
Allow(Target::AssocTy(AssocCtxt::Impl { of_trait: false })),
60+
Allow(Target::AssocTy(AssocCtxt::Trait)),
61+
AllowSilent(Target::AssocTy(AssocCtxt::Impl { of_trait: true })),
5362
Allow(Target::Variant),
5463
Allow(Target::Impl { of_trait: false }),
5564
Allow(Target::Crate),
@@ -182,6 +191,22 @@ impl SingleAttributeParser for DeprecatedParser {
182191
return None;
183192
}
184193

194+
// `#[deprecated]` on trait-impl associated items has no effect (deprecation comes from the
195+
// trait definition). Methods also get `useless_deprecated` from target checking.
196+
if matches!(
197+
cx.target,
198+
Target::Method(MethodKind::TraitImpl)
199+
| Target::AssocConst(AssocCtxt::Impl { of_trait: true })
200+
| Target::AssocTy(AssocCtxt::Impl { of_trait: true })
201+
) {
202+
let attr_span = cx.attr_span;
203+
cx.emit_lint(
204+
UNUSED_ATTRIBUTES,
205+
DeprecatedAnnotationHasNoEffect { span: attr_span },
206+
attr_span,
207+
);
208+
}
209+
185210
Some(AttributeKind::Deprecated {
186211
deprecation: Deprecation { since, note, suggestion },
187212
span: cx.attr_span,

compiler/rustc_attr_parsing/src/attributes/doc.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,12 +777,16 @@ impl AttributeParser for DocParser {
777777
// Allow(Target::TraitAlias),
778778
// Allow(Target::Impl { of_trait: true }),
779779
// Allow(Target::Impl { of_trait: false }),
780-
// Allow(Target::AssocConst),
780+
// Allow(Target::AssocConst(AssocCtxt::Impl { of_trait: false })),
781+
// Allow(Target::AssocConst(AssocCtxt::Trait)),
782+
// Allow(Target::AssocConst(AssocCtxt::Impl { of_trait: true })),
781783
// Allow(Target::Method(MethodKind::Inherent)),
782784
// Allow(Target::Method(MethodKind::Trait { body: true })),
783785
// Allow(Target::Method(MethodKind::Trait { body: false })),
784786
// Allow(Target::Method(MethodKind::TraitImpl)),
785-
// Allow(Target::AssocTy),
787+
// Allow(Target::AssocTy(AssocCtxt::Impl { of_trait: false })),
788+
// Allow(Target::AssocTy(AssocCtxt::Trait)),
789+
// Allow(Target::AssocTy(AssocCtxt::Impl { of_trait: true })),
786790
// Allow(Target::ForeignFn),
787791
// Allow(Target::ForeignStatic),
788792
// Allow(Target::ForeignTy),

compiler/rustc_attr_parsing/src/attributes/inline.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ impl SingleAttributeParser for InlineParser {
2222
Warn(Target::Field),
2323
Warn(Target::MacroDef),
2424
Warn(Target::Arm),
25-
Warn(Target::AssocConst),
25+
Warn(Target::AssocConst(AssocCtxt::Impl { of_trait: false })),
26+
Warn(Target::AssocConst(AssocCtxt::Trait)),
27+
Warn(Target::AssocConst(AssocCtxt::Impl { of_trait: true })),
2628
Warn(Target::MacroCall),
2729
]);
2830
const TEMPLATE: AttributeTemplate = template!(

0 commit comments

Comments
 (0)