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                let span = cx.attr_span;
49                cx.emit_lint(AttributeLintKind::IllFormedAttributeInput { suggestions }, span);
50                return None;
51            }
52        }
53    }
54}
55
56pub(crate) struct RustcForceInlineParser;
57
58impl<S: Stage> SingleAttributeParser<S> for RustcForceInlineParser {
59    const PATH: &'static [Symbol] = &[sym::rustc_force_inline];
60    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
61    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
62    const TEMPLATE: AttributeTemplate = template!(Word, List: "reason", NameValueStr: "reason");
63
64    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
65        let reason = match args {
66            ArgParser::NoArgs => None,
67            ArgParser::List(list) => {
68                let Some(l) = list.single() else {
69                    cx.expected_single_argument(list.span);
70                    return None;
71                };
72
73                let Some(reason) = l.lit().and_then(|i| i.kind.str()) else {
74                    cx.expected_string_literal(l.span(), l.lit());
75                    return None;
76                };
77
78                Some(reason)
79            }
80            ArgParser::NameValue(v) => {
81                let Some(reason) = v.value_as_str() else {
82                    cx.expected_string_literal(v.value_span, Some(v.value_as_lit()));
83                    return None;
84                };
85
86                Some(reason)
87            }
88        };
89
90        Some(AttributeKind::Inline(
91            InlineAttr::Force { attr_span: cx.attr_span, reason },
92            cx.attr_span,
93        ))
94    }
95}