Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect no_mangle attribute warning #78989

Closed
pca006132 opened this issue Nov 12, 2020 · 4 comments · Fixed by #86376
Closed

Incorrect no_mangle attribute warning #78989

pca006132 opened this issue Nov 12, 2020 · 4 comments · Fixed by #86376
Assignees
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@pca006132
Copy link

pca006132 commented Nov 12, 2020

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

@rustbot rustbot added regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Nov 12, 2020
@jonas-schievink
Copy link
Contributor

The attribute is actually applied correctly, otherwise we would get undefined symbol error.

No, this is working correctly. #[no_mangle] does nothing on extern items, which already do not get mangled, even without the attribute.

@pca006132
Copy link
Author

The attribute is actually applied correctly, otherwise we would get undefined symbol error.

No, this is working correctly. #[no_mangle] does nothing on extern items, which already do not get mangled, even without the attribute.

OK, sorry for not understanding the behavior of #[no_mangle] on extern items.

@pca006132
Copy link
Author

The attribute is actually applied correctly, otherwise we would get undefined symbol error.

No, this is working correctly. #[no_mangle] does nothing on extern items, which already do not get mangled, even without the attribute.

So maybe this should not be marked as regression, this is just a confusing but indeed correct warning message. Is there a way to remove the labels?

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-attributes Area: Attributes (`#[…]`, `#![…]`) A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. labels Nov 12, 2020
@asquared31415
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
4 participants