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