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

FP no_mangle_with_rust_abi, suggestion does not apply #10409

Closed
matthiaskrgr opened this issue Feb 26, 2023 · 2 comments · Fixed by #10420
Closed

FP no_mangle_with_rust_abi, suggestion does not apply #10409

matthiaskrgr opened this issue Feb 26, 2023 · 2 comments · Fixed by #10420
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@matthiaskrgr
Copy link
Member

Summary

.

Lint Name

no_mangle_with_rust_abi

Reproducer

I tried this code:

// compile-flags: -C no-prepopulate-passes -Copt-level=0

#![crate_type = "lib"]
#![feature(const_eval_select)]
#![feature(core_intrinsics)]

use std::intrinsics::const_eval_select;

const fn foo(_: i32) -> i32 { 1 }

#[no_mangle]
pub fn hi(n: i32) -> i32 { n }

#[no_mangle]
pub unsafe fn hey() {
    // CHECK: call i32 @hi(i32
    const_eval_select((42,), foo, hi);
}

pub fn main() {}

I saw this happen:
cargo clippy --fix --allow-dirty -- -Aclippy::all -Wclippy::no-mangle-with-rust-abi

The suggestion does not compile when applied:

warning: failed to automatically apply fixes suggested by rustc to crate `clippyfix`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0277]: expected a `std::ops::FnOnce<(i32,)>` closure, found `extern "C" fn(i32) -> i32 {hi}`
    --> src/main.rs:17:35
     |
17   |     const_eval_select((42,), foo, hi);
     |     -----------------             ^^ expected an `FnOnce<(i32,)>` closure, found `extern "C" fn(i32) -> i32 {hi}`
     |     |
     |     required by a bound introduced by this call
     |
     = help: the trait `std::ops::FnOnce<(i32,)>` is not implemented for fn item `extern "C" fn(i32) -> i32 {hi}`
note: required by a bound in `std::intrinsics::const_eval_select`
    --> /home/matthias/.rustup/toolchains/master/lib/rustlib/src/rust/library/core/src/intrinsics.rs:2182:12
     |
2182 |         G: FnOnce<ARG, Output = RET>,
     |            ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `const_eval_select`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
Original diagnostics will follow.

warning: attribute #[no_mangle] set on a Rust ABI function
  --> src/main.rs:12:1
   |
12 | pub fn hi(n: i32) -> i32 { n }
   | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `pub extern "C" fn hi(n: i32) -> i32`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#no_mangle_with_rust_abi
   = note: requested on the command line with `-W clippy::no-mangle-with-rust-abi`

warning: attribute #[no_mangle] set on a Rust ABI function
  --> src/main.rs:15:1
   |
15 | pub unsafe fn hey() {
   | ^^^^^^^^^^^^^^^^^^^ help: try: `pub unsafe extern "C" fn hey()`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#no_mangle_with_rust_abi

Version

rustc 1.69.0-nightly (d962ea578 2023-02-26)
binary: rustc
commit-hash: d962ea57899d64dc8a57040142c6b498a57c8064
commit-date: 2023-02-26
host: x86_64-unknown-linux-gnu
release: 1.69.0-nightly
LLVM version: 15.0.7

Additional Labels

No response

@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied I-false-positive Issue: The lint was triggered on code it shouldn't have labels Feb 26, 2023
@matthiaskrgr
Copy link
Member Author

no_mangle on main() also introduces some interesting suggestion which does not compile 😅

warning: attribute #[no_mangle] set on a Rust ABI function
  --> src/main.rs:11:1
   |
11 | pub fn main() {
   | ^^^^^^^^^^^^^ help: try: `pub extern "C" fn main()`
   |

@matthiaskrgr
Copy link
Member Author

^

#![crate_type = "cdylib"]

#[link(wasm_import_module = "test")]
extern "C" {
    fn log(message_data: u32, message_size: u32);
}

#[no_mangle]
pub fn main() {
    let message = "Hello, world!";
    unsafe {
        log(message.as_ptr() as u32, message.len() as u32);
    }
}

cc @nindalf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant