Skip to content

Unexpected error: duplicate struct field: '0' in translated C code #4181

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

Closed
artob opened this issue Jan 14, 2020 · 3 comments · Fixed by #4326
Closed

Unexpected error: duplicate struct field: '0' in translated C code #4181

artob opened this issue Jan 14, 2020 · 3 comments · Fixed by #4326
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. frontend Tokenization, parsing, AstGen, Sema, and Liveness. translate-c C to Zig source translation feature (@cImport)
Milestone

Comments

@artob
Copy link

artob commented Jan 14, 2020

Screen Shot 2020-01-14 at 21 49 10

The contents of the file duplicate-struct-field-0.zig:

const std = @import("std");
const testing = std.testing;

// zig translate-c api-level.h | head -n8 | pbcopy
pub export fn android_get_device_api_level() c_int {
    var value: [92]u8 = .{
        @bitCast(u8, @truncate(i8, @as(c_int, 0))),
    } ++ .{0} ** 91;
    if (__system_property_get("ro.build.version.sdk", value) < @as(c_int, 1)) return -@as(c_int, 1);
    var api_level: c_int = atoi(value);
    return if (api_level > @as(c_int, 0)) api_level else -@as(c_int, 1);
}

test "error: duplicate struct field: '0'" {
    testing.expect(android_get_device_api_level() == -1);
}

The contents of the file api-level.h:

int android_get_device_api_level() {
  char value[92] = { 0 };
  if (__system_property_get("ro.build.version.sdk", value) < 1) return -1;
  int api_level = atoi(value);
  return (api_level > 0) ? api_level : -1;
}

This is with HEAD built a few weeks ago (2e53425).

@artob
Copy link
Author

artob commented Jan 14, 2020

Updated my Zig build to the latest HEAD (7ee0e77), and the translated code looks slightly different:

8,9c8,9
<     if (__system_property_get("ro.build.version.sdk", value) < @as(c_int, 1)) return -@as(c_int, 1);
<     var api_level: c_int = atoi(value);
---
>     if (__system_property_get("ro.build.version.sdk", &value) < @as(c_int, 1)) return -@as(c_int, 1);
>     var api_level: c_int = atoi(&value);

However, the exact same error nonetheless persists on zig test duplicate-struct-field-0.zig.

@daurnimator daurnimator added bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport) and removed translate-c C to Zig source translation feature (@cImport) labels Jan 14, 2020
@LemonBoy
Copy link
Contributor

Duplicate of #3882 and fixed in #4152 if I remember correctly.

@andrewrk andrewrk added the stage1 The process of building from source via WebAssembly and the C backend. label Jan 26, 2020
@andrewrk
Copy link
Member

andrewrk commented Jan 28, 2020

After #4152, this gives me:

./test2.zig:6:26: error: use of undefined value here causes undefined behavior
    var value: [92]u8 = .{
                         ^
./test2.zig:8:7: note: referenced here
    } ++ [1]u8{0} ** 91;
      ^

Maybe that can be considered a separate bug, but let's focus here. The code that translate-c should generate is:

    var value: [92]u8 = [1]u8{
        @bitCast(u8, @truncate(i8, @as(c_int, 0))),
    } ++ [1]u8{0} ** 91;

...which compiles fine. So I'm labeling this a translate-c bug.

@andrewrk andrewrk added contributor friendly This issue is limited in scope and/or knowledge of Zig internals. frontend Tokenization, parsing, AstGen, Sema, and Liveness. translate-c C to Zig source translation feature (@cImport) and removed stage1 The process of building from source via WebAssembly and the C backend. labels Jan 28, 2020
@andrewrk andrewrk added this to the 0.7.0 milestone Jan 28, 2020
LemonBoy added a commit to LemonBoy/zig that referenced this issue Jan 29, 2020
The compiler still doesn't like too much the newfangled anonymous arrays
so let's use the old-style declarations.

Closes ziglang#4181
LemonBoy added a commit to LemonBoy/zig that referenced this issue Jan 30, 2020
The compiler still doesn't like too much the newfangled anonymous arrays
so let's use the old-style declarations.

Closes ziglang#4181
@andrewrk andrewrk modified the milestones: 0.7.0, 0.6.0 Feb 29, 2020
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 contributor friendly This issue is limited in scope and/or knowledge of Zig internals. frontend Tokenization, parsing, AstGen, Sema, and Liveness. translate-c C to Zig source translation feature (@cImport)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants