Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enum case containing Box<T> is not lowered to a pointer #745

Closed
morrisonlevi opened this issue Mar 29, 2022 · 0 comments · Fixed by #749
Closed

enum case containing Box<T> is not lowered to a pointer #745

morrisonlevi opened this issue Mar 29, 2022 · 0 comments · Fixed by #749

Comments

@morrisonlevi
Copy link

morrisonlevi commented Mar 29, 2022

The example below generates two functions. The Box<u32> in make_option's return type is correctly lowered to uint32_t * but for the EntryResult in make_result the codegen generates an opaque type that it uses incorrectly:

#[repr(C)]
pub enum EntryResult {
    Ok(Box<u32>),
    Err(u32),
}

#[no_mangle]
pub extern "C" fn make_option() -> Option<Box<u32>> {
    None
}

#[no_mangle]
pub extern "C" fn make_result() -> EntryResult {
    EntryResult::Err(0)
}

Generated C code (though I've added comments):

#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

typedef struct Box_u32 Box_u32; // uh-oh

typedef enum EntryResult_Tag {
  Ok,
  Err,
} EntryResult_Tag;

typedef struct EntryResult {
  EntryResult_Tag tag;
  union {
    struct {
      // this is an incomplete type; it cannot be used here. Expected uint32 *.
      struct Box_u32 ok;
    };
    struct {
      uint32_t err;
    };
  };
} EntryResult;

uint32_t *make_option(void);

struct EntryResult make_result(void);

My actual use case is pretty close to what's shown here: I want to return a Result type and since Result isn't repr(C) I roll my own. I've done this with other types that are repr(C) before, but this is the first time I've wanted to use a Box<T> and was surprised at the result (ha).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant