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

@cInclude("sqlite3ext.h") generates invalid Zig #998

Closed
bb010g opened this Issue May 9, 2018 · 6 comments

Comments

Projects
None yet
3 participants
@bb010g

bb010g commented May 9, 2018

Given a test.zig of

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

pub fn main() void {
    c.sqlite3_libversion();
}

this error is produced on master:

zig run test.zig
/home/bb010g/Documents/test.zig:1:11: error: compiler bug: @cImport generated invalid zig code
const c = @cImport({
          ^
?.c:1:1: note: redefinition of 'sqlite3_module'
TODO: remember C source location to display here
^
?.c:1:1: note: previous definition is here
TODO: remember C source location to display here
^
/home/bb010g/Documents/test.zig:1:11: error: compiler bug: @cImport generated invalid zig code
const c = @cImport({
          ^
?.c:1:1: note: redefinition of 'sqlite3_module'
TODO: remember C source location to display here
^
?.c:1:1: note: previous definition is here
TODO: remember C source location to display here
^
/home/bb010g/Documents/test.zig:1:11: error: compiler bug: @cImport generated invalid zig code
const c = @cImport({
          ^
?.c:1:1: note: redefinition of 'sqlite3_vfs'
TODO: remember C source location to display here
^
?.c:1:1: note: previous definition is here
TODO: remember C source location to display here
^
/home/bb010g/Documents/test.zig:1:11: error: compiler bug: @cImport generated invalid zig code
const c = @cImport({
          ^
?.c:1:1: note: redefinition of 'sqlite3_vfs'
TODO: remember C source location to display here
^
?.c:1:1: note: previous definition is here
TODO: remember C source location to display here
^

@andrewrk andrewrk added this to the 0.3.0 milestone May 9, 2018

@andrewrk andrewrk added the bug label May 9, 2018

@andrewrk

This comment has been minimized.

Member

andrewrk commented May 9, 2018

Thanks for the report! Here's a workaround if you want it:

Use the command line, zig translate-c sqlite3ext.h to print to stdout the translation of the C code. Then manually fix up the invalid zig.

@binary132

This comment has been minimized.

binary132 commented May 9, 2018

I'm curious to see what invalid C this generated.

@andrewrk

This comment has been minimized.

Member

andrewrk commented May 9, 2018

The invalid zig generated was:

pub const sqlite3_module = struct_sqlite3_module;
pub const sqlite3_module = struct_sqlite3_module;

The rest looks good. I'm pretty sure this is some simple thing where translate-c forgot to check a hash table before emitting a macro, or something like that. Will look into it more later.

@bb010g

This comment has been minimized.

bb010g commented May 9, 2018

Past that, it seems to run into a variant of #624/#679.

@andrewrk

This comment has been minimized.

Member

andrewrk commented May 9, 2018

I'm working on a small test case. I have it down to 355 lines: https://clbin.com/GMcTt

@andrewrk

This comment has been minimized.

Member

andrewrk commented May 10, 2018

Here's a reproducer:

typedef struct Bar Bar;
typedef struct Foo Foo;

struct Foo {
    Foo *a;
};

struct Bar {
    Foo *a;
};

translates to

pub const Foo = struct_Foo;
pub const struct_Foo = extern struct {
    a: ?&Foo,
};
pub const Foo = struct_Foo;
pub const struct_Bar = extern struct {
    a: ?&Foo,
};

@andrewrk andrewrk closed this in bf21747 May 10, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment