Skip to main content

rustc_attr_parsing/attributes/
lint_helpers.rs

1use rustc_attr_ir::{ReprAttr, find_attr};
2use rustc_feature::AttributeStability;
3
4use super::prelude::*;
5use crate::diagnostics::RustcPubTransparent;
6
7pub(crate) struct RustcAsPtrParser;
8impl NoArgsAttributeParser for RustcAsPtrParser {
9    const PATH: &[Symbol] = &[sym::rustc_as_ptr];
10    const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
11        Allow(Target::Fn),
12        Allow(Target::Method(MethodKind::Inherent)),
13        Allow(Target::Method(MethodKind::Trait { body: false })),
14        Allow(Target::Method(MethodKind::Trait { body: true })),
15        Allow(Target::Method(MethodKind::TraitImpl)),
16    ]);
17    const STABILITY: AttributeStability = AttributeStability::Unstable {
    gate_name: rustc_span::sym::rustc_attrs,
    gate_check: rustc_feature::Features::rustc_attrs,
    notes: &[],
}unstable!(rustc_attrs);
18    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcAsPtr;
19}
20
21pub(crate) struct RustcPubTransparentParser;
22impl NoArgsAttributeParser for RustcPubTransparentParser {
23    const PATH: &[Symbol] = &[sym::rustc_pub_transparent];
24    const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
25        Allow(Target::Struct),
26        Allow(Target::Enum),
27        Allow(Target::Union),
28    ]);
29    const STABILITY: AttributeStability = AttributeStability::Unstable {
    gate_name: rustc_span::sym::rustc_attrs,
    gate_check: rustc_feature::Features::rustc_attrs,
    notes: &[],
}unstable!(rustc_attrs);
30    const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcPubTransparent;
31
32    fn finalize_check(cx: &FinalizeCheckContext<'_, '_>, attr_span: Span) {
33        // `#[rustc_pub_transparent]` may only be applied to `#[repr(transparent)]` types.
34        let is_transparent = {
    {
            '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(Repr { reprs, .. }) if
                            reprs.iter().any(|(r, _)| r == &ReprAttr::ReprTransparent)
                            => {
                            break 'done Some(());
                        }
                        ::rustc_attr_ir::Attribute::Unparsed(..) =>
                            {}
                            #[deny(unreachable_patterns)]
                            _ => {}
                    }
                }
                None
            }
        }.is_some()
}find_attr!(
35            cx.parsed_attrs,
36            Repr { reprs, .. } if reprs.iter().any(|(r, _)| r == &ReprAttr::ReprTransparent)
37        );
38        if !is_transparent {
39            cx.emit_err(RustcPubTransparent { span: cx.target_span, attr_span });
40        }
41    }
42}
43
44pub(crate) struct RustcPassByValueParser;
45impl NoArgsAttributeParser for RustcPassByValueParser {
46    const PATH: &[Symbol] = &[sym::rustc_pass_by_value];
47    const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
48        Allow(Target::Struct),
49        Allow(Target::Enum),
50        Allow(Target::TyAlias),
51    ]);
52    const STABILITY: AttributeStability = AttributeStability::Unstable {
    gate_name: rustc_span::sym::rustc_attrs,
    gate_check: rustc_feature::Features::rustc_attrs,
    notes: &[],
}unstable!(rustc_attrs);
53    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcPassByValue;
54}
55
56pub(crate) struct RustcShouldNotBeCalledOnConstItemsParser;
57impl NoArgsAttributeParser for RustcShouldNotBeCalledOnConstItemsParser {
58    const PATH: &[Symbol] = &[sym::rustc_should_not_be_called_on_const_items];
59    const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
60        Allow(Target::Method(MethodKind::Inherent)),
61        Allow(Target::Method(MethodKind::TraitImpl)),
62    ]);
63    const STABILITY: AttributeStability = AttributeStability::Unstable {
    gate_name: rustc_span::sym::rustc_attrs,
    gate_check: rustc_feature::Features::rustc_attrs,
    notes: &[],
}unstable!(rustc_attrs);
64    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcShouldNotBeCalledOnConstItems;
65}
66
67pub(crate) struct AutomaticallyDerivedParser;
68impl NoArgsAttributeParser for AutomaticallyDerivedParser {
69    const PATH: &[Symbol] = &[sym::automatically_derived];
70    const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn;
71    const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowListWarnRest(&[
72        Allow(Target::Impl { of_trait: true }),
73        Error(Target::Crate),
74        Error(Target::WherePredicate),
75    ]);
76    const STABILITY: AttributeStability = AttributeStability::Stable;
77    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::AutomaticallyDerived;
78}