Skip to content

Conflicting constified module enum/rustified enum regex leads to compile error when C enum has non-unique discriminant values #1198

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

Closed
tmfink opened this issue Dec 26, 2017 · 4 comments

Comments

@tmfink
Copy link
Contributor

tmfink commented Dec 26, 2017

Input C/C++ Header

typedef enum MyDupeEnum {
	A = 0,
	A_alias = 0,
	B,
} MyDupeEnum;

Bindgen Invocation

$ bindgen input.h test-constify-mod-enum.h --rustified-enum '.*' "--constified-enum-module" ".*"

Actual Results

/* automatically generated by rust-bindgen */

pub const MyDupeEnum_A_alias: MyDupeEnum::Type = MyDupeEnum::A;
#[repr(u32)]
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum MyDupeEnum {
    A = 0,
    B = 1,
}

and/or

error[E0223]: ambiguous associated type
 --> /tmp/a.rs:3:31
  |
3 | pub const MyDupeEnum_A_alias: MyDupeEnum::Type = MyDupeEnum::A;
  |                               ^^^^^^^^^^^^^^^^ ambiguous associated type
  |
  = note: specify the type using the syntax `<MyDupeEnum as Trait>::Type`

error: aborting due to previous error

Expected Results

There are several possibilities for correct behavior:

  1. Error out immediately because regex passed to --rustified-enum/--constified-enum-module overlap.
  2. Define the precedence of enum strategies (rustified, constified module, and constified).
  3. Support a more complete way for users to convey what enum strategy is used for a given enum. For example, bindgen could take a callback or struct that implements a ConveysEnumStrategy trait).
  4. Whichever regex is passed first has precedence (the order of arguments/builder pattern calls matters). This would complicate the internal bindgen logic.

Example of behavior 2:

/* automatically generated by rust-bindgen */

#[repr(u32)]
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum MyDupeEnum {
    A = 0,
    A_alias = 0,
    B = 1,
}
@fitzgen
Copy link
Member

fitzgen commented Jan 3, 2018

Thanks for the bug report @tmfink !

I think we should define a precedence and go with that.

tmfink added a commit to tmfink/rust-bindgen that referenced this issue Mar 13, 2018
If an enum matched a pattern for rustified enum and constified module
enum, then rust code would fail to compile with "ambiguous associated
type" error. We fix the error by giving constified module enum "higher
precedence".

Fixes issue rust-lang#1198
bors-servo pushed a commit that referenced this issue Mar 13, 2018
Declare precedence on enum types

Fixes issue #1198 where an enum matches the pattern for multiple enum types, such as constified module enum AND rustified enum.

Documents precedence in `Builder` doc comment.
@tmfink tmfink closed this as completed Mar 14, 2018
tmfink added a commit to tmfink/rust-bindgen that referenced this issue Mar 17, 2018
The previous fix for issue rust-lang#1198 was incomplete.
tmfink added a commit to tmfink/rust-bindgen that referenced this issue Mar 17, 2018
The previous fix for issue rust-lang#1198 was incomplete.
bors-servo pushed a commit that referenced this issue Mar 18, 2018
Handle bitfield enum pattern aliasing

The previous fix for issue #1198 was incomplete.
@tmfink
Copy link
Contributor Author

tmfink commented Mar 25, 2018

When can we expect a new bindgen release? I want to release a new version of capstone-sys that takes advantage of these bug fixes.

@emilio
Copy link
Contributor

emilio commented Mar 25, 2018

I just bumped the version, will publish it in a sec.

@tmfink
Copy link
Contributor Author

tmfink commented Mar 26, 2018 via email

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

No branches or pull requests

3 participants