Skip to content

Commit 12f6487

Browse files
committed
Port #[marker] to the new attribute system
1 parent a57a885 commit 12f6487

File tree

9 files changed

+47
-21
lines changed

9 files changed

+47
-21
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ pub enum AttributeKind {
294294
/// Represents `#[rustc_macro_transparency]`.
295295
MacroTransparency(Transparency),
296296

297+
/// Represents `#[marker]`.
298+
Marker(Span),
299+
297300
/// Represents [`#[may_dangle]`](https://std-dev-guide.rust-lang.org/tricky/may-dangle.html).
298301
MayDangle(Span),
299302

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ impl AttributeKind {
4040
LinkSection { .. } => No,
4141
LoopMatch(..) => No,
4242
MacroTransparency(..) => Yes,
43+
Marker(..) => No,
4344
MayDangle(..) => No,
4445
MustUse { .. } => Yes,
4546
Naked(..) => No,

compiler/rustc_attr_parsing/src/attributes/traits.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,10 @@ impl<S: Stage> NoArgsAttributeParser<S> for UnsafeSpecializationMarkerParser {
103103
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
104104
const CREATE: fn(Span) -> AttributeKind = AttributeKind::UnsafeSpecializationMarker;
105105
}
106+
107+
pub(crate) struct MarkerParser;
108+
impl<S: Stage> NoArgsAttributeParser<S> for MarkerParser {
109+
const PATH: &[Symbol] = &[sym::marker];
110+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
111+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::Marker;
112+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use crate::attributes::stability::{
4545
use crate::attributes::test_attrs::IgnoreParser;
4646
use crate::attributes::traits::{
4747
CoinductiveParser, ConstTraitParser, DenyExplicitImplParser, DoNotImplementViaObjectParser,
48-
SkipDuringMethodDispatchParser, SpecializationTraitParser, TypeConstParser,
48+
MarkerParser, SkipDuringMethodDispatchParser, SpecializationTraitParser, TypeConstParser,
4949
UnsafeSpecializationMarkerParser,
5050
};
5151
use crate::attributes::transparency::TransparencyParser;
@@ -162,6 +162,7 @@ attribute_parsers!(
162162
Single<WithoutArgs<FfiConstParser>>,
163163
Single<WithoutArgs<FfiPureParser>>,
164164
Single<WithoutArgs<LoopMatchParser>>,
165+
Single<WithoutArgs<MarkerParser>>,
165166
Single<WithoutArgs<MayDangleParser>>,
166167
Single<WithoutArgs<NoImplicitPreludeParser>>,
167168
Single<WithoutArgs<NoMangleParser>>,

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
866866
}
867867

868868
// Only regular traits can be marker.
869-
let is_marker = !is_alias && attrs.iter().any(|attr| attr.has_name(sym::marker));
869+
let is_marker = !is_alias && find_attr!(attrs, AttributeKind::Marker(_));
870870

871871
let rustc_coinductive = find_attr!(attrs, AttributeKind::Coinductive(_));
872872
let is_fundamental = attrs.iter().any(|attr| attr.has_name(sym::fundamental));

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ pub fn check_builtin_meta_item(
289289
| sym::const_trait
290290
| sym::rustc_specialization_trait
291291
| sym::rustc_unsafe_specialization_marker
292+
| sym::marker
292293
| sym::type_const
293294
| sym::repr
294295
| sym::align

compiler/rustc_passes/src/check_attr.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
139139
&Attribute::Parsed(AttributeKind::TypeConst(attr_span)) => {
140140
self.check_type_const(hir_id, attr_span, target)
141141
}
142+
&Attribute::Parsed(AttributeKind::Marker(attr_span)) => {
143+
self.check_marker(hir_id, attr_span, span, target)
144+
}
142145
Attribute::Parsed(AttributeKind::Confusables { first_span, .. }) => {
143146
self.check_confusables(*first_span, target);
144147
}
@@ -272,7 +275,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
272275
[sym::no_sanitize, ..] => {
273276
self.check_no_sanitize(attr, span, target)
274277
}
275-
[sym::marker, ..] => self.check_marker(hir_id, attr, span, target),
276278
[sym::thread_local, ..] => self.check_thread_local(attr, span, target),
277279
[sym::doc, ..] => self.check_doc_attrs(
278280
attr,
@@ -836,21 +838,19 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
836838
}
837839

838840
/// Checks if the `#[marker]` attribute on an `item` is valid.
839-
fn check_marker(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
841+
fn check_marker(&self, hir_id: HirId, attr_span: Span, span: Span, target: Target) {
840842
match target {
841843
Target::Trait => {}
842844
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
843845
// `#[marker]` attribute with just a lint, because we previously
844846
// erroneously allowed it and some crates used it accidentally, to be compatible
845847
// with crates depending on them, we can't throw an error here.
846848
Target::Field | Target::Arm | Target::MacroDef => {
847-
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "marker");
849+
self.inline_attr_str_error_with_macro_def(hir_id, attr_span, "marker");
848850
}
849851
_ => {
850-
self.dcx().emit_err(errors::AttrShouldBeAppliedToTrait {
851-
attr_span: attr.span(),
852-
defn_span: span,
853-
});
852+
self.dcx()
853+
.emit_err(errors::AttrShouldBeAppliedToTrait { attr_span, defn_span: span });
854854
}
855855
}
856856
}

tests/ui/attributes/malformed-attrs.stderr

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,6 @@ error: malformed `cfi_encoding` attribute input
116116
LL | #[cfi_encoding]
117117
| ^^^^^^^^^^^^^^^ help: must be of the form: `#[cfi_encoding = "encoding"]`
118118

119-
error: malformed `marker` attribute input
120-
--> $DIR/malformed-attrs.rs:155:1
121-
|
122-
LL | #[marker = 3]
123-
| ^^^^^^^^^^^^^ help: must be of the form: `#[marker]`
124-
125119
error: malformed `fundamental` attribute input
126120
--> $DIR/malformed-attrs.rs:157:1
127121
|
@@ -522,6 +516,15 @@ LL | #[rustc_layout_scalar_valid_range_end]
522516
| expected this to be a list
523517
| help: must be of the form: `#[rustc_layout_scalar_valid_range_end(end)]`
524518

519+
error[E0565]: malformed `marker` attribute input
520+
--> $DIR/malformed-attrs.rs:155:1
521+
|
522+
LL | #[marker = 3]
523+
| ^^^^^^^^^---^
524+
| | |
525+
| | didn't expect any arguments here
526+
| help: must be of the form: `#[marker]`
527+
525528
error[E0565]: malformed `ffi_pure` attribute input
526529
--> $DIR/malformed-attrs.rs:165:5
527530
|
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1-
error: malformed `marker` attribute input
1+
error[E0565]: malformed `marker` attribute input
22
--> $DIR/marker-attribute-with-values.rs:3:1
33
|
44
LL | #[marker(always)]
5-
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#[marker]`
5+
| ^^^^^^^^--------^
6+
| | |
7+
| | didn't expect any arguments here
8+
| help: must be of the form: `#[marker]`
69

7-
error: malformed `marker` attribute input
10+
error[E0565]: malformed `marker` attribute input
811
--> $DIR/marker-attribute-with-values.rs:6:1
912
|
1013
LL | #[marker("never")]
11-
| ^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[marker]`
14+
| ^^^^^^^^---------^
15+
| | |
16+
| | didn't expect any arguments here
17+
| help: must be of the form: `#[marker]`
1218

13-
error: malformed `marker` attribute input
19+
error[E0565]: malformed `marker` attribute input
1420
--> $DIR/marker-attribute-with-values.rs:9:1
1521
|
1622
LL | #[marker(key = "value")]
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[marker]`
23+
| ^^^^^^^^---------------^
24+
| | |
25+
| | didn't expect any arguments here
26+
| help: must be of the form: `#[marker]`
1827

1928
error: aborting due to 3 previous errors
2029

30+
For more information about this error, try `rustc --explain E0565`.

0 commit comments

Comments
 (0)