rustc_attr_parsing/attributes/
inline.rs

1// FIXME(jdonszelmann): merge these two parsers and error when both attributes are present here.
2//                      note: need to model better how duplicate attr errors work when not using
3//                      SingleAttributeParser which is what we have two of here.
4
5use rustc_attr_data_structures::lints::AttributeLintKind;
6use rustc_attr_data_structures::{AttributeKind, InlineAttr};
7use rustc_feature::{AttributeTemplate, template};
8use rustc_span::{Symbol, sym};
9
10use super::{AcceptContext, AttributeOrder, OnDuplicate};
11use crate::attributes::SingleAttributeParser;
12use crate::context::Stage;
13use crate::parser::ArgParser;
14
15pub(crate) struct InlineParser;
16
17impl<S: Stage> SingleAttributeParser<S> for InlineParser {
18    const PATH: &'static [Symbol] = &[sym::inline];
19    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
20    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
21    const TEMPLATE: AttributeTemplate = template!(Word, List: "always|never");
22
23    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
24        match args {
25            ArgParser::NoArgs => Some(AttributeKind::Inline(InlineAttr::Hint, cx.attr_span)),
26            ArgParser::List(list) => {
27                let Some(l) = list.single() else {
28                    cx.expected_single_argument(list.span);
29                    return None;
30                };
31
32                match l.meta_item().and_then(|i| i.path().word_sym()) {
33                    Some(sym::always) => {
34                        Some(AttributeKind::Inline(InlineAttr::Always, cx.attr_span))
35                    }
36                    Some(sym::never) => {
37                        Some(AttributeKind::Inline(InlineAttr::Never, cx.attr_span))
38                    }
39                    _ => {
40                        cx.expected_specific_argument(l.span(), vec!["always", "never"]);
41                        return None;
42                    }
43                }
44            }
45            ArgParser::NameValue(_) => {
46                let suggestions =
47                    <Self as SingleAttributeParser<S>>::TEMPLATE.suggestions(false, "inline");
48                cx.emit_lint(
49                    AttributeLintKind::IllFormedAttributeInput { suggestions },
50                    cx.attr_span,
51                );
52                return None;
53            }
54        }
55    }
56}
57
58pub(crate) struct RustcForceInlineParser;
59
60impl<S: Stage> SingleAttributeParser<S> for RustcForceInlineParser {
61    const PATH: &'static [Symbol] = &[sym::rustc_force_inline];
62    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
63    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
64    const TEMPLATE: AttributeTemplate = template!(Word, List: "reason", NameValueStr: "reason");
65
66    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
67        let reason = match args {
68            ArgParser::NoArgs => None,
69            ArgParser::List(list) => {
70                let Some(l) = list.single() else {
71                    cx.expected_single_argument(list.span);
72                    return None;
73                };
74
75                let Some(reason) = l.lit().and_then(|i| i.kind.str()) else {
76                    cx.expected_string_literal(l.span(), l.lit());
77                    return None;
78                };
79
80                Some(reason)
81            }
82            ArgParser::NameValue(v) => {
83                let Some(reason) = v.value_as_str() else {
84                    cx.expected_string_literal(v.value_span, Some(v.value_as_lit()));
85                    return None;
86                };
87
88                Some(reason)
89            }
90        };
91
92        Some(AttributeKind::Inline(
93            InlineAttr::Force { attr_span: cx.attr_span, reason },
94            cx.attr_span,
95        ))
96    }
97}