Skip to content

regression: Mismatching types for an enum and its members #216

@sagehane

Description

@sagehane

Versions

Tested on NixOS with

    .version = "0.16.0-dev.747+493ad58ff",
    .dependencies = .{
        .translate_c = .{
            .url = "git+https://github.com/ziglang/translate-c#d698816184885c9cf7eaa04e8d27cf3855f368d3",
            .hash = "translate_c-0.0.0-Q_BUWlqMBgC96AnDdjyMJp6VxKT1jIcXPQCtFLYwkjBQ",
        },
    },

using both zig translate-c and the package here. I presume I'm supposed to be reporting these type of things in this repo instead of the Zig repo.

How to reproduce

Translate an enum without a fixed underlying type with zig translate-c or the header translation example in the README:

enum Foo { bar, baz };
enum Foo { bar = 0, baz = -1U };
enum Foo { bar = 0, baz = -1LU };

Expected Behaviour

The type of bar and baz ought to be equal to enum_Foo.

Actual Behaviour

enum_Foo seems to always be unsigned and the members seem to use the smallest type that can fit them.

pub const __builtin = @import("std").zig.c_translation.builtins;
pub const __helpers = @import("std").zig.c_translation.helpers;

pub const bar: c_int = 0;
pub const baz: c_int = 1;
pub const enum_Foo = c_uint;

pub const __VERSION__ = "Aro aro-zig";
// <omitted>
pub const __builtin = @import("std").zig.c_translation.builtins;
pub const __helpers = @import("std").zig.c_translation.helpers;

pub const bar: c_int = 0;
pub const baz: c_uint = 4294967295;
pub const enum_Foo = c_uint;

pub const __VERSION__ = "Aro aro-zig";
// <omitted>
pub const __builtin = @import("std").zig.c_translation.builtins;
pub const __helpers = @import("std").zig.c_translation.helpers;

pub const bar: c_int = 0;
pub const baz: c_ulong = 18446744073709551615;
pub const enum_Foo = c_ulong;

pub const __VERSION__ = "Aro aro-zig";
// <omitted>

If my understanding is correct, the first example should translate to enum_Foo = c_int and the latter two examples should have the type of bar instead be the type of baz.

Notes

enum Foo : long { bar = 0, baz = 1 };

As stated earlier, everything seems to work as expected when specifying the underlying data type (C23 feature):

pub const __builtin = @import("std").zig.c_translation.builtins;
pub const __helpers = @import("std").zig.c_translation.helpers;

pub const bar: c_long = 0;
pub const baz: c_long = 1;
pub const enum_Foo = c_long;

pub const __VERSION__ = "Aro aro-zig";
// <omitted>

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions