Skip to content

Commit 923fb76

Browse files
Rollup merge of #152570 - Ozzy1423:attr-parse, r=JonathanBrouwer
Port #[rustc_test_marker] to the attribute parser Tracking issue: #131229 Targets: Const is for normal tests (const test::TestDescAndFn is inserted before the test fn) Const/Static/Fn is for custom_test_framework's #[test_case] e.g. tests/ui/custom_test_frameworks/full.rs r? @JonathanBrouwer Again I left the use-sites as is since they are early uses.
2 parents cf0cb74 + 6ed7615 commit 923fb76

6 files changed

Lines changed: 40 additions & 6 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/test_attrs.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,36 @@ impl<S: Stage> SingleAttributeParser<S> for TestRunnerParser {
257257
Some(AttributeKind::TestRunner(meta.path().0.clone()))
258258
}
259259
}
260+
261+
pub(crate) struct RustcTestMarkerParser;
262+
263+
impl<S: Stage> SingleAttributeParser<S> for RustcTestMarkerParser {
264+
const PATH: &[Symbol] = &[sym::rustc_test_marker];
265+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
266+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
267+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
268+
Allow(Target::Const),
269+
Allow(Target::Fn),
270+
Allow(Target::Static),
271+
]);
272+
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "test_path");
273+
274+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
275+
let Some(name_value) = args.name_value() else {
276+
cx.expected_name_value(cx.attr_span, Some(sym::rustc_test_marker));
277+
return None;
278+
};
279+
280+
let Some(value_str) = name_value.value_as_str() else {
281+
cx.expected_string_literal(name_value.value_span, None);
282+
return None;
283+
};
284+
285+
if value_str.as_str().trim().is_empty() {
286+
cx.expected_non_empty_string_literal(name_value.value_span);
287+
return None;
288+
}
289+
290+
Some(AttributeKind::RustcTestMarker(value_str))
291+
}
292+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ attribute_parsers!(
217217
Single<RustcScalableVectorParser>,
218218
Single<RustcSimdMonomorphizeLaneLimitParser>,
219219
Single<RustcSymbolName>,
220+
Single<RustcTestMarkerParser>,
220221
Single<SanitizeParser>,
221222
Single<ShouldPanicParser>,
222223
Single<SkipDuringMethodDispatchParser>,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,9 @@ pub enum AttributeKind {
13391339
/// Represents `#[rustc_symbol_name]`
13401340
RustcSymbolName(Span),
13411341

1342+
/// Represents `#[rustc_test_marker]`
1343+
RustcTestMarker(Symbol),
1344+
13421345
/// Represents `#[rustc_then_this_would_need]`
13431346
RustcThenThisWouldNeed(Span, ThinVec<Ident>),
13441347

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ impl AttributeKind {
171171
RustcStdInternalSymbol(..) => No,
172172
RustcStrictCoherence(..) => Yes,
173173
RustcSymbolName(..) => Yes,
174+
RustcTestMarker(..) => No,
174175
RustcThenThisWouldNeed(..) => No,
175176
RustcTrivialFieldReads => Yes,
176177
RustcUnsafeSpecializationMarker(..) => No,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
362362
| AttributeKind::RustcStdInternalSymbol (..)
363363
| AttributeKind::RustcStrictCoherence(..)
364364
| AttributeKind::RustcSymbolName(..)
365+
| AttributeKind::RustcTestMarker(..)
365366
| AttributeKind::RustcThenThisWouldNeed(..)
366367
| AttributeKind::RustcTrivialFieldReads
367368
| AttributeKind::RustcUnsafeSpecializationMarker(..)
@@ -402,7 +403,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
402403
| sym::rustc_on_unimplemented
403404
| sym::rustc_do_not_const_check
404405
| sym::rustc_doc_primitive
405-
| sym::rustc_test_marker
406406
| sym::rustc_layout
407407
| sym::rustc_autodiff
408408
| sym::rustc_capture_analysis

src/tools/clippy/clippy_utils/src/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2343,11 +2343,7 @@ fn with_test_item_names(tcx: TyCtxt<'_>, module: LocalModDefId, f: impl FnOnce(&
23432343
// We could also check for the type name `test::TestDescAndFn`
23442344
&& let Res::Def(DefKind::Struct, _) = path.res
23452345
{
2346-
let has_test_marker = tcx
2347-
.hir_attrs(item.hir_id())
2348-
.iter()
2349-
.any(|a| a.has_name(sym::rustc_test_marker));
2350-
if has_test_marker {
2346+
if find_attr!(tcx.hir_attrs(item.hir_id()), AttributeKind::RustcTestMarker(..)) {
23512347
names.push(ident.name);
23522348
}
23532349
}

0 commit comments

Comments
 (0)