Skip to content

Commit 48958e5

Browse files
committed
Port #[marker] to the new attribute system
1 parent ddb9890 commit 48958e5

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
@@ -277,6 +277,9 @@ pub enum AttributeKind {
277277
/// Represents `#[rustc_macro_transparency]`.
278278
MacroTransparency(Transparency),
279279

280+
/// Represents `#[marker]`.
281+
Marker(Span),
282+
280283
/// Represents [`#[may_dangle]`](https://std-dev-guide.rust-lang.org/tricky/may-dangle.html).
281284
MayDangle(Span),
282285

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ impl AttributeKind {
3535
LinkSection { .. } => No,
3636
LoopMatch(..) => No,
3737
MacroTransparency(..) => Yes,
38+
Marker(..) => No,
3839
MayDangle(..) => No,
3940
MustUse { .. } => Yes,
4041
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
@@ -39,7 +39,7 @@ use crate::attributes::stability::{
3939
};
4040
use crate::attributes::traits::{
4141
CoinductiveParser, ConstTraitParser, DenyExplicitImplParser, DoNotImplementViaObjectParser,
42-
SkipDuringMethodDispatchParser, SpecializationTraitParser, TypeConstParser,
42+
MarkerParser, SkipDuringMethodDispatchParser, SpecializationTraitParser, TypeConstParser,
4343
UnsafeSpecializationMarkerParser,
4444
};
4545
use crate::attributes::transparency::TransparencyParser;
@@ -150,6 +150,7 @@ attribute_parsers!(
150150
Single<WithoutArgs<DenyExplicitImplParser>>,
151151
Single<WithoutArgs<DoNotImplementViaObjectParser>>,
152152
Single<WithoutArgs<LoopMatchParser>>,
153+
Single<WithoutArgs<MarkerParser>>,
153154
Single<WithoutArgs<MayDangleParser>>,
154155
Single<WithoutArgs<NoImplicitPreludeParser>>,
155156
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
@@ -300,6 +300,7 @@ fn emit_malformed_attribute(
300300
| sym::const_trait
301301
| sym::rustc_specialization_trait
302302
| sym::rustc_unsafe_specialization_marker
303+
| sym::marker
303304
| sym::type_const
304305
| sym::repr
305306
| 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
}
@@ -253,7 +256,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
253256
[sym::no_sanitize, ..] => {
254257
self.check_no_sanitize(attr, span, target)
255258
}
256-
[sym::marker, ..] => self.check_marker(hir_id, attr, span, target),
257259
[sym::thread_local, ..] => self.check_thread_local(attr, span, target),
258260
[sym::doc, ..] => self.check_doc_attrs(
259261
attr,
@@ -823,21 +825,19 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
823825
}
824826

825827
/// Checks if the `#[marker]` attribute on an `item` is valid.
826-
fn check_marker(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
828+
fn check_marker(&self, hir_id: HirId, attr_span: Span, span: Span, target: Target) {
827829
match target {
828830
Target::Trait => {}
829831
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
830832
// `#[marker]` attribute with just a lint, because we previously
831833
// erroneously allowed it and some crates used it accidentally, to be compatible
832834
// with crates depending on them, we can't throw an error here.
833835
Target::Field | Target::Arm | Target::MacroDef => {
834-
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "marker");
836+
self.inline_attr_str_error_with_macro_def(hir_id, attr_span, "marker");
835837
}
836838
_ => {
837-
self.dcx().emit_err(errors::AttrShouldBeAppliedToTrait {
838-
attr_span: attr.span(),
839-
defn_span: span,
840-
});
839+
self.dcx()
840+
.emit_err(errors::AttrShouldBeAppliedToTrait { attr_span, defn_span: span });
841841
}
842842
}
843843
}

tests/ui/attributes/malformed-attrs.stderr

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

125-
error: malformed `marker` attribute input
126-
--> $DIR/malformed-attrs.rs:154:1
127-
|
128-
LL | #[marker = 3]
129-
| ^^^^^^^^^^^^^ help: must be of the form: `#[marker]`
130-
131125
error: malformed `fundamental` attribute input
132126
--> $DIR/malformed-attrs.rs:156:1
133127
|
@@ -540,6 +534,15 @@ LL | #[rustc_layout_scalar_valid_range_end]
540534
| expected this to be a list
541535
| help: must be of the form: `#[rustc_layout_scalar_valid_range_end(end)]`
542536

537+
error[E0565]: malformed `marker` attribute input
538+
--> $DIR/malformed-attrs.rs:154:1
539+
|
540+
LL | #[marker = 3]
541+
| ^^^^^^^^^---^
542+
| | |
543+
| | didn't expect any arguments here
544+
| help: must be of the form: `#[marker]`
545+
543546
error[E0565]: malformed `non_exhaustive` attribute input
544547
--> $DIR/malformed-attrs.rs:196:1
545548
|
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)