Skip to content

Commit ddcd55f

Browse files
Don't allow codegen attributes on trait methods
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
1 parent ce63e5d commit ddcd55f

File tree

5 files changed

+7
-6
lines changed

5 files changed

+7
-6
lines changed

compiler/rustc_attr_parsing/src/attributes/allow_unstable.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ impl<S: Stage> CombineAttributeParser<S> for AllowConstFnUnstableParser {
6161
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
6262
Allow(Target::Fn),
6363
Allow(Target::Method(MethodKind::Inherent)),
64-
Allow(Target::Method(MethodKind::Trait { body: false })),
6564
Allow(Target::Method(MethodKind::Trait { body: true })),
6665
Allow(Target::Method(MethodKind::TraitImpl)),
6766
]);

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ impl<S: Stage> NoArgsAttributeParser<S> for ColdParser {
5757
Allow(Target::Fn),
5858
Allow(Target::Method(MethodKind::Trait { body: true })),
5959
Allow(Target::Method(MethodKind::TraitImpl)),
60-
Allow(Target::Method(MethodKind::Trait { body: false })),
6160
Allow(Target::Method(MethodKind::Inherent)),
6261
Allow(Target::ForeignFn),
6362
Allow(Target::Closure),
@@ -343,7 +342,7 @@ impl<S: Stage> NoArgsAttributeParser<S> for TrackCallerParser {
343342
Allow(Target::Method(MethodKind::Inherent)),
344343
Allow(Target::Method(MethodKind::Trait { body: true })),
345344
Allow(Target::Method(MethodKind::TraitImpl)),
346-
Allow(Target::Method(MethodKind::Trait { body: false })),
345+
Allow(Target::Method(MethodKind::Trait { body: false })), // `#[track_caller]` is inherited from trait methods
347346
Allow(Target::ForeignFn),
348347
Allow(Target::Closure),
349348
Warn(Target::MacroDef),

compiler/rustc_attr_parsing/src/attributes/link_attrs.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,6 @@ impl<S: Stage> SingleAttributeParser<S> for LinkSectionParser {
469469
Allow(Target::Static),
470470
Allow(Target::Fn),
471471
Allow(Target::Method(MethodKind::Inherent)),
472-
Allow(Target::Method(MethodKind::Trait { body: false })),
473472
Allow(Target::Method(MethodKind::Trait { body: true })),
474473
Allow(Target::Method(MethodKind::TraitImpl)),
475474
]);
@@ -587,12 +586,12 @@ impl<S: Stage> SingleAttributeParser<S> for LinkageParser {
587586
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
588587
Allow(Target::Fn),
589588
Allow(Target::Method(MethodKind::Inherent)),
590-
Allow(Target::Method(MethodKind::Trait { body: false })),
591589
Allow(Target::Method(MethodKind::Trait { body: true })),
592590
Allow(Target::Method(MethodKind::TraitImpl)),
593591
Allow(Target::Static),
594592
Allow(Target::ForeignStatic),
595593
Allow(Target::ForeignFn),
594+
Warn(Target::Method(MethodKind::Trait { body: false })), // Not inherited
596595
]);
597596

598597
const TEMPLATE: AttributeTemplate = template!(NameValueStr: [

compiler/rustc_attr_parsing/src/attributes/repr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ impl<S: Stage> AttributeParser<S> for AlignParser {
315315
Allow(Target::Method(MethodKind::Inherent)),
316316
Allow(Target::Method(MethodKind::Trait { body: true })),
317317
Allow(Target::Method(MethodKind::TraitImpl)),
318-
Allow(Target::Method(MethodKind::Trait { body: false })),
318+
Allow(Target::Method(MethodKind::Trait { body: false })), // `#[align]` is inherited from trait methods
319319
Allow(Target::ForeignFn),
320320
]);
321321

tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,10 @@ mod link_section {
680680
//~| HELP remove the attribute
681681
trait Tr {
682682
#[link_section = "1800"]
683+
//~^ WARN attribute cannot be used on
684+
//~| WARN previously accepted
685+
//~| HELP can be applied to
686+
//~| HELP remove the attribute
683687
fn inside_tr_no_default(&self);
684688

685689
#[link_section = "1800"]

0 commit comments

Comments
 (0)