Skip to content

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

Closed

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;

Activity

  1. alexcrichton commented on Apr 14, 2022

    @alexcrichton
    Member

    Definitely a bug in the C generator, it'll be a bit before I have a chance to dig in but this probably doesn't require too much refactoring to fix.

  2. added 3 commits that reference this issue on Apr 21, 2022
    2c22a53
    65c26ad
    108ef4e
  3. added a commit that references this issue on Apr 22, 2022
    8aa2b37
  4. added a commit that references this issue on May 31, 2022
    45b5f69
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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