Skip to content

FP no_mangle_with_rust_abi, suggestion does not apply #10409

Closed

Description

@matthiaskrgr

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

Activity

  1. added
    C-bugCategory: Clippy is not doing the correct thing
    I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied
    I-false-positiveIssue: The lint was triggered on code it shouldn't have
    on Feb 26, 2023
  2. matthiaskrgr commented on Feb 26, 2023

    @matthiaskrgr
    MemberAuthor

    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()`
       |
    
  3. matthiaskrgr commented on Feb 26, 2023

    @matthiaskrgr
    MemberAuthor

    ^

    #![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

  4. added a commit that references this issue on Mar 11, 2023
    e426ba4
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

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions