-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Lint check for using incorrect types in native fns should check crust functions #1843
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
Comments
@brson what is this bug asking for, more specifically?
It also looks like and that there are some hidden gotcha's that need to be addressed, like the following: (In these examples, I am linking to a simple object file whose C source, The first example compiles, but something is wrong with the calling convention, and it produces the wrong answer: type c_int = ::std::libc::c_int;
mod ntwice {
type c_int = ::std::libc::c_int;
#[abi = "cdecl"]
pub extern {
fn twice(tf: *u8, x: c_int) -> c_int;
fn callback(rf: *u8, // <===== no decl for callback
x: c_int) -> c_int;
}
}
extern fn incr2(x:c_int) -> c_int { x+2 }
extern "Rust" // <===== extern decl; none given for `callback` param `rf`.
fn rtwice(f: extern "C" fn(c_int) -> c_int, x: c_int) -> c_int {
println(fmt!("---- Rust rtwice calling f(%?)", x));
let ret = f(f(x));
println(fmt!("---- Rust rtwice calling f(%?) => %?", x, ret));
ret
}
fn main() {
println("Hello world");
unsafe {
let x = ntwice::twice(incr2, 3);
println(fmt!("Rust ntwice::twice(incr2, 3): %?", x));
let y = ntwice::callback(rtwice, 4);
println(fmt!("Rust ntwice::callback(rtwice, 4): %?", y));
}
}
This second example also compiles, but something again goes wrong with the calling convention, and it segfaults on my machine: type c_int = ::std::libc::c_int;
mod ntwice {
type c_int = ::std::libc::c_int;
#[abi = "cdecl"]
pub extern {
fn twice(tf: *u8, x: c_int) -> c_int;
// a "real" decl for callback
fn callback(rf: extern "Rust" fn(extern "C" fn(c_int) -> c_int,
x: c_int) -> c_int,
x: c_int) -> c_int;
}
}
extern fn incr2(x:c_int) -> c_int { x+2 }
// <===== no extern decl, unlike `callback` param `rf`.
fn rtwice(f: extern "C" fn(c_int) -> c_int, x: c_int) -> c_int {
println(fmt!("---- Rust rtwice calling f(%?)", x));
let ret = f(f(x));
println(fmt!("---- Rust rtwice calling f(%?) => %?", x, ret));
ret
}
fn main() {
println("Hello world");
unsafe {
let x = ntwice::twice(incr2, 3);
println(fmt!("Rust ntwice::twice(incr2, 3): %?", x));
let y = ntwice::callback(rtwice, 4);
println(fmt!("Rust ntwice::callback(rtwice, 4): %?", y));
}
}
This third example fails to compile, I assume because a fn item prefixed by type c_int = ::std::libc::c_int;
mod ntwice {
type c_int = ::std::libc::c_int;
#[abi = "cdecl"]
pub extern {
fn twice(tf: *u8, x: c_int) -> c_int;
// a "real" decl for callback
fn callback(rf: extern "Rust" fn(extern "C" fn(c_int) -> c_int,
x: c_int) -> c_int,
x: c_int) -> c_int;
}
}
extern fn incr2(x:c_int) -> c_int { x+2 }
extern "Rust" // <===== extern decl matches that of `callback` param `rf`.
fn rtwice(f: extern "C" fn(c_int) -> c_int, x: c_int) -> c_int {
println(fmt!("---- Rust rtwice calling f(%?)", x));
let ret = f(f(x));
println(fmt!("---- Rust rtwice calling f(%?) => %?", x, ret));
ret
}
fn main() {
println("Hello world");
unsafe {
let x = ntwice::twice(incr2, 3);
println(fmt!("Rust ntwice::twice(incr2, 3): %?", x));
let y = ntwice::callback(rtwice, 4);
println(fmt!("Rust ntwice::callback(rtwice, 4): %?", y));
}
}
#include <stdio.h>
int twice(int (*f)(int), int x) {
int ret, arg;
printf("-- C twice calling f(%d)\n", x);
ret = f(f(x));
printf("-- C twice done w/ f(%d) => %d\n", x, ret);
return ret;
}
int incr3(int x) {
int ret;
printf("------ C Enter incr3(f, %d)\n", x);
ret = x + 3;
printf("------ C Finis incr3(f, %d) => %d\n", x, ret);
return ret;
}
int callback(int (*rtwice)(int (*f)(int), int x), int x) {
int ret;
printf("-- C callback calling rtwice(incr3, %d)\n", x);
ret = rtwice(incr3, x);
printf("-- C callback done w/ rtwice(incr3, %d) => %d\n", x, ret);
return ret;
} |
brson did write a recent mailing list post answering some FFI questions and noting current hacks versus long term plans, see: https://mail.mozilla.org/pipermail/rust-dev/2013-May/004107.html |
visiting for bug triage, email 2013-08-19. I don't think the questions I posted to brson have been answered. To be fair, I haven't taken the time to try to distill my questions down to a single three line example, so its okay that they haven't been answered. But nonetheless, I don't know how to categorize this ticket without more information. |
A lot of this has changed with my recent pushes, but I think what @brson was referring to specifically was the lint that checks that people do not use Many of your other questions, specifically regarding the ABI of extern fns and the like, aren't relevant to this bug per se, but I think that they ought to work now. |
Low priority, no milestone |
oh noooooooooo 😉 I think this ticket is old enough to be completely outdated, especially with the changes to |
No description provided.
The text was updated successfully, but these errors were encountered: