Commit f0530385b5

Andrew Kelley <andrew@ziglang.org>
2023-02-18 04:23:33
update existing behavior tests and std lib to new for loop semantics
1 parent 321ccbd
lib/std/crypto/blake3.zig
@@ -211,7 +211,7 @@ fn first8Words(words: [16]u32) [8]u32 {
 
 fn wordsFromLittleEndianBytes(comptime count: usize, bytes: [count * 4]u8) [count]u32 {
     var words: [count]u32 = undefined;
-    for (words) |*word, i| {
+    for (&words) |*word, i| {
         word.* = mem.readIntSliceLittle(u32, bytes[4 * i ..]);
     }
     return words;
test/behavior/bugs/1607.zig
@@ -5,7 +5,7 @@ const builtin = @import("builtin");
 const a = [_]u8{ 1, 2, 3 };
 
 fn checkAddress(s: []const u8) !void {
-    for (s) |*i, j| {
+    for (s, 0..) |*i, j| {
         try testing.expect(i == &a[j]);
     }
 }
test/behavior/bugs/920.zig
@@ -23,13 +23,13 @@ fn ZigTableGen(comptime is_symmetric: bool, comptime r: f64, comptime v: f64, co
     tables.x[0] = v / f(r);
     tables.x[1] = r;
 
-    for (tables.x[2..256]) |*entry, i| {
+    for (tables.x[2..256], 0..) |*entry, i| {
         const last = tables.x[2 + i - 1];
         entry.* = f_inv(v / last + f(last));
     }
     tables.x[256] = 0;
 
-    for (tables.f[0..]) |*entry, i| {
+    for (tables.f[0..], 0..) |*entry, i| {
         entry.* = f(tables.x[i]);
     }
 
@@ -67,7 +67,7 @@ test "bug 920 fixed" {
         break :blk ZigTableGen(true, norm_r, norm_v, norm_f, norm_f_inv, norm_zero_case);
     };
 
-    for (NormalDist1.f) |_, i| {
+    for (NormalDist1.f, 0..) |_, i| {
         // Here we use `expectApproxEqAbs` instead of `expectEqual` to account for the small
         // differences in math functions of different libcs. For example, if the compiler
         // links against glibc, but the target is musl libc, then these values might be
test/behavior/array.zig
@@ -185,7 +185,7 @@ test "nested arrays of strings" {
     if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
 
     const array_of_strings = [_][]const u8{ "hello", "this", "is", "my", "thing" };
-    for (array_of_strings) |s, i| {
+    for (array_of_strings, 0..) |s, i| {
         if (i == 0) try expect(mem.eql(u8, s, "hello"));
         if (i == 1) try expect(mem.eql(u8, s, "this"));
         if (i == 2) try expect(mem.eql(u8, s, "is"));
test/behavior/bit_shifting.zig
@@ -84,14 +84,14 @@ fn testShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, c
 
     var table = Table.create();
     var node_buffer: [node_count]Table.Node = undefined;
-    for (node_buffer) |*node, i| {
+    for (&node_buffer, 0..) |*node, i| {
         const key = @intCast(Key, i);
         try expect(table.get(key) == null);
         node.init(key, {});
         table.put(node);
     }
 
-    for (node_buffer) |*node, i| {
+    for (&node_buffer, 0..) |*node, i| {
         try expect(table.get(@intCast(Key, i)) == node);
     }
 }
test/behavior/call.zig
@@ -364,7 +364,7 @@ test "Enum constructed by @Type passed as generic argument" {
             try expect(@enumToInt(a) == b);
         }
     };
-    inline for (@typeInfo(S.E).Enum.fields) |_, i| {
+    inline for (@typeInfo(S.E).Enum.fields, 0..) |_, i| {
         try S.foo(@intToEnum(S.E, i), i);
     }
 }
test/behavior/const_slice_child.zig
@@ -26,7 +26,7 @@ fn foo(args: [][]const u8) !void {
 fn bar(argc: usize) !void {
     var args_buffer: [10][]const u8 = undefined;
     const args = args_buffer[0..argc];
-    for (args) |_, i| {
+    for (args, 0..) |_, i| {
         const ptr = argv[i];
         args[i] = ptr[0..strlen(ptr)];
     }
@@ -41,7 +41,7 @@ fn strlen(ptr: [*]const u8) usize {
 
 fn streql(a: []const u8, b: []const u8) bool {
     if (a.len != b.len) return false;
-    for (a) |item, index| {
+    for (a, 0..) |item, index| {
         if (b[index] != item) return false;
     }
     return true;
test/behavior/eval.zig
@@ -317,7 +317,7 @@ test "create global array with for loop" {
 
 const global_array = x: {
     var result: [10]usize = undefined;
-    for (result) |*item, index| {
+    for (&result, 0..) |*item, index| {
         item.* = index * index;
     }
     break :x result;
@@ -447,7 +447,7 @@ test "binary math operator in partially inlined function" {
     var s: [4]u32 = undefined;
     var b: [16]u8 = undefined;
 
-    for (b) |*r, i|
+    for (&b, 0..) |*r, i|
         r.* = @intCast(u8, i + 1);
 
     copyWithPartialInline(s[0..], b[0..]);
@@ -915,7 +915,7 @@ test "comptime pointer load through elem_ptr" {
 
     comptime {
         var array: [10]S = undefined;
-        for (array) |*elem, i| {
+        for (&array, 0..) |*elem, i| {
             elem.* = .{
                 .x = i,
             };
test/behavior/fn.zig
@@ -311,7 +311,7 @@ test "function pointers" {
         &fn3,
         &fn4,
     };
-    for (fns) |f, i| {
+    for (fns, 0..) |f, i| {
         try expect(f() == @intCast(u32, i) + 5);
     }
 }
test/behavior/for.zig
@@ -55,9 +55,9 @@ fn testContinueOuter() !void {
 }
 
 test "ignore lval with underscore (for loop)" {
-    for ([_]void{}) |_, i| {
+    for ([_]void{}, 0..) |_, i| {
         _ = i;
-        for ([_]void{}) |_, j| {
+        for ([_]void{}, 0..) |_, j| {
             _ = j;
             break;
         }
@@ -81,7 +81,7 @@ test "basic for loop" {
         buffer[buf_index] = item;
         buf_index += 1;
     }
-    for (array) |item, index| {
+    for (array, 0..) |item, index| {
         _ = item;
         buffer[buf_index] = @intCast(u8, index);
         buf_index += 1;
@@ -91,7 +91,7 @@ test "basic for loop" {
         buffer[buf_index] = item;
         buf_index += 1;
     }
-    for (array_ptr) |item, index| {
+    for (array_ptr, 0..) |item, index| {
         _ = item;
         buffer[buf_index] = @intCast(u8, index);
         buf_index += 1;
@@ -101,7 +101,7 @@ test "basic for loop" {
         buffer[buf_index] = item;
         buf_index += 1;
     }
-    for (unknown_size) |_, index| {
+    for (unknown_size, 0..) |_, index| {
         buffer[buf_index] = @intCast(u8, index);
         buf_index += 1;
     }
@@ -163,11 +163,11 @@ test "for loop with pointer elem var" {
     mangleString(target[0..]);
     try expect(mem.eql(u8, &target, "bcdefgh"));
 
-    for (source) |*c, i| {
+    for (source, 0..) |*c, i| {
         _ = i;
         try expect(@TypeOf(c) == *const u8);
     }
-    for (target) |*c, i| {
+    for (&target, 0..) |*c, i| {
         _ = i;
         try expect(@TypeOf(c) == *u8);
     }
@@ -186,7 +186,7 @@ test "for copies its payload" {
     const S = struct {
         fn doTheTest() !void {
             var x = [_]usize{ 1, 2, 3 };
-            for (x) |value, i| {
+            for (x, 0..) |value, i| {
                 // Modify the original array
                 x[i] += 99;
                 try expect(value == i + 1);
@@ -206,8 +206,8 @@ test "for on slice with allowzero ptr" {
     const S = struct {
         fn doTheTest(slice: []const u8) !void {
             var ptr = @ptrCast([*]allowzero const u8, slice.ptr)[0..slice.len];
-            for (ptr) |x, i| try expect(x == i + 1);
-            for (ptr) |*x, i| try expect(x.* == i + 1);
+            for (ptr, 0..) |x, i| try expect(x == i + 1);
+            for (ptr, 0..) |*x, i| try expect(x.* == i + 1);
         }
     };
     try S.doTheTest(&[_]u8{ 1, 2, 3, 4 });
test/behavior/generics.zig
@@ -325,7 +325,7 @@ test "generic function instantiation non-duplicates" {
     const S = struct {
         fn copy(comptime T: type, dest: []T, source: []const T) void {
             @export(foo, .{ .name = "test_generic_instantiation_non_dupe" });
-            for (source) |s, i| dest[i] = s;
+            for (source, 0..) |s, i| dest[i] = s;
         }
 
         fn foo() callconv(.C) void {}
test/behavior/lower_strlit_to_vector.zig
@@ -12,7 +12,7 @@ test "strlit to vector" {
     const strlit = "0123456789abcdef0123456789ABCDEF";
     const vec_from_strlit: @Vector(32, u8) = strlit.*;
     const arr_from_vec = @as([32]u8, vec_from_strlit);
-    for (strlit) |c, i|
+    for (strlit, 0..) |c, i|
         try std.testing.expect(c == arr_from_vec[i]);
     try std.testing.expectEqualSlices(u8, strlit, &arr_from_vec);
 }
test/behavior/math.zig
@@ -1221,7 +1221,7 @@ test "quad hex float literal parsing accurate" {
                 0xb6a0000000000000,
             };
 
-            for (exp2ft) |x, i| {
+            for (exp2ft, 0..) |x, i| {
                 try expect(@bitCast(u64, x) == answers[i]);
             }
         }
test/behavior/slice.zig
@@ -99,7 +99,7 @@ test "comptime slice of slice preserves comptime var" {
 test "slice of type" {
     comptime {
         var types_array = [_]type{ i32, f64, type };
-        for (types_array) |T, i| {
+        for (types_array, 0..) |T, i| {
             switch (i) {
                 0 => try expect(T == i32),
                 1 => try expect(T == f64),
@@ -107,7 +107,7 @@ test "slice of type" {
                 else => unreachable,
             }
         }
-        for (types_array[0..]) |T, i| {
+        for (types_array[0..], 0..) |T, i| {
             switch (i) {
                 0 => try expect(T == i32),
                 1 => try expect(T == f64),
test/behavior/tuple.zig
@@ -40,7 +40,7 @@ test "tuple multiplication" {
             {
                 const t = .{ 1, 2, 3 } ** 4;
                 try expect(@typeInfo(@TypeOf(t)).Struct.fields.len == 12);
-                inline for (t) |x, i| try expect(x == 1 + i % 3);
+                inline for (t, 0..) |x, i| try expect(x == 1 + i % 3);
             }
         }
     };
test/behavior/vector.zig
@@ -456,20 +456,20 @@ test "vector division operators" {
         fn doTheTestDiv(comptime T: type, x: @Vector(4, T), y: @Vector(4, T)) !void {
             if (!comptime std.meta.trait.isSignedInt(T)) {
                 const d0 = x / y;
-                for (@as([4]T, d0)) |v, i| {
+                for (@as([4]T, d0), 0..) |v, i| {
                     try expect(x[i] / y[i] == v);
                 }
             }
             const d1 = @divExact(x, y);
-            for (@as([4]T, d1)) |v, i| {
+            for (@as([4]T, d1), 0..) |v, i| {
                 try expect(@divExact(x[i], y[i]) == v);
             }
             const d2 = @divFloor(x, y);
-            for (@as([4]T, d2)) |v, i| {
+            for (@as([4]T, d2), 0..) |v, i| {
                 try expect(@divFloor(x[i], y[i]) == v);
             }
             const d3 = @divTrunc(x, y);
-            for (@as([4]T, d3)) |v, i| {
+            for (@as([4]T, d3), 0..) |v, i| {
                 try expect(@divTrunc(x[i], y[i]) == v);
             }
         }
@@ -477,16 +477,16 @@ test "vector division operators" {
         fn doTheTestMod(comptime T: type, x: @Vector(4, T), y: @Vector(4, T)) !void {
             if ((!comptime std.meta.trait.isSignedInt(T)) and @typeInfo(T) != .Float) {
                 const r0 = x % y;
-                for (@as([4]T, r0)) |v, i| {
+                for (@as([4]T, r0), 0..) |v, i| {
                     try expect(x[i] % y[i] == v);
                 }
             }
             const r1 = @mod(x, y);
-            for (@as([4]T, r1)) |v, i| {
+            for (@as([4]T, r1), 0..) |v, i| {
                 try expect(@mod(x[i], y[i]) == v);
             }
             const r2 = @rem(x, y);
-            for (@as([4]T, r2)) |v, i| {
+            for (@as([4]T, r2), 0..) |v, i| {
                 try expect(@rem(x[i], y[i]) == v);
             }
         }
@@ -538,7 +538,7 @@ test "vector bitwise not operator" {
     const S = struct {
         fn doTheTestNot(comptime T: type, x: @Vector(4, T)) !void {
             var y = ~x;
-            for (@as([4]T, y)) |v, i| {
+            for (@as([4]T, y), 0..) |v, i| {
                 try expect(~x[i] == v);
             }
         }
@@ -577,11 +577,11 @@ test "vector shift operators" {
             var yv = @as(@Vector(N, TY), y);
 
             var z0 = xv >> yv;
-            for (@as([N]TX, z0)) |v, i| {
+            for (@as([N]TX, z0), 0..) |v, i| {
                 try expect(x[i] >> y[i] == v);
             }
             var z1 = xv << yv;
-            for (@as([N]TX, z1)) |v, i| {
+            for (@as([N]TX, z1), 0..) |v, i| {
                 try expect(x[i] << y[i] == v);
             }
         }
@@ -594,7 +594,7 @@ test "vector shift operators" {
             var yv = @as(@Vector(N, TY), y);
 
             var z = if (dir == .Left) @shlExact(xv, yv) else @shrExact(xv, yv);
-            for (@as([N]TX, z)) |v, i| {
+            for (@as([N]TX, z), 0..) |v, i| {
                 const check = if (dir == .Left) x[i] << y[i] else x[i] >> y[i];
                 try expect(check == v);
             }
test/behavior/void.zig
@@ -22,7 +22,7 @@ test "iterate over a void slice" {
     if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
 
     var j: usize = 0;
-    for (times(10)) |_, i| {
+    for (times(10), 0..) |_, i| {
         try expect(i == j);
         j += 1;
     }
test/standalone/brace_expansion/main.zig
@@ -29,7 +29,7 @@ fn tokenize(input: []const u8) !ArrayList(Token) {
     var tok_begin: usize = undefined;
     var state = State.Start;
 
-    for (input) |b, i| {
+    for (input, 0..) |b, i| {
         switch (state) {
             .Start => switch (b) {
                 'a'...'z', 'A'...'Z' => {
@@ -159,7 +159,7 @@ fn expandString(input: []const u8, output: *ArrayList(u8)) !void {
     try expandNode(root, &result_list);
 
     try output.resize(0);
-    for (result_list.items) |buf, i| {
+    for (result_list.items, 0..) |buf, i| {
         if (i != 0) {
             try output.append(' ');
         }
test/tests.zig
@@ -958,7 +958,7 @@ pub const StackTracesContext = struct {
                     // locate delims/anchor
                     const delims = [_][]const u8{ ":", ":", ":", " in ", "(", ")" };
                     var marks = [_]usize{0} ** delims.len;
-                    for (delims) |delim, i| {
+                    for (delims, 0..) |delim, i| {
                         marks[i] = mem.indexOfPos(u8, line, pos, delim) orelse {
                             // unexpected pattern: emit raw line and cont
                             try buf.appendSlice(line);