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

Empty struct value implicitly casts to pointer? #2325

Open
ghost opened this issue Apr 21, 2019 · 2 comments
Open

Empty struct value implicitly casts to pointer? #2325

ghost opened this issue Apr 21, 2019 · 2 comments
Labels
Milestone

Comments

@ghost
Copy link

ghost commented Apr 21, 2019

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 andrewrk added the docs label Apr 21, 2019
@andrewrk andrewrk added this to the 0.5.0 milestone Apr 21, 2019
@andrewrk
Copy link
Member

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
Copy link
Author

ghost commented Apr 21, 2019

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.

@andrewrk andrewrk modified the milestones: 0.5.0, 0.6.0 Sep 20, 2019
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Jan 5, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 30, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 6, 2020
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant