Skip to content
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

Variadic Functions in C with Ternery Operators Return Error with Unified Header Files #21751

Open
9p4 opened this issue Oct 19, 2024 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport)
Milestone

Comments

@9p4
Copy link

9p4 commented Oct 19, 2024

Zig Version

0.13.0

Steps to Reproduce and Observed Behavior

Minimal reproduction:

// In file a.h
#include <stdio.h>

void a(int a) {
    printf("a %s\n", (a) ? "a" : "bb");
}
// In file test.zig
const a = @cImport({
    @cInclude("a.h");
});

pub fn main() void {
    a.a(1);
}

Build with zig build-exe test.zig --library c -I.

Compiler error:

/home/user/.cache/zig/o/0200dc12bc56a44b101a174bbd0f10db/cimport.zig:297:26: error: cannot pass '[:0]const u8' to variadic function
    _ = printf("a %s\n", if (a_1 != 0) "a" else "bb");
                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/user/.cache/zig/o/0200dc12bc56a44b101a174bbd0f10db/cimport.zig:297:26: note: slices have no guaranteed in-memory representation
referenced by:
    main: test.zig:6:6
    callMain: /nix/store/vm8x0fdp3gn99adhig5g0z9ri40hrq3w-zig-0.13.0/lib/zig/std/start.zig:514:17
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

Expected Behavior

Interestingly, this issue doesn't happen when the header is separated from the functions themselves.

// In file a.h
#include <stdio.h>

void a(int);
// In file a.c
#include "a.h"

void a(int a) {
    printf("a %s\n", (a) ? "a" : "bb");
}

Build with zig build-exe test.zig a.c --library c -I., no error

@9p4 9p4 added the bug Observed behavior contradicts documented or intended behavior label Oct 19, 2024
@Vexu Vexu added the translate-c C to Zig source translation feature (@cImport) label Oct 20, 2024
@Vexu Vexu added this to the 0.14.0 milestone Oct 20, 2024
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

No branches or pull requests

2 participants