Skip to content

segfault using non empty init for tuple with comptime fields #18400

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
xdBronch opened this issue Dec 29, 2023 · 3 comments · Fixed by #18436
Closed

segfault using non empty init for tuple with comptime fields #18400

xdBronch opened this issue Dec 29, 2023 · 3 comments · Fixed by #18436
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@xdBronch
Copy link
Contributor

Zig Version

0.12.0-dev.1856+94c63f31f

Steps to Reproduce and Observed Behavior

test {
    const a: struct { comptime comptime_int = 0 } = .{0};
    _ = a;
}

https://gist.github.com/xdBronch/95e58100607a391599c2386efcd7d93c

Expected Behavior

probably test pass since the init has the same value as the default value, error otherwise

@xdBronch xdBronch added the bug Observed behavior contradicts documented or intended behavior label Dec 29, 2023
@Vexu Vexu added contributor friendly This issue is limited in scope and/or knowledge of Zig internals. frontend Tokenization, parsing, AstGen, Sema, and Liveness. labels Dec 30, 2023
@Vexu Vexu added this to the 0.13.0 milestone Dec 30, 2023
@Vexu
Copy link
Member

Vexu commented Dec 30, 2023

tupleFieldPtr and friends should be calling resolveStructFieldInits before calling structFieldValueComptime as is done in zirArrayInit for example.

Regression from #17692.

@xdBronch
Copy link
Contributor Author

is this diff correct/all thats needed? id like to pr if so :) my test case passes now and i ran test-behavior + test-std locally and both passed

diff --git a/src/Sema.zig b/src/Sema.zig
index f54966f33..741e1dbd7 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -27477,6 +27477,9 @@ fn tupleFieldPtr(
         },
     });
 
+    if (tuple_ty.structFieldIsComptime(field_index, mod))
+        try sema.resolveStructFieldInits(tuple_ty);
+
     if (try tuple_ty.structFieldValueComptime(mod, field_index)) |default_val| {
         return Air.internedToRef((try mod.intern(.{ .ptr = .{
             .ty = ptr_field_ty.toIntern(),

@Vexu
Copy link
Member

Vexu commented Dec 30, 2023

Should be. Could you also apply it to the other related functions?

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 contributor friendly This issue is limited in scope and/or knowledge of Zig internals. frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants