rustc_attr_parsing/attributes/
deprecation.rs

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