Skip to content

Commit 6ed7615

Browse files
committed
Port #[rustc_test_marker] to the attribute parser
1 parent 3f808f2 commit 6ed7615

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
@@ -1336,6 +1336,9 @@ pub enum AttributeKind {
13361336
/// Represents `#[rustc_symbol_name]`
13371337
RustcSymbolName(Span),
13381338

1339+
/// Represents `#[rustc_test_marker]`
1340+
RustcTestMarker(Symbol),
1341+
13391342
/// Represents `#[rustc_then_this_would_need]`
13401343
RustcThenThisWouldNeed(Span, ThinVec<Ident>),
13411344

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

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

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
361361
| AttributeKind::RustcStdInternalSymbol (..)
362362
| AttributeKind::RustcStrictCoherence(..)
363363
| AttributeKind::RustcSymbolName(..)
364+
| AttributeKind::RustcTestMarker(..)
364365
| AttributeKind::RustcThenThisWouldNeed(..)
365366
| AttributeKind::RustcTrivialFieldReads
366367
| AttributeKind::RustcUnsafeSpecializationMarker(..)
@@ -401,7 +402,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
401402
| sym::rustc_on_unimplemented
402403
| sym::rustc_do_not_const_check
403404
| sym::rustc_doc_primitive
404-
| sym::rustc_test_marker
405405
| sym::rustc_layout
406406
| sym::rustc_proc_macro_decls
407407
| sym::rustc_autodiff

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)