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_hir::attrs::{AttributeKind, InlineAttr};
6
7use super::prelude::*;
8
9pub(crate) struct InlineParser;
10
11impl<S: Stage> SingleAttributeParser<S> for InlineParser {
12    const PATH: &'static [Symbol] = &[sym::inline];
13    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
14    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
15    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
16        Allow(Target::Fn),
17        Allow(Target::Method(MethodKind::Inherent)),
18        Allow(Target::Method(MethodKind::Trait { body: true })),
19        Allow(Target::Method(MethodKind::TraitImpl)),
20        Allow(Target::Closure),
21        Allow(Target::Delegation { mac: false }),
22        Warn(Target::Method(MethodKind::Trait { body: false })),
23        Warn(Target::ForeignFn),
24        Warn(Target::Field),
25        Warn(Target::MacroDef),
26        Warn(Target::Arm),
27        Warn(Target::AssocConst),
28    ]);
29    const TEMPLATE: AttributeTemplate = template!(
30        Word,
31        List: &["always", "never"],
32        "https://doc.rust-lang.org/reference/attributes/codegen.html#the-inline-attribute"
33    );
34
35    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
36        match args {
37            ArgParser::NoArgs => Some(AttributeKind::Inline(InlineAttr::Hint, cx.attr_span)),
38            ArgParser::List(list) => {
39                let Some(l) = list.single() else {
40                    cx.expected_single_argument(list.span);
41                    return None;
42                };
43
44                match l.meta_item().and_then(|i| i.path().word_sym()) {
45                    Some(sym::always) => {
46                        Some(AttributeKind::Inline(InlineAttr::Always, cx.attr_span))
47                    }
48                    Some(sym::never) => {
49                        Some(AttributeKind::Inline(InlineAttr::Never, cx.attr_span))
50                    }
51                    _ => {
52                        cx.expected_specific_argument(l.span(), &[sym::always, sym::never]);
53                        return None;
54                    }
55                }
56            }
57            ArgParser::NameValue(_) => {
58                let suggestions = <Self as SingleAttributeParser<S>>::TEMPLATE
59                    .suggestions(cx.attr_style, "inline");
60                let span = cx.attr_span;
61                cx.emit_lint(AttributeLintKind::IllFormedAttributeInput { suggestions }, span);
62                return None;
63            }
64        }
65    }
66}
67
68pub(crate) struct RustcForceInlineParser;
69
70impl<S: Stage> SingleAttributeParser<S> for RustcForceInlineParser {
71    const PATH: &'static [Symbol] = &[sym::rustc_force_inline];
72    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
73    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
74    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
75    const TEMPLATE: AttributeTemplate = template!(Word, List: &["reason"], NameValueStr: "reason");
76
77    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
78        let reason = match args {
79            ArgParser::NoArgs => None,
80            ArgParser::List(list) => {
81                let Some(l) = list.single() else {
82                    cx.expected_single_argument(list.span);
83                    return None;
84                };
85
86                let Some(reason) = l.lit().and_then(|i| i.kind.str()) else {
87                    cx.expected_string_literal(l.span(), l.lit());
88                    return None;
89                };
90
91                Some(reason)
92            }
93            ArgParser::NameValue(v) => {
94                let Some(reason) = v.value_as_str() else {
95                    cx.expected_string_literal(v.value_span, Some(v.value_as_lit()));
96                    return None;
97                };
98
99                Some(reason)
100            }
101        };
102
103        Some(AttributeKind::Inline(
104            InlineAttr::Force { attr_span: cx.attr_span, reason },
105            cx.attr_span,
106        ))
107    }
108}