-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Can't define unsafe extern "C" fn #10025
Comments
I don't quite understand how this breaks memory safety, you explicitly have an It seems like a bug that you can't declare an |
Right, the bug is that I can't declare a function to be called from C, without allowing safe Rust code to call it too. The memory unsafety results from the workaround of declaring a safe I agree that you should be able to declare safe (This came up when I was defining |
There seems to be a discrepancy between extern "C" fns defined in Rust and those that bind to native functions. The latter are supposed to be unsafe by default, and since they're the same type, so should the former. |
Nominating. |
Hmm. It seems to me that for maximum precision, we would set it up like so:
The only downside of this I can see is that the type of C functions is cumbersome. Note that the type system is mostly setup this way (that is, types like |
1.0, backwards-compat |
I just ran into this. I agree with @nikomatsakis's analysis. The type of C functions may be cumbersome, but nobody declaring an FFI function would have to type |
Previously, the parser would not allow you to simultaneously implement a function with a different abi as well as being unsafe at the same time. This extends the parser to allow functions of the form: unsafe extern fn foo() { // ... } The closure type grammar was also changed to reflect this reversal, types previously written as "extern unsafe fn()" must now be written as "unsafe extern fn()". The parser currently has a hack which allows the old style, but this will go away once a snapshot has landed. Closes rust-lang#10025 [breaking-change]
Previously, the parser would not allow you to simultaneously implement a function with a different abi as well as being unsafe at the same time. This extends the parser to allow functions of the form: unsafe extern fn foo() { // ... } The closure type grammar was also changed to reflect this reversal, types previously written as "extern unsafe fn()" must now be written as "unsafe extern fn()". The parser currently has a hack which allows the old style, but this will go away once a snapshot has landed. Closes #10025 [breaking-change]
Previously, the parser would not allow you to simultaneously implement a function with a different abi as well as being unsafe at the same time. This extends the parser to allow functions of the form: unsafe extern fn foo() { // ... } The closure type grammar was also changed to reflect this reversal, types previously written as "extern unsafe fn()" must now be written as "unsafe extern fn()". The parser currently has a hack which allows the old style, but this will go away once a snapshot has landed. Closes rust-lang#10025 [breaking-change]
Both
and
give me a syntax error. But it's not the case that an
extern "C"
function is automatically unsafe. Dropping theunsafe
keyword gives meAnd this code compiles and segfaults:
So I can't declare an
extern "C"
function (for external linkage, with#[no_mangle]
) without also allowing safe Rust code to call that function, breaking memory safety.The text was updated successfully, but these errors were encountered: