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

C Backend Miscompiles With static void const Incomplete Type #22364

Open
Bud-ro opened this issue Dec 30, 2024 · 0 comments
Open

C Backend Miscompiles With static void const Incomplete Type #22364

Bud-ro opened this issue Dec 30, 2024 · 0 comments
Labels
backend-c The C backend (CBE) outputs C source code. bug Observed behavior contradicts documented or intended behavior miscompilation The compiler reports success but produces semantically incorrect code.
Milestone

Comments

@Bud-ro
Copy link

Bud-ro commented Dec 30, 2024

Zig Version

0.14.0-dev.2384+cbc05e0b1

Steps to Reproduce and Observed Behavior

OS: Windows 10

When using the C-backend I run into an issue of a static void const type being generated, which leads to a compile error in GCC. A minimized example can be found on my branch: https://github.com/Bud-ro/zig-pub-sub/tree/error_reproduction, code pasted for convenience:

const std = @import("std");

const Erd = struct {
    T: type,
    owner: ErdOwner,

    pub const ErdOwner = enum {
        Ram,
        Indirect,
    };
};

pub const ErdDefinitions = struct {
    // zig fmt: off
    application_version:  Erd = .{ .T = u32,   .owner = .Ram      },
    some_bool:            Erd = .{ .T = bool,  .owner = .Ram      },
    unaligned_u16:        Erd = .{ .T = u16,   .owner = .Ram      },
    always_42:            Erd = .{ .T = u16,   .owner = .Indirect },
    pointer_to_something: Erd = .{ .T = ?*u16, .owner = .Ram      },
    another_erd_plus_one: Erd = .{ .T = u16,   .owner = .Indirect },
    // zig fmt: on
};

const erds = ErdDefinitions{};

pub export fn main() void {
    std.log.err("some_bool has type {}", .{erds.some_bool.T});
}

When this is compiled with zig build-exe -target x86_64-linux-gnu -OReleaseSmall -ofmt=c ./src/example_repro.zig -femit-bin=".\zig-out\bin\main.c" and put through GCC (inside WSL, this was necessary because the C-backend doesn't support Windows to my knowledge) the output is the following:

$ gcc --version
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
$ gcc ./zig-out/bin/main.c
./zig-out/bin/main.c:697:19: error: variable or field ‘example_repro_erds__227’ declared void
  697 | static void const example_repro_erds__227;
      |                   ^~~~~~~~~~~~~~~~~~~~~~~
./zig-out/bin/main.c:4339:19: error: variable or field ‘example_repro_erds__227’ declared void
 4339 | static void const example_repro_erds__227 = {};
      |                   ^~~~~~~~~~~~~~~~~~~~~~~
./zig-out/bin/main.c:4339:45: error: empty scalar initializer
 4339 | static void const example_repro_erds__227 = {};

Removing that ErdOwner enum field and definition leads to a C file free of this empty type.

Expected Behavior

Expected behavior is for types only used at comptime to not have code generation in the C-backend. Further, it is expected that no incomplete types would be generated by the C-backend.

@Bud-ro Bud-ro added the bug Observed behavior contradicts documented or intended behavior label Dec 30, 2024
@jacobly0 jacobly0 added backend-c The C backend (CBE) outputs C source code. miscompilation The compiler reports success but produces semantically incorrect code. labels Jan 5, 2025
@jacobly0 jacobly0 added this to the 0.15.0 milestone Jan 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend-c The C backend (CBE) outputs C source code. bug Observed behavior contradicts documented or intended behavior miscompilation The compiler reports success but produces semantically incorrect code.
Projects
None yet
Development

No branches or pull requests

2 participants