From e0db54e89d6f4e9f56800ddb1017e30be1b7d58a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 6 Nov 2019 23:25:57 -0500 Subject: update the codebase to use `@as` --- lib/std/packed_int_array.zig | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'lib/std/packed_int_array.zig') diff --git a/lib/std/packed_int_array.zig b/lib/std/packed_int_array.zig index 5cbab2d33b..c22d701bf2 100644 --- a/lib/std/packed_int_array.zig +++ b/lib/std/packed_int_array.zig @@ -331,7 +331,7 @@ test "PackedIntArray" { var data = PackedArray(undefined); //write values, counting up - var i = usize(0); + var i = @as(usize, 0); var count = I(0); while (i < data.len()) : (i += 1) { data.set(i, count); @@ -352,7 +352,7 @@ test "PackedIntArray" { test "PackedIntArray init" { const PackedArray = PackedIntArray(u3, 8); var packed_array = PackedArray.init([_]u3{ 0, 1, 2, 3, 4, 5, 6, 7 }); - var i = usize(0); + var i = @as(usize, 0); while (i < packed_array.len()) : (i += 1) testing.expect(packed_array.get(i) == i); } @@ -375,7 +375,7 @@ test "PackedIntSlice" { var data = P.init(&buffer, int_count); //write values, counting up - var i = usize(0); + var i = @as(usize, 0); var count = I(0); while (i < data.len()) : (i += 1) { data.set(i, count); @@ -406,7 +406,7 @@ test "PackedIntSlice of PackedInt(Array/Slice)" { const limit = (1 << bits); - var i = usize(0); + var i = @as(usize, 0); while (i < packed_array.len()) : (i += 1) { packed_array.set(i, @intCast(Int, i % limit)); } @@ -466,7 +466,7 @@ test "PackedIntSlice accumulating bit offsets" { var packed_array = PackedArray(undefined); var packed_slice = packed_array.slice(0, packed_array.len()); - var i = usize(0); + var i = @as(usize, 0); while (i < packed_array.len() - 1) : (i += 1) { packed_slice = packed_slice.slice(1, packed_slice.len()); } @@ -476,7 +476,7 @@ test "PackedIntSlice accumulating bit offsets" { var packed_array = PackedArray(undefined); var packed_slice = packed_array.slice(0, packed_array.len()); - var i = usize(0); + var i = @as(usize, 0); while (i < packed_array.len() - 1) : (i += 1) { packed_slice = packed_slice.slice(1, packed_slice.len()); } @@ -493,7 +493,7 @@ test "PackedInt(Array/Slice) sliceCast" { var packed_slice_cast_9 = packed_array.slice(0, (packed_array.len() / 9) * 9).sliceCast(u9); const packed_slice_cast_3 = packed_slice_cast_9.sliceCast(u3); - var i = usize(0); + var i = @as(usize, 0); while (i < packed_slice_cast_2.len()) : (i += 1) { const val = switch (builtin.endian) { .Big => 0b01, @@ -541,7 +541,7 @@ test "PackedInt(Array/Slice)Endian" { testing.expect(packed_array_be.bytes[0] == 0b00000001); testing.expect(packed_array_be.bytes[1] == 0b00100011); - var i = usize(0); + var i = @as(usize, 0); while (i < packed_array_be.len()) : (i += 1) { testing.expect(packed_array_be.get(i) == i); } @@ -579,7 +579,7 @@ test "PackedInt(Array/Slice)Endian" { testing.expect(packed_array_be.bytes[3] == 0b00000001); testing.expect(packed_array_be.bytes[4] == 0b00000000); - var i = usize(0); + var i = @as(usize, 0); while (i < packed_array_be.len()) : (i += 1) { testing.expect(packed_array_be.get(i) == i); } -- cgit v1.2.3 From aa0daea5415dd2a20e4d7c2fd047b7bcee6c9ea4 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 7 Nov 2019 18:52:09 -0500 Subject: update more of the std lib to use `@as` --- lib/std/bloom_filter.zig | 16 +++++++------- lib/std/crypto/x25519.zig | 46 ++++++++++++++++++++--------------------- lib/std/debug/leb128.zig | 2 +- lib/std/fmt.zig | 22 ++++++++++---------- lib/std/fmt/parse_float.zig | 6 +++--- lib/std/heap.zig | 4 ++-- lib/std/io.zig | 26 +++++++++++------------ lib/std/io/test.zig | 24 ++++++++++----------- lib/std/lazy_init.zig | 2 +- lib/std/math.zig | 24 ++++++++++----------- lib/std/math/big/int.zig | 20 +++++++++--------- lib/std/math/complex.zig | 8 +++---- lib/std/math/complex/acos.zig | 2 +- lib/std/math/complex/ldexp.zig | 2 +- lib/std/math/exp.zig | 2 +- lib/std/math/ln.zig | 2 +- lib/std/math/log.zig | 4 ++-- lib/std/math/log10.zig | 2 +- lib/std/math/sqrt.zig | 4 ++-- lib/std/meta.zig | 2 +- lib/std/net.zig | 8 +++---- lib/std/os/linux/test.zig | 6 +++--- lib/std/packed_int_array.zig | 18 ++++++++-------- lib/std/rand.zig | 8 +++---- lib/std/unicode.zig | 16 +++++++------- src-self-hosted/stage1.zig | 2 +- src-self-hosted/translate_c.zig | 2 +- test/compile_errors.zig | 10 ++++----- 28 files changed, 145 insertions(+), 145 deletions(-) (limited to 'lib/std/packed_int_array.zig') diff --git a/lib/std/bloom_filter.zig b/lib/std/bloom_filter.zig index 2734e7d54f..6c4d713076 100644 --- a/lib/std/bloom_filter.zig +++ b/lib/std/bloom_filter.zig @@ -28,7 +28,7 @@ pub fn BloomFilter( assert(n_items > 0); assert(math.isPowerOfTwo(n_items)); assert(K > 0); - const cellEmpty = if (Cell == bool) false else Cell(0); + const cellEmpty = if (Cell == bool) false else @as(Cell, 0); const cellMax = if (Cell == bool) true else math.maxInt(Cell); const n_bytes = (n_items * comptime std.meta.bitCount(Cell)) / 8; assert(n_bytes > 0); @@ -137,7 +137,7 @@ pub fn BloomFilter( var i: usize = 0; while (i < n_items) : (i += 1) { const cell = self.getCell(@intCast(Index, i)); - n += if (if (Cell == bool) cell else cell > 0) Index(1) else Index(0); + n += if (if (Cell == bool) cell else cell > 0) @as(Index, 1) else @as(Index, 0); } } return n; @@ -161,7 +161,7 @@ fn hashFunc(out: []u8, Ki: usize, in: []const u8) void { test "std.BloomFilter" { inline for ([_]type{ bool, u1, u2, u3, u4 }) |Cell| { - const emptyCell = if (Cell == bool) false else Cell(0); + const emptyCell = if (Cell == bool) false else @as(Cell, 0); const BF = BloomFilter(128 * 8, 8, Cell, builtin.endian, hashFunc); var bf = BF{}; var i: usize = undefined; @@ -170,7 +170,7 @@ test "std.BloomFilter" { while (i < BF.items) : (i += 1) { testing.expectEqual(emptyCell, bf.getCell(@intCast(BF.Index, i))); } - testing.expectEqual(BF.Index(0), bf.popCount()); + testing.expectEqual(@as(BF.Index, 0), bf.popCount()); testing.expectEqual(@as(f64, 0), bf.estimateItems()); // fill in a few items bf.incrementCell(42); @@ -196,7 +196,7 @@ test "std.BloomFilter" { while (i < BF.items) : (i += 1) { testing.expectEqual(emptyCell, bf.getCell(@intCast(BF.Index, i))); } - testing.expectEqual(BF.Index(0), bf.popCount()); + testing.expectEqual(@as(BF.Index, 0), bf.popCount()); testing.expectEqual(@as(f64, 0), bf.estimateItems()); // Lets add a string @@ -218,7 +218,7 @@ test "std.BloomFilter" { while (i < BF.items) : (i += 1) { testing.expectEqual(emptyCell, bf.getCell(@intCast(BF.Index, i))); } - testing.expectEqual(BF.Index(0), bf.popCount()); + testing.expectEqual(@as(BF.Index, 0), bf.popCount()); testing.expectEqual(@as(f64, 0), bf.estimateItems()); comptime var teststrings = [_][]const u8{ @@ -246,12 +246,12 @@ test "std.BloomFilter" { inline for (teststrings) |str| { testing.expectEqual(true, larger_bf.contains(str)); } - testing.expectEqual(u12(bf.popCount()) * (4096 / 1024), larger_bf.popCount()); + testing.expectEqual(@as(u12, bf.popCount()) * (4096 / 1024), larger_bf.popCount()); const smaller_bf = bf.resize(64); inline for (teststrings) |str| { testing.expectEqual(true, smaller_bf.contains(str)); } - testing.expect(bf.popCount() <= u10(smaller_bf.popCount()) * (1024 / 64)); + testing.expect(bf.popCount() <= @as(u10, smaller_bf.popCount()) * (1024 / 64)); } } diff --git a/lib/std/crypto/x25519.zig b/lib/std/crypto/x25519.zig index 223d65f8f4..cd908b0868 100644 --- a/lib/std/crypto/x25519.zig +++ b/lib/std/crypto/x25519.zig @@ -199,9 +199,9 @@ const Fe = struct { inline fn carryRound(c: []i64, t: []i64, comptime i: comptime_int, comptime shift: comptime_int, comptime mult: comptime_int) void { const j = (i + 1) % 10; - c[i] = (t[i] + (i64(1) << shift)) >> (shift + 1); + c[i] = (t[i] + (@as(i64, 1) << shift)) >> (shift + 1); t[j] += c[i] * mult; - t[i] -= c[i] * (i64(1) << (shift + 1)); + t[i] -= c[i] * (@as(i64, 1) << (shift + 1)); } fn carry1(h: *Fe, t: []i64) void { @@ -273,7 +273,7 @@ const Fe = struct { var t: [10]i64 = undefined; for (t[0..]) |_, i| { - t[i] = i64(f.b[i]) * g; + t[i] = @as(i64, f.b[i]) * g; } carry1(h, t[0..]); @@ -305,16 +305,16 @@ const Fe = struct { // t's become h var t: [10]i64 = undefined; - t[0] = f[0] * i64(g[0]) + F[1] * i64(G[9]) + f[2] * i64(G[8]) + F[3] * i64(G[7]) + f[4] * i64(G[6]) + F[5] * i64(G[5]) + f[6] * i64(G[4]) + F[7] * i64(G[3]) + f[8] * i64(G[2]) + F[9] * i64(G[1]); - t[1] = f[0] * i64(g[1]) + f[1] * i64(g[0]) + f[2] * i64(G[9]) + f[3] * i64(G[8]) + f[4] * i64(G[7]) + f[5] * i64(G[6]) + f[6] * i64(G[5]) + f[7] * i64(G[4]) + f[8] * i64(G[3]) + f[9] * i64(G[2]); - t[2] = f[0] * i64(g[2]) + F[1] * i64(g[1]) + f[2] * i64(g[0]) + F[3] * i64(G[9]) + f[4] * i64(G[8]) + F[5] * i64(G[7]) + f[6] * i64(G[6]) + F[7] * i64(G[5]) + f[8] * i64(G[4]) + F[9] * i64(G[3]); - t[3] = f[0] * i64(g[3]) + f[1] * i64(g[2]) + f[2] * i64(g[1]) + f[3] * i64(g[0]) + f[4] * i64(G[9]) + f[5] * i64(G[8]) + f[6] * i64(G[7]) + f[7] * i64(G[6]) + f[8] * i64(G[5]) + f[9] * i64(G[4]); - t[4] = f[0] * i64(g[4]) + F[1] * i64(g[3]) + f[2] * i64(g[2]) + F[3] * i64(g[1]) + f[4] * i64(g[0]) + F[5] * i64(G[9]) + f[6] * i64(G[8]) + F[7] * i64(G[7]) + f[8] * i64(G[6]) + F[9] * i64(G[5]); - t[5] = f[0] * i64(g[5]) + f[1] * i64(g[4]) + f[2] * i64(g[3]) + f[3] * i64(g[2]) + f[4] * i64(g[1]) + f[5] * i64(g[0]) + f[6] * i64(G[9]) + f[7] * i64(G[8]) + f[8] * i64(G[7]) + f[9] * i64(G[6]); - t[6] = f[0] * i64(g[6]) + F[1] * i64(g[5]) + f[2] * i64(g[4]) + F[3] * i64(g[3]) + f[4] * i64(g[2]) + F[5] * i64(g[1]) + f[6] * i64(g[0]) + F[7] * i64(G[9]) + f[8] * i64(G[8]) + F[9] * i64(G[7]); - t[7] = f[0] * i64(g[7]) + f[1] * i64(g[6]) + f[2] * i64(g[5]) + f[3] * i64(g[4]) + f[4] * i64(g[3]) + f[5] * i64(g[2]) + f[6] * i64(g[1]) + f[7] * i64(g[0]) + f[8] * i64(G[9]) + f[9] * i64(G[8]); - t[8] = f[0] * i64(g[8]) + F[1] * i64(g[7]) + f[2] * i64(g[6]) + F[3] * i64(g[5]) + f[4] * i64(g[4]) + F[5] * i64(g[3]) + f[6] * i64(g[2]) + F[7] * i64(g[1]) + f[8] * i64(g[0]) + F[9] * i64(G[9]); - t[9] = f[0] * i64(g[9]) + f[1] * i64(g[8]) + f[2] * i64(g[7]) + f[3] * i64(g[6]) + f[4] * i64(g[5]) + f[5] * i64(g[4]) + f[6] * i64(g[3]) + f[7] * i64(g[2]) + f[8] * i64(g[1]) + f[9] * i64(g[0]); + t[0] = f[0] * @as(i64, g[0]) + F[1] * @as(i64, G[9]) + f[2] * @as(i64, G[8]) + F[3] * @as(i64, G[7]) + f[4] * @as(i64, G[6]) + F[5] * @as(i64, G[5]) + f[6] * @as(i64, G[4]) + F[7] * @as(i64, G[3]) + f[8] * @as(i64, G[2]) + F[9] * @as(i64, G[1]); + t[1] = f[0] * @as(i64, g[1]) + f[1] * @as(i64, g[0]) + f[2] * @as(i64, G[9]) + f[3] * @as(i64, G[8]) + f[4] * @as(i64, G[7]) + f[5] * @as(i64, G[6]) + f[6] * @as(i64, G[5]) + f[7] * @as(i64, G[4]) + f[8] * @as(i64, G[3]) + f[9] * @as(i64, G[2]); + t[2] = f[0] * @as(i64, g[2]) + F[1] * @as(i64, g[1]) + f[2] * @as(i64, g[0]) + F[3] * @as(i64, G[9]) + f[4] * @as(i64, G[8]) + F[5] * @as(i64, G[7]) + f[6] * @as(i64, G[6]) + F[7] * @as(i64, G[5]) + f[8] * @as(i64, G[4]) + F[9] * @as(i64, G[3]); + t[3] = f[0] * @as(i64, g[3]) + f[1] * @as(i64, g[2]) + f[2] * @as(i64, g[1]) + f[3] * @as(i64, g[0]) + f[4] * @as(i64, G[9]) + f[5] * @as(i64, G[8]) + f[6] * @as(i64, G[7]) + f[7] * @as(i64, G[6]) + f[8] * @as(i64, G[5]) + f[9] * @as(i64, G[4]); + t[4] = f[0] * @as(i64, g[4]) + F[1] * @as(i64, g[3]) + f[2] * @as(i64, g[2]) + F[3] * @as(i64, g[1]) + f[4] * @as(i64, g[0]) + F[5] * @as(i64, G[9]) + f[6] * @as(i64, G[8]) + F[7] * @as(i64, G[7]) + f[8] * @as(i64, G[6]) + F[9] * @as(i64, G[5]); + t[5] = f[0] * @as(i64, g[5]) + f[1] * @as(i64, g[4]) + f[2] * @as(i64, g[3]) + f[3] * @as(i64, g[2]) + f[4] * @as(i64, g[1]) + f[5] * @as(i64, g[0]) + f[6] * @as(i64, G[9]) + f[7] * @as(i64, G[8]) + f[8] * @as(i64, G[7]) + f[9] * @as(i64, G[6]); + t[6] = f[0] * @as(i64, g[6]) + F[1] * @as(i64, g[5]) + f[2] * @as(i64, g[4]) + F[3] * @as(i64, g[3]) + f[4] * @as(i64, g[2]) + F[5] * @as(i64, g[1]) + f[6] * @as(i64, g[0]) + F[7] * @as(i64, G[9]) + f[8] * @as(i64, G[8]) + F[9] * @as(i64, G[7]); + t[7] = f[0] * @as(i64, g[7]) + f[1] * @as(i64, g[6]) + f[2] * @as(i64, g[5]) + f[3] * @as(i64, g[4]) + f[4] * @as(i64, g[3]) + f[5] * @as(i64, g[2]) + f[6] * @as(i64, g[1]) + f[7] * @as(i64, g[0]) + f[8] * @as(i64, G[9]) + f[9] * @as(i64, G[8]); + t[8] = f[0] * @as(i64, g[8]) + F[1] * @as(i64, g[7]) + f[2] * @as(i64, g[6]) + F[3] * @as(i64, g[5]) + f[4] * @as(i64, g[4]) + F[5] * @as(i64, g[3]) + f[6] * @as(i64, g[2]) + F[7] * @as(i64, g[1]) + f[8] * @as(i64, g[0]) + F[9] * @as(i64, G[9]); + t[9] = f[0] * @as(i64, g[9]) + f[1] * @as(i64, g[8]) + f[2] * @as(i64, g[7]) + f[3] * @as(i64, g[6]) + f[4] * @as(i64, g[5]) + f[5] * @as(i64, g[4]) + f[6] * @as(i64, g[3]) + f[7] * @as(i64, g[2]) + f[8] * @as(i64, g[1]) + f[9] * @as(i64, g[0]); carry2(h, t[0..]); } @@ -348,16 +348,16 @@ const Fe = struct { var t: [10]i64 = undefined; - t[0] = f0 * i64(f0) + f1_2 * i64(f9_38) + f2_2 * i64(f8_19) + f3_2 * i64(f7_38) + f4_2 * i64(f6_19) + f5 * i64(f5_38); - t[1] = f0_2 * i64(f1) + f2 * i64(f9_38) + f3_2 * i64(f8_19) + f4 * i64(f7_38) + f5_2 * i64(f6_19); - t[2] = f0_2 * i64(f2) + f1_2 * i64(f1) + f3_2 * i64(f9_38) + f4_2 * i64(f8_19) + f5_2 * i64(f7_38) + f6 * i64(f6_19); - t[3] = f0_2 * i64(f3) + f1_2 * i64(f2) + f4 * i64(f9_38) + f5_2 * i64(f8_19) + f6 * i64(f7_38); - t[4] = f0_2 * i64(f4) + f1_2 * i64(f3_2) + f2 * i64(f2) + f5_2 * i64(f9_38) + f6_2 * i64(f8_19) + f7 * i64(f7_38); - t[5] = f0_2 * i64(f5) + f1_2 * i64(f4) + f2_2 * i64(f3) + f6 * i64(f9_38) + f7_2 * i64(f8_19); - t[6] = f0_2 * i64(f6) + f1_2 * i64(f5_2) + f2_2 * i64(f4) + f3_2 * i64(f3) + f7_2 * i64(f9_38) + f8 * i64(f8_19); - t[7] = f0_2 * i64(f7) + f1_2 * i64(f6) + f2_2 * i64(f5) + f3_2 * i64(f4) + f8 * i64(f9_38); - t[8] = f0_2 * i64(f8) + f1_2 * i64(f7_2) + f2_2 * i64(f6) + f3_2 * i64(f5_2) + f4 * i64(f4) + f9 * i64(f9_38); - t[9] = f0_2 * i64(f9) + f1_2 * i64(f8) + f2_2 * i64(f7) + f3_2 * i64(f6) + f4 * i64(f5_2); + t[0] = f0 * @as(i64, f0) + f1_2 * @as(i64, f9_38) + f2_2 * @as(i64, f8_19) + f3_2 * @as(i64, f7_38) + f4_2 * @as(i64, f6_19) + f5 * @as(i64, f5_38); + t[1] = f0_2 * @as(i64, f1) + f2 * @as(i64, f9_38) + f3_2 * @as(i64, f8_19) + f4 * @as(i64, f7_38) + f5_2 * @as(i64, f6_19); + t[2] = f0_2 * @as(i64, f2) + f1_2 * @as(i64, f1) + f3_2 * @as(i64, f9_38) + f4_2 * @as(i64, f8_19) + f5_2 * @as(i64, f7_38) + f6 * @as(i64, f6_19); + t[3] = f0_2 * @as(i64, f3) + f1_2 * @as(i64, f2) + f4 * @as(i64, f9_38) + f5_2 * @as(i64, f8_19) + f6 * @as(i64, f7_38); + t[4] = f0_2 * @as(i64, f4) + f1_2 * @as(i64, f3_2) + f2 * @as(i64, f2) + f5_2 * @as(i64, f9_38) + f6_2 * @as(i64, f8_19) + f7 * @as(i64, f7_38); + t[5] = f0_2 * @as(i64, f5) + f1_2 * @as(i64, f4) + f2_2 * @as(i64, f3) + f6 * @as(i64, f9_38) + f7_2 * @as(i64, f8_19); + t[6] = f0_2 * @as(i64, f6) + f1_2 * @as(i64, f5_2) + f2_2 * @as(i64, f4) + f3_2 * @as(i64, f3) + f7_2 * @as(i64, f9_38) + f8 * @as(i64, f8_19); + t[7] = f0_2 * @as(i64, f7) + f1_2 * @as(i64, f6) + f2_2 * @as(i64, f5) + f3_2 * @as(i64, f4) + f8 * @as(i64, f9_38); + t[8] = f0_2 * @as(i64, f8) + f1_2 * @as(i64, f7_2) + f2_2 * @as(i64, f6) + f3_2 * @as(i64, f5_2) + f4 * @as(i64, f4) + f9 * @as(i64, f9_38); + t[9] = f0_2 * @as(i64, f9) + f1_2 * @as(i64, f8) + f2_2 * @as(i64, f7) + f3_2 * @as(i64, f6) + f4 * @as(i64, f5_2); carry2(h, t[0..]); } diff --git a/lib/std/debug/leb128.zig b/lib/std/debug/leb128.zig index ee1d1ac9ee..dba57e1f97 100644 --- a/lib/std/debug/leb128.zig +++ b/lib/std/debug/leb128.zig @@ -101,7 +101,7 @@ pub fn readILEB128Mem(comptime T: type, ptr: *[*]const u8) !T { return error.Overflow; var operand: UT = undefined; - if (@shlWithOverflow(UT, UT(byte & 0x7f), @intCast(ShiftT, shift), &operand)) { + if (@shlWithOverflow(UT, @as(UT, byte & 0x7f), @intCast(ShiftT, shift), &operand)) { if (byte != 0x7f) return error.Overflow; } diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index c6f788deee..e901d8d3f7 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -382,10 +382,10 @@ pub fn formatType( const info = @typeInfo(T).Union; if (info.tag_type) |UnionTagType| { try output(context, "{ ."); - try output(context, @tagName(UnionTagType(value))); + try output(context, @tagName(@as(UnionTagType, value))); try output(context, " = "); inline for (info.fields) |u_field| { - if (@enumToInt(UnionTagType(value)) == u_field.enum_field.?.value) { + if (@enumToInt(@as(UnionTagType, value)) == u_field.enum_field.?.value) { try formatType(@field(value, u_field.name), "", options, context, Errors, output, max_depth - 1); } } @@ -503,7 +503,7 @@ pub fn formatIntValue( const int_value = if (@typeOf(value) == comptime_int) blk: { const Int = math.IntFittingRange(value, value); - break :blk Int(value); + break :blk @as(Int, value); } else value; @@ -578,7 +578,7 @@ pub fn formatAsciiChar( comptime Errors: type, output: fn (@typeOf(context), []const u8) Errors!void, ) Errors!void { - return output(context, (*const [1]u8)(&c)[0..]); + return output(context, @as(*const [1]u8, &c)[0..]); } pub fn formatBuf( @@ -998,7 +998,7 @@ fn formatIntCallback(context: *FormatIntBuf, bytes: []const u8) (error{}!void) { pub fn parseInt(comptime T: type, buf: []const u8, radix: u8) !T { if (!T.is_signed) return parseUnsigned(T, buf, radix); - if (buf.len == 0) return T(0); + if (buf.len == 0) return @as(T, 0); if (buf[0] == '-') { return math.negate(try parseUnsigned(T, buf[1..], radix)); } else if (buf[0] == '+') { @@ -1325,8 +1325,8 @@ test "float.scientific" { // TODO https://github.com/ziglang/zig/issues/3289 return error.SkipZigTest; } - try testFmt("f32: 1.34000003e+00", "f32: {e}", f32(1.34)); - try testFmt("f32: 1.23400001e+01", "f32: {e}", f32(12.34)); + try testFmt("f32: 1.34000003e+00", "f32: {e}", @as(f32, 1.34)); + try testFmt("f32: 1.23400001e+01", "f32: {e}", @as(f32, 12.34)); try testFmt("f64: -1.234e+11", "f64: {e}", @as(f64, -12.34e10)); try testFmt("f64: 9.99996e-40", "f64: {e}", @as(f64, 9.999960e-40)); } @@ -1365,12 +1365,12 @@ test "float.decimal" { return error.SkipZigTest; } try testFmt("f64: 152314000000000000000000000000", "f64: {d}", @as(f64, 1.52314e+29)); - try testFmt("f32: 1.1", "f32: {d:.1}", f32(1.1234)); - try testFmt("f32: 1234.57", "f32: {d:.2}", f32(1234.567)); + try testFmt("f32: 1.1", "f32: {d:.1}", @as(f32, 1.1234)); + try testFmt("f32: 1234.57", "f32: {d:.2}", @as(f32, 1234.567)); // -11.1234 is converted to f64 -11.12339... internally (errol3() function takes f64). // -11.12339... is rounded back up to -11.1234 - try testFmt("f32: -11.1234", "f32: {d:.4}", f32(-11.1234)); - try testFmt("f32: 91.12345", "f32: {d:.5}", f32(91.12345)); + try testFmt("f32: -11.1234", "f32: {d:.4}", @as(f32, -11.1234)); + try testFmt("f32: 91.12345", "f32: {d:.5}", @as(f32, 91.12345)); try testFmt("f64: 91.1234567890", "f64: {d:.10}", @as(f64, 91.12345678901235)); try testFmt("f64: 0.00000", "f64: {d:.5}", @as(f64, 0.0)); try testFmt("f64: 6", "f64: {d:.0}", @as(f64, 5.700)); diff --git a/lib/std/fmt/parse_float.zig b/lib/std/fmt/parse_float.zig index aa3ac6d458..78ce0b7d0a 100644 --- a/lib/std/fmt/parse_float.zig +++ b/lib/std/fmt/parse_float.zig @@ -375,7 +375,7 @@ pub fn parseFloat(comptime T: type, s: []const u8) !T { return switch (try parseRepr(s, &r)) { ParseResult.Ok => convertRepr(T, r), ParseResult.PlusZero => 0.0, - ParseResult.MinusZero => -T(0.0), + ParseResult.MinusZero => -@as(T, 0.0), ParseResult.PlusInf => std.math.inf(T), ParseResult.MinusInf => -std.math.inf(T), }; @@ -426,8 +426,8 @@ test "fmt.parseFloat" { expect(approxEq(T, try parseFloat(T, "1234e-2"), 12.34, epsilon)); expect(approxEq(T, try parseFloat(T, "123142.1"), 123142.1, epsilon)); - expect(approxEq(T, try parseFloat(T, "-123142.1124"), T(-123142.1124), epsilon)); - expect(approxEq(T, try parseFloat(T, "0.7062146892655368"), T(0.7062146892655368), epsilon)); + expect(approxEq(T, try parseFloat(T, "-123142.1124"), @as(T, -123142.1124), epsilon)); + expect(approxEq(T, try parseFloat(T, "0.7062146892655368"), @as(T, 0.7062146892655368), epsilon)); } } } diff --git a/lib/std/heap.zig b/lib/std/heap.zig index c9e9e3533d..24ab395734 100644 --- a/lib/std/heap.zig +++ b/lib/std/heap.zig @@ -893,10 +893,10 @@ fn testAllocatorLargeAlignment(allocator: *mem.Allocator) mem.Allocator.Error!vo if (mem.page_size << 2 > maxInt(usize)) return; const USizeShift = @IntType(false, std.math.log2(usize.bit_count)); - const large_align = u29(mem.page_size << 2); + const large_align = @as(u29, mem.page_size << 2); var align_mask: usize = undefined; - _ = @shlWithOverflow(usize, ~@as(usize, 0), USizeShift(@ctz(u29, large_align)), &align_mask); + _ = @shlWithOverflow(usize, ~@as(usize, 0), @as(USizeShift, @ctz(u29, large_align)), &align_mask); var slice = try allocator.alignedAlloc(u8, large_align, 500); testing.expect(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr)); diff --git a/lib/std/io.zig b/lib/std/io.zig index 4fcee6ec14..f2cbeff922 100644 --- a/lib/std/io.zig +++ b/lib/std/io.zig @@ -355,19 +355,19 @@ pub fn BitInStream(endian: builtin.Endian, comptime Error: type) type { out_bits.* = @as(usize, 0); if (U == u0 or bits == 0) return 0; - var out_buffer = Buf(0); + var out_buffer = @as(Buf, 0); if (self.bit_count > 0) { const n = if (self.bit_count >= bits) @intCast(u3, bits) else self.bit_count; const shift = u7_bit_count - n; switch (endian) { builtin.Endian.Big => { - out_buffer = Buf(self.bit_buffer >> shift); + out_buffer = @as(Buf, self.bit_buffer >> shift); self.bit_buffer <<= n; }, builtin.Endian.Little => { const value = (self.bit_buffer << shift) >> shift; - out_buffer = Buf(value); + out_buffer = @as(Buf, value); self.bit_buffer >>= n; }, } @@ -393,28 +393,28 @@ pub fn BitInStream(endian: builtin.Endian, comptime Error: type) type { if (n >= u8_bit_count) { out_buffer <<= @intCast(u3, u8_bit_count - 1); out_buffer <<= 1; - out_buffer |= Buf(next_byte); + out_buffer |= @as(Buf, next_byte); out_bits.* += u8_bit_count; continue; } const shift = @intCast(u3, u8_bit_count - n); out_buffer <<= @intCast(BufShift, n); - out_buffer |= Buf(next_byte >> shift); + out_buffer |= @as(Buf, next_byte >> shift); out_bits.* += n; self.bit_buffer = @truncate(u7, next_byte << @intCast(u3, n - 1)); self.bit_count = shift; }, builtin.Endian.Little => { if (n >= u8_bit_count) { - out_buffer |= Buf(next_byte) << @intCast(BufShift, out_bits.*); + out_buffer |= @as(Buf, next_byte) << @intCast(BufShift, out_bits.*); out_bits.* += u8_bit_count; continue; } const shift = @intCast(u3, u8_bit_count - n); const value = (next_byte << shift) >> shift; - out_buffer |= Buf(value) << @intCast(BufShift, out_bits.*); + out_buffer |= @as(Buf, value) << @intCast(BufShift, out_bits.*); out_bits.* += n; self.bit_buffer = @truncate(u7, next_byte >> @intCast(u3, n)); self.bit_count = shift; @@ -949,14 +949,14 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing, return @truncate(T, @bitCast(PossiblySignedByte, buffer[0])); } - var result = U(0); + var result = @as(U, 0); for (buffer) |byte, i| { switch (endian) { builtin.Endian.Big => { result = (result << u8_bit_count) | byte; }, builtin.Endian.Little => { - result |= U(byte) << @intCast(Log2U, u8_bit_count * i); + result |= @as(U, byte) << @intCast(Log2U, u8_bit_count * i); }, } } @@ -1050,7 +1050,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing, return; } - ptr.* = OC(undefined); //make it non-null so the following .? is guaranteed safe + ptr.* = @as(OC, undefined); //make it non-null so the following .? is guaranteed safe const val_ptr = &ptr.*.?; try self.deserializeInto(val_ptr); }, @@ -1154,7 +1154,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co switch (@typeId(T)) { builtin.TypeId.Void => return, - builtin.TypeId.Bool => try self.serializeInt(u1(@boolToInt(value))), + builtin.TypeId.Bool => try self.serializeInt(@as(u1, @boolToInt(value))), builtin.TypeId.Float, builtin.TypeId.Int => try self.serializeInt(value), builtin.TypeId.Struct => { const info = @typeInfo(T); @@ -1197,10 +1197,10 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co }, builtin.TypeId.Optional => { if (value == null) { - try self.serializeInt(u1(@boolToInt(false))); + try self.serializeInt(@as(u1, @boolToInt(false))); return; } - try self.serializeInt(u1(@boolToInt(true))); + try self.serializeInt(@as(u1, @boolToInt(true))); const OC = comptime meta.Child(T); const val_ptr = &value.?; diff --git a/lib/std/io/test.zig b/lib/std/io/test.zig index 1fa7fc594c..28b8371d9d 100644 --- a/lib/std/io/test.zig +++ b/lib/std/io/test.zig @@ -226,18 +226,18 @@ test "BitOutStream" { const OutError = io.SliceOutStream.Error; var bit_stream_be = io.BitOutStream(builtin.Endian.Big, OutError).init(&mem_out_be.stream); - try bit_stream_be.writeBits(u2(1), 1); - try bit_stream_be.writeBits(u5(2), 2); + try bit_stream_be.writeBits(@as(u2, 1), 1); + try bit_stream_be.writeBits(@as(u5, 2), 2); try bit_stream_be.writeBits(@as(u128, 3), 3); try bit_stream_be.writeBits(@as(u8, 4), 4); - try bit_stream_be.writeBits(u9(5), 5); - try bit_stream_be.writeBits(u1(1), 1); + try bit_stream_be.writeBits(@as(u9, 5), 5); + try bit_stream_be.writeBits(@as(u1, 1), 1); expect(mem_be[0] == 0b11001101 and mem_be[1] == 0b00001011); mem_out_be.pos = 0; - try bit_stream_be.writeBits(u15(0b110011010000101), 15); + try bit_stream_be.writeBits(@as(u15, 0b110011010000101), 15); try bit_stream_be.flushBits(); expect(mem_be[0] == 0b11001101 and mem_be[1] == 0b00001010); @@ -345,8 +345,8 @@ fn testIntSerializerDeserializer(comptime endian: builtin.Endian, comptime packi inline while (i <= max_test_bitsize) : (i += 1) { const U = @IntType(false, i); const S = @IntType(true, i); - try serializer.serializeInt(U(i)); - if (i != 0) try serializer.serializeInt(S(-1)) else try serializer.serialize(S(0)); + try serializer.serializeInt(@as(U, i)); + if (i != 0) try serializer.serializeInt(@as(S, -1)) else try serializer.serialize(@as(S, 0)); } try serializer.flush(); @@ -356,8 +356,8 @@ fn testIntSerializerDeserializer(comptime endian: builtin.Endian, comptime packi const S = @IntType(true, i); const x = try deserializer.deserializeInt(U); const y = try deserializer.deserializeInt(S); - expect(x == U(i)); - if (i != 0) expect(y == S(-1)) else expect(y == 0); + expect(x == @as(U, i)); + if (i != 0) expect(y == @as(S, -1)) else expect(y == 0); } const u8_bit_count = comptime meta.bitCount(u8); @@ -577,11 +577,11 @@ fn testBadData(comptime endian: builtin.Endian, comptime packing: io.Packing) !v var in_stream = &in.stream; var deserializer = io.Deserializer(endian, packing, InError).init(in_stream); - try serializer.serialize(u14(3)); + try serializer.serialize(@as(u14, 3)); expectError(error.InvalidEnumTag, deserializer.deserialize(A)); out.pos = 0; - try serializer.serialize(u14(3)); - try serializer.serialize(u14(88)); + try serializer.serialize(@as(u14, 3)); + try serializer.serialize(@as(u14, 88)); expectError(error.InvalidEnumTag, deserializer.deserialize(C)); } diff --git a/lib/std/lazy_init.zig b/lib/std/lazy_init.zig index 7beabb9cde..add40a372d 100644 --- a/lib/std/lazy_init.zig +++ b/lib/std/lazy_init.zig @@ -39,7 +39,7 @@ fn LazyInit(comptime T: type) type { }, 2 => { if (@sizeOf(T) == 0) { - return T(undefined); + return @as(T, undefined); } else { return &self.data; } diff --git a/lib/std/math.zig b/lib/std/math.zig index 7c672c7ae8..8e08cacc42 100644 --- a/lib/std/math.zig +++ b/lib/std/math.zig @@ -543,8 +543,8 @@ test "math.absFloat" { comptime testAbsFloat(); } fn testAbsFloat() void { - testing.expect(absFloat(f32(-10.05)) == 10.05); - testing.expect(absFloat(f32(10.05)) == 10.05); + testing.expect(absFloat(@as(f32, -10.05)) == 10.05); + testing.expect(absFloat(@as(f32, 10.05)) == 10.05); } pub fn divTrunc(comptime T: type, numerator: T, denominator: T) !T { @@ -731,8 +731,8 @@ pub fn cast(comptime T: type, x: var) (error{Overflow}!T) { test "math.cast" { testing.expectError(error.Overflow, cast(u8, @as(u32, 300))); testing.expectError(error.Overflow, cast(i8, @as(i32, -200))); - testing.expectError(error.Overflow, cast(u8, i8(-1))); - testing.expectError(error.Overflow, cast(u64, i8(-1))); + testing.expectError(error.Overflow, cast(u8, @as(i8, -1))); + testing.expectError(error.Overflow, cast(u64, @as(i8, -1))); testing.expect((try cast(u8, @as(u32, 255))) == @as(u8, 255)); testing.expect(@typeOf(try cast(u8, @as(u32, 255))) == u8); @@ -786,9 +786,9 @@ pub fn ceilPowerOfTwoPromote(comptime T: type, value: T) @IntType(T.is_signed, T comptime assert(@typeId(T) == builtin.TypeId.Int); comptime assert(!T.is_signed); assert(value != 0); - comptime const promotedType = @IntType(T.is_signed, T.bit_count + 1); - comptime const shiftType = std.math.Log2Int(promotedType); - return promotedType(1) << @intCast(shiftType, T.bit_count - @clz(T, value - 1)); + comptime const PromotedType = @IntType(T.is_signed, T.bit_count + 1); + comptime const shiftType = std.math.Log2Int(PromotedType); + return @as(PromotedType, 1) << @intCast(shiftType, T.bit_count - @clz(T, value - 1)); } /// Returns the next power of two (if the value is not already a power of two). @@ -797,8 +797,8 @@ pub fn ceilPowerOfTwoPromote(comptime T: type, value: T) @IntType(T.is_signed, T pub fn ceilPowerOfTwo(comptime T: type, value: T) (error{Overflow}!T) { comptime assert(@typeId(T) == builtin.TypeId.Int); comptime assert(!T.is_signed); - comptime const promotedType = @IntType(T.is_signed, T.bit_count + 1); - comptime const overflowBit = promotedType(1) << T.bit_count; + comptime const PromotedType = @IntType(T.is_signed, T.bit_count + 1); + comptime const overflowBit = @as(PromotedType, 1) << T.bit_count; var x = ceilPowerOfTwoPromote(T, value); if (overflowBit & x != 0) { return error.Overflow; @@ -848,7 +848,7 @@ pub fn log2_int(comptime T: type, x: T) Log2Int(T) { pub fn log2_int_ceil(comptime T: type, x: T) Log2Int(T) { assert(x != 0); const log2_val = log2_int(T, x); - if (T(1) << log2_val == x) + if (@as(T, 1) << log2_val == x) return log2_val; return log2_val + 1; } @@ -870,8 +870,8 @@ pub fn lossyCast(comptime T: type, value: var) T { switch (@typeInfo(@typeOf(value))) { builtin.TypeId.Int => return @intToFloat(T, value), builtin.TypeId.Float => return @floatCast(T, value), - builtin.TypeId.ComptimeInt => return T(value), - builtin.TypeId.ComptimeFloat => return T(value), + builtin.TypeId.ComptimeInt => return @as(T, value), + builtin.TypeId.ComptimeFloat => return @as(T, value), else => @compileError("bad type"), } } diff --git a/lib/std/math/big/int.zig b/lib/std/math/big/int.zig index fa2424de07..0459b0b158 100644 --- a/lib/std/math/big/int.zig +++ b/lib/std/math/big/int.zig @@ -281,7 +281,7 @@ pub const Int = struct { var w_value: UT = if (value < 0) @intCast(UT, -value) else @intCast(UT, value); if (info.bits <= Limb.bit_count) { - self.limbs[0] = Limb(w_value); + self.limbs[0] = @as(Limb, w_value); self.metadata += 1; } else { var i: usize = 0; @@ -453,7 +453,7 @@ pub const Int = struct { for (self.limbs[0..self.len()]) |limb| { var shift: usize = 0; while (shift < Limb.bit_count) : (shift += base_shift) { - const r = @intCast(u8, (limb >> @intCast(Log2Limb, shift)) & Limb(base - 1)); + const r = @intCast(u8, (limb >> @intCast(Log2Limb, shift)) & @as(Limb, base - 1)); const ch = try digitToChar(r, base); try digits.append(ch); } @@ -560,7 +560,7 @@ pub const Int = struct { /// Returns -1, 0, 1 if a < b, a == b or a > b respectively. pub fn cmp(a: Int, b: Int) i8 { if (a.isPositive() != b.isPositive()) { - return if (a.isPositive()) i8(1) else -1; + return if (a.isPositive()) @as(i8, 1) else -1; } else { const r = cmpAbs(a, b); return if (a.isPositive()) r else -r; @@ -785,7 +785,7 @@ pub const Int = struct { const c1: Limb = @boolToInt(@addWithOverflow(Limb, a, carry.*, &r1)); // r2 = b * c - const bc = DoubleLimb(math.mulWide(Limb, b, c)); + const bc = @as(DoubleLimb, math.mulWide(Limb, b, c)); const r2 = @truncate(Limb, bc); const c2 = @truncate(Limb, bc >> Limb.bit_count); @@ -1084,7 +1084,7 @@ pub const Int = struct { rem.* = 0; for (a) |_, ri| { const i = a.len - ri - 1; - const pdiv = ((DoubleLimb(rem.*) << Limb.bit_count) | a[i]); + const pdiv = ((@as(DoubleLimb, rem.*) << Limb.bit_count) | a[i]); if (pdiv == 0) { quo[i] = 0; @@ -1143,9 +1143,9 @@ pub const Int = struct { if (x.limbs[i] == y.limbs[t]) { q.limbs[i - t - 1] = maxInt(Limb); } else { - const num = (DoubleLimb(x.limbs[i]) << Limb.bit_count) | DoubleLimb(x.limbs[i - 1]); - const z = @intCast(Limb, num / DoubleLimb(y.limbs[t])); - q.limbs[i - t - 1] = if (z > maxInt(Limb)) maxInt(Limb) else Limb(z); + const num = (@as(DoubleLimb, x.limbs[i]) << Limb.bit_count) | @as(DoubleLimb, x.limbs[i - 1]); + const z = @intCast(Limb, num / @as(DoubleLimb, y.limbs[t])); + q.limbs[i - t - 1] = if (z > maxInt(Limb)) maxInt(Limb) else @as(Limb, z); } // 3.2 @@ -1362,7 +1362,7 @@ test "big.int comptime_int set" { comptime var i: usize = 0; inline while (i < s_limb_count) : (i += 1) { - const result = Limb(s & maxInt(Limb)); + const result = @as(Limb, s & maxInt(Limb)); s >>= Limb.bit_count / 2; s >>= Limb.bit_count / 2; testing.expect(a.limbs[i] == result); @@ -1377,7 +1377,7 @@ test "big.int comptime_int set negative" { } test "big.int int set unaligned small" { - var a = try Int.initSet(al, u7(45)); + var a = try Int.initSet(al, @as(u7, 45)); testing.expect(a.limbs[0] == 45); testing.expect(a.isPositive() == true); diff --git a/lib/std/math/complex.zig b/lib/std/math/complex.zig index e5574f9cee..8bff2313fc 100644 --- a/lib/std/math/complex.zig +++ b/lib/std/math/complex.zig @@ -133,8 +133,8 @@ test "complex.div" { const b = Complex(f32).new(2, 7); const c = a.div(b); - testing.expect(math.approxEq(f32, c.re, f32(31) / 53, epsilon) and - math.approxEq(f32, c.im, f32(-29) / 53, epsilon)); + testing.expect(math.approxEq(f32, c.re, @as(f32, 31) / 53, epsilon) and + math.approxEq(f32, c.im, @as(f32, -29) / 53, epsilon)); } test "complex.conjugate" { @@ -148,8 +148,8 @@ test "complex.reciprocal" { const a = Complex(f32).new(5, 3); const c = a.reciprocal(); - testing.expect(math.approxEq(f32, c.re, f32(5) / 34, epsilon) and - math.approxEq(f32, c.im, f32(-3) / 34, epsilon)); + testing.expect(math.approxEq(f32, c.re, @as(f32, 5) / 34, epsilon) and + math.approxEq(f32, c.im, @as(f32, -3) / 34, epsilon)); } test "complex.magnitude" { diff --git a/lib/std/math/complex/acos.zig b/lib/std/math/complex/acos.zig index f3526cc9ff..b078ebf345 100644 --- a/lib/std/math/complex/acos.zig +++ b/lib/std/math/complex/acos.zig @@ -8,7 +8,7 @@ const Complex = cmath.Complex; pub fn acos(z: var) Complex(@typeOf(z.re)) { const T = @typeOf(z.re); const q = cmath.asin(z); - return Complex(T).new(T(math.pi) / 2 - q.re, -q.im); + return Complex(T).new(@as(T, math.pi) / 2 - q.re, -q.im); } const epsilon = 0.0001; diff --git a/lib/std/math/complex/ldexp.zig b/lib/std/math/complex/ldexp.zig index d74ac86148..d5d4cc64a6 100644 --- a/lib/std/math/complex/ldexp.zig +++ b/lib/std/math/complex/ldexp.zig @@ -65,7 +65,7 @@ fn frexp_exp64(x: f64, expt: *i32) f64 { fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) { var ex_expt: i32 = undefined; const exp_x = frexp_exp64(z.re, &ex_expt); - const exptf = i64(expt + ex_expt); + const exptf = @as(i64, expt + ex_expt); const half_expt1 = @divTrunc(exptf, 2); const scale1 = @bitCast(f64, (0x3ff + half_expt1) << 20); diff --git a/lib/std/math/exp.zig b/lib/std/math/exp.zig index e1297b11bc..986c35bf0d 100644 --- a/lib/std/math/exp.zig +++ b/lib/std/math/exp.zig @@ -134,7 +134,7 @@ fn exp64(x_: f64) f64 { } if (x < -708.39641853226410622) { // underflow if x != -inf - // math.forceEval(f32(-0x1.0p-149 / x)); + // math.forceEval(@as(f32, -0x1.0p-149 / x)); if (x < -745.13321910194110842) { return 0; } diff --git a/lib/std/math/ln.zig b/lib/std/math/ln.zig index 362e44a869..fd5741a818 100644 --- a/lib/std/math/ln.zig +++ b/lib/std/math/ln.zig @@ -34,7 +34,7 @@ pub fn ln(x: var) @typeOf(x) { return @typeOf(1)(math.floor(ln_64(@as(f64, x)))); }, TypeId.Int => { - return T(math.floor(ln_64(@as(f64, x)))); + return @as(T, math.floor(ln_64(@as(f64, x)))); }, else => @compileError("ln not implemented for " ++ @typeName(T)), } diff --git a/lib/std/math/log.zig b/lib/std/math/log.zig index 84dbcf939d..40b716b005 100644 --- a/lib/std/math/log.zig +++ b/lib/std/math/log.zig @@ -64,8 +64,8 @@ test "math.log float" { } test "math.log float_special" { - expect(log(f32, 2, 0.2301974) == math.log2(f32(0.2301974))); - expect(log(f32, 10, 0.2301974) == math.log10(f32(0.2301974))); + expect(log(f32, 2, 0.2301974) == math.log2(@as(f32, 0.2301974))); + expect(log(f32, 10, 0.2301974) == math.log10(@as(f32, 0.2301974))); expect(log(f64, 2, 213.23019799993) == math.log2(@as(f64, 213.23019799993))); expect(log(f64, 10, 213.23019799993) == math.log10(@as(f64, 213.23019799993))); diff --git a/lib/std/math/log10.zig b/lib/std/math/log10.zig index a4b3fef26b..f895e102a0 100644 --- a/lib/std/math/log10.zig +++ b/lib/std/math/log10.zig @@ -177,7 +177,7 @@ pub fn log10_64(x_: f64) f64 { } test "math.log10" { - testing.expect(log10(f32(0.2)) == log10_32(0.2)); + testing.expect(log10(@as(f32, 0.2)) == log10_32(0.2)); testing.expect(log10(@as(f64, 0.2)) == log10_64(0.2)); } diff --git a/lib/std/math/sqrt.zig b/lib/std/math/sqrt.zig index 1b74c34eda..89eda4ea2b 100644 --- a/lib/std/math/sqrt.zig +++ b/lib/std/math/sqrt.zig @@ -15,7 +15,7 @@ const maxInt = std.math.maxInt; pub fn sqrt(x: var) (if (@typeId(@typeOf(x)) == TypeId.Int) @IntType(false, @typeOf(x).bit_count / 2) else @typeOf(x)) { const T = @typeOf(x); switch (@typeId(T)) { - TypeId.ComptimeFloat => return T(@sqrt(f64, x)), // TODO upgrade to f128 + TypeId.ComptimeFloat => return @as(T, @sqrt(f64, x)), // TODO upgrade to f128 TypeId.Float => return @sqrt(T, x), TypeId.ComptimeInt => comptime { if (x > maxInt(u128)) { @@ -24,7 +24,7 @@ pub fn sqrt(x: var) (if (@typeId(@typeOf(x)) == TypeId.Int) @IntType(false, @typ if (x < 0) { @compileError("sqrt on negative number"); } - return T(sqrt_int(u128, x)); + return @as(T, sqrt_int(u128, x)); }, TypeId.Int => return sqrt_int(T, x), else => @compileError("sqrt not implemented for " ++ @typeName(T)), diff --git a/lib/std/meta.zig b/lib/std/meta.zig index ded5aa4b1a..903c3a12b3 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -341,7 +341,7 @@ test "std.meta.TagType" { ///Returns the active tag of a tagged union pub fn activeTag(u: var) @TagType(@typeOf(u)) { const T = @typeOf(u); - return @TagType(T)(u); + return @as(@TagType(T), u); } test "std.meta.activeTag" { diff --git a/lib/std/net.zig b/lib/std/net.zig index fcb64d029a..d1db147866 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -117,7 +117,7 @@ pub const IpAddress = extern union { ip_slice[10] = 0xff; ip_slice[11] = 0xff; - const ptr = @sliceToBytes((*const [1]u32)(&addr)[0..]); + const ptr = @sliceToBytes(@as(*const [1]u32, &addr)[0..]); ip_slice[12] = ptr[0]; ip_slice[13] = ptr[1]; @@ -161,7 +161,7 @@ pub const IpAddress = extern union { .addr = undefined, }, }; - const out_ptr = @sliceToBytes((*[1]u32)(&result.in.addr)[0..]); + const out_ptr = @sliceToBytes(@as(*[1]u32, &result.in.addr)[0..]); var x: u8 = 0; var index: u8 = 0; @@ -271,7 +271,7 @@ pub const IpAddress = extern union { }, os.AF_INET6 => { const port = mem.bigToNative(u16, self.in6.port); - if (mem.eql(u8, self.in6.addr[0..12], [_]u8{0,0,0,0,0,0,0,0,0,0,0xff,0xff})) { + if (mem.eql(u8, self.in6.addr[0..12], [_]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff })) { try std.fmt.format( context, Errors, @@ -1133,7 +1133,7 @@ fn resMSendRc( } // Wait for a response, or until time to retry - const clamped_timeout = std.math.min(u31(std.math.maxInt(u31)), t1 + retry_interval - t2); + const clamped_timeout = std.math.min(@as(u31, std.math.maxInt(u31)), t1 + retry_interval - t2); const nevents = os.poll(&pfd, clamped_timeout) catch 0; if (nevents == 0) continue; diff --git a/lib/std/os/linux/test.zig b/lib/std/os/linux/test.zig index e089a02445..bccb7beb1e 100644 --- a/lib/std/os/linux/test.zig +++ b/lib/std/os/linux/test.zig @@ -72,7 +72,7 @@ test "statx" { expect(stat_buf.mode == statx_buf.mode); expect(@bitCast(u32, stat_buf.uid) == statx_buf.uid); expect(@bitCast(u32, stat_buf.gid) == statx_buf.gid); - expect(@bitCast(u64, i64(stat_buf.size)) == statx_buf.size); - expect(@bitCast(u64, i64(stat_buf.blksize)) == statx_buf.blksize); - expect(@bitCast(u64, i64(stat_buf.blocks)) == statx_buf.blocks); + expect(@bitCast(u64, @as(i64, stat_buf.size)) == statx_buf.size); + expect(@bitCast(u64, @as(i64, stat_buf.blksize)) == statx_buf.blksize); + expect(@bitCast(u64, @as(i64, stat_buf.blocks)) == statx_buf.blocks); } diff --git a/lib/std/packed_int_array.zig b/lib/std/packed_int_array.zig index c22d701bf2..d8c77db59b 100644 --- a/lib/std/packed_int_array.zig +++ b/lib/std/packed_int_array.zig @@ -193,7 +193,7 @@ pub fn PackedIntArrayEndian(comptime Int: type, comptime endian: builtin.Endian, ///Initialize a packed array using an unpacked array /// or, more likely, an array literal. pub fn init(ints: [int_count]Int) Self { - var self = Self(undefined); + var self = @as(Self, undefined); for (ints) |int, i| self.set(i, int); return self; } @@ -328,11 +328,11 @@ test "PackedIntArray" { const expected_bytes = ((bits * int_count) + 7) / 8; testing.expect(@sizeOf(PackedArray) == expected_bytes); - var data = PackedArray(undefined); + var data = @as(PackedArray, undefined); //write values, counting up var i = @as(usize, 0); - var count = I(0); + var count = @as(I, 0); while (i < data.len()) : (i += 1) { data.set(i, count); if (bits > 0) count +%= 1; @@ -376,7 +376,7 @@ test "PackedIntSlice" { //write values, counting up var i = @as(usize, 0); - var count = I(0); + var count = @as(I, 0); while (i < data.len()) : (i += 1) { data.set(i, count); if (bits > 0) count +%= 1; @@ -402,7 +402,7 @@ test "PackedIntSlice of PackedInt(Array/Slice)" { const Int = @IntType(false, bits); const PackedArray = PackedIntArray(Int, int_count); - var packed_array = PackedArray(undefined); + var packed_array = @as(PackedArray, undefined); const limit = (1 << bits); @@ -463,7 +463,7 @@ test "PackedIntSlice accumulating bit offsets" { // anything { const PackedArray = PackedIntArray(u3, 16); - var packed_array = PackedArray(undefined); + var packed_array = @as(PackedArray, undefined); var packed_slice = packed_array.slice(0, packed_array.len()); var i = @as(usize, 0); @@ -473,7 +473,7 @@ test "PackedIntSlice accumulating bit offsets" { } { const PackedArray = PackedIntArray(u11, 88); - var packed_array = PackedArray(undefined); + var packed_array = @as(PackedArray, undefined); var packed_slice = packed_array.slice(0, packed_array.len()); var i = @as(usize, 0); @@ -518,8 +518,8 @@ test "PackedInt(Array/Slice) sliceCast" { i = 0; while (i < packed_slice_cast_3.len()) : (i += 1) { const val = switch (builtin.endian) { - .Big => if (i % 2 == 0) u3(0b111) else u3(0b000), - .Little => if (i % 2 == 0) u3(0b111) else u3(0b000), + .Big => if (i % 2 == 0) @as(u3, 0b111) else @as(u3, 0b000), + .Little => if (i % 2 == 0) @as(u3, 0b111) else @as(u3, 0b000), }; testing.expect(packed_slice_cast_3.get(i) == val); } diff --git a/lib/std/rand.zig b/lib/std/rand.zig index 2cd1583f63..1fea0526ed 100644 --- a/lib/std/rand.zig +++ b/lib/std/rand.zig @@ -93,13 +93,13 @@ pub const Random = struct { // http://www.pcg-random.org/posts/bounded-rands.html // "Lemire's (with an extra tweak from me)" var x: Small = r.int(Small); - var m: Large = Large(x) * Large(less_than); + var m: Large = @as(Large, x) * @as(Large, less_than); var l: Small = @truncate(Small, m); if (l < less_than) { // TODO: workaround for https://github.com/ziglang/zig/issues/1770 // should be: // var t: Small = -%less_than; - var t: Small = @bitCast(Small, -%@bitCast(@IntType(true, Small.bit_count), Small(less_than))); + var t: Small = @bitCast(Small, -%@bitCast(@IntType(true, Small.bit_count), @as(Small, less_than))); if (t >= less_than) { t -= less_than; @@ -109,7 +109,7 @@ pub const Random = struct { } while (l < t) { x = r.int(Small); - m = Large(x) * Large(less_than); + m = @as(Large, x) * @as(Large, less_than); l = @truncate(Small, m); } } @@ -286,7 +286,7 @@ pub fn limitRangeBiased(comptime T: type, random_int: T, less_than: T) T { // adapted from: // http://www.pcg-random.org/posts/bounded-rands.html // "Integer Multiplication (Biased)" - var m: T2 = T2(random_int) * T2(less_than); + var m: T2 = @as(T2, random_int) * @as(T2, less_than); return @intCast(T, m >> T.bit_count); } diff --git a/lib/std/unicode.zig b/lib/std/unicode.zig index 5509f5f9dd..726b84f125 100644 --- a/lib/std/unicode.zig +++ b/lib/std/unicode.zig @@ -7,10 +7,10 @@ const mem = std.mem; /// Returns how many bytes the UTF-8 representation would require /// for the given codepoint. pub fn utf8CodepointSequenceLength(c: u32) !u3 { - if (c < 0x80) return u3(1); - if (c < 0x800) return u3(2); - if (c < 0x10000) return u3(3); - if (c < 0x110000) return u3(4); + if (c < 0x80) return @as(u3, 1); + if (c < 0x800) return @as(u3, 2); + if (c < 0x10000) return @as(u3, 3); + if (c < 0x110000) return @as(u3, 4); return error.CodepointTooLarge; } @@ -18,10 +18,10 @@ pub fn utf8CodepointSequenceLength(c: u32) !u3 { /// returns a number 1-4 indicating the total length of the codepoint in bytes. /// If this byte does not match the form of a UTF-8 start byte, returns Utf8InvalidStartByte. pub fn utf8ByteSequenceLength(first_byte: u8) !u3 { - if (first_byte < 0b10000000) return u3(1); - if (first_byte & 0b11100000 == 0b11000000) return u3(2); - if (first_byte & 0b11110000 == 0b11100000) return u3(3); - if (first_byte & 0b11111000 == 0b11110000) return u3(4); + if (first_byte < 0b10000000) return @as(u3, 1); + if (first_byte & 0b11100000 == 0b11000000) return @as(u3, 2); + if (first_byte & 0b11110000 == 0b11100000) return @as(u3, 3); + if (first_byte & 0b11111000 == 0b11110000) return @as(u3, 4); return error.Utf8InvalidStartByte; } diff --git a/src-self-hosted/stage1.zig b/src-self-hosted/stage1.zig index 3524c4f5b5..e84f7b0ac1 100644 --- a/src-self-hosted/stage1.zig +++ b/src-self-hosted/stage1.zig @@ -224,7 +224,7 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void { } if (flags.present("check")) { const anything_changed = try std.zig.render(allocator, io.null_out_stream, tree); - const code = if (anything_changed) u8(1) else u8(0); + const code = if (anything_changed) @as(u8, 1) else @as(u8, 0); process.exit(code); } diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index b4f69753b1..0d3a99a683 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -123,7 +123,7 @@ const Context = struct { fn locStr(c: *Context, loc: ZigClangSourceLocation) ![]u8 { const spelling_loc = ZigClangSourceManager_getSpellingLoc(c.source_manager, loc); const filename_c = ZigClangSourceManager_getFilename(c.source_manager, spelling_loc); - const filename = if (filename_c) |s| try c.str(s) else ([]const u8)("(no file)"); + const filename = if (filename_c) |s| try c.str(s) else @as([]const u8, "(no file)"); const line = ZigClangSourceManager_getSpellingLineNumber(c.source_manager, spelling_loc); const column = ZigClangSourceManager_getSpellingColumnNumber(c.source_manager, spelling_loc); diff --git a/test/compile_errors.zig b/test/compile_errors.zig index c7f286ce3f..e3d80e4b63 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -1332,7 +1332,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { cases.add( "@bitCast with different sizes inside an expression", \\export fn entry() void { - \\ var foo = (@bitCast(u8, f32(1.0)) == 0xf); + \\ var foo = (@bitCast(u8, @as(f32, 1.0)) == 0xf); \\} , "tmp.zig:2:25: error: destination type 'u8' has size 1 but source type 'f32' has size 4", @@ -2120,13 +2120,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { cases.add( "@floatToInt comptime safety", \\comptime { - \\ _ = @floatToInt(i8, f32(-129.1)); + \\ _ = @floatToInt(i8, @as(f32, -129.1)); \\} \\comptime { - \\ _ = @floatToInt(u8, f32(-1.1)); + \\ _ = @floatToInt(u8, @as(f32, -1.1)); \\} \\comptime { - \\ _ = @floatToInt(u8, f32(256.1)); + \\ _ = @floatToInt(u8, @as(f32, 256.1)); \\} , "tmp.zig:2:9: error: integer value '-129' cannot be stored in type 'i8'", @@ -3888,7 +3888,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\const lit_int_x = 1 / 0; \\const lit_float_x = 1.0 / 0.0; \\const int_x = @as(u32, 1) / @as(u32, 0); - \\const float_x = f32(1.0) / f32(0.0); + \\const float_x = @as(f32, 1.0) / @as(f32, 0.0); \\ \\export fn entry1() usize { return @sizeOf(@typeOf(lit_int_x)); } \\export fn entry2() usize { return @sizeOf(@typeOf(lit_float_x)); } -- cgit v1.2.3