Skip to content

"warning: #[link_name] attribute cannot be used on statics" warning/error should mention #[export_name] #159247

Description

@zachs18

Code

#[link_name = "VALUE"]
static VALUE_DEFINITION: u8 = 0;

Current output

warning: `#[link_name]` attribute cannot be used on statics
 --> src/lib.rs:1:1
  |
1 | #[link_name = "VALUE"]
  | ^^^^^^^^^^^^^^^^^^^^^^
  |
  = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
  = help: `#[link_name]` can be applied to foreign functions and foreign statics
  = note: `#[warn(unused_attributes)]` (part of `#[warn(unused)]`) on by default

Desired output

warning: `#[link_name]` attribute cannot be used on statics
 --> src/lib.rs:1:1
  |
1 | #[link_name = "VALUE"]
  | ^^^^^^^^^^^^^^^^^^^^^^
  |
  = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
  = help: `#[link_name]` can be applied to foreign functions and foreign statics
  = note: `#[warn(unused_attributes)]` (part of `#[warn(unused)]`) on by default
help: did you mean to use `#[export_name]`?
  |
1 - #[link_name = "VALUE"]
1 + #[unsafe(export_name = "VALUE")]

Rationale and extra context

cc #47179

#[link_name] is for items in extern blocks, to tell the compiler what symbol to import; #[export_name] is for Rust-defined items to tell the compiler what symbol to export. But link_name doesn't say in the name which "direction" it goes, so it's easy to confuse it for the other direction.

Other cases

Rust Version

rustc 1.99.0-nightly (375b1431b 2026-07-10)
binary: rustc
commit-hash: 375b1431b7d89d1c2e2bc168c011848ae12b7d14
commit-date: 2026-07-10
host: x86_64-unknown-linux-gnu
release: 1.99.0-nightly
LLVM version: 22.1.8

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions