Skip to content

Incorrect no_mangle attribute warning #78989

Closed
@pca006132

Description

@pca006132

Code

I tried this code:

extern "C" {
    #[no_mangle]
    static __init_array_start: i32;
}

fn main() {
    unsafe {
        println!("{:p}", &__init_array_start);
    }
}

I expected to see this happen: no warning.

Instead, this happened:

   Compiling no_mangle v0.1.0 (/home/pca/code/rust/no_mangle)
warning: attribute should be applied to a function or static
 --> src/main.rs:2:5
  |
2 |     #[no_mangle]
  |     ^^^^^^^^^^^^
3 |     static __init_array_start: i32;
  |     ------------------------------- not a function or static
  |
  = note: `#[warn(unused_attributes)]` on by default
  = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!

warning: 1 warning emitted

    Finished dev [unoptimized + debuginfo] target(s) in 0.19s
     Running `target/debug/no_mangle`
0x55626ded9540

The attribute is actually applied correctly, otherwise we would get undefined symbol error. Also, the extern variable is clearly declared as static, so the warning does not really make sense.

Edit: it seems that the code also works without the no_mangle attribute. So is the no_mangle attribute actually not needed? Anyway, the warning about not a function or static is confusing.

This problem occurred in our embedded project artiq-zynq, where the static global variables are used to provide references to linker symbols.

Version it worked on

It most recently worked on: Rust 1.47 stable. Haven't tried newer nightly.

Version with regression

rustc --version --verbose:

rustc 1.49.0-nightly (5404efc28 2020-11-11)
binary: rustc
commit-hash: 5404efc28a0cddee103ef6396c48ea71ff9631c8
commit-date: 2020-11-11
host: x86_64-unknown-linux-gnu
release: 1.49.0-nightly

@rustbot modify labels: +regression-from-stable-to-nightly -regression-untriaged

Metadata

Metadata

Assignees

Labels

A-attributesArea: Attributes (`#[…]`, `#![…]`)A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.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