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