Skip to main content

rustc_attr_parsing/attributes/
deprecation.rs

1use 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: &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.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.expected_string_literal(v.value_span, Some(&v.value_as_lit()));
26            None
27        }
28    } else {
29        cx.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 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 since.as_str() == VERSION_PLACEHOLDER {
147                DeprecatedSince::RustcVersion(RustcVersion::CURRENT)
148            } else if let Some(version) = parse_version(since) {
149                DeprecatedSince::RustcVersion(version)
150            } else {
151                cx.emit_err(InvalidSince { span: cx.attr_span });
152                DeprecatedSince::Err
153            }
154        } else if is_rustc {
155            cx.emit_err(MissingSince { span: cx.attr_span });
156            DeprecatedSince::Err
157        } else {
158            DeprecatedSince::Unspecified
159        };
160
161        if is_rustc && note.is_none() {
162            cx.emit_err(MissingNote { span: cx.attr_span });
163            return None;
164        }
165
166        Some(AttributeKind::Deprecated {
167            deprecation: Deprecation { since, note, suggestion },
168            span: cx.attr_span,
169        })
170    }
171}