From 12c175622cc69177ad94a2f05de386a992a7ba96 Mon Sep 17 00:00:00 2001 From: Jonathan Brouwer Date: Sun, 22 Nov 2026 13:46:53 +0100 Subject: [PATCH] Error when `export_name` is applied to wrong targets --- .../src/attributes/codegen_attrs.rs | 5 - tests/ui/attributes/attr-on-mac-call.rs | 3 +- tests/ui/attributes/attr-on-mac-call.stderr | 109 +++++++++--------- ...sue-43106-gating-of-builtin-attrs-error.rs | 1 + ...43106-gating-of-builtin-attrs-error.stderr | 30 +++-- 5 files changed, 75 insertions(+), 73 deletions(-) diff --git a/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs index ffd48612d0ec3..27c08ccb4acd3 100644 --- a/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs +++ b/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs @@ -122,12 +122,7 @@ impl SingleAttributeParser for ExportNameParser { Allow(Target::Static), Allow(Target::Fn), Allow(Target::Method(MethodKind::Inherent)), - Allow(Target::Method(MethodKind::Trait { body: true })), Allow(Target::Method(MethodKind::TraitImpl)), - Warn(Target::Field), - Warn(Target::Arm), - Warn(Target::MacroDef), - Warn(Target::MacroCall), ]); const TEMPLATE: AttributeTemplate = template!(NameValueStr: "name"); const STABILITY: AttributeStability = AttributeStability::Stable; diff --git a/tests/ui/attributes/attr-on-mac-call.rs b/tests/ui/attributes/attr-on-mac-call.rs index da57853f4bddd..c4ca70635e433 100644 --- a/tests/ui/attributes/attr-on-mac-call.rs +++ b/tests/ui/attributes/attr-on-mac-call.rs @@ -6,8 +6,7 @@ fn main() { #[export_name = "x"] - //~^ WARN attribute cannot be used on macro calls - //~| WARN previously accepted + //~^ ERROR attribute cannot be used on macro calls unreachable!(); #[unsafe(naked)] //~^ WARN attribute cannot be used on macro calls diff --git a/tests/ui/attributes/attr-on-mac-call.stderr b/tests/ui/attributes/attr-on-mac-call.stderr index d51f3ef574688..533e15341bebf 100644 --- a/tests/ui/attributes/attr-on-mac-call.stderr +++ b/tests/ui/attributes/attr-on-mac-call.stderr @@ -1,5 +1,16 @@ +error: the `export_name` attribute cannot be used on macro calls + --> $DIR/attr-on-mac-call.rs:8:7 + | +LL | #[export_name = "x"] + | ^^^^^^^^^^^ +LL | +LL | unreachable!(); + | + = help: the `export_name` attribute can be applied to functions and statics + = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion + error: the `sanitize` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:100:7 + --> $DIR/attr-on-mac-call.rs:99:7 | LL | #[sanitize(address = "off")] | ^^^^^^^^ @@ -10,7 +21,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion error[E0658]: SIMD types are experimental and possibly buggy - --> $DIR/attr-on-mac-call.rs:129:12 + --> $DIR/attr-on-mac-call.rs:128:12 | LL | #[repr(simd)] | ^^^^ @@ -20,7 +31,7 @@ LL | #[repr(simd)] = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date error: the `repr(simd)` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:129:7 + --> $DIR/attr-on-mac-call.rs:128:7 | LL | #[repr(simd)] | ^^^^^^^^^^ @@ -31,31 +42,31 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion error: crate-level attribute should be an inner attribute: add an exclamation mark: `#![register_tool]` - --> $DIR/attr-on-mac-call.rs:133:5 + --> $DIR/attr-on-mac-call.rs:132:5 | LL | #[register_tool(xyz)] | ^^^^^^^^^^^^^^^^^^^^^ | note: this attribute does not have an `!`, which means it is applied to this macro call - --> $DIR/attr-on-mac-call.rs:135:5 + --> $DIR/attr-on-mac-call.rs:134:5 | LL | unreachable!(); | ^^^^^^^^^^^^^^ error: attribute value must be a literal - --> $DIR/attr-on-mac-call.rs:136:20 + --> $DIR/attr-on-mac-call.rs:135:20 | LL | #[deprecated = concat!("woah", "dude")] | ^^^^^^^^^^^^^^^^^^^^^^^ error: attribute value must be a literal - --> $DIR/attr-on-mac-call.rs:138:13 + --> $DIR/attr-on-mac-call.rs:137:13 | LL | #[doc = concat!("woah", "dude")] | ^^^^^^^^^^^^^^^^^^^^^^^ error: attribute value must be a literal - --> $DIR/attr-on-mac-call.rs:141:13 + --> $DIR/attr-on-mac-call.rs:140:13 | LL | #[doc = { | _____________^ @@ -67,7 +78,7 @@ LL | | }] | |_____^ error: attribute value must be a literal - --> $DIR/attr-on-mac-call.rs:149:13 + --> $DIR/attr-on-mac-call.rs:148:13 | LL | #[doc = { | _____________^ @@ -75,15 +86,15 @@ LL | | let expressions @ r#in @ doc @ attributes @ at = home; LL | | }] | |_____^ -warning: the `export_name` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:8:7 +warning: the `naked` attribute cannot be used on macro calls + --> $DIR/attr-on-mac-call.rs:11:14 | -LL | #[export_name = "x"] - | ^^^^^^^^^^^ +LL | #[unsafe(naked)] + | ^^^^^ ... LL | unreachable!(); | - = help: the `export_name` attribute can be applied to functions and statics + = help: the `naked` attribute can only be applied to functions = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion note: the lint level is defined here @@ -92,20 +103,8 @@ note: the lint level is defined here LL | #![warn(unused_attributes)] | ^^^^^^^^^^^^^^^^^ -warning: the `naked` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:12:14 - | -LL | #[unsafe(naked)] - | ^^^^^ -... -LL | unreachable!(); - | - = help: the `naked` attribute can only be applied to functions - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion - warning: the `track_caller` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:16:7 + --> $DIR/attr-on-mac-call.rs:15:7 | LL | #[track_caller] | ^^^^^^^^^^^^ @@ -117,7 +116,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `used` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:20:7 + --> $DIR/attr-on-mac-call.rs:19:7 | LL | #[used] | ^^^^ @@ -129,7 +128,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `target_feature` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:24:7 + --> $DIR/attr-on-mac-call.rs:23:7 | LL | #[target_feature(enable = "x")] | ^^^^^^^^^^^^^^ @@ -141,7 +140,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `deprecated` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:28:7 + --> $DIR/attr-on-mac-call.rs:27:7 | LL | #[deprecated] | ^^^^^^^^^^ @@ -153,7 +152,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `inline` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:32:7 + --> $DIR/attr-on-mac-call.rs:31:7 | LL | #[inline] | ^^^^^^ @@ -165,7 +164,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `link_name` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:36:7 + --> $DIR/attr-on-mac-call.rs:35:7 | LL | #[link_name = "x"] | ^^^^^^^^^ @@ -177,7 +176,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `link_section` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:40:7 + --> $DIR/attr-on-mac-call.rs:39:7 | LL | #[link_section = "__TEXT,__text"] | ^^^^^^^^^^^^ @@ -189,7 +188,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `link_ordinal` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:44:7 + --> $DIR/attr-on-mac-call.rs:43:7 | LL | #[link_ordinal(42)] | ^^^^^^^^^^^^ @@ -201,7 +200,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `non_exhaustive` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:48:7 + --> $DIR/attr-on-mac-call.rs:47:7 | LL | #[non_exhaustive] | ^^^^^^^^^^^^^^ @@ -213,7 +212,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `proc_macro` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:52:7 + --> $DIR/attr-on-mac-call.rs:51:7 | LL | #[proc_macro] | ^^^^^^^^^^ @@ -225,7 +224,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `cold` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:56:7 + --> $DIR/attr-on-mac-call.rs:55:7 | LL | #[cold] | ^^^^ @@ -237,7 +236,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `no_mangle` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:60:7 + --> $DIR/attr-on-mac-call.rs:59:7 | LL | #[no_mangle] | ^^^^^^^^^ @@ -249,7 +248,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `deprecated` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:64:7 + --> $DIR/attr-on-mac-call.rs:63:7 | LL | #[deprecated] | ^^^^^^^^^^ @@ -261,7 +260,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `automatically_derived` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:68:7 + --> $DIR/attr-on-mac-call.rs:67:7 | LL | #[automatically_derived] | ^^^^^^^^^^^^^^^^^^^^^ @@ -273,7 +272,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `macro_use` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:72:7 + --> $DIR/attr-on-mac-call.rs:71:7 | LL | #[macro_use] | ^^^^^^^^^ @@ -285,7 +284,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `must_use` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:76:7 + --> $DIR/attr-on-mac-call.rs:75:7 | LL | #[must_use] | ^^^^^^^^ @@ -297,7 +296,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `no_implicit_prelude` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:80:7 + --> $DIR/attr-on-mac-call.rs:79:7 | LL | #[no_implicit_prelude] | ^^^^^^^^^^^^^^^^^^^ @@ -309,7 +308,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `path` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:84:7 + --> $DIR/attr-on-mac-call.rs:83:7 | LL | #[path = ""] | ^^^^ @@ -321,7 +320,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `ignore` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:88:7 + --> $DIR/attr-on-mac-call.rs:87:7 | LL | #[ignore] | ^^^^^^ @@ -333,7 +332,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `should_panic` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:92:7 + --> $DIR/attr-on-mac-call.rs:91:7 | LL | #[should_panic] | ^^^^^^^^^^^^ @@ -345,7 +344,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `link_name` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:96:7 + --> $DIR/attr-on-mac-call.rs:95:7 | LL | #[link_name = "x"] | ^^^^^^^^^ @@ -357,7 +356,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `repr()` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:104:7 + --> $DIR/attr-on-mac-call.rs:103:7 | LL | #[repr()] | ^^^^^^ @@ -369,7 +368,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: unused attribute - --> $DIR/attr-on-mac-call.rs:104:5 + --> $DIR/attr-on-mac-call.rs:103:5 | LL | #[repr()] | ^^^^^^^^^ help: remove this attribute @@ -377,7 +376,7 @@ LL | #[repr()] = note: using `repr` with an empty list has no effect warning: the `repr(u8)` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:109:7 + --> $DIR/attr-on-mac-call.rs:108:7 | LL | #[repr(u8)] | ^^^^^^^^ @@ -389,7 +388,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `repr(align(...))` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:113:7 + --> $DIR/attr-on-mac-call.rs:112:7 | LL | #[repr(align(8))] | ^^^^^^^^^^^^^^ @@ -401,7 +400,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `repr(packed)` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:117:7 + --> $DIR/attr-on-mac-call.rs:116:7 | LL | #[repr(packed)] | ^^^^^^^^^^^^ @@ -413,7 +412,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `repr(C)` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:121:7 + --> $DIR/attr-on-mac-call.rs:120:7 | LL | #[repr(C)] | ^^^^^^^ @@ -425,7 +424,7 @@ LL | unreachable!(); = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion warning: the `repr(Rust)` attribute cannot be used on macro calls - --> $DIR/attr-on-mac-call.rs:125:7 + --> $DIR/attr-on-mac-call.rs:124:7 | LL | #[repr(Rust)] | ^^^^^^^^^^ @@ -436,6 +435,6 @@ LL | unreachable!(); = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion -error: aborting due to 8 previous errors; 30 warnings emitted +error: aborting due to 9 previous errors; 29 warnings emitted For more information about this error, try `rustc --explain E0658`. diff --git a/tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.rs b/tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.rs index 3c8c3c28e1b6e..b67a6951af1a2 100644 --- a/tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.rs +++ b/tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.rs @@ -146,6 +146,7 @@ mod export_name { //~^ ERROR attribute cannot be used on #[export_name = "2200"] fn bar() {} + //~^ ERROR attribute cannot be used on } } diff --git a/tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.stderr b/tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.stderr index ec0a8a9d1a418..3a40fcbec7bcd 100644 --- a/tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.stderr +++ b/tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.stderr @@ -216,8 +216,16 @@ LL | #[export_name = "2200"] fn foo(); | = help: the `export_name` attribute can be applied to functions with a body and statics +error: the `export_name` attribute cannot be used on provided trait methods + --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:148:11 + | +LL | #[export_name = "2200"] fn bar() {} + | ^^^^^^^^^^^ + | + = help: the `export_name` attribute can be applied to functions, inherent methods, statics, and trait methods in impl blocks + error: the `repr(C)` attribute cannot be used on modules - --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:152:3 + --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:153:3 | LL | #[repr(C)] | ^^^^^^^ @@ -227,7 +235,7 @@ LL | mod repr { = help: the `repr(C)` attribute can only be applied to data types error: the `repr(C)` attribute cannot be used on modules - --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:155:20 + --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:156:20 | LL | mod inner { #![repr(C)] } | ^^^^^^^ @@ -235,7 +243,7 @@ LL | mod inner { #![repr(C)] } = help: the `repr(C)` attribute can only be applied to data types error: the `repr(C)` attribute cannot be used on functions - --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:158:7 + --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:159:7 | LL | #[repr(C)] fn f() { } | ^^^^^^^ @@ -243,7 +251,7 @@ LL | #[repr(C)] fn f() { } = help: the `repr(C)` attribute can only be applied to data types error: the `repr(C)` attribute cannot be used on type aliases - --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:163:7 + --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:164:7 | LL | #[repr(C)] type T = S; | ^^^^^^^ @@ -251,7 +259,7 @@ LL | #[repr(C)] type T = S; = help: the `repr(C)` attribute can only be applied to data types error: the `repr(C)` attribute cannot be used on inherent impl blocks - --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:166:7 + --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:167:7 | LL | #[repr(C)] impl S { } | ^^^^^^^ @@ -259,7 +267,7 @@ LL | #[repr(C)] impl S { } = help: the `repr(C)` attribute can only be applied to data types error: the `repr(Rust)` attribute cannot be used on modules - --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:171:3 + --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:172:3 | LL | #[repr(Rust)] | ^^^^^^^^^^ @@ -269,7 +277,7 @@ LL | mod repr_rust { = help: the `repr(Rust)` attribute can only be applied to data types error: the `repr(Rust)` attribute cannot be used on modules - --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:174:20 + --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:175:20 | LL | mod inner { #![repr(Rust)] } | ^^^^^^^^^^ @@ -277,7 +285,7 @@ LL | mod inner { #![repr(Rust)] } = help: the `repr(Rust)` attribute can only be applied to data types error: the `repr(Rust)` attribute cannot be used on functions - --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:177:7 + --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:178:7 | LL | #[repr(Rust)] fn f() { } | ^^^^^^^^^^ @@ -285,7 +293,7 @@ LL | #[repr(Rust)] fn f() { } = help: the `repr(Rust)` attribute can only be applied to data types error: the `repr(Rust)` attribute cannot be used on type aliases - --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:182:7 + --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:183:7 | LL | #[repr(Rust)] type T = S; | ^^^^^^^^^^ @@ -293,7 +301,7 @@ LL | #[repr(Rust)] type T = S; = help: the `repr(Rust)` attribute can only be applied to data types error: the `repr(Rust)` attribute cannot be used on inherent impl blocks - --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:185:7 + --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:186:7 | LL | #[repr(Rust)] impl S { } | ^^^^^^^^^^ @@ -468,7 +476,7 @@ LL | #[no_link] = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #164032 -error: aborting due to 47 previous errors; 6 warnings emitted +error: aborting due to 48 previous errors; 6 warnings emitted For more information about this error, try `rustc --explain E0658`. Future incompatibility report: Future breakage diagnostic: