Skip to main content

rustc_attr_parsing/attributes/
inline.rs

1use rustc_attr_ir::{AttributeKind, InlineAttr, find_attr};
2use rustc_feature::AttributeStability;
3use rustc_session::lint::builtin::ILL_FORMED_ATTRIBUTE_INPUT;
4
5use super::prelude::*;
6use crate::diagnostics::InlineForceInlineConflict;
7
8pub(crate) struct InlineParser;
9
10impl SingleAttributeParser for InlineParser {
11    const PATH: &[Symbol] = &[sym::inline];
12    const ON_DUPLICATE: OnDuplicate = OnDuplicate::WarnButFutureError;
13    const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
14        Allow(Target::Fn),
15        Allow(Target::Method(MethodKind::Inherent)),
16        Allow(Target::Method(MethodKind::Trait { body: true })),
17        Allow(Target::Method(MethodKind::TraitImpl)),
18        Allow(Target::Closure),
19        Allow(Target::Delegation { mac: false }),
20        Warn(Target::Method(MethodKind::Trait { body: false })),
21        Warn(Target::ForeignFn),
22        Warn(Target::Field),
23        Warn(Target::MacroDef),
24        Warn(Target::Arm),
25        Warn(Target::AssocConst),
26        Warn(Target::MacroCall),
27    ]);
28    const TEMPLATE: AttributeTemplate = crate::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!(
29        Word,
30        List: &["always", "never"],
31        "https://doc.rust-lang.org/reference/attributes/codegen.html#the-inline-attribute"
32    );
33    const STABILITY: AttributeStability = AttributeStability::Stable;
34
35    fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
36        match args {
37            ArgParser::NoArgs => Some(AttributeKind::Inline(InlineAttr::Hint, cx.attr_span)),
38            ArgParser::List(list) => {
39                let l = cx.expect_single(list)?;
40
41                match l.meta_item_no_args().and_then(|i| i.path().word_sym()) {
42                    Some(sym::always) => {
43                        Some(AttributeKind::Inline(InlineAttr::Always, cx.attr_span))
44                    }
45                    Some(sym::never) => {
46                        Some(AttributeKind::Inline(InlineAttr::Never, cx.attr_span))
47                    }
48                    _ => {
49                        cx.adcx().expected_specific_argument(l.span(), &[sym::always, sym::never]);
50                        None
51                    }
52                }
53            }
54            ArgParser::NameValue(_) => {
55                cx.adcx().warn_ill_formed_attribute_input(ILL_FORMED_ATTRIBUTE_INPUT);
56                None
57            }
58        }
59    }
60}
61
62pub(crate) struct RustcForceInlineParser;
63
64impl SingleAttributeParser for RustcForceInlineParser {
65    const PATH: &[Symbol] = &[sym::rustc_force_inline];
66    const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
67        Allow(Target::Fn),
68        Allow(Target::Method(MethodKind::Inherent)),
69    ]);
70    const STABILITY: AttributeStability = AttributeStability::Unstable {
    gate_name: rustc_span::sym::rustc_attrs,
    gate_check: rustc_feature::Features::rustc_attrs,
    notes: &["the `rustc_force_inline` attribute forces a free function to be inlined"],
}unstable!(
71        rustc_attrs,
72        "the `rustc_force_inline` attribute forces a free function to be inlined"
73    );
74    const TEMPLATE: AttributeTemplate = crate::AttributeTemplate {
    word: true,
    list: Some(&["reason"]),
    one_of: &[],
    name_value_str: Some(&["reason"]),
    docs: None,
}template!(Word, List: &["reason"], NameValueStr: "reason");
75
76    fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
77        let reason = match args {
78            ArgParser::NoArgs => None,
79            ArgParser::List(list) => {
80                let l = cx.expect_single(list)?;
81
82                let reason = cx.expect_string_literal(l)?;
83
84                Some(reason)
85            }
86            ArgParser::NameValue(v) => cx.expect_string_literal(v),
87        };
88
89        Some(AttributeKind::Inline(
90            InlineAttr::Force { attr_span: cx.attr_span, reason },
91            cx.attr_span,
92        ))
93    }
94
95    fn finalize_check(cx: &FinalizeCheckContext<'_, '_>, attr_span: Span) {
96        let Some(inline_span) = {
    'done:
        {
        for i in cx.parsed_attrs {
            #[allow(unused_imports)]
            use ::rustc_attr_ir::AttributeKind::*;
            let i: &::rustc_attr_ir::Attribute = i;
            match i {
                ::rustc_attr_ir::Attribute::Parsed(Inline(attr, span)) if
                    !#[allow(non_exhaustive_omitted_patterns)] match attr {
                            InlineAttr::Force { .. } => true,
                            _ => false,
                        } => {
                    break 'done Some(span);
                }
                ::rustc_attr_ir::Attribute::Unparsed(..) =>
                    {}
                    #[deny(unreachable_patterns)]
                    _ => {}
            }
        }
        None
    }
}find_attr!(cx.parsed_attrs, Inline(attr, span) if !matches!(attr, InlineAttr::Force { .. }) => span)
97        else {
98            return;
99        };
100
101        cx.emit_err(InlineForceInlineConflict {
102            inline_span: *inline_span,
103            force_inline_span: attr_span,
104        });
105    }
106}