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

Interaction between comptime var and runtime struct creation #17063

Closed
justin-geopipe opened this issue Sep 3, 2023 · 1 comment
Closed

Interaction between comptime var and runtime struct creation #17063

justin-geopipe opened this issue Sep 3, 2023 · 1 comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@justin-geopipe
Copy link

Zig Version

0.11.0 (playground)

Steps to Reproduce and Observed Behavior

This code runs without error:

pub fn main() !void {
    comptime var MyType: type = i32;
    const MyStruct = struct { v: MyType };

    //_ = MyStruct { .v = 5 }; // line A

    MyType = [2]f32;
    _ = MyStruct { .v = [2]f32 {1.4, 2.4} }; // line B
}

However, if you uncomment "line A", then "line B" errors. That is, the introduction of the runtime code _ = MyStruct { .v = 5 }; changes the type of MyStruct.

Expected Behavior

My guess as to what's going on is that this is an interaction between mutation (comptime var) and laziness. Mutation and laziness should never mix, so I'd expect either:

  • Prevent the laziness: the above code (with line A still commented out) produces a type error on line B, because MyStruct contains an i32 at the time it is defined.
  • Prevent the mutation: the above code (with line A still commented out) produces an error saying that comptime var is not allowed in type definitions.

Related to #7396, though so far as I've seen this isn't exactly covered by any of the other issues it cites. For example, there are no closures involved.

@justin-geopipe justin-geopipe added the bug Observed behavior contradicts documented or intended behavior label Sep 3, 2023
@Vexu Vexu added proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. and removed bug Observed behavior contradicts documented or intended behavior labels Sep 7, 2023
@Vexu Vexu added this to the 0.12.0 milestone Sep 7, 2023
@andrewrk andrewrk modified the milestones: 0.12.0, 0.13.0 Sep 26, 2023
@mlugg
Copy link
Member

mlugg commented Jul 28, 2024

This design issue is solved on master. The value of MyType is resolved eagerly at the point of the struct declaration, so the same error occurs regardless of whether line A is commented.

@mlugg mlugg closed this as not planned Won't fix, can't repro, duplicate, stale Jul 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

4 participants