File tree Expand file tree Collapse file tree
compiler/rustc_attr_parsing/src Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -403,6 +403,18 @@ pub(crate) struct InvalidTarget {
403403
404404#[ derive( Subdiagnostic ) ]
405405pub ( crate ) enum InvalidTargetHelp {
406+ #[ multipart_suggestion(
407+ "did you mean to use `#[export_name]`?" ,
408+ applicability = "maybe-incorrect"
409+ ) ]
410+ UseExportName {
411+ #[ suggestion_part( code = "unsafe(" ) ]
412+ unsafe_open : Option < Span > ,
413+ #[ suggestion_part( code = "export_name" ) ]
414+ name : Span ,
415+ #[ suggestion_part( code = ")" ) ]
416+ unsafe_close : Option < Span > ,
417+ } ,
406418 #[ help( "use `#[rustc_align(...)]` instead" ) ]
407419 UseRustcAlign ,
408420 #[ help( "use `#[rustc_align_static(...)]` instead" ) ]
Original file line number Diff line number Diff line change 11use std:: borrow:: Cow ;
22
3- use rustc_ast:: AttrStyle ;
3+ use rustc_ast:: { AttrStyle , Safety } ;
44use rustc_errors:: { DiagArgValue , MultiSpan , StashKey } ;
55use rustc_feature:: Features ;
66use rustc_hir:: attrs:: AttributeKind ;
@@ -187,6 +187,15 @@ impl<'sess> AttributeParser<'sess> {
187187 cx : & AcceptContext < ' _ , ' _ > ,
188188 ) -> Option < InvalidTargetHelp > {
189189 match & * cx. attr_path . segments {
190+ [ sym:: link_name] if cx. target == Target :: Static => {
191+ let needs_unsafe_wrapper = matches ! ( cx. attr_safety, Safety :: Default ) ;
192+
193+ Some ( InvalidTargetHelp :: UseExportName {
194+ unsafe_open : needs_unsafe_wrapper. then ( || cx. inner_span . shrink_to_lo ( ) ) ,
195+ name : cx. attr_path . span ,
196+ unsafe_close : needs_unsafe_wrapper. then ( || cx. inner_span . shrink_to_hi ( ) ) ,
197+ } )
198+ }
190199 [ sym:: repr] if attribute_args == "(align(...))" => match cx. target {
191200 Target :: Fn | Target :: Method ( ..) if cx. features ( ) . fn_align ( ) => {
192201 Some ( InvalidTargetHelp :: UseRustcAlign )
Original file line number Diff line number Diff line change 1+ #[ link_name = "VALUE" ]
2+ //~^ WARN the `link_name` attribute cannot be used on statics
3+ //~| WARN this was previously accepted by the compiler but is being phased out
4+ static VALUE_DEFINITION : u8 = 0 ;
5+
6+ #[ unsafe( link_name = "UNSAFE_VALUE" ) ]
7+ //~^ ERROR `link_name` is not an unsafe attribute
8+ //~| WARN the `link_name` attribute cannot be used on statics
9+ //~| WARN this was previously accepted by the compiler but is being phased out
10+ static UNSAFE_VALUE_DEFINITION : u8 = 0 ;
11+
12+ unsafe extern "C" {
13+ #[ link_name = "VALUE" ]
14+ static VALUE_DECLARATION : u8 ;
15+ }
16+
17+ fn main ( ) { }
Original file line number Diff line number Diff line change 1+ error: `link_name` is not an unsafe attribute
2+ --> $DIR/link-name-on-static.rs:6:3
3+ |
4+ LL | #[unsafe(link_name = "UNSAFE_VALUE")]
5+ | ^^^^^^ this is not an unsafe attribute
6+ |
7+ = note: extraneous unsafe is not allowed in attributes
8+
9+ warning: the `link_name` attribute cannot be used on statics
10+ --> $DIR/link-name-on-static.rs:1:3
11+ |
12+ LL | #[link_name = "VALUE"]
13+ | ^^^^^^^^^
14+ |
15+ = help: the `link_name` attribute can be applied to foreign functions and foreign statics
16+ = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
17+ = note: requested on the command line with `-W unused-attributes`
18+ help: did you mean to use `#[export_name]`?
19+ |
20+ LL - #[link_name = "VALUE"]
21+ LL + #[unsafe(export_name = "VALUE")]
22+ |
23+
24+ warning: the `link_name` attribute cannot be used on statics
25+ --> $DIR/link-name-on-static.rs:6:10
26+ |
27+ LL | #[unsafe(link_name = "UNSAFE_VALUE")]
28+ | ^^^^^^^^^
29+ |
30+ = help: the `link_name` attribute can be applied to foreign functions and foreign statics
31+ = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
32+ help: did you mean to use `#[export_name]`?
33+ |
34+ LL - #[unsafe(link_name = "UNSAFE_VALUE")]
35+ LL + #[unsafe(export_name = "UNSAFE_VALUE")]
36+ |
37+
38+ error: aborting due to 1 previous error; 2 warnings emitted
39+
You can’t perform that action at this time.
0 commit comments