rustc_attr_parsing/attributes/
deprecation.rs1use rustc_hir::attrs::{DeprecatedSince, Deprecation};
2use rustc_hir::{RustcVersion, VERSION_PLACEHOLDER};
3
4use super::prelude::*;
5use super::util::parse_version;
6use crate::session_diagnostics::{
7 DeprecatedItemSuggestion, InvalidSince, MissingNote, MissingSince,
8};
9
10fn get<S: Stage>(
11 cx: &mut AcceptContext<'_, '_, S>,
12 name: Symbol,
13 param_span: Span,
14 arg: &ArgParser,
15 item: Option<Symbol>,
16) -> Option<Ident> {
17 if item.is_some() {
18 cx.adcx().duplicate_key(param_span, name);
19 return None;
20 }
21 if let Some(v) = arg.name_value() {
22 if let Some(value_str) = v.value_as_ident() {
23 Some(value_str)
24 } else {
25 cx.adcx().expected_string_literal(v.value_span, Some(&v.value_as_lit()));
26 None
27 }
28 } else {
29 cx.adcx().expected_name_value(param_span, Some(name));
30 None
31 }
32}
33
34pub(crate) struct DeprecatedParser;
35impl<S: Stage> SingleAttributeParser<S> for DeprecatedParser {
36 const PATH: &[Symbol] = &[sym::deprecated];
37 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowListWarnRest(&[
38 Allow(Target::Fn),
39 Allow(Target::Mod),
40 Allow(Target::Struct),
41 Allow(Target::Enum),
42 Allow(Target::Union),
43 Allow(Target::Const),
44 Allow(Target::Static),
45 Allow(Target::MacroDef),
46 Allow(Target::Method(MethodKind::Inherent)),
47 Allow(Target::Method(MethodKind::Trait { body: false })),
48 Allow(Target::Method(MethodKind::Trait { body: true })),
49 Allow(Target::TyAlias),
50 Allow(Target::Use),
51 Allow(Target::ForeignFn),
52 Allow(Target::ForeignStatic),
53 Allow(Target::ForeignTy),
54 Allow(Target::Field),
55 Allow(Target::Trait),
56 Allow(Target::AssocTy),
57 Allow(Target::AssocConst),
58 Allow(Target::Variant),
59 Allow(Target::Impl { of_trait: false }),
60 Allow(Target::Crate),
61 Error(Target::WherePredicate),
62 ]);
63 const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
word: true,
list: Some(&[r#"since = "version""#, r#"note = "reason""#,
r#"since = "version", note = "reason""#]),
one_of: &[],
name_value_str: Some(&["reason"]),
docs: None,
}template!(
64 Word,
65 List: &[r#"since = "version""#, r#"note = "reason""#, r#"since = "version", note = "reason""#],
66 NameValueStr: "reason"
67 );
68
69 fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
70 let features = cx.features();
71
72 let mut since = None;
73 let mut note: Option<Ident> = None;
74 let mut suggestion = None;
75
76 let is_rustc = features.staged_api();
77
78 match args {
79 ArgParser::NoArgs => {
80 }
82 ArgParser::List(list) => {
83 for param in list.mixed() {
84 let Some(param) = param.meta_item() else {
85 cx.adcx().expected_not_literal(param.span());
86 return None;
87 };
88
89 let ident_name = param.path().word_sym();
90
91 match ident_name {
92 Some(name @ sym::since) => {
93 since = Some(get(cx, name, param.span(), param.args(), since)?.name);
94 }
95 Some(name @ sym::note) => {
96 note = Some(get(
97 cx,
98 name,
99 param.span(),
100 param.args(),
101 note.map(|ident| ident.name),
102 )?);
103 }
104 Some(name @ sym::suggestion) => {
105 if !features.deprecated_suggestion() {
106 cx.emit_err(DeprecatedItemSuggestion {
107 span: param.span(),
108 is_nightly: cx.sess().is_nightly_build(),
109 details: (),
110 });
111 }
112
113 suggestion =
114 Some(get(cx, name, param.span(), param.args(), suggestion)?.name);
115 }
116 _ => {
117 cx.adcx().expected_specific_argument(
118 param.span(),
119 if features.deprecated_suggestion() {
120 &[sym::since, sym::note, sym::suggestion]
121 } else {
122 &[sym::since, sym::note]
123 },
124 );
125 return None;
126 }
127 }
128 }
129 }
130 ArgParser::NameValue(v) => {
131 let Some(value) = v.value_as_ident() else {
132 cx.adcx().expected_string_literal(v.value_span, Some(v.value_as_lit()));
133 return None;
134 };
135 note = Some(value);
136 }
137 }
138
139 let since = if let Some(since) = since {
140 if since.as_str() == "TBD" {
141 DeprecatedSince::Future
142 } else if !is_rustc {
143 DeprecatedSince::NonStandard(since)
144 } else if since.as_str() == VERSION_PLACEHOLDER {
145 DeprecatedSince::RustcVersion(RustcVersion::CURRENT)
146 } else if let Some(version) = parse_version(since) {
147 DeprecatedSince::RustcVersion(version)
148 } else {
149 cx.emit_err(InvalidSince { span: cx.attr_span });
150 DeprecatedSince::Err
151 }
152 } else if is_rustc {
153 cx.emit_err(MissingSince { span: cx.attr_span });
154 DeprecatedSince::Err
155 } else {
156 DeprecatedSince::Unspecified
157 };
158
159 if is_rustc && note.is_none() {
160 cx.emit_err(MissingNote { span: cx.attr_span });
161 return None;
162 }
163
164 Some(AttributeKind::Deprecated {
165 deprecation: Deprecation { since, note, suggestion },
166 span: cx.attr_span,
167 })
168 }
169}