Skip to content

FFI: improper_ctypes warning when using associated type in FFI signature #26230

Closed
@ghost

Description

The associated type used in the Bar struct causes it to trigger the warning, even though the actual type FooVtable has the #[repr(C)] attribute. Using the type directly in the Baz struct works as expected so it seems that associated types don't "inherit" the attribute correctly.

Should associated types be usable in this way?
Hopefully this is just a false warning as it does seem to work correctly, but the warning is slightly worrying.

#[repr(C)]
trait Test {
    type Vtable;
}

#[repr(C)] pub struct Foo;
#[repr(C)] pub struct FooVtable;

impl Test for Foo {
    type Vtable = FooVtable;
}

#[repr(C)] pub struct Bar {
    base: <Foo as Test>::Vtable
}

#[repr(C)] pub struct Baz {
    base: FooVtable
}

extern "C" {
    pub fn external_foobar(x: *const Bar);
    pub fn external_foobar_2(x: *const Baz);
}

Error:

<anon>:22:38: 22:41 warning: found type without foreign-function-safe representation annotation
in foreign module, consider adding a #[repr(...)] attribute to the type, #[warn(improper_ctypes)]
on by default
<anon>:22     pub fn external_foobar(x: *const Bar);
                                               ^~~

Playground link

Tested on stable 1.0.0 and latest nightly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions