Skip to main content

rustc_attr_parsing/attributes/
lint_helpers.rs

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