Skip to content

Returning a result of an option and error does not generate all structs for the C import bindgen #194

@radu-matei

Description

@radu-matei

Consider the following interface:

// test.wit
enum error {
    success,
    failure
}

option-test: function() -> expected<option<string>, error>

Generating the C import bindings:

$ wit-bindgen c --import test.wit

Then, trying to compile results in the following error:

  test_error_t test_option_test(test_option_string_t *ret0);
                                ^
1 error generated.

Looking at the generated bindings, test_option_string_t is clearly missing:

#ifndef __BINDINGS_TEST_H
#define __BINDINGS_TEST_H
#ifdef __cplusplus
extern "C"
{
  #endif
  
  #include <stdint.h>
  #include <stdbool.h>
  
  typedef struct {
    char *ptr;
    size_t len;
  } test_string_t;
  
  void test_string_set(test_string_t *ret, const char *s);
  void test_string_dup(test_string_t *ret, const char *s);
  void test_string_free(test_string_t *ret);
  typedef uint8_t test_error_t;
  #define TEST_ERROR_SUCCESS 0
  #define TEST_ERROR_FAILURE 1
  test_error_t test_option_test(test_option_string_t *ret0);
  #ifdef __cplusplus
}
#endif
#endif

Changing the interface to:

option-test: function() -> expected<option<string>, _>

This time, the header file includes the structure definition:

  typedef struct {
    // `true` if `val` is present, `false` otherwise
    bool tag;
    test_string_t val;
  } test_option_string_t;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions