-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Rust arrays on C FFI are super confusing #58905
Copy link
Copy link
Closed
Labels
A-FFIArea: Foreign function interface (FFI)Area: Foreign function interface (FFI)A-coercionsArea: implicit and explicit `expr as Type` coercionsArea: implicit and explicit `expr as Type` coercionsA-inferenceArea: Type inferenceArea: Type inferenceA-type-systemArea: Type systemArea: Type systemT-langRelevant to the language teamRelevant to the language teamneeds-rfcThis change is large or controversial enough that it should have an RFC accepted before doing it.This change is large or controversial enough that it should have an RFC accepted before doing it.
Metadata
Metadata
Assignees
Labels
A-FFIArea: Foreign function interface (FFI)Area: Foreign function interface (FFI)A-coercionsArea: implicit and explicit `expr as Type` coercionsArea: implicit and explicit `expr as Type` coercionsA-inferenceArea: Type inferenceArea: Type inferenceA-type-systemArea: Type systemArea: Type systemT-langRelevant to the language teamRelevant to the language teamneeds-rfcThis change is large or controversial enough that it should have an RFC accepted before doing it.This change is large or controversial enough that it should have an RFC accepted before doing it.
Type
Fields
Give feedbackNo fields configured for issues without a type.
Arrays are passed to C as a raw pointers, which means that
foodoes 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
xwill 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
foodoesn'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.