Skip to main content

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};
6use rustc_session::lint::builtin::ILL_FORMED_ATTRIBUTE_INPUT;
7
8use super::prelude::*;
9
10pub(crate) struct InlineParser;
11
12impl SingleAttributeParser for InlineParser {
13    const PATH: &[Symbol] = &[sym::inline];
14    const ON_DUPLICATE: OnDuplicate = 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        Warn(Target::MacroCall),
29    ]);
30    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: true,
    list: Some(&["always", "never"]),
    one_of: &[],
    name_value_str: None,
    docs: Some("https://doc.rust-lang.org/reference/attributes/codegen.html#the-inline-attribute"),
}template!(
31        Word,
32        List: &["always", "never"],
33        "https://doc.rust-lang.org/reference/attributes/codegen.html#the-inline-attribute"
34    );
35
36    fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
37        match args {
38            ArgParser::NoArgs => Some(AttributeKind::Inline(InlineAttr::Hint, cx.attr_span)),
39            ArgParser::List(list) => {
40                let l = cx.expect_single(list)?;
41
42                match l.meta_item().and_then(|i| i.path().word_sym()) {
43                    Some(sym::always) => {
44                        Some(AttributeKind::Inline(InlineAttr::Always, cx.attr_span))
45                    }
46                    Some(sym::never) => {
47                        Some(AttributeKind::Inline(InlineAttr::Never, cx.attr_span))
48                    }
49                    _ => {
50                        cx.adcx().expected_specific_argument(l.span(), &[sym::always, sym::never]);
51                        return None;
52                    }
53                }
54            }
55            ArgParser::NameValue(_) => {
56                cx.adcx().warn_ill_formed_attribute_input(ILL_FORMED_ATTRIBUTE_INPUT);
57                return None;
58            }
59        }
60    }
61}
62
63pub(crate) struct RustcForceInlineParser;
64
65impl SingleAttributeParser for RustcForceInlineParser {
66    const PATH: &[Symbol] = &[sym::rustc_force_inline];
67    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
68        Allow(Target::Fn),
69        Allow(Target::Method(MethodKind::Inherent)),
70    ]);
71
72    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: true,
    list: Some(&["reason"]),
    one_of: &[],
    name_value_str: Some(&["reason"]),
    docs: None,
}template!(Word, List: &["reason"], NameValueStr: "reason");
73
74    fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
75        let reason = match args {
76            ArgParser::NoArgs => None,
77            ArgParser::List(list) => {
78                let l = cx.expect_single(list)?;
79
80                let Some(reason) = l.lit().and_then(|i| i.kind.str()) else {
81                    cx.adcx().expected_string_literal(l.span(), l.lit());
82                    return None;
83                };
84
85                Some(reason)
86            }
87            ArgParser::NameValue(v) => {
88                let Some(reason) = v.value_as_str() else {
89                    cx.adcx().expected_string_literal(v.value_span, Some(v.value_as_lit()));
90                    return None;
91                };
92
93                Some(reason)
94            }
95        };
96
97        Some(AttributeKind::Inline(
98            InlineAttr::Force { attr_span: cx.attr_span, reason },
99            cx.attr_span,
100        ))
101    }
102}