Port #[marker] to the new attribute system

This commit is contained in:
Pavel Grigorenko 2025-06-24 01:49:26 +03:00
parent a57a885abc
commit 12f6487d79
9 changed files with 47 additions and 21 deletions

View file

@ -294,6 +294,9 @@ pub enum AttributeKind {
/// Represents `#[rustc_macro_transparency]`.
MacroTransparency(Transparency),
/// Represents `#[marker]`.
Marker(Span),
/// Represents [`#[may_dangle]`](https://std-dev-guide.rust-lang.org/tricky/may-dangle.html).
MayDangle(Span),

View file

@ -40,6 +40,7 @@ impl AttributeKind {
LinkSection { .. } => No,
LoopMatch(..) => No,
MacroTransparency(..) => Yes,
Marker(..) => No,
MayDangle(..) => No,
MustUse { .. } => Yes,
Naked(..) => No,

View file

@ -103,3 +103,10 @@ impl<S: Stage> NoArgsAttributeParser<S> for UnsafeSpecializationMarkerParser {
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
const CREATE: fn(Span) -> AttributeKind = AttributeKind::UnsafeSpecializationMarker;
}
pub(crate) struct MarkerParser;
impl<S: Stage> NoArgsAttributeParser<S> for MarkerParser {
const PATH: &[Symbol] = &[sym::marker];
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
const CREATE: fn(Span) -> AttributeKind = AttributeKind::Marker;
}

View file

@ -45,7 +45,7 @@ use crate::attributes::stability::{
use crate::attributes::test_attrs::IgnoreParser;
use crate::attributes::traits::{
CoinductiveParser, ConstTraitParser, DenyExplicitImplParser, DoNotImplementViaObjectParser,
SkipDuringMethodDispatchParser, SpecializationTraitParser, TypeConstParser,
MarkerParser, SkipDuringMethodDispatchParser, SpecializationTraitParser, TypeConstParser,
UnsafeSpecializationMarkerParser,
};
use crate::attributes::transparency::TransparencyParser;
@ -162,6 +162,7 @@ attribute_parsers!(
Single<WithoutArgs<FfiConstParser>>,
Single<WithoutArgs<FfiPureParser>>,
Single<WithoutArgs<LoopMatchParser>>,
Single<WithoutArgs<MarkerParser>>,
Single<WithoutArgs<MayDangleParser>>,
Single<WithoutArgs<NoImplicitPreludeParser>>,
Single<WithoutArgs<NoMangleParser>>,

View file

@ -866,7 +866,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
}
// Only regular traits can be marker.
let is_marker = !is_alias && attrs.iter().any(|attr| attr.has_name(sym::marker));
let is_marker = !is_alias && find_attr!(attrs, AttributeKind::Marker(_));
let rustc_coinductive = find_attr!(attrs, AttributeKind::Coinductive(_));
let is_fundamental = attrs.iter().any(|attr| attr.has_name(sym::fundamental));

View file

@ -289,6 +289,7 @@ pub fn check_builtin_meta_item(
| sym::const_trait
| sym::rustc_specialization_trait
| sym::rustc_unsafe_specialization_marker
| sym::marker
| sym::type_const
| sym::repr
| sym::align

View file

@ -139,6 +139,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
&Attribute::Parsed(AttributeKind::TypeConst(attr_span)) => {
self.check_type_const(hir_id, attr_span, target)
}
&Attribute::Parsed(AttributeKind::Marker(attr_span)) => {
self.check_marker(hir_id, attr_span, span, target)
}
Attribute::Parsed(AttributeKind::Confusables { first_span, .. }) => {
self.check_confusables(*first_span, target);
}
@ -272,7 +275,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
[sym::no_sanitize, ..] => {
self.check_no_sanitize(attr, span, target)
}
[sym::marker, ..] => self.check_marker(hir_id, attr, span, target),
[sym::thread_local, ..] => self.check_thread_local(attr, span, target),
[sym::doc, ..] => self.check_doc_attrs(
attr,
@ -836,7 +838,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}
/// Checks if the `#[marker]` attribute on an `item` is valid.
fn check_marker(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
fn check_marker(&self, hir_id: HirId, attr_span: Span, span: Span, target: Target) {
match target {
Target::Trait => {}
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
@ -844,13 +846,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
// erroneously allowed it and some crates used it accidentally, to be compatible
// with crates depending on them, we can't throw an error here.
Target::Field | Target::Arm | Target::MacroDef => {
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "marker");
self.inline_attr_str_error_with_macro_def(hir_id, attr_span, "marker");
}
_ => {
self.dcx().emit_err(errors::AttrShouldBeAppliedToTrait {
attr_span: attr.span(),
defn_span: span,
});
self.dcx()
.emit_err(errors::AttrShouldBeAppliedToTrait { attr_span, defn_span: span });
}
}
}

View file

@ -116,12 +116,6 @@ error: malformed `cfi_encoding` attribute input
LL | #[cfi_encoding]
| ^^^^^^^^^^^^^^^ help: must be of the form: `#[cfi_encoding = "encoding"]`
error: malformed `marker` attribute input
--> $DIR/malformed-attrs.rs:155:1
|
LL | #[marker = 3]
| ^^^^^^^^^^^^^ help: must be of the form: `#[marker]`
error: malformed `fundamental` attribute input
--> $DIR/malformed-attrs.rs:157:1
|
@ -522,6 +516,15 @@ LL | #[rustc_layout_scalar_valid_range_end]
| expected this to be a list
| help: must be of the form: `#[rustc_layout_scalar_valid_range_end(end)]`
error[E0565]: malformed `marker` attribute input
--> $DIR/malformed-attrs.rs:155:1
|
LL | #[marker = 3]
| ^^^^^^^^^---^
| | |
| | didn't expect any arguments here
| help: must be of the form: `#[marker]`
error[E0565]: malformed `ffi_pure` attribute input
--> $DIR/malformed-attrs.rs:165:5
|

View file

@ -1,20 +1,30 @@
error: malformed `marker` attribute input
error[E0565]: malformed `marker` attribute input
--> $DIR/marker-attribute-with-values.rs:3:1
|
LL | #[marker(always)]
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#[marker]`
| ^^^^^^^^--------^
| | |
| | didn't expect any arguments here
| help: must be of the form: `#[marker]`
error: malformed `marker` attribute input
error[E0565]: malformed `marker` attribute input
--> $DIR/marker-attribute-with-values.rs:6:1
|
LL | #[marker("never")]
| ^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[marker]`
| ^^^^^^^^---------^
| | |
| | didn't expect any arguments here
| help: must be of the form: `#[marker]`
error: malformed `marker` attribute input
error[E0565]: malformed `marker` attribute input
--> $DIR/marker-attribute-with-values.rs:9:1
|
LL | #[marker(key = "value")]
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[marker]`
| ^^^^^^^^---------------^
| | |
| | didn't expect any arguments here
| help: must be of the form: `#[marker]`
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0565`.