rustc_attr_parsing/attributes/
traits.rs1use std::mem;
2
3use super::prelude::*;
4use crate::attributes::{
5 AttributeOrder, NoArgsAttributeParser, OnDuplicate, SingleAttributeParser,
6};
7use crate::context::{AcceptContext, Stage};
8use crate::parser::ArgParser;
9use crate::target_checking::Policy::{Allow, Warn};
10use crate::target_checking::{ALL_TARGETS, AllowedTargets};
11
12pub(crate) struct SkipDuringMethodDispatchParser;
13impl<S: Stage> SingleAttributeParser<S> for SkipDuringMethodDispatchParser {
14 const PATH: &[Symbol] = &[sym::rustc_skip_during_method_dispatch];
15 const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
16 const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
17 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Trait)]);
18
19 const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
word: false,
list: Some(&["array, boxed_slice"]),
one_of: &[],
name_value_str: None,
docs: None,
}template!(List: &["array, boxed_slice"]);
20
21 fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
22 let mut array = false;
23 let mut boxed_slice = false;
24 let Some(args) = args.list() else {
25 cx.expected_list(cx.attr_span, args);
26 return None;
27 };
28 if args.is_empty() {
29 cx.expected_at_least_one_argument(args.span);
30 return None;
31 }
32 for arg in args.mixed() {
33 let Some(arg) = arg.meta_item() else {
34 cx.unexpected_literal(arg.span());
35 continue;
36 };
37 if let Err(span) = arg.args().no_args() {
38 cx.expected_no_args(span);
39 }
40 let path = arg.path();
41 let (key, skip): (Symbol, &mut bool) = match path.word_sym() {
42 Some(key @ sym::array) => (key, &mut array),
43 Some(key @ sym::boxed_slice) => (key, &mut boxed_slice),
44 _ => {
45 cx.expected_specific_argument(path.span(), &[sym::array, sym::boxed_slice]);
46 continue;
47 }
48 };
49 if mem::replace(skip, true) {
50 cx.duplicate_key(arg.span(), key);
51 }
52 }
53 Some(AttributeKind::RustcSkipDuringMethodDispatch {
54 array,
55 boxed_slice,
56 span: cx.attr_span,
57 })
58 }
59}
60
61pub(crate) struct ParenSugarParser;
62impl<S: Stage> NoArgsAttributeParser<S> for ParenSugarParser {
63 const PATH: &[Symbol] = &[sym::rustc_paren_sugar];
64 const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
65 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Trait)]);
66 const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcParenSugar;
67}
68
69pub(crate) struct MarkerParser;
72impl<S: Stage> NoArgsAttributeParser<S> for MarkerParser {
73 const PATH: &[Symbol] = &[sym::marker];
74 const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
75 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
76 Allow(Target::Trait),
77 Warn(Target::Field),
78 Warn(Target::Arm),
79 Warn(Target::MacroDef),
80 ]);
81 const CREATE: fn(Span) -> AttributeKind = AttributeKind::Marker;
82}
83
84pub(crate) struct DenyExplicitImplParser;
85impl<S: Stage> NoArgsAttributeParser<S> for DenyExplicitImplParser {
86 const PATH: &[Symbol] = &[sym::rustc_deny_explicit_impl];
87 const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
88 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Trait)]);
89 const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcDenyExplicitImpl;
90}
91
92pub(crate) struct DynIncompatibleTraitParser;
93impl<S: Stage> NoArgsAttributeParser<S> for DynIncompatibleTraitParser {
94 const PATH: &[Symbol] = &[sym::rustc_dyn_incompatible_trait];
95 const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
96 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Trait)]);
97 const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcDynIncompatibleTrait;
98}
99
100pub(crate) struct SpecializationTraitParser;
103impl<S: Stage> NoArgsAttributeParser<S> for SpecializationTraitParser {
104 const PATH: &[Symbol] = &[sym::rustc_specialization_trait];
105 const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
106 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Trait)]);
107 const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcSpecializationTrait;
108}
109
110pub(crate) struct UnsafeSpecializationMarkerParser;
111impl<S: Stage> NoArgsAttributeParser<S> for UnsafeSpecializationMarkerParser {
112 const PATH: &[Symbol] = &[sym::rustc_unsafe_specialization_marker];
113 const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
114 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Trait)]);
115 const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcUnsafeSpecializationMarker;
116}
117
118pub(crate) struct CoinductiveParser;
121impl<S: Stage> NoArgsAttributeParser<S> for CoinductiveParser {
122 const PATH: &[Symbol] = &[sym::rustc_coinductive];
123 const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
124 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Trait)]);
125 const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcCoinductive;
126}
127
128pub(crate) struct AllowIncoherentImplParser;
129impl<S: Stage> NoArgsAttributeParser<S> for AllowIncoherentImplParser {
130 const PATH: &[Symbol] = &[sym::rustc_allow_incoherent_impl];
131 const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
132 const ALLOWED_TARGETS: AllowedTargets =
133 AllowedTargets::AllowList(&[Allow(Target::Method(MethodKind::Inherent))]);
134 const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcAllowIncoherentImpl;
135}
136
137pub(crate) struct FundamentalParser;
138impl<S: Stage> NoArgsAttributeParser<S> for FundamentalParser {
139 const PATH: &[Symbol] = &[sym::fundamental];
140 const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
141 const ALLOWED_TARGETS: AllowedTargets =
142 AllowedTargets::AllowList(&[Allow(Target::Struct), Allow(Target::Trait)]);
143 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::Fundamental;
144}
145
146pub(crate) struct PointeeParser;
147impl<S: Stage> NoArgsAttributeParser<S> for PointeeParser {
148 const PATH: &[Symbol] = &[sym::pointee];
149 const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
150 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS); const CREATE: fn(Span) -> AttributeKind = AttributeKind::Pointee;
152}