Skip to content

Confusing warning when using link_section on extern static #136220

Closed

Description

@RalfJung

Code

unsafe extern {
    #[unsafe(link_section = ".buffer")]
    pub static mut a: [u8; 1024];
}

Current output

warning: attribute should be applied to a function or static
  --> <source>:10:5
   |
10 |     #[unsafe(link_section = ".buffer")]
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11 |     pub static mut a: [u8; 1024];
   |     ----------------------------- not a function or static
   |
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: `#[warn(unused_attributes)]` on by default

warning: 1 warning emitted

Desired output

Not sure if the attribute should be allowed on extern statics... but if the answer is no, the warning should still be reworded to avoid saying that an extern static is "not a static".

Rationale and extra context

No response

Other cases

Rust Version

Rust 1.84

Anything else?

Example due by @moridinga

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jan 28, 2025
  2. Noratrieb commented on Jan 29, 2025

    @Noratrieb
    Member

    This attribute does not make sense for external statics, it only makes sense for defined statics as it controls where they end up.

  3. tmiasko commented on Jan 29, 2025

    @tmiasko
    Contributor

    LLVM reference suggest that an explicit section is significant on declarations:

    Either global variable definitions or declarations may have an explicit section to be placed in and may have an optional explicit alignment specified. If there is a mismatch between the explicit or inferred section information for the variable declaration and its definition the resulting behavior is undefined.

    External declarations may have an explicit section specified. Section information is retained in LLVM IR for targets that make use of this information. Attaching section information to an external declaration is an assertion that its definition is located in the specified section. If the definition is located in a different section, the behavior is undefined.

  4. chenyukang commented on Sep 9, 2026

    @chenyukang
    Member

    this seems fixed by #145085

    now the output is:

    Compiling playground v0.0.1 (/playground)
    warning: `extern` declarations without an explicit ABI are deprecated
     --> src/lib.rs:1:8
      |
    1 | unsafe extern {
      |        ^^^^^^ help: explicitly specify the "C" ABI: `extern "C"`
      |
      = note: `#[warn(missing_abi)]` on by default
    
    warning: the `link_section` attribute cannot be used on foreign statics
     --> src/lib.rs:2:14
      |
    2 |     #[unsafe(link_section = ".buffer")]
      |              ^^^^^^^^^^^^
      |
      = help: the `link_section` attribute can be applied to functions and statics
      = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
      = note: `#[warn(unused_attributes)]` (part of `#[warn(unused)]`) on by default

    cc @JonathanBrouwer

  5. added
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    on Sep 11, 2026
  6. JonathanBrouwer commented on Sep 11, 2026

    @JonathanBrouwer
    Member

    Awesome, indeed wasn't aware of this issue but my refactor of the relevant code happened to fix it. This doesn't seem to have a test yet, feel free someone to go do that :3

  7. added a commit that references this issue on Sep 13, 2026
  8. added a commit that references this issue on Sep 13, 2026
  9. added a commit that references this issue on Sep 13, 2026
  10. machinedramon commented on Oct 18, 2026

    @machinedramon

    Looks like this one already has a test: #162665 added tests/ui/attributes/positions/link-section.rs, which links back to this issue. I think this can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.T-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