-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Language inconsistency: aliasing externs #21027
Comments
Naturally, after writing up this proposal, I immediately realised why this works like it does today. If I write
If we treat extern fns like other externs, then the second step... well, it can't be done at comptime because that's the rule we're introducing, but it can't be done at runtime because the pointee is comptime-only (we can't load a function value at runtime). In other words, I still think this design space is worth discussing, but status quo makes a lot more sense to me after realising this. |
I couldn't find the original proposal/discussion about the notion of "function values", maybe they've always been in the compiler implementation. IIUC, function values are a way for the compiler to distinguish scenarios where it is responsible for creating the function, so it has additional information/control over it (maybe more transformation opportunities like inlining, etc., maybe also generic instantiation uses them), vs a function pointer, which may be external or even runtime-known (which would be minimal information and control). Therefore the biggest discrepancy seems to be this second step of dereferencing - it is introduced by the language, and doesn't actually (need to) exist. We could keep flagging whether it's possible to "dereference a function pointer", whatever that implies semantically, and keep doing it when we know we're pointing to a Zig-generated function. |
Implementation question: how do we lower function calls, then? Specifically, given Since realising why this behavior exists, I've kind of come around to status quo. I'm leaving this proposal open in case anyone comes up with something better, but my current thoughts are that the example I give in the issue is just a weird but necessary consequence of the sanest possible design.
The inconsistency is that you can do it with |
I don't know why there should be a pointer load instruction assigning a pointer: I've not worked on the compiler. I'm sure your point is accurate to the rules currently implemented. |
I don't understand what you're saying. The expression |
I still don't know what the concept of a "function value" is actually useful for. |
Consider this code:
What happens when you reference these symbols? It turns out the second one (with the
extern var
) emits a compile error, but the first does not.Implementation-wise, this is because comptime pointer loading permits loading
.extern_func
values, but not general.variable
values (after #20964, it permits loading.@"extern"
values only if they have a function type). We can't trivially allow it for all extern values, because if a value is inspected later,.@"extern"
is not an expected tag (whereas for functions this is considered a "valid" tag).This seems pretty clearly inconsistent, so should probably be changed one way or the other -- either both are allowed, or both are disallowed. If they're disallowed, you can instead take a pointer to the whole expression. For functions the usage is the same; for other externs you'll need to use
.*
at times.Personally, I would propose that both of these forms are disallowed. This is essentially just for simplicity reasons -- the thing we're trying to do here doesn't actually make sense (you can't inspect the value of an extern at comptime!), so it's not worth bending the pointer access rules to make it work. It's the same reason you can't write
var x = 123; const y = x;
at container scope and havey
act as an alias forx
.If we go that route, some parts of
std
will need changing. For instance,std.c
aliases externs quite extensively:zig/lib/std/c.zig
Lines 9676 to 9685 in 531cd17
zig/lib/std/c.zig
Lines 8791 to 8794 in 531cd17
That second snippet would need to change
private.close
to&private.close
, and likewise for otherprivate.xyz
.The text was updated successfully, but these errors were encountered: