Skip to content

Commit 46c5460

Browse files
committed
Port #[marker] to the new attribute system
1 parent fccdb6a commit 46c5460

File tree

7 files changed

+40
-15
lines changed

7 files changed

+40
-15
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

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

258+
/// Represents `#[marker]`.
259+
Marker(Span),
260+
258261
/// Represents [`#[may_dangle]`](https://std-dev-guide.rust-lang.org/tricky/may-dangle.html).
259262
MayDangle(Span),
260263

compiler/rustc_attr_parsing/src/attributes/traits.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,13 @@ impl<S: Stage> NoArgsAttributeParser<S> for UnsafeSpecializationMarkerParser {
124124
AttributeKind::UnsafeSpecializationMarker(span)
125125
}
126126
}
127+
128+
pub(crate) struct MarkerParser;
129+
impl<S: Stage> NoArgsAttributeParser<S> for MarkerParser {
130+
const PATH: &[Symbol] = &[sym::marker];
131+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
132+
133+
fn create(span: Span) -> AttributeKind {
134+
AttributeKind::Marker(span)
135+
}
136+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::attributes::stability::{
2929
};
3030
use crate::attributes::traits::{
3131
CoinductiveParser, ConstTraitParser, DenyExplicitImplParser, DoNotImplementViaObjectParser,
32-
SkipDuringMethodDispatchParser, SpecializationTraitParser, TypeConstParser,
32+
MarkerParser, SkipDuringMethodDispatchParser, SpecializationTraitParser, TypeConstParser,
3333
UnsafeSpecializationMarkerParser,
3434
};
3535
use crate::attributes::transparency::TransparencyParser;
@@ -133,6 +133,7 @@ attribute_parsers!(
133133
Single<WithoutArgs<ExportStableParser>>,
134134
Single<WithoutArgs<FfiConstParser>>,
135135
Single<WithoutArgs<FfiPureParser>>,
136+
Single<WithoutArgs<MarkerParser>>,
136137
Single<WithoutArgs<MayDangleParser>>,
137138
Single<WithoutArgs<NoMangleParser>>,
138139
Single<WithoutArgs<PassByValueParser>>,

compiler/rustc_hir_analysis/src/collect.rs

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

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

11531153
let rustc_coinductive = find_attr!(attrs, AttributeKind::Coinductive(_));
11541154
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
@@ -302,6 +302,7 @@ fn emit_malformed_attribute(
302302
| sym::const_trait
303303
| sym::rustc_specialization_trait
304304
| sym::rustc_unsafe_specialization_marker
305+
| sym::marker
305306
| sym::type_const
306307
| sym::rustc_pass_by_value
307308
| sym::repr

compiler/rustc_passes/src/check_attr.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
137137
&Attribute::Parsed(AttributeKind::TypeConst(attr_span)) => {
138138
self.check_type_const(hir_id, attr_span, target)
139139
}
140+
&Attribute::Parsed(AttributeKind::Marker(attr_span)) => {
141+
self.check_marker(hir_id, attr_span, span, target)
142+
}
140143
Attribute::Parsed(AttributeKind::Confusables { first_span, .. }) => {
141144
self.check_confusables(*first_span, target);
142145
}
@@ -225,7 +228,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
225228
self.check_no_sanitize(attr, span, target)
226229
}
227230
[sym::non_exhaustive, ..] => self.check_non_exhaustive(hir_id, attr, span, target, item),
228-
[sym::marker, ..] => self.check_marker(hir_id, attr, span, target),
229231
[sym::target_feature, ..] => {
230232
self.check_target_feature(hir_id, attr, span, target, attrs)
231233
}
@@ -792,21 +794,19 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
792794
}
793795

794796
/// Checks if the `#[marker]` attribute on an `item` is valid.
795-
fn check_marker(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
797+
fn check_marker(&self, hir_id: HirId, attr_span: Span, span: Span, target: Target) {
796798
match target {
797799
Target::Trait => {}
798800
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
799801
// `#[marker]` attribute with just a lint, because we previously
800802
// erroneously allowed it and some crates used it accidentally, to be compatible
801803
// with crates depending on them, we can't throw an error here.
802804
Target::Field | Target::Arm | Target::MacroDef => {
803-
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "marker");
805+
self.inline_attr_str_error_with_macro_def(hir_id, attr_span, "marker");
804806
}
805807
_ => {
806-
self.dcx().emit_err(errors::AttrShouldBeAppliedToTrait {
807-
attr_span: attr.span(),
808-
defn_span: span,
809-
});
808+
self.dcx()
809+
.emit_err(errors::AttrShouldBeAppliedToTrait { attr_span, defn_span: span });
810810
}
811811
}
812812
}
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)