Skip to content

cimport.zig: use of undeclared identifier #9774

@enedil

Description

@enedil

I have such zig source file (build file is the stock one, and just adds include path and links C library).

// header.h contents:
//
// typedef int X;
// X make_x(void);

const h = @cImport({
    @cInclude("header.h");
});

pub fn f() @TypeOf(h.make_x()) {
    return h.make_x();
}

pub fn main() anyerror!void {
    _ = f();
}

This results in such an error:

~/l/zigpoc [1] $ zig build --verbose-cimport
info(compilation): C import output: /home/enedil/lol/zigpoc/zig-cache/o/735ed48abfd528be8b8d13c9b28b47c8/cimport.zig
./zig-cache/o/735ed48abfd528be8b8d13c9b28b47c8/cimport.zig:3:24: error: use of undeclared identifier 'X'
pub extern fn make_x() X;
                       ^
./zig-cache/o/735ed48abfd528be8b8d13c9b28b47c8/cimport.zig:3:24: note: called from here
pub extern fn make_x() X;
                       ^
./src/main.zig:10:12: note: called from here
pub fn f() @TypeOf(h.make_x()) {
           ^
./src/main.zig:10:28: note: referenced here
pub fn f() @TypeOf(h.make_x()) {
                           ^
./src/main.zig:15:9: note: referenced here
    _ = f();
        ^

The initial lines of this cached translated C file are:

pub usingnamespace @import("std").c.builtins;
pub const X = c_int;
pub extern fn make_x() X;

It looks like X is actually defined before.

This issue doesn't appear if not using h.make_x() inside @TypeOf.

OS: Fedora 34
Zig version: 0.8.0, as shipped with Fedora.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorstage1The process of building from source via WebAssembly and the C backend.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions