ziglang / zig

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

Empty struct value implicitly casts to pointer? #2325

Open ghost opened 5 years ago

ghost commented 5 years ago

I'm not sure this is a bug. It's probably harmless, but it seems wrong.

const EmptyStruct = struct {};

fn func(es: *EmptyStruct) void {}

test "" {
  var es = EmptyStruct {};

  func(es); // this ought to be `func(&es);`

  var esp: *EmptyStruct = es; // also compiles fine for some reason
}

If the struct has fields, it is an error.

andrewrk commented 5 years ago

This should be documented here: https://ziglang.org/documentation/master/#Pointers-to-Zero-Bit-Types

Types that have only 1 possible value have no address, and pointers to them are zero bit types themselves. Implicitly casting a value which has only 1 possible value to a mutable pointer of it is allowed since it's safe and has an obvious result.

I don't remember the motivation for this, but it's not just academic - there was something that implementing this solved and made cleaner in the compiler.

ghost commented 5 years ago

Ok. It does seem really particular that this is the only situation where incompatible-seeming types are allowed to coerce. *EmptyStruct to EmptyStruct doesn't compile, neither does any combination of two different structs (e.g. EmptyStruct to *AnotherEmptyStruct), nor a struct and another zero-bit type like u0.