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

Redeclaration with incompatible signature may use unexpected signature #123

Open
TheDan64 opened this issue Jun 27, 2019 · 0 comments
Open
Labels
bug Something isn't working

Comments

@TheDan64
Copy link
Contributor

TheDan64 commented Jun 27, 2019

Newlib does a little bit more complicated version of this:

#include <limits.h>

double __exp10(unsigned x);

int main(void) {
  __exp10(1U);

  return 0;
}

Where double __exp10(unsigned x) is defined in another local file.

When translating this code you get a warning from C:

/home/dkolsoi/repos/c2rust/test.c:3:8: warning: incompatible redeclaration of library function
      '__exp10' [-Wincompatible-library-redeclaration]
double __exp10(unsigned x);
       ^
/home/dkolsoi/repos/c2rust/test.c:3:8: note: '__exp10' is a builtin with type 'double (double)'

But the translated code:

extern "C" {
    #[no_mangle]
    fn __exp10(_: libc::c_double) -> libc::c_double;
}
unsafe fn main_0() -> libc::c_int { __exp10(1u32); return 0i32; }

Will fail to compile because it uses the builtin signature which takes a double and not an int.

Interestingly (but maybe not totally surprising), passing the -ffreestanding flag to clang via the transpiler will bypass this issue and use the correct signature:

extern "C" {
    #[no_mangle]
    fn __exp10(x: libc::c_uint) -> libc::c_double;
}
#[export_name = "main"]
pub unsafe extern "C" fn main_0() -> libc::c_int {
    __exp10(1u32);
    return 0i32;
}

(also interesting, but not problematic, note that it's using x not _ in the param sig)

This can be seen in newlib when configuring with --disable-newlib-fno-builtin

@TheDan64 TheDan64 added the bug Something isn't working label Jun 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant