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

Why isn't using *void in a struct field an error? #16444

Closed
Jarred-Sumner opened this issue Jul 19, 2023 · 5 comments
Closed

Why isn't using *void in a struct field an error? #16444

Jarred-Sumner opened this issue Jul 19, 2023 · 5 comments
Labels
question No questions on the issue tracker, please.

Comments

@Jarred-Sumner
Copy link
Contributor

Zig Version

0.11.0-dev.4006+bf827d0b5

Steps to Reproduce and Observed Behavior

Let's say you're writing some C/C++ and without noticing, you have this in your Zig:

pub const Struct = struct {
   // woops! this should be *anyopaque
   field: *void,
   trash: u32 = 0,
};

pub export fn Struct__exported(f: *Struct) *anyopaque {
    return f.field;
}

test {
   _ = Struct__exported(undefined);
}

*void was replaced by *anyopaque many months ago, but this code compiled successfully.

zig test repro.zig
All 1 tests passed.

Is *void a valid alternative to *anyopaque? If yes, why are there two (which goes against the philosophy of only one way to do things)? If no, why does this code compile successfully?

Expected Behavior

The code should not compile successfully

@Jarred-Sumner Jarred-Sumner added the bug Observed behavior contradicts documented or intended behavior label Jul 19, 2023
@nektro
Copy link
Contributor

nektro commented Jul 19, 2023

discussion in #6706

@nektro
Copy link
Contributor

nektro commented Jul 19, 2023

prior to that pointer-to-zero-sized were still valid but also zero-sized

@Jarred-Sumner
Copy link
Contributor Author

I can see an argument to be made for allowing *void as an "indirect" type, like pub fn MyType() { if (someCondition()) return void; return RealType; } and then having functions with fn function(arg: *MyType) where *MyType conditionally evaluates to *void

But are there any cases where writing *void as a type in a field isn't a mistake? I can't think of any reason (note that this implies a distinction between type literal names and type representations)

@Vexu
Copy link
Member

Vexu commented Jul 19, 2023

More discussion in #12075.

@silversquirl
Copy link
Contributor

*anyopaque did not replace *void. You're thinking of *c_void

@andrewrk andrewrk added question No questions on the issue tracker, please. and removed bug Observed behavior contradicts documented or intended behavior labels Jul 22, 2023
@andrewrk andrewrk closed this as not planned Won't fix, can't repro, duplicate, stale Jul 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question No questions on the issue tracker, please.
Projects
None yet
Development

No branches or pull requests

5 participants