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

Segmentation Fault When Using Anonymous Struct Literal Assignment #23190

Open
ArielBenichou opened this issue Mar 10, 2025 · 1 comment · May be fixed by #23263
Open

Segmentation Fault When Using Anonymous Struct Literal Assignment #23190

ArielBenichou opened this issue Mar 10, 2025 · 1 comment · May be fixed by #23263
Labels
bug Observed behavior contradicts documented or intended behavior regression It worked in a previous version of Zig, but stopped working.
Milestone

Comments

@ArielBenichou
Copy link

Zig Version

0.14.0

Steps to Reproduce and Observed Behavior

  1. Create a new Zig project (I've used zig init).
  2. Place the following minimal code into your main.zig:
fn createVec(x: f32, y: f32, z: f32) [3]f32 {
    return .{ x, y, z };
}

var camera = .{
    .pos = createVec(0, 0, 0),
};

pub fn main() !void {
    camera.pos = createVec(0, 1, 0);
}
  1. Build and run the project using zig build run.

Observed Behavior
The program terminates with a segmentation fault:

Segmentation fault at address 0xaaaaaaaaaaaaaaaa
aborting due to recursive panic
run
└─ run zig_seg_fault_anony_lit_struct failure
error: the following command terminated unexpectedly:
/Users/arielbenichou/personal/zig-seg-fault-anonymous-literal-struct/zig-out/bin/zig_seg_fault_anony_lit_struct
Build Summary: 3/5 steps succeeded; 1 failed
run transitive failure
└─ run zig_seg_fault_anony_lit_struct failure
error: the following build command failed with exit code 1:
/Users/arielbenichou/personal/zig-seg-fault-anonymous-literal-struct/.zig-cache/o/0bd88885df425ba4769286815bd940e7/build /Users/arielbenichou/.zvm/0.14.0/zig /Users/arielbenichou/.zvm/0.14.0/lib /Users/arielbenichou/personal/zig-seg-fault-anonymous-literal-struct /Users/arielbenichou/personal/zig-seg-fault-anonymous-literal-struct/.zig-cache /Users/arielbenichou/.cache/zig --seed 0x82075a23 -Z0d899954f16beb30 run

Expected Behavior

Now I understand that Anonymous Struct Literals create comptime fields that can't be modified.

But should it compile at all?

@ArielBenichou ArielBenichou added the bug Observed behavior contradicts documented or intended behavior label Mar 10, 2025
@linusg
Copy link
Collaborator

linusg commented Mar 10, 2025

Correctly caused a compile error in 0.13:

test.zig:10:11: error: cannot store runtime value in compile time variable
    camera.pos = createVec(0, 1, 0);
    ~~~~~~^~~~

@linusg linusg added the regression It worked in a previous version of Zig, but stopped working. label Mar 10, 2025
mlugg added a commit to mlugg/zig that referenced this issue Mar 16, 2025
@mlugg mlugg added this to the 0.14.1 milestone Mar 16, 2025
mlugg added a commit to mlugg/zig that referenced this issue Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior regression It worked in a previous version of Zig, but stopped working.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants