ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.81k stars 2.55k forks source link

Function with small C struct parameter compiles but behaves wrong #9574

Closed iacore closed 1 year ago

iacore commented 3 years ago

When you use valid C code to initialize a struct, the result isn't valid. I don't think that should be allowed to compile.

When a C struct is used during multiple compilations, their layout can change. Maybe it's some other bug I don't understand. This leads to very janky behavior like having -nan where assigned field should be. This may make the project compile but behave wrong. This is certainly very evil.

This happens when you have a C library header file that can be used as definition only or includes implementation. So far, this breaks Nuklear and microui

Here's a minimal project that reproduces the bug: https://github.com/locriacyber/test-zig-nuklear run make and then compare the output of two produced executables

related to #1481

> zig version
0.8.0
kcbanner commented 3 years ago

Shouldn't that @cInclude be for dummy.c, that way the implementation is there?

iacore commented 3 years ago

Shouldn't that @cInclude be for dummy.c, that way the implementation is there?

That will be a compiler error. However, when linking with dynamic library/existing object files, only struct/function definitions should be used

kcbanner commented 3 years ago

Shouldn't that @cInclude be for dummy.c, that way the implementation is there?

That will be a compiler error. However, when linking with dynamic library/existing object files, only struct/function definitions should be used

Ah, sorry I misunderstood that you were linking it separately there.

Vexu commented 1 year ago

C ABI issues should be mostly solved.