rustc_attr_parsing/attributes/
test_attrs.rs1use rustc_hir::attrs::RustcAbiAttrKind;
2use rustc_session::lint::builtin::ILL_FORMED_ATTRIBUTE_INPUT;
3
4use super::prelude::*;
5
6pub(crate) struct IgnoreParser;
7
8impl<S: Stage> SingleAttributeParser<S> for IgnoreParser {
9 const PATH: &[Symbol] = &[sym::ignore];
10 const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
11 const ALLOWED_TARGETS: AllowedTargets =
12 AllowedTargets::AllowListWarnRest(&[Allow(Target::Fn), Error(Target::WherePredicate)]);
13 const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
word: true,
list: None,
one_of: &[],
name_value_str: Some(&["reason"]),
docs: Some("https://doc.rust-lang.org/reference/attributes/testing.html#the-ignore-attribute"),
}template!(
14 Word, NameValueStr: "reason",
15 "https://doc.rust-lang.org/reference/attributes/testing.html#the-ignore-attribute"
16 );
17
18 fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
19 Some(AttributeKind::Ignore {
20 span: cx.attr_span,
21 reason: match args {
22 ArgParser::NoArgs => None,
23 ArgParser::NameValue(name_value) => {
24 let Some(str_value) = name_value.value_as_str() else {
25 cx.adcx().warn_ill_formed_attribute_input(ILL_FORMED_ATTRIBUTE_INPUT);
26 return None;
27 };
28 Some(str_value)
29 }
30 ArgParser::List(list) => {
31 let help = list.single().and_then(|item| item.meta_item()).and_then(|item| {
32 item.args().no_args().ok()?;
33 Some(item.path().to_string())
34 });
35 cx.adcx().warn_ill_formed_attribute_input_with_help(
36 ILL_FORMED_ATTRIBUTE_INPUT,
37 help,
38 );
39 return None;
40 }
41 },
42 })
43 }
44}
45
46pub(crate) struct ShouldPanicParser;
47
48impl<S: Stage> SingleAttributeParser<S> for ShouldPanicParser {
49 const PATH: &[Symbol] = &[sym::should_panic];
50 const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
51 const ALLOWED_TARGETS: AllowedTargets =
52 AllowedTargets::AllowListWarnRest(&[Allow(Target::Fn), Error(Target::WherePredicate)]);
53 const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
word: true,
list: Some(&[r#"expected = "reason""#]),
one_of: &[],
name_value_str: Some(&["reason"]),
docs: Some("https://doc.rust-lang.org/reference/attributes/testing.html#the-should_panic-attribute"),
}template!(
54 Word, List: &[r#"expected = "reason""#], NameValueStr: "reason",
55 "https://doc.rust-lang.org/reference/attributes/testing.html#the-should_panic-attribute"
56 );
57
58 fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
59 Some(AttributeKind::ShouldPanic {
60 span: cx.attr_span,
61 reason: match args {
62 ArgParser::NoArgs => None,
63 ArgParser::NameValue(name_value) => {
64 let Some(str_value) = name_value.value_as_str() else {
65 cx.adcx().expected_string_literal(
66 name_value.value_span,
67 Some(name_value.value_as_lit()),
68 );
69 return None;
70 };
71 Some(str_value)
72 }
73 ArgParser::List(list) => {
74 let Some(single) = list.single() else {
75 cx.adcx().expected_single_argument(list.span, list.len());
76 return None;
77 };
78 let Some(single) = single.meta_item() else {
79 cx.adcx().expected_name_value(single.span(), Some(sym::expected));
80 return None;
81 };
82 if !single.path().word_is(sym::expected) {
83 cx.adcx().expected_specific_argument_strings(list.span, &[sym::expected]);
84 return None;
85 }
86 let Some(nv) = single.args().name_value() else {
87 cx.adcx().expected_name_value(single.span(), Some(sym::expected));
88 return None;
89 };
90 let Some(expected) = nv.value_as_str() else {
91 cx.adcx().expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
92 return None;
93 };
94 Some(expected)
95 }
96 },
97 })
98 }
99}
100
101pub(crate) struct ReexportTestHarnessMainParser;
102
103impl<S: Stage> SingleAttributeParser<S> for ReexportTestHarnessMainParser {
104 const PATH: &[Symbol] = &[sym::reexport_test_harness_main];
105 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
106 const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
word: false,
list: None,
one_of: &[],
name_value_str: Some(&["name"]),
docs: None,
}template!(NameValueStr: "name");
107
108 fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
109 let Some(nv) = args.name_value() else {
110 let inner_span = cx.inner_span;
111 cx.adcx().expected_name_value(
112 args.span().unwrap_or(inner_span),
113 Some(sym::reexport_test_harness_main),
114 );
115 return None;
116 };
117
118 let Some(name) = nv.value_as_str() else {
119 cx.adcx().expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
120 return None;
121 };
122
123 Some(AttributeKind::ReexportTestHarnessMain(name))
124 }
125}
126
127pub(crate) struct RustcAbiParser;
128
129impl<S: Stage> SingleAttributeParser<S> for RustcAbiParser {
130 const PATH: &[Symbol] = &[sym::rustc_abi];
131 const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
word: false,
list: None,
one_of: &[sym::debug, sym::assert_eq],
name_value_str: None,
docs: None,
}template!(OneOf: &[sym::debug, sym::assert_eq]);
132 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
133 Allow(Target::TyAlias),
134 Allow(Target::Fn),
135 Allow(Target::ForeignFn),
136 Allow(Target::Method(MethodKind::Inherent)),
137 Allow(Target::Method(MethodKind::Trait { body: true })),
138 Allow(Target::Method(MethodKind::Trait { body: false })),
139 Allow(Target::Method(MethodKind::TraitImpl)),
140 ]);
141
142 fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
143 let Some(args) = args.list() else {
144 let attr_span = cx.attr_span;
145 cx.adcx().expected_specific_argument_and_list(attr_span, &[sym::assert_eq, sym::debug]);
146 return None;
147 };
148
149 let Some(arg) = args.single() else {
150 let attr_span = cx.attr_span;
151 cx.adcx().expected_single_argument(attr_span, args.len());
152 return None;
153 };
154
155 let mut fail_incorrect_argument =
156 |span| cx.adcx().expected_specific_argument(span, &[sym::assert_eq, sym::debug]);
157
158 let Some(arg) = arg.meta_item() else {
159 fail_incorrect_argument(args.span);
160 return None;
161 };
162
163 let kind: RustcAbiAttrKind = match arg.path().word_sym() {
164 Some(sym::assert_eq) => RustcAbiAttrKind::AssertEq,
165 Some(sym::debug) => RustcAbiAttrKind::Debug,
166 None | Some(_) => {
167 fail_incorrect_argument(arg.span());
168 return None;
169 }
170 };
171
172 Some(AttributeKind::RustcAbi { attr_span: cx.attr_span, kind })
173 }
174}
175
176pub(crate) struct RustcDelayedBugFromInsideQueryParser;
177
178impl<S: Stage> NoArgsAttributeParser<S> for RustcDelayedBugFromInsideQueryParser {
179 const PATH: &[Symbol] = &[sym::rustc_delayed_bug_from_inside_query];
180 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
181 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcDelayedBugFromInsideQuery;
182}
183
184pub(crate) struct RustcEvaluateWhereClausesParser;
185
186impl<S: Stage> NoArgsAttributeParser<S> for RustcEvaluateWhereClausesParser {
187 const PATH: &[Symbol] = &[sym::rustc_evaluate_where_clauses];
188 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
189 Allow(Target::Fn),
190 Allow(Target::Method(MethodKind::Inherent)),
191 Allow(Target::Method(MethodKind::Trait { body: true })),
192 Allow(Target::Method(MethodKind::TraitImpl)),
193 Allow(Target::Method(MethodKind::Trait { body: false })),
194 ]);
195 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcEvaluateWhereClauses;
196}
197
198pub(crate) struct TestRunnerParser;
199
200impl<S: Stage> SingleAttributeParser<S> for TestRunnerParser {
201 const PATH: &[Symbol] = &[sym::test_runner];
202 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
203 const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
word: false,
list: Some(&["path"]),
one_of: &[],
name_value_str: None,
docs: None,
}template!(List: &["path"]);
204
205 fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
206 let single = cx.single_element_list(args, cx.attr_span)?;
207
208 let Some(meta) = single.meta_item() else {
209 cx.adcx().expected_not_literal(single.span());
210 return None;
211 };
212
213 Some(AttributeKind::TestRunner(meta.path().0.clone()))
214 }
215}
216
217pub(crate) struct RustcTestMarkerParser;
218
219impl<S: Stage> SingleAttributeParser<S> for RustcTestMarkerParser {
220 const PATH: &[Symbol] = &[sym::rustc_test_marker];
221 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
222 Allow(Target::Const),
223 Allow(Target::Fn),
224 Allow(Target::Static),
225 ]);
226 const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
word: false,
list: None,
one_of: &[],
name_value_str: Some(&["test_path"]),
docs: None,
}template!(NameValueStr: "test_path");
227
228 fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
229 let Some(name_value) = args.name_value() else {
230 let attr_span = cx.attr_span;
231 cx.adcx().expected_name_value(attr_span, Some(sym::rustc_test_marker));
232 return None;
233 };
234
235 let Some(value_str) = name_value.value_as_str() else {
236 cx.adcx().expected_string_literal(name_value.value_span, None);
237 return None;
238 };
239
240 if value_str.as_str().trim().is_empty() {
241 cx.adcx().expected_non_empty_string_literal(name_value.value_span);
242 return None;
243 }
244
245 Some(AttributeKind::RustcTestMarker(value_str))
246 }
247}