Skip to content

Commit 0089ddd

Browse files
authored
Unrolled build for #148756
Rollup merge of #148756 - JonathanBrouwer:link_section_targets2, r=jdonszelmann Warn on codegen attributes on required trait methods This PR turns applying the following attributes on required trait methods (that is, trait methods **without** a default implementation) into a FCW: - `#[cold]` - `#[link_section]` - `#[linkage]` (unstable) - `#[rustc_allow_const_fn_unstable]` (internal attribute) These attributes already had no effect when applied to a required trait method, this PR only adds a warning. Furthermore, it adds a comment in the code that the following codegen attributes are *inherited* when applied to a required trait method: - `#[track_caller]` - `#[align]` (unstable) ````@rustbot```` labels +I-lang-nominated ````@rust-lang/lang```` Two questions for the lang team: - Is adding this warning ok? - Does the current behaviour of these attributes align with that you would expect them to be? Fixes #147432
2 parents cec7008 + 8fa10c0 commit 0089ddd

File tree

8 files changed

+154
-86
lines changed

8 files changed

+154
-86
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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![deny(unused_attributes)]
2+
#![feature(linkage)]
3+
#![feature(fn_align)]
4+
5+
trait Test {
6+
#[cold]
7+
//~^ ERROR cannot be used on required trait methods [unused_attributes]
8+
//~| WARN previously accepted
9+
fn method1(&self);
10+
#[link_section = ".text"]
11+
//~^ ERROR cannot be used on required trait methods [unused_attributes]
12+
//~| WARN previously accepted
13+
fn method2(&self);
14+
#[linkage = "common"]
15+
//~^ ERROR cannot be used on required trait methods [unused_attributes]
16+
//~| WARN previously accepted
17+
fn method3(&self);
18+
#[track_caller]
19+
fn method4(&self);
20+
#[rustc_align(1)]
21+
fn method5(&self);
22+
}
23+
24+
fn main() {}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error: `#[cold]` attribute cannot be used on required trait methods
2+
--> $DIR/codegen_attr_on_required_trait_method.rs:6:5
3+
|
4+
LL | #[cold]
5+
| ^^^^^^^
6+
|
7+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
8+
= help: `#[cold]` can be applied to closures, foreign functions, functions, inherent methods, provided trait methods, and trait methods in impl blocks
9+
note: the lint level is defined here
10+
--> $DIR/codegen_attr_on_required_trait_method.rs:1:9
11+
|
12+
LL | #![deny(unused_attributes)]
13+
| ^^^^^^^^^^^^^^^^^
14+
15+
error: `#[link_section]` attribute cannot be used on required trait methods
16+
--> $DIR/codegen_attr_on_required_trait_method.rs:10:5
17+
|
18+
LL | #[link_section = ".text"]
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
20+
|
21+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
22+
= help: `#[link_section]` can be applied to functions, inherent methods, provided trait methods, statics, and trait methods in impl blocks
23+
24+
error: `#[linkage]` attribute cannot be used on required trait methods
25+
--> $DIR/codegen_attr_on_required_trait_method.rs:14:5
26+
|
27+
LL | #[linkage = "common"]
28+
| ^^^^^^^^^^^^^^^^^^^^^
29+
|
30+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
31+
= help: `#[linkage]` can be applied to foreign functions, foreign statics, functions, inherent methods, provided trait methods, statics, and trait methods in impl blocks
32+
33+
error: aborting due to 3 previous errors
34+

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)