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_feature::{AttributeTemplate, template};
6use rustc_hir::attrs::{AttributeKind, InlineAttr};
7use rustc_hir::lints::AttributeLintKind;
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::KeepOutermost;
20    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
21    const TEMPLATE: AttributeTemplate = template!(
22        Word,
23        List: &["always", "never"],
24        "https://doc.rust-lang.org/reference/attributes/codegen.html#the-inline-attribute"
25    );
26
27    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
28        match args {
29            ArgParser::NoArgs => Some(AttributeKind::Inline(InlineAttr::Hint, cx.attr_span)),
30            ArgParser::List(list) => {
31                let Some(l) = list.single() else {
32                    cx.expected_single_argument(list.span);
33                    return None;
34                };
35
36                match l.meta_item().and_then(|i| i.path().word_sym()) {
37                    Some(sym::always) => {
38                        Some(AttributeKind::Inline(InlineAttr::Always, cx.attr_span))
39                    }
40                    Some(sym::never) => {
41                        Some(AttributeKind::Inline(InlineAttr::Never, cx.attr_span))
42                    }
43                    _ => {
44                        cx.expected_specific_argument(l.span(), vec!["always", "never"]);
45                        return None;
46                    }
47                }
48            }
49            ArgParser::NameValue(_) => {
50                let suggestions =
51                    <Self as SingleAttributeParser<S>>::TEMPLATE.suggestions(false, "inline");
52                let span = cx.attr_span;
53                cx.emit_lint(AttributeLintKind::IllFormedAttributeInput { suggestions }, span);
54                return None;
55            }
56        }
57    }
58}
59
60pub(crate) struct RustcForceInlineParser;
61
62impl<S: Stage> SingleAttributeParser<S> for RustcForceInlineParser {
63    const PATH: &'static [Symbol] = &[sym::rustc_force_inline];
64    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
65    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
66    const TEMPLATE: AttributeTemplate = template!(Word, List: &["reason"], NameValueStr: "reason");
67
68    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
69        let reason = match args {
70            ArgParser::NoArgs => None,
71            ArgParser::List(list) => {
72                let Some(l) = list.single() else {
73                    cx.expected_single_argument(list.span);
74                    return None;
75                };
76
77                let Some(reason) = l.lit().and_then(|i| i.kind.str()) else {
78                    cx.expected_string_literal(l.span(), l.lit());
79                    return None;
80                };
81
82                Some(reason)
83            }
84            ArgParser::NameValue(v) => {
85                let Some(reason) = v.value_as_str() else {
86                    cx.expected_string_literal(v.value_span, Some(v.value_as_lit()));
87                    return None;
88                };
89
90                Some(reason)
91            }
92        };
93
94        Some(AttributeKind::Inline(
95            InlineAttr::Force { attr_span: cx.attr_span, reason },
96            cx.attr_span,
97        ))
98    }
99}