You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Some struct values directly declared/created in tuples are not correctly initialized when the tuple also contains a runtime/var value. Example of a value that is not initialized is 1002 in the following code snippet: const S = struct {i: i32}; var v = S{ .i = 1001}; std.debug.print("{} {}\n", .{v, S{.i = 1002}});. Instead of printing something like "S{.i = 1001} S{.i = 1002}", it prints something like "S{.i = 1001} S{.i = 0}" or "S{.i = 1001} S{.i = 1535116088}". I originally came across this issue on 0.9.0 while learning string formatting, writing a program for 99 bottles of beer on the wall. Initially I thought it was an issue with print/format, but I found that it also happened in tuples without using print/format. I am on windows, but I believe this also happens on linux as I was able to see the bug on godbolt (zig version 0.9.0).
I searched the issue tracker and found a few tuple-related bugs, of the ones I found I think #7878 is probably the most similar or potentially related. Seems like it is also potentially related to result location semantics, which I do not really understand much about.
A full program that tests some different tuples:
conststd=@import("std");
constexpect=std.testing.expect;
constS=struct { i: i32 };
fnexpectCorrect(tuple: anytype) !void {
tryexpect(tuple[0].i==1001);
tryexpect(tuple[1].i==1002);
tryexpect(tuple[2].i==1003);
}
pubfnmain() !void {
varv1001: S= .{ .i=1001 };
varv1002: S= .{ .i=1002 };
constc1002: S= .{ .i=1002 };
// First three work as expectedtryexpectCorrect(.{ S{ .i=1001 }, S{ .i=1002 }, S{ .i=1003 } }); // All comptime-known/const (?)tryexpectCorrect(.{ S{ .i=1001 }, c1002, S{ .i=1003 } }); // All comptime-known/const (?)tryexpectCorrect(.{ v1001, c1002, comptimeS{ .i=1003 } }); // the struct is explicitly marked comptime// neither S in the following line gets initializedconstbad_tuple= .{ S{ .i=1001 }, v1002, S{ .i=1003 } };
std.debug.print("Uninitialized values: {} {} {}\n", bad_tuple);
tryexpectCorrect(bad_tuple);
// also fails if it is not stored in a const variable but used directlytryexpectCorrect(.{ v1001, c1002, S{ .i=1003 } });
}
For me this compiles and outputs:
$ zig.exe run uninit-bug.zig
Uninitialized values: S{ .i = 0 } S{ .i = 1002 } S{ .i = -194575560 }
error: TestUnexpectedResult
C:\...\zig-windows-x86_64-0.10.0-dev.2224+5b03d55c5\lib\std\testing.zig:347:14: 0x7ff7171e114b in td.testing.expect (uninit-bug.obj)
if (!ok) return error.TestUnexpectedResult;
^
C:\...\Code\uninit-bug.zig:21:5: 0x7ff71720c79e in xpectCorrect (uninit-bug.obj)
try expect(tuple[0].i == 1001);
^
C:\...\Code\uninit-bug.zig:37:5: 0x7ff7171e35bf in ain (uninit-bug.obj)
try expectCorrect(bad_tuple);
^
Expected Behavior
I expected that the values would be correctly initialized as part of the tuple, and received by the functions expectCorrect and print. Or that I would receive some kind of compiler error for invalid code.
Actual Behavior
The compiler does not emit any error messages, but when the tuple contains a value declared as var and not const, the structs that are directly declared inside the tuple are not initialized (unless marked with comptime, or if you take a reference to them).
The text was updated successfully, but these errors were encountered:
tim-barry
added
the
bug
Observed behavior contradicts documented or intended behavior
label
May 17, 2022
Vexu
added
the
stage1
The process of building from source via WebAssembly and the C backend.
label
Jun 1, 2022
Zig Version
0.10.0-dev.2224+5b03d55c5
Steps to Reproduce
Some struct values directly declared/created in tuples are not correctly initialized when the tuple also contains a runtime/var value. Example of a value that is not initialized is 1002 in the following code snippet:
const S = struct {i: i32}; var v = S{ .i = 1001}; std.debug.print("{} {}\n", .{v, S{.i = 1002}});
. Instead of printing something like "S{.i = 1001} S{.i = 1002}", it prints something like "S{.i = 1001} S{.i = 0}" or "S{.i = 1001} S{.i = 1535116088}". I originally came across this issue on 0.9.0 while learning string formatting, writing a program for 99 bottles of beer on the wall. Initially I thought it was an issue with print/format, but I found that it also happened in tuples without using print/format. I am on windows, but I believe this also happens on linux as I was able to see the bug on godbolt (zig version 0.9.0).I searched the issue tracker and found a few tuple-related bugs, of the ones I found I think #7878 is probably the most similar or potentially related. Seems like it is also potentially related to result location semantics, which I do not really understand much about.
A full program that tests some different tuples:
For me this compiles and outputs:
Expected Behavior
I expected that the values would be correctly initialized as part of the tuple, and received by the functions
expectCorrect
andprint
. Or that I would receive some kind of compiler error for invalid code.Actual Behavior
The compiler does not emit any error messages, but when the tuple contains a value declared as
var
and notconst
, the structs that are directly declared inside the tuple are not initialized (unless marked withcomptime
, or if you take a reference to them).The text was updated successfully, but these errors were encountered: