Skip to content

translate-c: enum constants can have incorrect type #9153

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
ehaas opened this issue Jun 18, 2021 · 1 comment · Fixed by #9164
Closed

translate-c: enum constants can have incorrect type #9153

ehaas opened this issue Jun 18, 2021 · 1 comment · Fixed by #9164
Labels
bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport)
Milestone

Comments

@ehaas
Copy link
Contributor

ehaas commented Jun 18, 2021

The following code translates incorrectly due to the underlying int type of an enum type being different from the int type of an enum constant (in the below C code, a variable of type foo would have an underlying representation of unsigned, but the enum constant bar has type int). In the translated Zig code, bar has type c_uint since that is the return type of @enumToInt(enum_foo.bar); it should be c_int

This bug was revealed (but not caused by) #8394, I believe the fix is to coerce enum redecls to the correct int type.

enum foo { bar };
int main(void) {
   char c = bar;
}
pub const enum_foo = extern enum(c_uint) {
    bar,
    _,
};
pub const bar = @enumToInt(enum_foo.bar);
pub export fn main() c_int {
    var c: u8 = @bitCast(u8, @truncate(i8, bar));
    return 0;
}
./enums.zig:8:44: error: expected signed integer type, found 'c_uint'
    var c: u8 = @bitCast(u8, @truncate(i8, bar));
                                           ^
./enums.zig:8:30: note: referenced here
    var c: u8 = @bitCast(u8, @truncate(i8, bar));
                             ^
@Vexu Vexu added bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport) labels Jun 18, 2021
@Vexu Vexu added this to the 0.9.0 milestone Jun 18, 2021
@LemonBoy
Copy link
Contributor

The long-term plan was to stop translating C enumeration types into Zig's extern enum and instead emit each single value as a standalone constant of suitable type.

The idea was proposed and approved here.

ehaas added a commit to ehaas/zig that referenced this issue Jun 19, 2021
If the enum type cannot be translated for some reason, omit it. In that
case the previous behavior will occur - the enum constant's type will be
the enum's tag type.

Fixes ziglang#9153
ehaas added a commit to ehaas/zig that referenced this issue Jun 21, 2021
Translate enum types as the underlying integer type. Translate enum constants
as top-level integer constants of the correct type (which does not necessarily
match the enum integer type).

If an enum constant's type cannot be translated for some reason, omit it.

See discussion ziglang#2115 (comment)

Fixes ziglang#9153
ehaas added a commit to ehaas/zig that referenced this issue Jun 22, 2021
Translate enum types as the underlying integer type. Translate enum constants
as top-level integer constants of the correct type (which does not necessarily
match the enum integer type).

If an enum constant's type cannot be translated for some reason, omit it.

See discussion ziglang#2115 (comment)

Fixes ziglang#9153
Vexu pushed a commit that referenced this issue Jun 23, 2021
Translate enum types as the underlying integer type. Translate enum constants
as top-level integer constants of the correct type (which does not necessarily
match the enum integer type).

If an enum constant's type cannot be translated for some reason, omit it.

See discussion #2115 (comment)

Fixes #9153
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants