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

expose the root source file with @import("root") #2189

Closed
andrewrk opened this issue Apr 5, 2019 · 5 comments · Fixed by #2659
Closed

expose the root source file with @import("root") #2189

andrewrk opened this issue Apr 5, 2019 · 5 comments · Fixed by #2659
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Apr 5, 2019

We already rely on this behavior in bootstrap.zig but it is special magic:

const root = @import("@root");

This proposal would make it always available and so the above code would not be special magic anymore.

It's reasonable for userland libraries to have global configuration and use the root source file for that. For example this would be a necessary component of #1778.

This, along with #1439 would allow us to move some of the global panic function magic to userland, and potentially even the startup code. build-exe would unconditionally include the startup code, which would have a comptime block which examines the root source file for main, _start, etc, and determines what it should export.

@andrewrk andrewrk added proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. accepted This proposal is planned. labels Apr 5, 2019
@andrewrk andrewrk added this to the 0.5.0 milestone Apr 5, 2019
@Rocknest
Copy link
Contributor

Rocknest commented Apr 6, 2019

Do i understand this correctly: a file that was specified when invoking the zig compiler is the root source file (for example zig build-exe main.zig -> main.zig)?

@emekoi
Copy link
Contributor

emekoi commented Apr 9, 2019

that is correct.

@shawnl
Copy link
Contributor

shawnl commented May 6, 2019

There is a problem. If you pub const foo = @import("root"); you end up with infinite recursion. These problems are difficult to solve, as anything from the root import would have to be resolved into a constant (including a constant function pointer) before it was imported. While I agree that there should only be one root source file, an @import("root") is much more complicated that it seems at first glance.

Anyways, this muddles with the very clean namespace semantics zig has.

@shawnl
Copy link
Contributor

shawnl commented May 6, 2019

#1778 does not need this.

You just use a comptime global in a package's namespace (does that exist?), set from the root source file.

@emekoi
Copy link
Contributor

emekoi commented Jun 11, 2019

@shawnl how would this introduce infinite recursion? due to zig's lazy nature that would be perfectly fine and is no different than:

pub const foo = @This();

for the root file. the following:

use @import("@root"); // or use @This();

would be an infinite loop but that is another issue (#2580) altogether.

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

Successfully merging a pull request may close this issue.

4 participants