Skip to content

Cannot include files with large C structs (evaluation exceeded 1000 backwards branches) #206

@er-azh

Description

@er-azh

Steps to reproduce:

  1. generate any sufficiently large struct (or used the attached example), for example using:
with open("test.h", "w") as header_file:
    header_file.write("struct Test {\n")

    for i in range(1001):
        header_file.write(f"    int field_{i};\n")

    header_file.write("};\n\n")
  1. use zig translate-c or @cImport to include it.

Actual behavior:

for example building

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

pub fn main() void {
    _ = c.Test;
}

with

zig build-exe -I. test.zig

will always fail with:

/home/.../cimport.zig:1058:50: error: evaluation exceeded 1000 backwards branches
    field_1000: c_int = @import("std").mem.zeroes(c_int),
                        ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
/home/.../cimport.zig:1058:50: note: use @setEvalBranchQuota() to raise the branch limit from 1000
referenced by:
    struct_Test: /home/.../cimport.zig:57:32
    Test: /home/.../cimport.zig:1522:18
    6 reference(s) hidden; use '-freference-trace=8' to see all references

and since this error happens inside the generated struct definition, putting @setEvalBranchQuota() inside of the @cImport block will not have any effect. i also could not find any documentation or mechanism that would allow users to set a custom quota inside of the generated code.

Expected behavior:

translate-c should handle the quota increase or at least provide a mechanism and documentation that allows the users to do so.

Versions:

Zig 0.15.2 (installed through archlinux repos)

zig env
.{
    .zig_exe = "/usr/bin/zig",
    .lib_dir = "/usr/lib/zig",
    .std_dir = "/usr/lib/zig/std",
    .global_cache_dir = ".cache/zig",
    .version = "0.15.2",
    .target = "x86_64-linux.6.12.51...6.12.51-gnu.2.42",
    .env = .{
        .ZIG_GLOBAL_CACHE_DIR = null,
        .ZIG_LOCAL_CACHE_DIR = null,
        .ZIG_LIB_DIR = null,
        .ZIG_LIBC = null,
        .ZIG_BUILD_RUNNER = null,
        .ZIG_VERBOSE_LINK = null,
        .ZIG_VERBOSE_CC = null,
        .ZIG_BTRFS_WORKAROUND = null,
        .ZIG_DEBUG_CMD = null,
        .CC = null,
        .NO_COLOR = null,
        .CLICOLOR_FORCE = null,
        .XDG_CACHE_HOME = null,
        .HOME = "/home/user",
    },
}

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