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

This commit is contained in:
Andrew Kelley 2023-02-17 20:23:33 -07:00
parent 321ccbdc52
commit f0530385b5
19 changed files with 46 additions and 46 deletions

View File

@ -211,7 +211,7 @@ fn first8Words(words: [16]u32) [8]u32 {
fn wordsFromLittleEndianBytes(comptime count: usize, bytes: [count * 4]u8) [count]u32 { fn wordsFromLittleEndianBytes(comptime count: usize, bytes: [count * 4]u8) [count]u32 {
var words: [count]u32 = undefined; var words: [count]u32 = undefined;
for (words) |*word, i| { for (&words) |*word, i| {
word.* = mem.readIntSliceLittle(u32, bytes[4 * i ..]); word.* = mem.readIntSliceLittle(u32, bytes[4 * i ..]);
} }
return words; return words;

View File

@ -185,7 +185,7 @@ test "nested arrays of strings" {
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
const array_of_strings = [_][]const u8{ "hello", "this", "is", "my", "thing" }; 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 == 0) try expect(mem.eql(u8, s, "hello"));
if (i == 1) try expect(mem.eql(u8, s, "this")); if (i == 1) try expect(mem.eql(u8, s, "this"));
if (i == 2) try expect(mem.eql(u8, s, "is")); if (i == 2) try expect(mem.eql(u8, s, "is"));

View File

@ -84,14 +84,14 @@ fn testShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, c
var table = Table.create(); var table = Table.create();
var node_buffer: [node_count]Table.Node = undefined; var node_buffer: [node_count]Table.Node = undefined;
for (node_buffer) |*node, i| { for (&node_buffer, 0..) |*node, i| {
const key = @intCast(Key, i); const key = @intCast(Key, i);
try expect(table.get(key) == null); try expect(table.get(key) == null);
node.init(key, {}); node.init(key, {});
table.put(node); table.put(node);
} }
for (node_buffer) |*node, i| { for (&node_buffer, 0..) |*node, i| {
try expect(table.get(@intCast(Key, i)) == node); try expect(table.get(@intCast(Key, i)) == node);
} }
} }

View File

@ -5,7 +5,7 @@ const builtin = @import("builtin");
const a = [_]u8{ 1, 2, 3 }; const a = [_]u8{ 1, 2, 3 };
fn checkAddress(s: []const u8) !void { fn checkAddress(s: []const u8) !void {
for (s) |*i, j| { for (s, 0..) |*i, j| {
try testing.expect(i == &a[j]); try testing.expect(i == &a[j]);
} }
} }

View File

@ -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[0] = v / f(r);
tables.x[1] = 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]; const last = tables.x[2 + i - 1];
entry.* = f_inv(v / last + f(last)); entry.* = f_inv(v / last + f(last));
} }
tables.x[256] = 0; tables.x[256] = 0;
for (tables.f[0..]) |*entry, i| { for (tables.f[0..], 0..) |*entry, i| {
entry.* = f(tables.x[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); 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 // Here we use `expectApproxEqAbs` instead of `expectEqual` to account for the small
// differences in math functions of different libcs. For example, if the compiler // 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 // links against glibc, but the target is musl libc, then these values might be

View File

@ -364,7 +364,7 @@ test "Enum constructed by @Type passed as generic argument" {
try expect(@enumToInt(a) == b); 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); try S.foo(@intToEnum(S.E, i), i);
} }
} }

View File

@ -26,7 +26,7 @@ fn foo(args: [][]const u8) !void {
fn bar(argc: usize) !void { fn bar(argc: usize) !void {
var args_buffer: [10][]const u8 = undefined; var args_buffer: [10][]const u8 = undefined;
const args = args_buffer[0..argc]; const args = args_buffer[0..argc];
for (args) |_, i| { for (args, 0..) |_, i| {
const ptr = argv[i]; const ptr = argv[i];
args[i] = ptr[0..strlen(ptr)]; 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 { fn streql(a: []const u8, b: []const u8) bool {
if (a.len != b.len) return false; if (a.len != b.len) return false;
for (a) |item, index| { for (a, 0..) |item, index| {
if (b[index] != item) return false; if (b[index] != item) return false;
} }
return true; return true;

View File

@ -317,7 +317,7 @@ test "create global array with for loop" {
const global_array = x: { const global_array = x: {
var result: [10]usize = undefined; var result: [10]usize = undefined;
for (result) |*item, index| { for (&result, 0..) |*item, index| {
item.* = index * index; item.* = index * index;
} }
break :x result; break :x result;
@ -447,7 +447,7 @@ test "binary math operator in partially inlined function" {
var s: [4]u32 = undefined; var s: [4]u32 = undefined;
var b: [16]u8 = undefined; var b: [16]u8 = undefined;
for (b) |*r, i| for (&b, 0..) |*r, i|
r.* = @intCast(u8, i + 1); r.* = @intCast(u8, i + 1);
copyWithPartialInline(s[0..], b[0..]); copyWithPartialInline(s[0..], b[0..]);
@ -915,7 +915,7 @@ test "comptime pointer load through elem_ptr" {
comptime { comptime {
var array: [10]S = undefined; var array: [10]S = undefined;
for (array) |*elem, i| { for (&array, 0..) |*elem, i| {
elem.* = .{ elem.* = .{
.x = i, .x = i,
}; };

View File

@ -311,7 +311,7 @@ test "function pointers" {
&fn3, &fn3,
&fn4, &fn4,
}; };
for (fns) |f, i| { for (fns, 0..) |f, i| {
try expect(f() == @intCast(u32, i) + 5); try expect(f() == @intCast(u32, i) + 5);
} }
} }

View File

@ -55,9 +55,9 @@ fn testContinueOuter() !void {
} }
test "ignore lval with underscore (for loop)" { test "ignore lval with underscore (for loop)" {
for ([_]void{}) |_, i| { for ([_]void{}, 0..) |_, i| {
_ = i; _ = i;
for ([_]void{}) |_, j| { for ([_]void{}, 0..) |_, j| {
_ = j; _ = j;
break; break;
} }
@ -81,7 +81,7 @@ test "basic for loop" {
buffer[buf_index] = item; buffer[buf_index] = item;
buf_index += 1; buf_index += 1;
} }
for (array) |item, index| { for (array, 0..) |item, index| {
_ = item; _ = item;
buffer[buf_index] = @intCast(u8, index); buffer[buf_index] = @intCast(u8, index);
buf_index += 1; buf_index += 1;
@ -91,7 +91,7 @@ test "basic for loop" {
buffer[buf_index] = item; buffer[buf_index] = item;
buf_index += 1; buf_index += 1;
} }
for (array_ptr) |item, index| { for (array_ptr, 0..) |item, index| {
_ = item; _ = item;
buffer[buf_index] = @intCast(u8, index); buffer[buf_index] = @intCast(u8, index);
buf_index += 1; buf_index += 1;
@ -101,7 +101,7 @@ test "basic for loop" {
buffer[buf_index] = item; buffer[buf_index] = item;
buf_index += 1; buf_index += 1;
} }
for (unknown_size) |_, index| { for (unknown_size, 0..) |_, index| {
buffer[buf_index] = @intCast(u8, index); buffer[buf_index] = @intCast(u8, index);
buf_index += 1; buf_index += 1;
} }
@ -163,11 +163,11 @@ test "for loop with pointer elem var" {
mangleString(target[0..]); mangleString(target[0..]);
try expect(mem.eql(u8, &target, "bcdefgh")); try expect(mem.eql(u8, &target, "bcdefgh"));
for (source) |*c, i| { for (source, 0..) |*c, i| {
_ = i; _ = i;
try expect(@TypeOf(c) == *const u8); try expect(@TypeOf(c) == *const u8);
} }
for (target) |*c, i| { for (&target, 0..) |*c, i| {
_ = i; _ = i;
try expect(@TypeOf(c) == *u8); try expect(@TypeOf(c) == *u8);
} }
@ -186,7 +186,7 @@ test "for copies its payload" {
const S = struct { const S = struct {
fn doTheTest() !void { fn doTheTest() !void {
var x = [_]usize{ 1, 2, 3 }; var x = [_]usize{ 1, 2, 3 };
for (x) |value, i| { for (x, 0..) |value, i| {
// Modify the original array // Modify the original array
x[i] += 99; x[i] += 99;
try expect(value == i + 1); try expect(value == i + 1);
@ -206,8 +206,8 @@ test "for on slice with allowzero ptr" {
const S = struct { const S = struct {
fn doTheTest(slice: []const u8) !void { fn doTheTest(slice: []const u8) !void {
var ptr = @ptrCast([*]allowzero const u8, slice.ptr)[0..slice.len]; var ptr = @ptrCast([*]allowzero const u8, slice.ptr)[0..slice.len];
for (ptr) |x, i| try expect(x == i + 1); for (ptr, 0..) |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);
} }
}; };
try S.doTheTest(&[_]u8{ 1, 2, 3, 4 }); try S.doTheTest(&[_]u8{ 1, 2, 3, 4 });

View File

@ -325,7 +325,7 @@ test "generic function instantiation non-duplicates" {
const S = struct { const S = struct {
fn copy(comptime T: type, dest: []T, source: []const T) void { fn copy(comptime T: type, dest: []T, source: []const T) void {
@export(foo, .{ .name = "test_generic_instantiation_non_dupe" }); @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 {} fn foo() callconv(.C) void {}

View File

@ -12,7 +12,7 @@ test "strlit to vector" {
const strlit = "0123456789abcdef0123456789ABCDEF"; const strlit = "0123456789abcdef0123456789ABCDEF";
const vec_from_strlit: @Vector(32, u8) = strlit.*; const vec_from_strlit: @Vector(32, u8) = strlit.*;
const arr_from_vec = @as([32]u8, vec_from_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.expect(c == arr_from_vec[i]);
try std.testing.expectEqualSlices(u8, strlit, &arr_from_vec); try std.testing.expectEqualSlices(u8, strlit, &arr_from_vec);
} }

View File

@ -1221,7 +1221,7 @@ test "quad hex float literal parsing accurate" {
0xb6a0000000000000, 0xb6a0000000000000,
}; };
for (exp2ft) |x, i| { for (exp2ft, 0..) |x, i| {
try expect(@bitCast(u64, x) == answers[i]); try expect(@bitCast(u64, x) == answers[i]);
} }
} }

View File

@ -99,7 +99,7 @@ test "comptime slice of slice preserves comptime var" {
test "slice of type" { test "slice of type" {
comptime { comptime {
var types_array = [_]type{ i32, f64, type }; var types_array = [_]type{ i32, f64, type };
for (types_array) |T, i| { for (types_array, 0..) |T, i| {
switch (i) { switch (i) {
0 => try expect(T == i32), 0 => try expect(T == i32),
1 => try expect(T == f64), 1 => try expect(T == f64),
@ -107,7 +107,7 @@ test "slice of type" {
else => unreachable, else => unreachable,
} }
} }
for (types_array[0..]) |T, i| { for (types_array[0..], 0..) |T, i| {
switch (i) { switch (i) {
0 => try expect(T == i32), 0 => try expect(T == i32),
1 => try expect(T == f64), 1 => try expect(T == f64),

View File

@ -40,7 +40,7 @@ test "tuple multiplication" {
{ {
const t = .{ 1, 2, 3 } ** 4; const t = .{ 1, 2, 3 } ** 4;
try expect(@typeInfo(@TypeOf(t)).Struct.fields.len == 12); 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);
} }
} }
}; };

View File

@ -456,20 +456,20 @@ test "vector division operators" {
fn doTheTestDiv(comptime T: type, x: @Vector(4, T), y: @Vector(4, T)) !void { fn doTheTestDiv(comptime T: type, x: @Vector(4, T), y: @Vector(4, T)) !void {
if (!comptime std.meta.trait.isSignedInt(T)) { if (!comptime std.meta.trait.isSignedInt(T)) {
const d0 = x / y; 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); try expect(x[i] / y[i] == v);
} }
} }
const d1 = @divExact(x, y); 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); try expect(@divExact(x[i], y[i]) == v);
} }
const d2 = @divFloor(x, y); 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); try expect(@divFloor(x[i], y[i]) == v);
} }
const d3 = @divTrunc(x, y); 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); 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 { 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) { if ((!comptime std.meta.trait.isSignedInt(T)) and @typeInfo(T) != .Float) {
const r0 = x % y; 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); try expect(x[i] % y[i] == v);
} }
} }
const r1 = @mod(x, y); 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); try expect(@mod(x[i], y[i]) == v);
} }
const r2 = @rem(x, y); 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); try expect(@rem(x[i], y[i]) == v);
} }
} }
@ -538,7 +538,7 @@ test "vector bitwise not operator" {
const S = struct { const S = struct {
fn doTheTestNot(comptime T: type, x: @Vector(4, T)) !void { fn doTheTestNot(comptime T: type, x: @Vector(4, T)) !void {
var y = ~x; var y = ~x;
for (@as([4]T, y)) |v, i| { for (@as([4]T, y), 0..) |v, i| {
try expect(~x[i] == v); try expect(~x[i] == v);
} }
} }
@ -577,11 +577,11 @@ test "vector shift operators" {
var yv = @as(@Vector(N, TY), y); var yv = @as(@Vector(N, TY), y);
var z0 = xv >> yv; 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); try expect(x[i] >> y[i] == v);
} }
var z1 = xv << yv; 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); try expect(x[i] << y[i] == v);
} }
} }
@ -594,7 +594,7 @@ test "vector shift operators" {
var yv = @as(@Vector(N, TY), y); var yv = @as(@Vector(N, TY), y);
var z = if (dir == .Left) @shlExact(xv, yv) else @shrExact(xv, yv); 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]; const check = if (dir == .Left) x[i] << y[i] else x[i] >> y[i];
try expect(check == v); try expect(check == v);
} }

View File

@ -22,7 +22,7 @@ test "iterate over a void slice" {
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
var j: usize = 0; var j: usize = 0;
for (times(10)) |_, i| { for (times(10), 0..) |_, i| {
try expect(i == j); try expect(i == j);
j += 1; j += 1;
} }

View File

@ -29,7 +29,7 @@ fn tokenize(input: []const u8) !ArrayList(Token) {
var tok_begin: usize = undefined; var tok_begin: usize = undefined;
var state = State.Start; var state = State.Start;
for (input) |b, i| { for (input, 0..) |b, i| {
switch (state) { switch (state) {
.Start => switch (b) { .Start => switch (b) {
'a'...'z', 'A'...'Z' => { 'a'...'z', 'A'...'Z' => {
@ -159,7 +159,7 @@ fn expandString(input: []const u8, output: *ArrayList(u8)) !void {
try expandNode(root, &result_list); try expandNode(root, &result_list);
try output.resize(0); try output.resize(0);
for (result_list.items) |buf, i| { for (result_list.items, 0..) |buf, i| {
if (i != 0) { if (i != 0) {
try output.append(' '); try output.append(' ');
} }

View File

@ -958,7 +958,7 @@ pub const StackTracesContext = struct {
// locate delims/anchor // locate delims/anchor
const delims = [_][]const u8{ ":", ":", ":", " in ", "(", ")" }; const delims = [_][]const u8{ ":", ":", ":", " in ", "(", ")" };
var marks = [_]usize{0} ** delims.len; var marks = [_]usize{0} ** delims.len;
for (delims) |delim, i| { for (delims, 0..) |delim, i| {
marks[i] = mem.indexOfPos(u8, line, pos, delim) orelse { marks[i] = mem.indexOfPos(u8, line, pos, delim) orelse {
// unexpected pattern: emit raw line and cont // unexpected pattern: emit raw line and cont
try buf.appendSlice(line); try buf.appendSlice(line);