Returning struct not supported

Issue #288 resolved
Paweł Bylica
created an issue

I have an issue with returning a struct from a function. The struct looks more/less like this:

struct result {
    int code;
    int64_t gas_left;

    union
    {
        struct
        {
            uint8_t const* output_data;
            size_t output_size;
        };
        struct hash160 create_address;
    };

    void* release;
    char const* error_message;
    void* internal_memory;
};

The error I get is strange: NotImplementedError: ctype 'struct result' not supported as return value (it is a struct declared with "...;", but the C calling convention may depend on the missing fields) The result struct is not declared with .... I know the issue is with the union inside the struct, but help me understand it more. Can I do anything about it?

Comments (3)

  1. Armin Rigo

    The error message is indeed confusing; at least I should fix that. What it should be saying is "oops, we can't really support function that return such a complicated structure with a union inside". In some cases, like out-of-line API mode, such functions are supported, but you still can't take function pointers to this type. The problem is that the libffi library doesn't support that.

  2. Armin Rigo

    So, the details are obscure, the error message is confusing, and likely the logic is wrong in some cases. Looking at it. But the official version, I think, should be this:

    A struct is "too complicated" if it either contains "...;", if it is a union, or if it has nested structs or unions.

    Such a "too complicated" struct cannot generally be used as argument or result in a function call with CFFI. The exception is that you can use it as arg or result in a "direct function" in API mode. Direct functions are the ones declared in the cdef() directly (not function pointers) and which don't take varargs.

  3. Log in to comment