about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPavel Grigorenko <GrigorenkoPV@ya.ru>2025-06-24 01:49:26 +0300
committerPavel Grigorenko <GrigorenkoPV@ya.ru>2025-07-09 01:18:28 +0300
commit12f6487d79199c660edfba58cfcdc34e717b9abc (patch)
treeb525340129c6e3900baa9e6e6d48b14868ae7305
parenta57a885abc342cf0fec0521da21b295f8694f75e (diff)
downloadrust-12f6487d79199c660edfba58cfcdc34e717b9abc.tar.gz
rust-12f6487d79199c660edfba58cfcdc34e717b9abc.zip
Port `#[marker]` to the new attribute system
-rw-r--r--compiler/rustc_attr_data_structures/src/attributes.rs3
-rw-r--r--compiler/rustc_attr_data_structures/src/encode_cross_crate.rs1
-rw-r--r--compiler/rustc_attr_parsing/src/attributes/traits.rs7
-rw-r--r--compiler/rustc_attr_parsing/src/context.rs3
-rw-r--r--compiler/rustc_hir_analysis/src/collect.rs2
-rw-r--r--compiler/rustc_parse/src/validate_attr.rs1
-rw-r--r--compiler/rustc_passes/src/check_attr.rs14
-rw-r--r--tests/ui/attributes/malformed-attrs.stderr15
-rw-r--r--tests/ui/marker_trait_attr/marker-attribute-with-values.stderr22
9 files changed, 47 insertions, 21 deletions
diff --git a/compiler/rustc_attr_data_structures/src/attributes.rs b/compiler/rustc_attr_data_structures/src/attributes.rs
index 9d924023e72..91eee14b8ea 100644
--- a/compiler/rustc_attr_data_structures/src/attributes.rs
+++ b/compiler/rustc_attr_data_structures/src/attributes.rs
@@ -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),
 
diff --git a/compiler/rustc_attr_data_structures/src/encode_cross_crate.rs b/compiler/rustc_attr_data_structures/src/encode_cross_crate.rs
index 5811cfa60c1..f326bbf41f2 100644
--- a/compiler/rustc_attr_data_structures/src/encode_cross_crate.rs
+++ b/compiler/rustc_attr_data_structures/src/encode_cross_crate.rs
@@ -40,6 +40,7 @@ impl AttributeKind {
             LinkSection { .. } => No,
             LoopMatch(..) => No,
             MacroTransparency(..) => Yes,
+            Marker(..) => No,
             MayDangle(..) => No,
             MustUse { .. } => Yes,
             Naked(..) => No,
diff --git a/compiler/rustc_attr_parsing/src/attributes/traits.rs b/compiler/rustc_attr_parsing/src/attributes/traits.rs
index a9e6f1f3a33..44b916b4cb3 100644
--- a/compiler/rustc_attr_parsing/src/attributes/traits.rs
+++ b/compiler/rustc_attr_parsing/src/attributes/traits.rs
@@ -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;
+}
diff --git a/compiler/rustc_attr_parsing/src/context.rs b/compiler/rustc_attr_parsing/src/context.rs
index fe8ac2c532a..edfb013fe0c 100644
--- a/compiler/rustc_attr_parsing/src/context.rs
+++ b/compiler/rustc_attr_parsing/src/context.rs
@@ -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>>,
diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs
index f0db98832d9..b5c7bfeed9b 100644
--- a/compiler/rustc_hir_analysis/src/collect.rs
+++ b/compiler/rustc_hir_analysis/src/collect.rs
@@ -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));
diff --git a/compiler/rustc_parse/src/validate_attr.rs b/compiler/rustc_parse/src/validate_attr.rs
index 6efa9edfa07..89670674042 100644
--- a/compiler/rustc_parse/src/validate_attr.rs
+++ b/compiler/rustc_parse/src/validate_attr.rs
@@ -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
diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs
index 7d30c36b463..7eedc748bd6 100644
--- a/compiler/rustc_passes/src/check_attr.rs
+++ b/compiler/rustc_passes/src/check_attr.rs
@@ -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 });
             }
         }
     }
diff --git a/tests/ui/attributes/malformed-attrs.stderr b/tests/ui/attributes/malformed-attrs.stderr
index ef5dcf7b2a0..aef54248bb6 100644
--- a/tests/ui/attributes/malformed-attrs.stderr
+++ b/tests/ui/attributes/malformed-attrs.stderr
@@ -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
    |
diff --git a/tests/ui/marker_trait_attr/marker-attribute-with-values.stderr b/tests/ui/marker_trait_attr/marker-attribute-with-values.stderr
index 6f9c9508e7e..9a2e5add37b 100644
--- a/tests/ui/marker_trait_attr/marker-attribute-with-values.stderr
+++ b/tests/ui/marker_trait_attr/marker-attribute-with-values.stderr
@@ -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`.