authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-02-17 20:23:33-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-02-18 19:17:21-07:00
logf0530385b57218ef323747bdb7438330a07d25cc
tree07d87e78355e518aa67ea48e3cdf1a242917a5d7
parent321ccbdc525ab0f5862e42378b962c10ec54e4a1

update existing behavior tests and std lib to new for loop semantics


19 files changed, 46 insertions(+), 46 deletions(-)

lib/std/crypto/blake3.zig+1-1
......@@ -211,7 +211,7 @@ fn first8Words(words: [16]u32) [8]u32 {
211211
212212fn wordsFromLittleEndianBytes(comptime count: usize, bytes: [count * 4]u8) [count]u32 {
213213 var words: [count]u32 = undefined;
214 for (words) |*word, i| {
214 for (&words) |*word, i| {
215215 word.* = mem.readIntSliceLittle(u32, bytes[4 * i ..]);
216216 }
217217 return words;
test/behavior/array.zig+1-1
......@@ -185,7 +185,7 @@ test "nested arrays of strings" {
185185 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
186186
187187 const array_of_strings = [_][]const u8{ "hello", "this", "is", "my", "thing" };
188 for (array_of_strings) |s, i| {
188 for (array_of_strings, 0..) |s, i| {
189189 if (i == 0) try expect(mem.eql(u8, s, "hello"));
190190 if (i == 1) try expect(mem.eql(u8, s, "this"));
191191 if (i == 2) try expect(mem.eql(u8, s, "is"));
test/behavior/bit_shifting.zig+2-2
......@@ -84,14 +84,14 @@ fn testShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, c
8484
8585 var table = Table.create();
8686 var node_buffer: [node_count]Table.Node = undefined;
87 for (node_buffer) |*node, i| {
87 for (&node_buffer, 0..) |*node, i| {
8888 const key = @intCast(Key, i);
8989 try expect(table.get(key) == null);
9090 node.init(key, {});
9191 table.put(node);
9292 }
9393
94 for (node_buffer) |*node, i| {
94 for (&node_buffer, 0..) |*node, i| {
9595 try expect(table.get(@intCast(Key, i)) == node);
9696 }
9797}
test/behavior/bugs/1607.zig+1-1
......@@ -5,7 +5,7 @@ const builtin = @import("builtin");
55const a = [_]u8{ 1, 2, 3 };
66
77fn checkAddress(s: []const u8) !void {
8 for (s) |*i, j| {
8 for (s, 0..) |*i, j| {
99 try testing.expect(i == &a[j]);
1010 }
1111}
test/behavior/bugs/920.zig+3-3
......@@ -23,13 +23,13 @@ fn ZigTableGen(comptime is_symmetric: bool, comptime r: f64, comptime v: f64, co
2323 tables.x[0] = v / f(r);
2424 tables.x[1] = r;
2525
26 for (tables.x[2..256]) |*entry, i| {
26 for (tables.x[2..256], 0..) |*entry, i| {
2727 const last = tables.x[2 + i - 1];
2828 entry.* = f_inv(v / last + f(last));
2929 }
3030 tables.x[256] = 0;
3131
32 for (tables.f[0..]) |*entry, i| {
32 for (tables.f[0..], 0..) |*entry, i| {
3333 entry.* = f(tables.x[i]);
3434 }
3535
......@@ -67,7 +67,7 @@ test "bug 920 fixed" {
6767 break :blk ZigTableGen(true, norm_r, norm_v, norm_f, norm_f_inv, norm_zero_case);
6868 };
6969
70 for (NormalDist1.f) |_, i| {
70 for (NormalDist1.f, 0..) |_, i| {
7171 // Here we use `expectApproxEqAbs` instead of `expectEqual` to account for the small
7272 // differences in math functions of different libcs. For example, if the compiler
7373 // links against glibc, but the target is musl libc, then these values might be
test/behavior/call.zig+1-1
......@@ -364,7 +364,7 @@ test "Enum constructed by @Type passed as generic argument" {
364364 try expect(@enumToInt(a) == b);
365365 }
366366 };
367 inline for (@typeInfo(S.E).Enum.fields) |_, i| {
367 inline for (@typeInfo(S.E).Enum.fields, 0..) |_, i| {
368368 try S.foo(@intToEnum(S.E, i), i);
369369 }
370370}
test/behavior/const_slice_child.zig+2-2
......@@ -26,7 +26,7 @@ fn foo(args: [][]const u8) !void {
2626fn bar(argc: usize) !void {
2727 var args_buffer: [10][]const u8 = undefined;
2828 const args = args_buffer[0..argc];
29 for (args) |_, i| {
29 for (args, 0..) |_, i| {
3030 const ptr = argv[i];
3131 args[i] = ptr[0..strlen(ptr)];
3232 }
......@@ -41,7 +41,7 @@ fn strlen(ptr: [*]const u8) usize {
4141
4242fn streql(a: []const u8, b: []const u8) bool {
4343 if (a.len != b.len) return false;
44 for (a) |item, index| {
44 for (a, 0..) |item, index| {
4545 if (b[index] != item) return false;
4646 }
4747 return true;
test/behavior/eval.zig+3-3
......@@ -317,7 +317,7 @@ test "create global array with for loop" {
317317
318318const global_array = x: {
319319 var result: [10]usize = undefined;
320 for (result) |*item, index| {
320 for (&result, 0..) |*item, index| {
321321 item.* = index * index;
322322 }
323323 break :x result;
......@@ -447,7 +447,7 @@ test "binary math operator in partially inlined function" {
447447 var s: [4]u32 = undefined;
448448 var b: [16]u8 = undefined;
449449
450 for (b) |*r, i|
450 for (&b, 0..) |*r, i|
451451 r.* = @intCast(u8, i + 1);
452452
453453 copyWithPartialInline(s[0..], b[0..]);
......@@ -915,7 +915,7 @@ test "comptime pointer load through elem_ptr" {
915915
916916 comptime {
917917 var array: [10]S = undefined;
918 for (array) |*elem, i| {
918 for (&array, 0..) |*elem, i| {
919919 elem.* = .{
920920 .x = i,
921921 };
test/behavior/fn.zig+1-1
......@@ -311,7 +311,7 @@ test "function pointers" {
311311 &fn3,
312312 &fn4,
313313 };
314 for (fns) |f, i| {
314 for (fns, 0..) |f, i| {
315315 try expect(f() == @intCast(u32, i) + 5);
316316 }
317317}
test/behavior/for.zig+10-10
......@@ -55,9 +55,9 @@ fn testContinueOuter() !void {
5555}
5656
5757test "ignore lval with underscore (for loop)" {
58 for ([_]void{}) |_, i| {
58 for ([_]void{}, 0..) |_, i| {
5959 _ = i;
60 for ([_]void{}) |_, j| {
60 for ([_]void{}, 0..) |_, j| {
6161 _ = j;
6262 break;
6363 }
......@@ -81,7 +81,7 @@ test "basic for loop" {
8181 buffer[buf_index] = item;
8282 buf_index += 1;
8383 }
84 for (array) |item, index| {
84 for (array, 0..) |item, index| {
8585 _ = item;
8686 buffer[buf_index] = @intCast(u8, index);
8787 buf_index += 1;
......@@ -91,7 +91,7 @@ test "basic for loop" {
9191 buffer[buf_index] = item;
9292 buf_index += 1;
9393 }
94 for (array_ptr) |item, index| {
94 for (array_ptr, 0..) |item, index| {
9595 _ = item;
9696 buffer[buf_index] = @intCast(u8, index);
9797 buf_index += 1;
......@@ -101,7 +101,7 @@ test "basic for loop" {
101101 buffer[buf_index] = item;
102102 buf_index += 1;
103103 }
104 for (unknown_size) |_, index| {
104 for (unknown_size, 0..) |_, index| {
105105 buffer[buf_index] = @intCast(u8, index);
106106 buf_index += 1;
107107 }
......@@ -163,11 +163,11 @@ test "for loop with pointer elem var" {
163163 mangleString(target[0..]);
164164 try expect(mem.eql(u8, &target, "bcdefgh"));
165165
166 for (source) |*c, i| {
166 for (source, 0..) |*c, i| {
167167 _ = i;
168168 try expect(@TypeOf(c) == *const u8);
169169 }
170 for (target) |*c, i| {
170 for (&target, 0..) |*c, i| {
171171 _ = i;
172172 try expect(@TypeOf(c) == *u8);
173173 }
......@@ -186,7 +186,7 @@ test "for copies its payload" {
186186 const S = struct {
187187 fn doTheTest() !void {
188188 var x = [_]usize{ 1, 2, 3 };
189 for (x) |value, i| {
189 for (x, 0..) |value, i| {
190190 // Modify the original array
191191 x[i] += 99;
192192 try expect(value == i + 1);
......@@ -206,8 +206,8 @@ test "for on slice with allowzero ptr" {
206206 const S = struct {
207207 fn doTheTest(slice: []const u8) !void {
208208 var ptr = @ptrCast([*]allowzero const u8, slice.ptr)[0..slice.len];
209 for (ptr) |x, i| try expect(x == i + 1);
210 for (ptr) |*x, i| try expect(x.* == i + 1);
209 for (ptr, 0..) |x, i| try expect(x == i + 1);
210 for (ptr, 0..) |*x, i| try expect(x.* == i + 1);
211211 }
212212 };
213213 try S.doTheTest(&[_]u8{ 1, 2, 3, 4 });
test/behavior/generics.zig+1-1
......@@ -325,7 +325,7 @@ test "generic function instantiation non-duplicates" {
325325 const S = struct {
326326 fn copy(comptime T: type, dest: []T, source: []const T) void {
327327 @export(foo, .{ .name = "test_generic_instantiation_non_dupe" });
328 for (source) |s, i| dest[i] = s;
328 for (source, 0..) |s, i| dest[i] = s;
329329 }
330330
331331 fn foo() callconv(.C) void {}
test/behavior/lower_strlit_to_vector.zig+1-1
......@@ -12,7 +12,7 @@ test "strlit to vector" {
1212 const strlit = "0123456789abcdef0123456789ABCDEF";
1313 const vec_from_strlit: @Vector(32, u8) = strlit.*;
1414 const arr_from_vec = @as([32]u8, vec_from_strlit);
15 for (strlit) |c, i|
15 for (strlit, 0..) |c, i|
1616 try std.testing.expect(c == arr_from_vec[i]);
1717 try std.testing.expectEqualSlices(u8, strlit, &arr_from_vec);
1818}
test/behavior/math.zig+1-1
......@@ -1221,7 +1221,7 @@ test "quad hex float literal parsing accurate" {
12211221 0xb6a0000000000000,
12221222 };
12231223
1224 for (exp2ft) |x, i| {
1224 for (exp2ft, 0..) |x, i| {
12251225 try expect(@bitCast(u64, x) == answers[i]);
12261226 }
12271227 }
test/behavior/slice.zig+2-2
......@@ -99,7 +99,7 @@ test "comptime slice of slice preserves comptime var" {
9999test "slice of type" {
100100 comptime {
101101 var types_array = [_]type{ i32, f64, type };
102 for (types_array) |T, i| {
102 for (types_array, 0..) |T, i| {
103103 switch (i) {
104104 0 => try expect(T == i32),
105105 1 => try expect(T == f64),
......@@ -107,7 +107,7 @@ test "slice of type" {
107107 else => unreachable,
108108 }
109109 }
110 for (types_array[0..]) |T, i| {
110 for (types_array[0..], 0..) |T, i| {
111111 switch (i) {
112112 0 => try expect(T == i32),
113113 1 => try expect(T == f64),
test/behavior/tuple.zig+1-1
......@@ -40,7 +40,7 @@ test "tuple multiplication" {
4040 {
4141 const t = .{ 1, 2, 3 } ** 4;
4242 try expect(@typeInfo(@TypeOf(t)).Struct.fields.len == 12);
43 inline for (t) |x, i| try expect(x == 1 + i % 3);
43 inline for (t, 0..) |x, i| try expect(x == 1 + i % 3);
4444 }
4545 }
4646 };
test/behavior/vector.zig+11-11
......@@ -456,20 +456,20 @@ test "vector division operators" {
456456 fn doTheTestDiv(comptime T: type, x: @Vector(4, T), y: @Vector(4, T)) !void {
457457 if (!comptime std.meta.trait.isSignedInt(T)) {
458458 const d0 = x / y;
459 for (@as([4]T, d0)) |v, i| {
459 for (@as([4]T, d0), 0..) |v, i| {
460460 try expect(x[i] / y[i] == v);
461461 }
462462 }
463463 const d1 = @divExact(x, y);
464 for (@as([4]T, d1)) |v, i| {
464 for (@as([4]T, d1), 0..) |v, i| {
465465 try expect(@divExact(x[i], y[i]) == v);
466466 }
467467 const d2 = @divFloor(x, y);
468 for (@as([4]T, d2)) |v, i| {
468 for (@as([4]T, d2), 0..) |v, i| {
469469 try expect(@divFloor(x[i], y[i]) == v);
470470 }
471471 const d3 = @divTrunc(x, y);
472 for (@as([4]T, d3)) |v, i| {
472 for (@as([4]T, d3), 0..) |v, i| {
473473 try expect(@divTrunc(x[i], y[i]) == v);
474474 }
475475 }
......@@ -477,16 +477,16 @@ test "vector division operators" {
477477 fn doTheTestMod(comptime T: type, x: @Vector(4, T), y: @Vector(4, T)) !void {
478478 if ((!comptime std.meta.trait.isSignedInt(T)) and @typeInfo(T) != .Float) {
479479 const r0 = x % y;
480 for (@as([4]T, r0)) |v, i| {
480 for (@as([4]T, r0), 0..) |v, i| {
481481 try expect(x[i] % y[i] == v);
482482 }
483483 }
484484 const r1 = @mod(x, y);
485 for (@as([4]T, r1)) |v, i| {
485 for (@as([4]T, r1), 0..) |v, i| {
486486 try expect(@mod(x[i], y[i]) == v);
487487 }
488488 const r2 = @rem(x, y);
489 for (@as([4]T, r2)) |v, i| {
489 for (@as([4]T, r2), 0..) |v, i| {
490490 try expect(@rem(x[i], y[i]) == v);
491491 }
492492 }
......@@ -538,7 +538,7 @@ test "vector bitwise not operator" {
538538 const S = struct {
539539 fn doTheTestNot(comptime T: type, x: @Vector(4, T)) !void {
540540 var y = ~x;
541 for (@as([4]T, y)) |v, i| {
541 for (@as([4]T, y), 0..) |v, i| {
542542 try expect(~x[i] == v);
543543 }
544544 }
......@@ -577,11 +577,11 @@ test "vector shift operators" {
577577 var yv = @as(@Vector(N, TY), y);
578578
579579 var z0 = xv >> yv;
580 for (@as([N]TX, z0)) |v, i| {
580 for (@as([N]TX, z0), 0..) |v, i| {
581581 try expect(x[i] >> y[i] == v);
582582 }
583583 var z1 = xv << yv;
584 for (@as([N]TX, z1)) |v, i| {
584 for (@as([N]TX, z1), 0..) |v, i| {
585585 try expect(x[i] << y[i] == v);
586586 }
587587 }
......@@ -594,7 +594,7 @@ test "vector shift operators" {
594594 var yv = @as(@Vector(N, TY), y);
595595
596596 var z = if (dir == .Left) @shlExact(xv, yv) else @shrExact(xv, yv);
597 for (@as([N]TX, z)) |v, i| {
597 for (@as([N]TX, z), 0..) |v, i| {
598598 const check = if (dir == .Left) x[i] << y[i] else x[i] >> y[i];
599599 try expect(check == v);
600600 }
test/behavior/void.zig+1-1
......@@ -22,7 +22,7 @@ test "iterate over a void slice" {
2222 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
2323
2424 var j: usize = 0;
25 for (times(10)) |_, i| {
25 for (times(10), 0..) |_, i| {
2626 try expect(i == j);
2727 j += 1;
2828 }
test/standalone/brace_expansion/main.zig+2-2
......@@ -29,7 +29,7 @@ fn tokenize(input: []const u8) !ArrayList(Token) {
2929 var tok_begin: usize = undefined;
3030 var state = State.Start;
3131
32 for (input) |b, i| {
32 for (input, 0..) |b, i| {
3333 switch (state) {
3434 .Start => switch (b) {
3535 'a'...'z', 'A'...'Z' => {
......@@ -159,7 +159,7 @@ fn expandString(input: []const u8, output: *ArrayList(u8)) !void {
159159 try expandNode(root, &result_list);
160160
161161 try output.resize(0);
162 for (result_list.items) |buf, i| {
162 for (result_list.items, 0..) |buf, i| {
163163 if (i != 0) {
164164 try output.append(' ');
165165 }
test/tests.zig+1-1
......@@ -958,7 +958,7 @@ pub const StackTracesContext = struct {
958958 // locate delims/anchor
959959 const delims = [_][]const u8{ ":", ":", ":", " in ", "(", ")" };
960960 var marks = [_]usize{0} ** delims.len;
961 for (delims) |delim, i| {
961 for (delims, 0..) |delim, i| {
962962 marks[i] = mem.indexOfPos(u8, line, pos, delim) orelse {
963963 // unexpected pattern: emit raw line and cont
964964 try buf.appendSlice(line);