rustc_attr_parsing/attributes/
inline.rs1use 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 Warn(Target::MacroCall),
29 ]);
30 const TEMPLATE: AttributeTemplate = 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<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
37 match args {
38 ArgParser::NoArgs => Some(AttributeKind::Inline(InlineAttr::Hint, cx.attr_span)),
39 ArgParser::List(list) => {
40 let Some(l) = list.single() else {
41 cx.expected_single_argument(list.span);
42 return None;
43 };
44
45 match l.meta_item().and_then(|i| i.path().word_sym()) {
46 Some(sym::always) => {
47 Some(AttributeKind::Inline(InlineAttr::Always, cx.attr_span))
48 }
49 Some(sym::never) => {
50 Some(AttributeKind::Inline(InlineAttr::Never, cx.attr_span))
51 }
52 _ => {
53 cx.expected_specific_argument(l.span(), &[sym::always, sym::never]);
54 return None;
55 }
56 }
57 }
58 ArgParser::NameValue(_) => {
59 let suggestions = <Self as SingleAttributeParser<S>>::TEMPLATE
60 .suggestions(cx.attr_style, "inline");
61 let span = cx.attr_span;
62 cx.emit_lint(AttributeLintKind::IllFormedAttributeInput { suggestions }, span);
63 return None;
64 }
65 }
66 }
67}
68
69pub(crate) struct RustcForceInlineParser;
70
71impl<S: Stage> SingleAttributeParser<S> for RustcForceInlineParser {
72 const PATH: &'static [Symbol] = &[sym::rustc_force_inline];
73 const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
74 const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
75 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
76 const TEMPLATE: AttributeTemplate = template!(Word, List: &["reason"], NameValueStr: "reason");
77
78 fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
79 let reason = match args {
80 ArgParser::NoArgs => None,
81 ArgParser::List(list) => {
82 let Some(l) = list.single() else {
83 cx.expected_single_argument(list.span);
84 return None;
85 };
86
87 let Some(reason) = l.lit().and_then(|i| i.kind.str()) else {
88 cx.expected_string_literal(l.span(), l.lit());
89 return None;
90 };
91
92 Some(reason)
93 }
94 ArgParser::NameValue(v) => {
95 let Some(reason) = v.value_as_str() else {
96 cx.expected_string_literal(v.value_span, Some(v.value_as_lit()));
97 return None;
98 };
99
100 Some(reason)
101 }
102 };
103
104 Some(AttributeKind::Inline(
105 InlineAttr::Force { attr_span: cx.attr_span, reason },
106 cx.attr_span,
107 ))
108 }
109}