Skip to content

Commit 30df403

Browse files
wip
1 parent c0ead89 commit 30df403

File tree

5 files changed

+25
-113
lines changed

5 files changed

+25
-113
lines changed

compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_hir::attrs::AttributeKind;
33
use rustc_span::{Span, Symbol, sym};
44

55
use crate::attributes::{NoArgsAttributeParser, OnDuplicate};
6-
use crate::context::MaybeWarn::Allow;
6+
use crate::context::MaybeWarn::{Allow, Warn};
77
use crate::context::{AllowedTargets, Stage};
88
pub(crate) struct NonExhaustiveParser;
99

@@ -14,6 +14,9 @@ impl<S: Stage> NoArgsAttributeParser<S> for NonExhaustiveParser {
1414
Allow(Target::Enum),
1515
Allow(Target::Struct),
1616
Allow(Target::Variant),
17+
Warn(Target::Field),
18+
Warn(Target::Arm),
19+
Warn(Target::MacroDef),
1720
]);
1821
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NonExhaustive;
1922
}

compiler/rustc_attr_parsing/src/attributes/traits.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_span::{Span, Symbol, sym};
88
use crate::attributes::{
99
AttributeOrder, NoArgsAttributeParser, OnDuplicate, SingleAttributeParser,
1010
};
11-
use crate::context::MaybeWarn::Allow;
11+
use crate::context::MaybeWarn::{Allow, Warn};
1212
use crate::context::{AcceptContext, AllowedTargets, Stage};
1313
use crate::parser::ArgParser;
1414
pub(crate) struct SkipDuringMethodDispatchParser;
@@ -78,7 +78,12 @@ pub(crate) struct MarkerParser;
7878
impl<S: Stage> NoArgsAttributeParser<S> for MarkerParser {
7979
const PATH: &[Symbol] = &[sym::marker];
8080
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
81-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Trait)]);
81+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
82+
Allow(Target::Trait),
83+
Warn(Target::Field),
84+
Warn(Target::Arm),
85+
Warn(Target::MacroDef),
86+
]);
8287
const CREATE: fn(Span) -> AttributeKind = AttributeKind::Marker;
8388
}
8489

compiler/rustc_passes/messages.ftl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -683,10 +683,6 @@ passes_should_be_applied_to_static =
683683
attribute should be applied to a static
684684
.label = not a static
685685
686-
passes_should_be_applied_to_struct_enum =
687-
attribute should be applied to a struct or enum
688-
.label = not a struct or enum
689-
690686
passes_should_be_applied_to_trait =
691687
attribute should be applied to a trait
692688
.label = not a trait

compiler/rustc_passes/src/check_attr.rs

Lines changed: 14 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
134134
Attribute::Parsed(AttributeKind::ProcMacroAttribute(_)) => {
135135
self.check_proc_macro(hir_id, target, ProcMacroKind::Attribute);
136136
}
137-
Attribute::Parsed(AttributeKind::ProcMacroDerive { span: attr_span, .. }) => {
138-
self.check_generic_attr(
139-
hir_id,
140-
sym::proc_macro_derive,
141-
*attr_span,
142-
target,
143-
Target::Fn,
144-
);
137+
Attribute::Parsed(AttributeKind::ProcMacroDerive { .. }) => {
145138
self.check_proc_macro(hir_id, target, ProcMacroKind::Derive)
146139
}
147140
Attribute::Parsed(
@@ -164,9 +157,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
164157
&Attribute::Parsed(AttributeKind::TypeConst(attr_span)) => {
165158
self.check_type_const(hir_id, attr_span, target)
166159
}
167-
&Attribute::Parsed(AttributeKind::Marker(attr_span)) => {
168-
self.check_marker(hir_id, attr_span, span, target)
169-
}
170160
Attribute::Parsed(AttributeKind::Fundamental | AttributeKind::CoherenceIsCore) => {
171161
// FIXME: add validation
172162
}
@@ -176,14 +166,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
176166
Attribute::Parsed(AttributeKind::Confusables { first_span, .. }) => {
177167
self.check_confusables(*first_span, target);
178168
}
179-
Attribute::Parsed(AttributeKind::AutomaticallyDerived(attr_span)) => self
180-
.check_generic_attr(
181-
hir_id,
182-
sym::automatically_derived,
183-
*attr_span,
184-
target,
185-
Target::Impl { of_trait: true },
186-
),
187169
Attribute::Parsed(
188170
AttributeKind::Stability {
189171
span: attr_span,
@@ -247,25 +229,14 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
247229
Attribute::Parsed(AttributeKind::MacroEscape(span)) => {
248230
self.check_macro_use(hir_id, sym::macro_escape, *span, target)
249231
}
250-
Attribute::Parsed(AttributeKind::Naked(attr_span)) => {
251-
self.check_naked(hir_id, *attr_span, span, target)
252-
}
253-
Attribute::Parsed(AttributeKind::NoImplicitPrelude(attr_span)) => self
254-
.check_generic_attr(
255-
hir_id,
256-
sym::no_implicit_prelude,
257-
*attr_span,
258-
target,
259-
Target::Mod,
260-
),
261-
Attribute::Parsed(AttributeKind::Path(_, attr_span)) => {
262-
self.check_generic_attr(hir_id, sym::path, *attr_span, target, Target::Mod)
232+
Attribute::Parsed(AttributeKind::Naked(..)) => {
233+
self.check_naked(hir_id, target)
263234
}
264235
Attribute::Parsed(AttributeKind::TrackCaller(attr_span)) => {
265236
self.check_track_caller(hir_id, *attr_span, attrs, span, target)
266237
}
267238
Attribute::Parsed(AttributeKind::NonExhaustive(attr_span)) => {
268-
self.check_non_exhaustive(hir_id, *attr_span, span, target, item)
239+
self.check_non_exhaustive(*attr_span, span, target, item)
269240
}
270241
Attribute::Parsed(
271242
AttributeKind::RustcLayoutScalarValidRangeStart(_num, attr_span)
@@ -289,7 +260,13 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
289260
| AttributeKind::MacroTransparency(_)
290261
| AttributeKind::Pointee(..)
291262
| AttributeKind::Dummy
292-
| AttributeKind::RustcBuiltinMacro { .. },
263+
| AttributeKind::RustcBuiltinMacro { .. }
264+
| AttributeKind::Ignore { .. }
265+
| AttributeKind::Path(..)
266+
| AttributeKind::NoImplicitPrelude(..)
267+
| AttributeKind::AutomaticallyDerived(..)
268+
| AttributeKind::Marker(..)
269+
,
293270
) => { /* do nothing */ }
294271
Attribute::Parsed(AttributeKind::AsPtr(attr_span)) => {
295272
self.check_applied_to_fn_or_method(hir_id, *attr_span, span, target)
@@ -303,9 +280,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
303280
Attribute::Parsed(AttributeKind::MayDangle(attr_span)) => {
304281
self.check_may_dangle(hir_id, *attr_span)
305282
}
306-
Attribute::Parsed(AttributeKind::Ignore { span, .. }) => {
307-
self.check_generic_attr(hir_id, sym::ignore, *span, target, Target::Fn)
308-
}
309283
Attribute::Parsed(AttributeKind::MustUse { span, .. }) => {
310284
self.check_must_use(hir_id, *span, target)
311285
}
@@ -715,29 +689,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
715689
}
716690
}
717691

718-
fn check_generic_attr(
719-
&self,
720-
hir_id: HirId,
721-
attr_name: Symbol,
722-
attr_span: Span,
723-
target: Target,
724-
allowed_target: Target,
725-
) {
726-
if target != allowed_target {
727-
self.tcx.emit_node_span_lint(
728-
UNUSED_ATTRIBUTES,
729-
hir_id,
730-
attr_span,
731-
errors::OnlyHasEffectOn {
732-
attr_name: attr_name.to_string(),
733-
target_name: allowed_target.name().replace(' ', "_"),
734-
},
735-
);
736-
}
737-
}
738-
739692
/// Checks if `#[naked]` is applied to a function definition.
740-
fn check_naked(&self, hir_id: HirId, attr_span: Span, span: Span, target: Target) {
693+
fn check_naked(&self, hir_id: HirId, target: Target) {
741694
match target {
742695
Target::Fn
743696
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => {
@@ -756,13 +709,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
756709
.emit();
757710
}
758711
}
759-
_ => {
760-
self.dcx().emit_err(errors::AttrShouldBeAppliedToFn {
761-
attr_span,
762-
defn_span: span,
763-
on_crate: hir_id == CRATE_HIR_ID,
764-
});
765-
}
712+
_ => {}
766713
}
767714
}
768715

@@ -846,7 +793,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
846793
/// Checks if the `#[non_exhaustive]` attribute on an `item` is valid.
847794
fn check_non_exhaustive(
848795
&self,
849-
hir_id: HirId,
850796
attr_span: Span,
851797
span: Span,
852798
target: Target,
@@ -867,36 +813,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
867813
});
868814
}
869815
}
870-
Target::Enum | Target::Variant => {}
871-
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
872-
// `#[non_exhaustive]` attribute with just a lint, because we previously
873-
// erroneously allowed it and some crates used it accidentally, to be compatible
874-
// with crates depending on them, we can't throw an error here.
875-
Target::Field | Target::Arm | Target::MacroDef => {
876-
self.inline_attr_str_error_with_macro_def(hir_id, attr_span, "non_exhaustive");
877-
}
878-
_ => {
879-
self.dcx()
880-
.emit_err(errors::NonExhaustiveWrongLocation { attr_span, defn_span: span });
881-
}
882-
}
883-
}
884-
885-
/// Checks if the `#[marker]` attribute on an `item` is valid.
886-
fn check_marker(&self, hir_id: HirId, attr_span: Span, span: Span, target: Target) {
887-
match target {
888-
Target::Trait => {}
889-
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
890-
// `#[marker]` attribute with just a lint, because we previously
891-
// erroneously allowed it and some crates used it accidentally, to be compatible
892-
// with crates depending on them, we can't throw an error here.
893-
Target::Field | Target::Arm | Target::MacroDef => {
894-
self.inline_attr_str_error_with_macro_def(hir_id, attr_span, "marker");
895-
}
896-
_ => {
897-
self.dcx()
898-
.emit_err(errors::AttrShouldBeAppliedToTrait { attr_span, defn_span: span });
899-
}
816+
_ => {}
900817
}
901818
}
902819

compiler/rustc_passes/src/errors.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,6 @@ pub(crate) struct TrackedCallerWrongLocation {
147147
pub on_crate: bool,
148148
}
149149

150-
#[derive(Diagnostic)]
151-
#[diag(passes_should_be_applied_to_struct_enum, code = E0701)]
152-
pub(crate) struct NonExhaustiveWrongLocation {
153-
#[primary_span]
154-
pub attr_span: Span,
155-
#[label]
156-
pub defn_span: Span,
157-
}
158-
159150
#[derive(Diagnostic)]
160151
#[diag(passes_non_exhaustive_with_default_field_values)]
161152
pub(crate) struct NonExhaustiveWithDefaultFieldValues {

0 commit comments

Comments
 (0)