-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Rust arrays on C FFI are super confusing #58905
Comments
FWIW, my preferred solution to this problem would be to just warn when arrays are not used behind a pointer on foreign function declarations, and maybe ban that in a future edition. I would prefer if we would just do that, and call it a day. However, I imagine that a lot of code will want to update to avoid this warning, or be compatible with a future edition. This will require API breaking changes all over the place. We could either say, "such is life", or we could try to allow updating the signatures of the FFI declarations without breaking their callers. Iff that were something worth doing, then the idea I suggested of automatically borrowing the array according to the mutability of the C FFI foreign function declaration fixes the error message of my example, where it is incorrectly diagnosed that the array was moved. In a sense, this is what we are already doing when we transform moves of arrays into pointers when calling foreign functions, so it isn't really a type system change, but rather a bug fix to the implementation of the type system that we already have. |
Is this not just a duplicate of #36464? |
It's an accident that the ABI matches up, since And then there's stuff like this, which I don't think we should have landed - the situation in question is impossible in C (since you always would have a wrapper rust/src/librustc_target/abi/call/mips64.rs Lines 91 to 95 in 0b7e28a
|
I think raw arrays should be marked as EDIT: Related: #24578. Will try to make a PR. |
Arrays are passed to C as a raw pointers, which means that
foo
does not move it, so this example is confusing at best.(Playground)
Errors:
If one does not try to use the moved value, this will silently compile, but
x
will be deallocated as soon as the function returns, yet the C code could still try to read (or even write - the code above doesn't make it clear what C can actually do with the pointer...) to it.It would be better if we would require code to be more explicit about this, e.g., by writing:
instead. This makes it clear that
foo
doesn't own the array, how many elements are expected behind the pointer, and whether the foreign function only reads or also might write to it.We could avoid breaking changes due to updating C FFI code by allowing people to still call
foo(x)
but treating it a as a unique or shared borrow depending on the mutability of the FFI declaration, and then applying a coercion to the raw pointer, while simultaneously emitting a warning to users that they should be more explicit and writefoo(&x as *const _)
instead.The text was updated successfully, but these errors were encountered: