Skip to content

[Proposal] Allow @export to export any value, not just a declaration #14911

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

Closed
mlugg opened this issue Mar 14, 2023 · 2 comments
Closed

[Proposal] Allow @export to export any value, not just a declaration #14911

mlugg opened this issue Mar 14, 2023 · 2 comments
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@mlugg
Copy link
Member

mlugg commented Mar 14, 2023

Currently, the first argument to @export needs to be a name of some sort. Quoting from the documentation:

declaration must be one of two things:

  • An identifier (x) identifying a function or a variable.
  • Field access (x.y) looking up a function or a variable.

Once #14796 lands, this restriction seems pretty needless. @export(comptime get_my_u32(), .{ .name = "foo" }) has a clear meaning which is no harder to lower (you just need the behaviour implemented in that PR). The only necessary restriction on this builtin is that the first argument is:

  • comptime-known; or
  • some (comptime-resolvable) reference to a global, including a plain identifier, field access, array access, etc.

Put more simply, it's just any expression x where &x is comptime-known.

Exporting an arbitrary comptime-known expression is already possible by simply putting it in a const first (since that variable is comptime-known, it is then able to be exported). But making this change would simplify the language by removing a needless restriction.

An alternative approach, which is arguably clearer, is to make @export actually take a pointer; so rather than @export(foo, ...), you have to do @export(&foo, ...). This makes @export a nice parallel to @extern, as well as making @export even less of a special-case: it has no specific restrictions on its argument at all other than that it has to be a comptime-known pointer. I don't have strong feelings either way on this.

@silversquirl
Copy link
Contributor

I definitely much prefer the option of @export taking a pointer, since it mirrors @extern and simplifies the rules.

@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Apr 10, 2023
@andrewrk andrewrk added this to the 0.11.0 milestone Apr 10, 2023
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Jul 20, 2023
@andrewrk andrewrk added the accepted This proposal is planned. label Sep 22, 2023
@mlugg
Copy link
Member Author

mlugg commented Sep 22, 2023

Accepted variant is to take a pointer.

mlugg added a commit to mlugg/zig that referenced this issue Mar 26, 2024
This commit does not yet migrate uses of this builtin.

Resolves: ziglang#14911
mlugg added a commit to mlugg/zig that referenced this issue Aug 26, 2024
@mlugg mlugg closed this as completed in f2d7096 Aug 27, 2024
richerfu pushed a commit to richerfu/zig that referenced this issue Oct 28, 2024
scottredig added a commit to dasimmet/zig-javascript-bridge that referenced this issue Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

3 participants