Skip to content

Commit 15467e0

Browse files
Rollup merge of rust-lang#159253 - asuto15:suggest-export-name, r=JonathanBrouwer
Add suggestions for using `#[export_name]` instead of `#[link_name]` on static Fixes rust-lang#159247. This adds a suggestion for using `#[unsafe(export_name = "...")]` when `#[link_name = "..."]` is applied to a static. It also handles the existing `#[unsafe(link_name = "...")]` form by replacing only the attribute name. Tested with: - `./x test tests/ui/attributes/link-name-on-static.rs` - `./x test tests/ui/attributes/unsafe-link-name-on-static.rs`
2 parents 4683313 + 5e5367b commit 15467e0

4 files changed

Lines changed: 78 additions & 1 deletion

File tree

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,18 @@ pub(crate) struct InvalidTarget {
403403

404404
#[derive(Subdiagnostic)]
405405
pub(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")]

compiler/rustc_attr_parsing/src/target_checking.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::borrow::Cow;
22

3-
use rustc_ast::AttrStyle;
3+
use rustc_ast::{AttrStyle, Safety};
44
use rustc_errors::{DiagArgValue, MultiSpan, StashKey};
55
use rustc_feature::Features;
66
use 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)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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() {}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+

0 commit comments

Comments
 (0)