Skip to main content

rustc_attr_parsing/attributes/
deprecation.rs

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