-
Notifications
You must be signed in to change notification settings - Fork 354
Merge generic enum tags #785
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
base: master
Are you sure you want to change the base?
Merge generic enum tags #785
Conversation
Looks good to me as a total outsider. @emilio, are you the right person to ping these days? You have the most commits last year. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the lag here, I haven't been able to take a look lately at cbindgen stuff.
In general the feature makes sense, but instead of the Rc<Cell<>>
shenanigans and having to keep the unmangled paths around, it seems it could be implemented more cleanly and efficiently in instantiate_monomorph
. At that point, you know you have a generic, and you're going to monomorphize it. It should be feasible to synthesize a tag enum there, and just carry an is_monomorphic: bool
or so to avoid generating the tag if needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Looks much better. Some final requests, and also can you add the new option to the docs?
With that it looks good.
Thanks, if CI is green this looks good. Can you squash your commits in the way you best see fit so that we don't have "Tidy up" commits? Or would you be fine with me squashing before merging? |
a9518c2
to
e8373f1
Compare
hi all, i am interested in getting this PR merged as it would greatly help me in my project 🙂 Is there something I can do to make this happen? |
generated code is ugly until we get mozilla/cbindgen#785 merged. when this is done, type aliases can be used and it will look okay
hi @emilio |
I emailed Emilio and Ben, and Ben gave approval for squash-merging! So we just need to get conflicts resolved, and we should good to go. |
Hmm, I merged in master (PR) and I think something is still not quite right. I'll work on a simpler reproducer. When mixing this with a few other things, I can get some strange results (ha), e.g.: typedef enum Result_Tag {
PROFILE_EXPORTER_RESULT_OK_HANDLE_PROFILE_EXPORTER,
PROFILE_EXPORTER_RESULT_ERR_HANDLE_PROFILE_EXPORTER,
} Result_Tag; |
Reproducer for the issues. Example rust code: #[repr(C)]
pub enum Result<T, E> {
Ok(T),
Err(E),
}
#[no_mangle]
pub extern "C" fn fn1() -> Result<u32, u64> {
Result::Ok(0)
}
#[no_mangle]
pub extern "C" fn fn2() -> Result<u64, u32> {
Result::Ok(0)
} With this language = "C"
include_guard = "DDOG_CBINDGEN_TEST_H"
no_includes = true
sys_includes = ["stdbool.h", "stddef.h", "stdint.h"]
[export]
prefix = "ddog_cbindgen_"
[enum]
merge_generic_tags = true
prefix_with_name = true
rename_variants = "ScreamingSnakeCase" Results in this output (except I've annotated certain things): #ifndef DDOG_CBINDGEN_TEST_H
#define DDOG_CBINDGEN_TEST_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
typedef enum ddog_cbindgen_Result_Tag {
DDOG_CBINDGEN_RESULT_OK,
DDOG_CBINDGEN_RESULT_ERR,
} ddog_cbindgen_Result_Tag;
// NOTE: Missed the rename on the enum here.
typedef enum Result_Tag {
// NOTE: missed merging generics here.
DDOG_CBINDGEN_RESULT_U32_U64_OK_U32_U64,
DDOG_CBINDGEN_RESULT_U32_U64_ERR_U32_U64,
} Result_Tag;
typedef struct ddog_cbindgen_Result_u32__u64 {
Result_Tag tag;
union {
struct {
uint32_t ok;
};
struct {
uint64_t err;
};
};
} ddog_cbindgen_Result_u32__u64;
// NOTE: Missed the rename on the enum here.
typedef enum Result_Tag {
// NOTE: missed merging generics here.
DDOG_CBINDGEN_RESULT_U64_U32_OK_U64_U32,
DDOG_CBINDGEN_RESULT_U64_U32_ERR_U64_U32,
} Result_Tag;
typedef struct ddog_cbindgen_Result_u64__u32 {
Result_Tag tag;
union {
struct {
uint64_t ok;
};
struct {
uint32_t err;
};
};
} ddog_cbindgen_Result_u64__u32;
struct ddog_cbindgen_Result_u32__u64 fn1(void);
struct ddog_cbindgen_Result_u64__u32 fn2(void);
#endif /* DDOG_CBINDGEN_TEST_H */ |
See attached issue.
Resolves #784