Thanks to visit codestin.com
Credit goes to github.com

Skip to content

unable to @cImport identifiers that are zig keywords #6382

@marler8997

Description

@marler8997

The following C header doesn't work with @cImport:

typedef enum {
    f32,
    u32,
} BadEnum;

Here's a zig file to demonstrate:

const c = @cImport({
    @cInclude("badenum.h");
});
pub fn main() void {
    var a : c.BadEnum = undefined;
}
$ zig build-exe badenum.zig -I.
zig-cache/o/HGGngBrkPyxavUDS-SZLX2-M6376Z_CrbZVjk7IMXz5X9huyNP6Zn7L4sGYyIly5/cimport.zig:1:5: error: declaration shadows primitive type 'f32'
pub const f32 = @enumToInt(enum_unnamed_1.f32);
    ^
zig-cache/o/HGGngBrkPyxavUDS-SZLX2-M6376Z_CrbZVjk7IMXz5X9huyNP6Zn7L4sGYyIly5/cimport.zig:2:5: error: declaration shadows primitive type 'u32'
pub const u32 = @enumToInt(enum_unnamed_1.u32);
    ^
./badenum.zig:5:13: note: referenced here
    var a : c.BadEnum = undefined;
            ^
zig-cache/o/HGGngBrkPyxavUDS-SZLX2-M6376Z_CrbZVjk7IMXz5X9huyNP6Zn7L4sGYyIly5/cimport.zig:8:21: note: referenced here
pub const BadEnum = enum_unnamed_1;
                    ^
./badenum.zig:5:14: note: referenced here
    var a : c.BadEnum = undefined;

Modifying the enum identifiers to non zigkeywords fixes the issue. Zig's translate-c needs to be aware whether or not a C identifier is a keyword and handle it accordingly.

Issue raised on reddit: https://www.reddit.com/r/Zig/comments/ivv1j3/declaration_shadows_primitive_type_error/

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviortranslate-cC to Zig source translation feature (@cImport)

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions