-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Proposal for semantics of u0 and i0 types #1625
Comments
Would the address of
If I understand correctly they will have the same address, but then I wonder a bit about the unique address rules. Would it not be simpler to just give |
f1 and f2 would have the same offset (zero in this case) and thus the same address. My thinking was they "must" have an address and that making them unique seemed reasonable. In C++ empty structs are required to be unique so it seemed a good choice. If people don't like the unique requirement then what should the value be, I'd suggest usize.max as 0 wouldn't be a good choice. |
So I've explored how C handles
And here is the execution using gcc:
As noted in my repo, clang actually give
|
✔️ status quo
✔️ status quo
❌ not planned. To get a unique address,
❌ not planned. Just as a memory allocation of 0 bytes gives you a slice of
✔️ planned
✔️ status quo |
It doesn't. You can explore how GCC or Clang or MSVC handle empty structs, but empty structs are not really allowed in C (or maybe they're allowed but compiler dependent, or maybe it's version specific, or maybe C and C++ differ; it's complicated.). I don't like the idea of looking at the corner case behavior of C implementations to inform language design. |
@winksaville what do you propose should be the behavior of an array of var array: [100]u0 = undefined; What is |
As I showed C allows an entity with a sizeof(xx) == 0 to have unique addresses. I'm not saying just because it works in C it should work in zig, but it is an example and seems reasonable. But if you don't like uniqueness, what about: Regarding So what about: We probaby need another rule: |
@thejoshwolfe I propose:
|
@thejoshwolfe what about a future "packed" arrays? Based on the behavior I saw with
And an array of u0 would be:
|
I don't think there are any plans for packed arrays. That should probably have its own discussion. |
I've updated the original post to contian "Version 2" of the proposed list of rules. |
It looks good, except instead of "indeterminate" the value should literally be |
If offsets are
When executed the results are:
|
@andrewrk, thoughts on make-struct.zig above. |
Proposal:
All members of
TypeId.Int
should have well defined semantics and all operationsperform as expected. This is currently true except for
u0
andi0
. This proposalattempts is to make
u0
andi0
first class members ofTypeId.Int
. Belowu0
andi0
are referred to asx0
.Discussions about x0:
Version 2
x0
: Shall have a value of 0.@sizeOf(x0)
: Shall be 0.Address of var x0 or var s
: struct { f: x0 }: Shall be an indeterminate non-null value.Fields in a struct or packed struct
: Shall have an address equal to address of itsstruct + @byteOffsetOf(struct, "field")
.x0 in a struct
: Has an indeterminate @byteOffsetOf and @bitOffsetOf.x0
in a
packed struct: Shall have the
@byteOffsetOfand
@bitOffsetOfof the following non-zero length field. If there is no following non-zero length field, it shall have the
@byteOffsetOfand
@bitOffsetOfas if there was a
u1` field following.x0
in aextern struct
: Not allowed since anx0
is not supported n a C struct.Version 1
x0
: Shall have a value of 0.@sizeOf(x0)
: Shall be 0.Address of
var x0
orvar s: struct { f: x0 }
: Shall be unique, this can be accomplishedby always allocating 1 byte for each
var x0
orvar s: struct { f: x0 }
. It is allowedto optimize away the allocation if the address of such a variable is known to never be taken.
x0
in astruct
: Has an indeterminate@byteOffsetOf
and@bitOffsetOf
. But shall be0 to
@sizeOf(struct)
inclusive for@bytteOffsetOf
and 0 to@sizeOf(struct) * 8
inclusivefor
@bitOffsetOf
.x0
in apacked struct
: Shall have the@byteOffsetOf
and@bitOffsetOf
of thefollowing non-zero length field. If there is no following non-zero length field, it shall have
the
@byteOffsetOf
and@bitOffsetOf
as if there was au1
field following.x0
in aextern struct
: Not allowed since anx0
is not supported n a C struct.The text was updated successfully, but these errors were encountered: