diff options
Diffstat (limited to 'test/behavior')
55 files changed, 393 insertions, 407 deletions
diff --git a/test/behavior/align.zig b/test/behavior/align.zig index d3e4d81250..c8eb71a433 100644 --- a/test/behavior/align.zig +++ b/test/behavior/align.zig @@ -24,7 +24,7 @@ test "slicing array of length 1 can not assume runtime index is always zero" { const slice = @as(*align(4) [1]u8, &foo)[runtime_index..]; try expect(@TypeOf(slice) == []u8); try expect(slice.len == 0); - try expect(@truncate(u2, @intFromPtr(slice.ptr) - 1) == 0); + try expect(@as(u2, @truncate(@intFromPtr(slice.ptr) - 1)) == 0); } test "default alignment allows unspecified in type syntax" { @@ -47,7 +47,7 @@ test "@alignCast pointers" { try expect(x == 2); } fn expectsOnly1(x: *align(1) u32) void { - expects4(@alignCast(4, x)); + expects4(@alignCast(x)); } fn expects4(x: *align(4) u32) void { x.* += 1; @@ -213,12 +213,6 @@ test "alignment and size of structs with 128-bit fields" { } } -test "@ptrCast preserves alignment of bigger source" { - var x: u32 align(16) = 1234; - const ptr = @ptrCast(*u8, &x); - try expect(@TypeOf(ptr) == *align(16) u8); -} - test "alignstack" { try expect(fnWithAlignedStack() == 1234); } @@ -249,7 +243,7 @@ test "specifying alignment allows pointer cast" { } fn testBytesAlign(b: u8) !void { var bytes align(4) = [_]u8{ b, b, b, b }; - const ptr = @ptrCast(*u32, &bytes[0]); + const ptr = @as(*u32, @ptrCast(&bytes[0])); try expect(ptr.* == 0x33333333); } @@ -265,7 +259,7 @@ test "@alignCast slices" { try expect(slice[0] == 2); } fn sliceExpectsOnly1(slice: []align(1) u32) void { - sliceExpects4(@alignCast(4, slice)); + sliceExpects4(@alignCast(slice)); } fn sliceExpects4(slice: []align(4) u32) void { slice[0] += 1; @@ -302,8 +296,8 @@ test "page aligned array on stack" { try expect(@intFromPtr(&array[0]) & 0xFFF == 0); try expect(array[3] == 4); - try expect(@truncate(u4, @intFromPtr(&number1)) == 0); - try expect(@truncate(u4, @intFromPtr(&number2)) == 0); + try expect(@as(u4, @truncate(@intFromPtr(&number1))) == 0); + try expect(@as(u4, @truncate(@intFromPtr(&number2))) == 0); try expect(number1 == 42); try expect(number2 == 43); } @@ -366,7 +360,7 @@ test "@alignCast functions" { try expect(fnExpectsOnly1(simple4) == 0x19); } fn fnExpectsOnly1(ptr: *const fn () align(1) i32) i32 { - return fnExpects4(@alignCast(4, ptr)); + return fnExpects4(@alignCast(ptr)); } fn fnExpects4(ptr: *const fn () align(4) i32) i32 { return ptr(); @@ -461,9 +455,11 @@ fn testIndex2(ptr: [*]align(4) u8, index: usize, comptime T: type) !void { test "alignment of function with c calling convention" { if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; + const a = @alignOf(@TypeOf(nothing)); + var runtime_nothing = ¬hing; - const casted1 = @ptrCast(*const u8, runtime_nothing); - const casted2 = @ptrCast(*const fn () callconv(.C) void, casted1); + const casted1: *align(a) const u8 = @ptrCast(runtime_nothing); + const casted2: *const fn () callconv(.C) void = @ptrCast(casted1); casted2(); } @@ -588,7 +584,7 @@ test "@alignCast null" { if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; var ptr: ?*anyopaque = null; - const aligned: ?*anyopaque = @alignCast(@alignOf(?*anyopaque), ptr); + const aligned: ?*anyopaque = @alignCast(ptr); try expect(aligned == null); } diff --git a/test/behavior/array.zig b/test/behavior/array.zig index 9ef4a55b39..bc8176aa9c 100644 --- a/test/behavior/array.zig +++ b/test/behavior/array.zig @@ -170,7 +170,7 @@ test "array with sentinels" { { var zero_sized: [0:0xde]u8 = [_:0xde]u8{}; try expect(zero_sized[0] == 0xde); - var reinterpreted = @ptrCast(*[1]u8, &zero_sized); + var reinterpreted = @as(*[1]u8, @ptrCast(&zero_sized)); try expect(reinterpreted[0] == 0xde); } var arr: [3:0x55]u8 = undefined; @@ -694,7 +694,7 @@ test "array init of container level array variable" { test "runtime initialized sentinel-terminated array literal" { var c: u16 = 300; const f = &[_:0x9999]u16{c}; - const g = @ptrCast(*const [4]u8, f); + const g = @as(*const [4]u8, @ptrCast(f)); try std.testing.expect(g[2] == 0x99); try std.testing.expect(g[3] == 0x99); } diff --git a/test/behavior/async_fn.zig b/test/behavior/async_fn.zig index dcbe78b091..7eaa5c78d0 100644 --- a/test/behavior/async_fn.zig +++ b/test/behavior/async_fn.zig @@ -136,12 +136,12 @@ test "@frameSize" { const S = struct { fn doTheTest() !void { { - var ptr = @ptrCast(fn (i32) callconv(.Async) void, other); + var ptr = @as(fn (i32) callconv(.Async) void, @ptrCast(other)); const size = @frameSize(ptr); try expect(size == @sizeOf(@Frame(other))); } { - var ptr = @ptrCast(fn () callconv(.Async) void, first); + var ptr = @as(fn () callconv(.Async) void, @ptrCast(first)); const size = @frameSize(ptr); try expect(size == @sizeOf(@Frame(first))); } @@ -1184,7 +1184,7 @@ test "using @TypeOf on a generic function call" { global_frame = @frame(); } const F = @TypeOf(async amain(x - 1)); - const frame = @ptrFromInt(*F, @intFromPtr(&buf)); + const frame = @as(*F, @ptrFromInt(@intFromPtr(&buf))); return await @asyncCall(frame, {}, amain, .{x - 1}); } }; @@ -1212,7 +1212,7 @@ test "recursive call of await @asyncCall with struct return type" { global_frame = @frame(); } const F = @TypeOf(async amain(x - 1)); - const frame = @ptrFromInt(*F, @intFromPtr(&buf)); + const frame = @as(*F, @ptrFromInt(@intFromPtr(&buf))); return await @asyncCall(frame, {}, amain, .{x - 1}); } @@ -1833,7 +1833,7 @@ test "avoid forcing frame alignment resolution implicit cast to *anyopaque" { } }; var frame = async S.foo(); - resume @ptrCast(anyframe->bool, @alignCast(@alignOf(@Frame(S.foo)), S.x)); + resume @as(anyframe->bool, @ptrCast(@alignCast(S.x))); try expect(nosuspend await frame); } diff --git a/test/behavior/atomics.zig b/test/behavior/atomics.zig index 4394e62f6f..5264ef75cf 100644 --- a/test/behavior/atomics.zig +++ b/test/behavior/atomics.zig @@ -326,7 +326,7 @@ fn testAtomicRmwInt128(comptime signedness: std.builtin.Signedness) !void { const uint = std.meta.Int(.unsigned, 128); const int = std.meta.Int(signedness, 128); - const initial: int = @bitCast(int, @as(uint, 0xaaaaaaaa_bbbbbbbb_cccccccc_dddddddd)); + const initial: int = @as(int, @bitCast(@as(uint, 0xaaaaaaaa_bbbbbbbb_cccccccc_dddddddd))); const replacement: int = 0x00000000_00000005_00000000_00000003; var x: int align(16) = initial; diff --git a/test/behavior/basic.zig b/test/behavior/basic.zig index f98cf8f237..87cbb3e242 100644 --- a/test/behavior/basic.zig +++ b/test/behavior/basic.zig @@ -20,7 +20,7 @@ test "truncate" { try comptime expect(testTruncate(0x10fd) == 0xfd); } fn testTruncate(x: u32) u8 { - return @truncate(u8, x); + return @as(u8, @truncate(x)); } test "truncate to non-power-of-two integers" { @@ -56,7 +56,7 @@ test "truncate to non-power-of-two integers from 128-bit" { } fn testTrunc(comptime Big: type, comptime Little: type, big: Big, little: Little) !void { - try expect(@truncate(Little, big) == little); + try expect(@as(Little, @truncate(big)) == little); } const g1: i32 = 1233 + 1; @@ -229,9 +229,9 @@ test "opaque types" { const global_a: i32 = 1234; const global_b: *const i32 = &global_a; -const global_c: *const f32 = @ptrCast(*const f32, global_b); +const global_c: *const f32 = @as(*const f32, @ptrCast(global_b)); test "compile time global reinterpret" { - const d = @ptrCast(*const i32, global_c); + const d = @as(*const i32, @ptrCast(global_c)); try expect(d.* == 1234); } @@ -362,7 +362,7 @@ test "variable is allowed to be a pointer to an opaque type" { if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; var x: i32 = 1234; - _ = hereIsAnOpaqueType(@ptrCast(*OpaqueA, &x)); + _ = hereIsAnOpaqueType(@as(*OpaqueA, @ptrCast(&x))); } fn hereIsAnOpaqueType(ptr: *OpaqueA) *OpaqueA { var a = ptr; @@ -442,7 +442,7 @@ test "array 3D const double ptr with offset" { } fn testArray2DConstDoublePtr(ptr: *const f32) !void { - const ptr2 = @ptrCast([*]const f32, ptr); + const ptr2 = @as([*]const f32, @ptrCast(ptr)); try expect(ptr2[0] == 1.0); try expect(ptr2[1] == 2.0); } @@ -574,9 +574,9 @@ test "constant equal function pointers" { fn emptyFn() void {} -const addr1 = @ptrCast(*const u8, &emptyFn); +const addr1 = @as(*const u8, @ptrCast(&emptyFn)); test "comptime cast fn to ptr" { - const addr2 = @ptrCast(*const u8, &emptyFn); + const addr2 = @as(*const u8, @ptrCast(&emptyFn)); try comptime expect(addr1 == addr2); } @@ -667,7 +667,7 @@ test "string escapes" { test "explicit cast optional pointers" { const a: ?*i32 = undefined; - const b: ?*f32 = @ptrCast(?*f32, a); + const b: ?*f32 = @as(?*f32, @ptrCast(a)); _ = b; } @@ -752,7 +752,7 @@ test "auto created variables have correct alignment" { const S = struct { fn foo(str: [*]const u8) u32 { - for (@ptrCast([*]align(1) const u32, str)[0..1]) |v| { + for (@as([*]align(1) const u32, @ptrCast(str))[0..1]) |v| { return v; } return 0; @@ -772,7 +772,7 @@ test "extern variable with non-pointer opaque type" { if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; @export(var_to_export, .{ .name = "opaque_extern_var" }); - try expect(@ptrCast(*align(1) u32, &opaque_extern_var).* == 42); + try expect(@as(*align(1) u32, @ptrCast(&opaque_extern_var)).* == 42); } extern var opaque_extern_var: opaque {}; var var_to_export: u32 = 42; diff --git a/test/behavior/bit_shifting.zig b/test/behavior/bit_shifting.zig index 03eb4433e1..8b605385d2 100644 --- a/test/behavior/bit_shifting.zig +++ b/test/behavior/bit_shifting.zig @@ -28,7 +28,7 @@ fn ShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, compt // TODO: https://github.com/ziglang/zig/issues/1544 // This cast could be implicit if we teach the compiler that // u32 >> 30 -> u2 - return @intCast(ShardKey, shard_key); + return @as(ShardKey, @intCast(shard_key)); } pub fn put(self: *Self, node: *Node) void { @@ -85,14 +85,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, 0..) |*node, i| { - const key = @intCast(Key, i); + const key = @as(Key, @intCast(i)); try expect(table.get(key) == null); node.init(key, {}); table.put(node); } for (&node_buffer, 0..) |*node, i| { - try expect(table.get(@intCast(Key, i)) == node); + try expect(table.get(@as(Key, @intCast(i))) == node); } } diff --git a/test/behavior/bitcast.zig b/test/behavior/bitcast.zig index f71a05cada..0c137a2baa 100644 --- a/test/behavior/bitcast.zig +++ b/test/behavior/bitcast.zig @@ -71,11 +71,11 @@ fn testBitCast(comptime N: usize) !void { } fn conv_iN(comptime N: usize, x: std.meta.Int(.signed, N)) std.meta.Int(.unsigned, N) { - return @bitCast(std.meta.Int(.unsigned, N), x); + return @as(std.meta.Int(.unsigned, N), @bitCast(x)); } fn conv_uN(comptime N: usize, x: std.meta.Int(.unsigned, N)) std.meta.Int(.signed, N) { - return @bitCast(std.meta.Int(.signed, N), x); + return @as(std.meta.Int(.signed, N), @bitCast(x)); } test "bitcast uX to bytes" { @@ -114,14 +114,14 @@ fn testBitCastuXToBytes(comptime N: usize) !void { while (byte_i < (byte_count - 1)) : (byte_i += 1) { try expect(bytes[byte_i] == 0xff); } - try expect(((bytes[byte_i] ^ 0xff) << -%@truncate(u3, N)) == 0); + try expect(((bytes[byte_i] ^ 0xff) << -%@as(u3, @truncate(N))) == 0); }, .Big => { var byte_i = byte_count - 1; while (byte_i > 0) : (byte_i -= 1) { try expect(bytes[byte_i] == 0xff); } - try expect(((bytes[byte_i] ^ 0xff) << -%@truncate(u3, N)) == 0); + try expect(((bytes[byte_i] ^ 0xff) << -%@as(u3, @truncate(N))) == 0); }, } } @@ -130,12 +130,12 @@ fn testBitCastuXToBytes(comptime N: usize) !void { test "nested bitcast" { const S = struct { fn moo(x: isize) !void { - try expect(@intCast(isize, 42) == x); + try expect(@as(isize, @intCast(42)) == x); } fn foo(x: isize) !void { try @This().moo( - @bitCast(isize, if (x != 0) @bitCast(usize, x) else @bitCast(usize, x)), + @as(isize, @bitCast(if (x != 0) @as(usize, @bitCast(x)) else @as(usize, @bitCast(x)))), ); } }; @@ -146,7 +146,7 @@ test "nested bitcast" { // issue #3010: compiler segfault test "bitcast literal [4]u8 param to u32" { - const ip = @bitCast(u32, [_]u8{ 255, 255, 255, 255 }); + const ip = @as(u32, @bitCast([_]u8{ 255, 255, 255, 255 })); try expect(ip == maxInt(u32)); } @@ -154,7 +154,7 @@ test "bitcast generates a temporary value" { if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; var y = @as(u16, 0x55AA); - const x = @bitCast(u16, @bitCast([2]u8, y)); + const x = @as(u16, @bitCast(@as([2]u8, @bitCast(y)))); try expect(y == x); } @@ -175,7 +175,7 @@ test "@bitCast packed structs at runtime and comptime" { const S = struct { fn doTheTest() !void { var full = Full{ .number = 0x1234 }; - var two_halves = @bitCast(Divided, full); + var two_halves = @as(Divided, @bitCast(full)); try expect(two_halves.half1 == 0x34); try expect(two_halves.quarter3 == 0x2); try expect(two_halves.quarter4 == 0x1); @@ -200,7 +200,7 @@ test "@bitCast extern structs at runtime and comptime" { const S = struct { fn doTheTest() !void { var full = Full{ .number = 0x1234 }; - var two_halves = @bitCast(TwoHalves, full); + var two_halves = @as(TwoHalves, @bitCast(full)); switch (native_endian) { .Big => { try expect(two_halves.half1 == 0x12); @@ -230,8 +230,8 @@ test "bitcast packed struct to integer and back" { const S = struct { fn doTheTest() !void { var move = LevelUpMove{ .move_id = 1, .level = 2 }; - var v = @bitCast(u16, move); - var back_to_a_move = @bitCast(LevelUpMove, v); + var v = @as(u16, @bitCast(move)); + var back_to_a_move = @as(LevelUpMove, @bitCast(v)); try expect(back_to_a_move.move_id == 1); try expect(back_to_a_move.level == 2); } @@ -250,7 +250,7 @@ test "implicit cast to error union by returning" { try expect((func(-1) catch unreachable) == maxInt(u64)); } pub fn func(sz: i64) anyerror!u64 { - return @bitCast(u64, sz); + return @as(u64, @bitCast(sz)); } }; try S.entry(); @@ -261,7 +261,7 @@ test "bitcast packed struct literal to byte" { const Foo = packed struct { value: u8, }; - const casted = @bitCast(u8, Foo{ .value = 0xF }); + const casted = @as(u8, @bitCast(Foo{ .value = 0xF })); try expect(casted == 0xf); } @@ -269,7 +269,7 @@ test "comptime bitcast used in expression has the correct type" { const Foo = packed struct { value: u8, }; - try expect(@bitCast(u8, Foo{ .value = 0xF }) == 0xf); + try expect(@as(u8, @bitCast(Foo{ .value = 0xF })) == 0xf); } test "bitcast passed as tuple element" { @@ -279,7 +279,7 @@ test "bitcast passed as tuple element" { try expect(args[0] == 12.34); } }; - try S.foo(.{@bitCast(f32, @as(u32, 0x414570A4))}); + try S.foo(.{@as(f32, @bitCast(@as(u32, 0x414570A4)))}); } test "triple level result location with bitcast sandwich passed as tuple element" { @@ -289,7 +289,7 @@ test "triple level result location with bitcast sandwich passed as tuple element try expect(args[0] > 12.33 and args[0] < 12.35); } }; - try S.foo(.{@as(f64, @bitCast(f32, @as(u32, 0x414570A4)))}); + try S.foo(.{@as(f64, @as(f32, @bitCast(@as(u32, 0x414570A4))))}); } test "@bitCast packed struct of floats" { @@ -318,7 +318,7 @@ test "@bitCast packed struct of floats" { const S = struct { fn doTheTest() !void { var foo = Foo{}; - var v = @bitCast(Foo2, foo); + var v = @as(Foo2, @bitCast(foo)); try expect(v.a == foo.a); try expect(v.b == foo.b); try expect(v.c == foo.c); @@ -360,12 +360,12 @@ test "comptime @bitCast packed struct to int and back" { // S -> Int var s: S = .{}; - try expectEqual(@bitCast(Int, s), comptime @bitCast(Int, S{})); + try expectEqual(@as(Int, @bitCast(s)), comptime @as(Int, @bitCast(S{}))); // Int -> S var i: Int = 0; - const rt_cast = @bitCast(S, i); - const ct_cast = comptime @bitCast(S, @as(Int, 0)); + const rt_cast = @as(S, @bitCast(i)); + const ct_cast = comptime @as(S, @bitCast(@as(Int, 0))); inline for (@typeInfo(S).Struct.fields) |field| { try expectEqual(@field(rt_cast, field.name), @field(ct_cast, field.name)); } @@ -381,10 +381,10 @@ test "comptime bitcast with fields following f80" { const FloatT = extern struct { f: f80, x: u128 align(16) }; const x: FloatT = .{ .f = 0.5, .x = 123 }; - var x_as_uint: u256 = comptime @bitCast(u256, x); + var x_as_uint: u256 = comptime @as(u256, @bitCast(x)); - try expect(x.f == @bitCast(FloatT, x_as_uint).f); - try expect(x.x == @bitCast(FloatT, x_as_uint).x); + try expect(x.f == @as(FloatT, @bitCast(x_as_uint)).f); + try expect(x.x == @as(FloatT, @bitCast(x_as_uint)).x); } test "bitcast vector to integer and back" { @@ -398,20 +398,20 @@ test "bitcast vector to integer and back" { const arr: [16]bool = [_]bool{ true, false } ++ [_]bool{true} ** 14; var x = @splat(16, true); x[1] = false; - try expect(@bitCast(u16, x) == comptime @bitCast(u16, @as(@Vector(16, bool), arr))); + try expect(@as(u16, @bitCast(x)) == comptime @as(u16, @bitCast(@as(@Vector(16, bool), arr)))); } fn bitCastWrapper16(x: f16) u16 { - return @bitCast(u16, x); + return @as(u16, @bitCast(x)); } fn bitCastWrapper32(x: f32) u32 { - return @bitCast(u32, x); + return @as(u32, @bitCast(x)); } fn bitCastWrapper64(x: f64) u64 { - return @bitCast(u64, x); + return @as(u64, @bitCast(x)); } fn bitCastWrapper128(x: f128) u128 { - return @bitCast(u128, x); + return @as(u128, @bitCast(x)); } test "bitcast nan float does modify signaling bit" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO @@ -425,37 +425,37 @@ test "bitcast nan float does modify signaling bit" { // 16 bit const snan_f16_const = math.nan_f16; - try expectEqual(math.nan_u16, @bitCast(u16, snan_f16_const)); + try expectEqual(math.nan_u16, @as(u16, @bitCast(snan_f16_const))); try expectEqual(math.nan_u16, bitCastWrapper16(snan_f16_const)); var snan_f16_var = math.nan_f16; - try expectEqual(math.nan_u16, @bitCast(u16, snan_f16_var)); + try expectEqual(math.nan_u16, @as(u16, @bitCast(snan_f16_var))); try expectEqual(math.nan_u16, bitCastWrapper16(snan_f16_var)); // 32 bit const snan_f32_const = math.nan_f32; - try expectEqual(math.nan_u32, @bitCast(u32, snan_f32_const)); + try expectEqual(math.nan_u32, @as(u32, @bitCast(snan_f32_const))); try expectEqual(math.nan_u32, bitCastWrapper32(snan_f32_const)); var snan_f32_var = math.nan_f32; - try expectEqual(math.nan_u32, @bitCast(u32, snan_f32_var)); + try expectEqual(math.nan_u32, @as(u32, @bitCast(snan_f32_var))); try expectEqual(math.nan_u32, bitCastWrapper32(snan_f32_var)); // 64 bit const snan_f64_const = math.nan_f64; - try expectEqual(math.nan_u64, @bitCast(u64, snan_f64_const)); + try expectEqual(math.nan_u64, @as(u64, @bitCast(snan_f64_const))); try expectEqual(math.nan_u64, bitCastWrapper64(snan_f64_const)); var snan_f64_var = math.nan_f64; - try expectEqual(math.nan_u64, @bitCast(u64, snan_f64_var)); + try expectEqual(math.nan_u64, @as(u64, @bitCast(snan_f64_var))); try expectEqual(math.nan_u64, bitCastWrapper64(snan_f64_var)); // 128 bit const snan_f128_const = math.nan_f128; - try expectEqual(math.nan_u128, @bitCast(u128, snan_f128_const)); + try expectEqual(math.nan_u128, @as(u128, @bitCast(snan_f128_const))); try expectEqual(math.nan_u128, bitCastWrapper128(snan_f128_const)); var snan_f128_var = math.nan_f128; - try expectEqual(math.nan_u128, @bitCast(u128, snan_f128_var)); + try expectEqual(math.nan_u128, @as(u128, @bitCast(snan_f128_var))); try expectEqual(math.nan_u128, bitCastWrapper128(snan_f128_var)); } diff --git a/test/behavior/bitreverse.zig b/test/behavior/bitreverse.zig index e19a560a9d..722edef25e 100644 --- a/test/behavior/bitreverse.zig +++ b/test/behavior/bitreverse.zig @@ -62,20 +62,20 @@ fn testBitReverse() !void { // using comptime_ints, signed, positive try expect(@bitReverse(@as(u8, 0)) == 0); - try expect(@bitReverse(@bitCast(i8, @as(u8, 0x92))) == @bitCast(i8, @as(u8, 0x49))); - try expect(@bitReverse(@bitCast(i16, @as(u16, 0x1234))) == @bitCast(i16, @as(u16, 0x2c48))); - try expect(@bitReverse(@bitCast(i24, @as(u24, 0x123456))) == @bitCast(i24, @as(u24, 0x6a2c48))); - try expect(@bitReverse(@bitCast(i24, @as(u24, 0x12345f))) == @bitCast(i24, @as(u24, 0xfa2c48))); - try expect(@bitReverse(@bitCast(i24, @as(u24, 0xf23456))) == @bitCast(i24, @as(u24, 0x6a2c4f))); - try expect(@bitReverse(@bitCast(i32, @as(u32, 0x12345678))) == @bitCast(i32, @as(u32, 0x1e6a2c48))); - try expect(@bitReverse(@bitCast(i32, @as(u32, 0xf2345678))) == @bitCast(i32, @as(u32, 0x1e6a2c4f))); - try expect(@bitReverse(@bitCast(i32, @as(u32, 0x1234567f))) == @bitCast(i32, @as(u32, 0xfe6a2c48))); - try expect(@bitReverse(@bitCast(i40, @as(u40, 0x123456789a))) == @bitCast(i40, @as(u40, 0x591e6a2c48))); - try expect(@bitReverse(@bitCast(i48, @as(u48, 0x123456789abc))) == @bitCast(i48, @as(u48, 0x3d591e6a2c48))); - try expect(@bitReverse(@bitCast(i56, @as(u56, 0x123456789abcde))) == @bitCast(i56, @as(u56, 0x7b3d591e6a2c48))); - try expect(@bitReverse(@bitCast(i64, @as(u64, 0x123456789abcdef1))) == @bitCast(i64, @as(u64, 0x8f7b3d591e6a2c48))); - try expect(@bitReverse(@bitCast(i96, @as(u96, 0x123456789abcdef111213141))) == @bitCast(i96, @as(u96, 0x828c84888f7b3d591e6a2c48))); - try expect(@bitReverse(@bitCast(i128, @as(u128, 0x123456789abcdef11121314151617181))) == @bitCast(i128, @as(u128, 0x818e868a828c84888f7b3d591e6a2c48))); + try expect(@bitReverse(@as(i8, @bitCast(@as(u8, 0x92)))) == @as(i8, @bitCast(@as(u8, 0x49)))); + try expect(@bitReverse(@as(i16, @bitCast(@as(u16, 0x1234)))) == @as(i16, @bitCast(@as(u16, 0x2c48)))); + try expect(@bitReverse(@as(i24, @bitCast(@as(u24, 0x123456)))) == @as(i24, @bitCast(@as(u24, 0x6a2c48)))); + try expect(@bitReverse(@as(i24, @bitCast(@as(u24, 0x12345f)))) == @as(i24, @bitCast(@as(u24, 0xfa2c48)))); + try expect(@bitReverse(@as(i24, @bitCast(@as(u24, 0xf23456)))) == @as(i24, @bitCast(@as(u24, 0x6a2c4f)))); + try expect(@bitReverse(@as(i32, @bitCast(@as(u32, 0x12345678)))) == @as(i32, @bitCast(@as(u32, 0x1e6a2c48)))); + try expect(@bitReverse(@as(i32, @bitCast(@as(u32, 0xf2345678)))) == @as(i32, @bitCast(@as(u32, 0x1e6a2c4f)))); + try expect(@bitReverse(@as(i32, @bitCast(@as(u32, 0x1234567f)))) == @as(i32, @bitCast(@as(u32, 0xfe6a2c48)))); + try expect(@bitReverse(@as(i40, @bitCast(@as(u40, 0x123456789a)))) == @as(i40, @bitCast(@as(u40, 0x591e6a2c48)))); + try expect(@bitReverse(@as(i48, @bitCast(@as(u48, 0x123456789abc)))) == @as(i48, @bitCast(@as(u48, 0x3d591e6a2c48)))); + try expect(@bitReverse(@as(i56, @bitCast(@as(u56, 0x123456789abcde)))) == @as(i56, @bitCast(@as(u56, 0x7b3d591e6a2c48)))); + try expect(@bitReverse(@as(i64, @bitCast(@as(u64, 0x123456789abcdef1)))) == @as(i64, @bitCast(@as(u64, 0x8f7b3d591e6a2c48)))); + try expect(@bitReverse(@as(i96, @bitCast(@as(u96, 0x123456789abcdef111213141)))) == @as(i96, @bitCast(@as(u96, 0x828c84888f7b3d591e6a2c48)))); + try expect(@bitReverse(@as(i128, @bitCast(@as(u128, 0x123456789abcdef11121314151617181)))) == @as(i128, @bitCast(@as(u128, 0x818e868a828c84888f7b3d591e6a2c48)))); // using signed, negative. Compare to runtime ints returned from llvm. var neg8: i8 = -18; diff --git a/test/behavior/bool.zig b/test/behavior/bool.zig index 50a098c111..5d09e5f8a0 100644 --- a/test/behavior/bool.zig +++ b/test/behavior/bool.zig @@ -15,8 +15,8 @@ test "cast bool to int" { const f = false; try expectEqual(@as(u32, 1), @intFromBool(t)); try expectEqual(@as(u32, 0), @intFromBool(f)); - try expectEqual(-1, @bitCast(i1, @intFromBool(t))); - try expectEqual(0, @bitCast(i1, @intFromBool(f))); + try expectEqual(-1, @as(i1, @bitCast(@intFromBool(t)))); + try expectEqual(0, @as(i1, @bitCast(@intFromBool(f)))); try expectEqual(u1, @TypeOf(@intFromBool(t))); try expectEqual(u1, @TypeOf(@intFromBool(f))); try nonConstCastIntFromBool(t, f); @@ -25,8 +25,8 @@ test "cast bool to int" { fn nonConstCastIntFromBool(t: bool, f: bool) !void { try expectEqual(@as(u32, 1), @intFromBool(t)); try expectEqual(@as(u32, 0), @intFromBool(f)); - try expectEqual(@as(i1, -1), @bitCast(i1, @intFromBool(t))); - try expectEqual(@as(i1, 0), @bitCast(i1, @intFromBool(f))); + try expectEqual(@as(i1, -1), @as(i1, @bitCast(@intFromBool(t)))); + try expectEqual(@as(i1, 0), @as(i1, @bitCast(@intFromBool(f)))); try expectEqual(u1, @TypeOf(@intFromBool(t))); try expectEqual(u1, @TypeOf(@intFromBool(f))); } diff --git a/test/behavior/bugs/11995.zig b/test/behavior/bugs/11995.zig index 0ee8e56214..fe554bc4bf 100644 --- a/test/behavior/bugs/11995.zig +++ b/test/behavior/bugs/11995.zig @@ -25,7 +25,7 @@ test { if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; var string: [5]u8 = "hello".*; - const arg_data = wuffs_base__slice_u8{ .ptr = @ptrCast([*c]u8, &string), .len = string.len }; + const arg_data = wuffs_base__slice_u8{ .ptr = @as([*c]u8, @ptrCast(&string)), .len = string.len }; var arg_meta = wuffs_base__io_buffer_meta{ .wi = 1, .ri = 2, .pos = 3, .closed = true }; wuffs_base__make_io_buffer(arg_data, &arg_meta); try std.testing.expectEqualStrings("wello", arg_data.ptr[0..arg_data.len]); diff --git a/test/behavior/bugs/12051.zig b/test/behavior/bugs/12051.zig index 5509ab97cd..342e851b77 100644 --- a/test/behavior/bugs/12051.zig +++ b/test/behavior/bugs/12051.zig @@ -30,8 +30,8 @@ const Y = struct { return .{ .a = 0, .b = false, - .c = @bitCast(Z, @as(u32, 0)), - .d = @bitCast(Z, @as(u32, 0)), + .c = @as(Z, @bitCast(@as(u32, 0))), + .d = @as(Z, @bitCast(@as(u32, 0))), }; } }; diff --git a/test/behavior/bugs/12119.zig b/test/behavior/bugs/12119.zig index bb5167a3da..6cfb015eb0 100644 --- a/test/behavior/bugs/12119.zig +++ b/test/behavior/bugs/12119.zig @@ -12,6 +12,6 @@ test { if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const zerox32: u8x32 = [_]u8{0} ** 32; - const bigsum: u32x8 = @bitCast(u32x8, zerox32); + const bigsum: u32x8 = @as(u32x8, @bitCast(zerox32)); try std.testing.expectEqual(0, @reduce(.Add, bigsum)); } diff --git a/test/behavior/bugs/12450.zig b/test/behavior/bugs/12450.zig index db91529051..5ab6565f3c 100644 --- a/test/behavior/bugs/12450.zig +++ b/test/behavior/bugs/12450.zig @@ -16,7 +16,7 @@ test { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; - var f1: *align(16) Foo = @alignCast(16, @ptrCast(*align(1) Foo, &buffer[0])); + var f1: *align(16) Foo = @alignCast(@as(*align(1) Foo, @ptrCast(&buffer[0]))); try expect(@typeInfo(@TypeOf(f1)).Pointer.alignment == 16); try expect(@intFromPtr(f1) == @intFromPtr(&f1.a)); try expect(@typeInfo(@TypeOf(&f1.a)).Pointer.alignment == 16); diff --git a/test/behavior/bugs/12723.zig b/test/behavior/bugs/12723.zig index abecf89025..955cc11c11 100644 --- a/test/behavior/bugs/12723.zig +++ b/test/behavior/bugs/12723.zig @@ -3,6 +3,6 @@ const expect = @import("std").testing.expect; test "Non-exhaustive enum backed by comptime_int" { const E = enum(comptime_int) { a, b, c, _ }; comptime var e: E = .a; - e = @enumFromInt(E, 378089457309184723749); + e = @as(E, @enumFromInt(378089457309184723749)); try expect(@intFromEnum(e) == 378089457309184723749); } diff --git a/test/behavior/bugs/13664.zig b/test/behavior/bugs/13664.zig index 34f6e9110b..b0ea3f70af 100644 --- a/test/behavior/bugs/13664.zig +++ b/test/behavior/bugs/13664.zig @@ -21,7 +21,7 @@ test { const timestamp: i64 = value(); const id = ID{ .fields = Fields{ - .timestamp = @intCast(u50, timestamp), + .timestamp = @as(u50, @intCast(timestamp)), .random_bits = 420, } }; try std.testing.expect((ID{ .value = id.value }).fields.timestamp == timestamp); diff --git a/test/behavior/bugs/421.zig b/test/behavior/bugs/421.zig index 1ed4a66738..f92bfb9899 100644 --- a/test/behavior/bugs/421.zig +++ b/test/behavior/bugs/421.zig @@ -16,6 +16,6 @@ fn testBitCastArray() !void { } fn extractOne64(a: u128) u64 { - const x = @bitCast([2]u64, a); + const x = @as([2]u64, @bitCast(a)); return x[1]; } diff --git a/test/behavior/bugs/6781.zig b/test/behavior/bugs/6781.zig index 2f5d7a3807..aac0c31a11 100644 --- a/test/behavior/bugs/6781.zig +++ b/test/behavior/bugs/6781.zig @@ -23,7 +23,7 @@ pub const JournalHeader = packed struct { var target: [32]u8 = undefined; std.crypto.hash.Blake3.hash(entry[checksum_offset + checksum_size ..], target[0..], .{}); - return @bitCast(u128, target[0..checksum_size].*); + return @as(u128, @bitCast(target[0..checksum_size].*)); } pub fn calculate_hash_chain_root(self: *const JournalHeader) u128 { @@ -42,16 +42,16 @@ pub const JournalHeader = packed struct { assert(prev_hash_chain_root_offset + prev_hash_chain_root_size == checksum_offset); - const header = @bitCast([@sizeOf(JournalHeader)]u8, self.*); + const header = @as([@sizeOf(JournalHeader)]u8, @bitCast(self.*)); const source = header[prev_hash_chain_root_offset .. checksum_offset + checksum_size]; assert(source.len == prev_hash_chain_root_size + checksum_size); var target: [32]u8 = undefined; std.crypto.hash.Blake3.hash(source, target[0..], .{}); if (segfault) { - return @bitCast(u128, target[0..hash_chain_root_size].*); + return @as(u128, @bitCast(target[0..hash_chain_root_size].*)); } else { var array = target[0..hash_chain_root_size].*; - return @bitCast(u128, array); + return @as(u128, @bitCast(array)); } } diff --git a/test/behavior/bugs/718.zig b/test/behavior/bugs/718.zig index b0f0d1ec52..0dad101e4b 100644 --- a/test/behavior/bugs/718.zig +++ b/test/behavior/bugs/718.zig @@ -15,7 +15,7 @@ test "zero keys with @memset" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; - @memset(@ptrCast([*]u8, &keys)[0..@sizeOf(@TypeOf(keys))], 0); + @memset(@as([*]u8, @ptrCast(&keys))[0..@sizeOf(@TypeOf(keys))], 0); try expect(!keys.up); try expect(!keys.down); try expect(!keys.left); diff --git a/test/behavior/bugs/726.zig b/test/behavior/bugs/726.zig index 0cd8abc1cf..37e8d31cc9 100644 --- a/test/behavior/bugs/726.zig +++ b/test/behavior/bugs/726.zig @@ -8,7 +8,7 @@ test "@ptrCast from const to nullable" { if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const c: u8 = 4; - var x: ?*const u8 = @ptrCast(?*const u8, &c); + var x: ?*const u8 = @as(?*const u8, @ptrCast(&c)); try expect(x.?.* == 4); } @@ -21,6 +21,6 @@ test "@ptrCast from var in empty struct to nullable" { const container = struct { var c: u8 = 4; }; - var x: ?*const u8 = @ptrCast(?*const u8, &container.c); + var x: ?*const u8 = @as(?*const u8, @ptrCast(&container.c)); try expect(x.?.* == 4); } diff --git a/test/behavior/builtin_functions_returning_void_or_noreturn.zig b/test/behavior/builtin_functions_returning_void_or_noreturn.zig index ae369c4e9d..1eb2ef3049 100644 --- a/test/behavior/builtin_functions_returning_void_or_noreturn.zig +++ b/test/behavior/builtin_functions_returning_void_or_noreturn.zig @@ -17,8 +17,8 @@ test { try testing.expectEqual(void, @TypeOf(@breakpoint())); try testing.expectEqual({}, @export(x, .{ .name = "x" })); try testing.expectEqual({}, @fence(.Acquire)); - try testing.expectEqual({}, @memcpy(@ptrFromInt([*]u8, 1)[0..0], @ptrFromInt([*]u8, 1)[0..0])); - try testing.expectEqual({}, @memset(@ptrFromInt([*]u8, 1)[0..0], undefined)); + try testing.expectEqual({}, @memcpy(@as([*]u8, @ptrFromInt(1))[0..0], @as([*]u8, @ptrFromInt(1))[0..0])); + try testing.expectEqual({}, @memset(@as([*]u8, @ptrFromInt(1))[0..0], undefined)); try testing.expectEqual(noreturn, @TypeOf(if (true) @panic("") else {})); try testing.expectEqual({}, @prefetch(&val, .{})); try testing.expectEqual({}, @setAlignStack(16)); diff --git a/test/behavior/byteswap.zig b/test/behavior/byteswap.zig index 8d28285d27..ce33834ffa 100644 --- a/test/behavior/byteswap.zig +++ b/test/behavior/byteswap.zig @@ -16,13 +16,13 @@ test "@byteSwap integers" { try t(u8, 0x12, 0x12); try t(u16, 0x1234, 0x3412); try t(u24, 0x123456, 0x563412); - try t(i24, @bitCast(i24, @as(u24, 0xf23456)), 0x5634f2); - try t(i24, 0x1234f6, @bitCast(i24, @as(u24, 0xf63412))); + try t(i24, @as(i24, @bitCast(@as(u24, 0xf23456))), 0x5634f2); + try t(i24, 0x1234f6, @as(i24, @bitCast(@as(u24, 0xf63412)))); try t(u32, 0x12345678, 0x78563412); - try t(i32, @bitCast(i32, @as(u32, 0xf2345678)), 0x785634f2); - try t(i32, 0x123456f8, @bitCast(i32, @as(u32, 0xf8563412))); + try t(i32, @as(i32, @bitCast(@as(u32, 0xf2345678))), 0x785634f2); + try t(i32, 0x123456f8, @as(i32, @bitCast(@as(u32, 0xf8563412)))); try t(u40, 0x123456789a, 0x9a78563412); - try t(i48, 0x123456789abc, @bitCast(i48, @as(u48, 0xbc9a78563412))); + try t(i48, 0x123456789abc, @as(i48, @bitCast(@as(u48, 0xbc9a78563412)))); try t(u56, 0x123456789abcde, 0xdebc9a78563412); try t(u64, 0x123456789abcdef1, 0xf1debc9a78563412); try t(u88, 0x123456789abcdef1112131, 0x312111f1debc9a78563412); @@ -31,19 +31,19 @@ test "@byteSwap integers" { try t(u0, @as(u0, 0), 0); try t(i8, @as(i8, -50), -50); - try t(i16, @bitCast(i16, @as(u16, 0x1234)), @bitCast(i16, @as(u16, 0x3412))); - try t(i24, @bitCast(i24, @as(u24, 0x123456)), @bitCast(i24, @as(u24, 0x563412))); - try t(i32, @bitCast(i32, @as(u32, 0x12345678)), @bitCast(i32, @as(u32, 0x78563412))); - try t(u40, @bitCast(i40, @as(u40, 0x123456789a)), @as(u40, 0x9a78563412)); - try t(i48, @bitCast(i48, @as(u48, 0x123456789abc)), @bitCast(i48, @as(u48, 0xbc9a78563412))); - try t(i56, @bitCast(i56, @as(u56, 0x123456789abcde)), @bitCast(i56, @as(u56, 0xdebc9a78563412))); - try t(i64, @bitCast(i64, @as(u64, 0x123456789abcdef1)), @bitCast(i64, @as(u64, 0xf1debc9a78563412))); - try t(i88, @bitCast(i88, @as(u88, 0x123456789abcdef1112131)), @bitCast(i88, @as(u88, 0x312111f1debc9a78563412))); - try t(i96, @bitCast(i96, @as(u96, 0x123456789abcdef111213141)), @bitCast(i96, @as(u96, 0x41312111f1debc9a78563412))); + try t(i16, @as(i16, @bitCast(@as(u16, 0x1234))), @as(i16, @bitCast(@as(u16, 0x3412)))); + try t(i24, @as(i24, @bitCast(@as(u24, 0x123456))), @as(i24, @bitCast(@as(u24, 0x563412)))); + try t(i32, @as(i32, @bitCast(@as(u32, 0x12345678))), @as(i32, @bitCast(@as(u32, 0x78563412)))); + try t(u40, @as(i40, @bitCast(@as(u40, 0x123456789a))), @as(u40, 0x9a78563412)); + try t(i48, @as(i48, @bitCast(@as(u48, 0x123456789abc))), @as(i48, @bitCast(@as(u48, 0xbc9a78563412)))); + try t(i56, @as(i56, @bitCast(@as(u56, 0x123456789abcde))), @as(i56, @bitCast(@as(u56, 0xdebc9a78563412)))); + try t(i64, @as(i64, @bitCast(@as(u64, 0x123456789abcdef1))), @as(i64, @bitCast(@as(u64, 0xf1debc9a78563412)))); + try t(i88, @as(i88, @bitCast(@as(u88, 0x123456789abcdef1112131))), @as(i88, @bitCast(@as(u88, 0x312111f1debc9a78563412)))); + try t(i96, @as(i96, @bitCast(@as(u96, 0x123456789abcdef111213141))), @as(i96, @bitCast(@as(u96, 0x41312111f1debc9a78563412)))); try t( i128, - @bitCast(i128, @as(u128, 0x123456789abcdef11121314151617181)), - @bitCast(i128, @as(u128, 0x8171615141312111f1debc9a78563412)), + @as(i128, @bitCast(@as(u128, 0x123456789abcdef11121314151617181))), + @as(i128, @bitCast(@as(u128, 0x8171615141312111f1debc9a78563412))), ); } fn t(comptime I: type, input: I, expected_output: I) !void { diff --git a/test/behavior/call.zig b/test/behavior/call.zig index 627df37e9b..633f5e9c3f 100644 --- a/test/behavior/call.zig +++ b/test/behavior/call.zig @@ -368,7 +368,7 @@ test "Enum constructed by @Type passed as generic argument" { } }; inline for (@typeInfo(S.E).Enum.fields, 0..) |_, i| { - try S.foo(@enumFromInt(S.E, i), i); + try S.foo(@as(S.E, @enumFromInt(i)), i); } } diff --git a/test/behavior/cast.zig b/test/behavior/cast.zig index d51d864ea1..e6aa53bd41 100644 --- a/test/behavior/cast.zig +++ b/test/behavior/cast.zig @@ -10,13 +10,13 @@ const native_endian = builtin.target.cpu.arch.endian(); test "int to ptr cast" { const x = @as(usize, 13); - const y = @ptrFromInt(*u8, x); + const y = @as(*u8, @ptrFromInt(x)); const z = @intFromPtr(y); try expect(z == 13); } test "integer literal to pointer cast" { - const vga_mem = @ptrFromInt(*u16, 0xB8000); + const vga_mem = @as(*u16, @ptrFromInt(0xB8000)); try expect(@intFromPtr(vga_mem) == 0xB8000); } @@ -52,7 +52,7 @@ fn testResolveUndefWithInt(b: bool, x: i32) !void { } test "@intCast to comptime_int" { - try expect(@intCast(comptime_int, 0) == 0); + try expect(@as(comptime_int, @intCast(0)) == 0); } test "implicit cast comptime numbers to any type when the value fits" { @@ -68,29 +68,29 @@ test "implicit cast comptime_int to comptime_float" { test "comptime_int @floatFromInt" { { - const result = @floatFromInt(f16, 1234); + const result = @as(f16, @floatFromInt(1234)); try expect(@TypeOf(result) == f16); try expect(result == 1234.0); } { - const result = @floatFromInt(f32, 1234); + const result = @as(f32, @floatFromInt(1234)); try expect(@TypeOf(result) == f32); try expect(result == 1234.0); } { - const result = @floatFromInt(f64, 1234); + const result = @as(f64, @floatFromInt(1234)); try expect(@TypeOf(result) == f64); try expect(result == 1234.0); } { - const result = @floatFromInt(f128, 1234); + const result = @as(f128, @floatFromInt(1234)); try expect(@TypeOf(result) == f128); try expect(result == 1234.0); } // big comptime_int (> 64 bits) to f128 conversion { - const result = @floatFromInt(f128, 0x1_0000_0000_0000_0000); + const result = @as(f128, @floatFromInt(0x1_0000_0000_0000_0000)); try expect(@TypeOf(result) == f128); try expect(result == 0x1_0000_0000_0000_0000.0); } @@ -107,8 +107,8 @@ test "@floatFromInt" { } fn testIntToFloat(k: i32) !void { - const f = @floatFromInt(f32, k); - const i = @intFromFloat(i32, f); + const f = @as(f32, @floatFromInt(k)); + const i = @as(i32, @intFromFloat(f)); try expect(i == k); } }; @@ -131,8 +131,8 @@ test "@floatFromInt(f80)" { fn testIntToFloat(comptime Int: type, k: Int) !void { @setRuntimeSafety(false); // TODO - const f = @floatFromInt(f80, k); - const i = @intFromFloat(Int, f); + const f = @as(f80, @floatFromInt(k)); + const i = @as(Int, @intFromFloat(f)); try expect(i == k); } }; @@ -165,7 +165,7 @@ test "@intFromFloat" { fn testIntFromFloats() !void { const x = @as(i32, 1e4); try expect(x == 10000); - const y = @intFromFloat(i32, @as(f32, 1e4)); + const y = @as(i32, @intFromFloat(@as(f32, 1e4))); try expect(y == 10000); try expectIntFromFloat(f32, 255.1, u8, 255); try expectIntFromFloat(f32, 127.2, i8, 127); @@ -173,7 +173,7 @@ fn testIntFromFloats() !void { } fn expectIntFromFloat(comptime F: type, f: F, comptime I: type, i: I) !void { - try expect(@intFromFloat(I, f) == i); + try expect(@as(I, @intFromFloat(f)) == i); } test "implicitly cast indirect pointer to maybe-indirect pointer" { @@ -208,29 +208,29 @@ test "implicitly cast indirect pointer to maybe-indirect pointer" { } test "@intCast comptime_int" { - const result = @intCast(i32, 1234); + const result = @as(i32, @intCast(1234)); try expect(@TypeOf(result) == i32); try expect(result == 1234); } test "@floatCast comptime_int and comptime_float" { { - const result = @floatCast(f16, 1234); + const result = @as(f16, @floatCast(1234)); try expect(@TypeOf(result) == f16); try expect(result == 1234.0); } { - const result = @floatCast(f16, 1234.0); + const result = @as(f16, @floatCast(1234.0)); try expect(@TypeOf(result) == f16); try expect(result == 1234.0); } { - const result = @floatCast(f32, 1234); + const result = @as(f32, @floatCast(1234)); try expect(@TypeOf(result) == f32); try expect(result == 1234.0); } { - const result = @floatCast(f32, 1234.0); + const result = @as(f32, @floatCast(1234.0)); try expect(@TypeOf(result) == f32); try expect(result == 1234.0); } @@ -276,21 +276,21 @@ test "*usize to *void" { if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; var i = @as(usize, 0); - var v = @ptrCast(*void, &i); + var v = @as(*void, @ptrCast(&i)); v.* = {}; } test "@enumFromInt passed a comptime_int to an enum with one item" { const E = enum { A }; - const x = @enumFromInt(E, 0); + const x = @as(E, @enumFromInt(0)); try expect(x == E.A); } test "@intCast to u0 and use the result" { const S = struct { fn doTheTest(zero: u1, one: u1, bigzero: i32) !void { - try expect((one << @intCast(u0, bigzero)) == 1); - try expect((zero << @intCast(u0, bigzero)) == 0); + try expect((one << @as(u0, @intCast(bigzero))) == 1); + try expect((zero << @as(u0, @intCast(bigzero))) == 0); } }; try S.doTheTest(0, 1, 0); @@ -605,7 +605,7 @@ test "cast *[1][*]const u8 to [*]const ?[*]const u8" { const window_name = [1][*]const u8{"window name"}; const x: [*]const ?[*]const u8 = &window_name; - try expect(mem.eql(u8, std.mem.sliceTo(@ptrCast([*:0]const u8, x[0].?), 0), "window name")); + try expect(mem.eql(u8, std.mem.sliceTo(@as([*:0]const u8, @ptrCast(x[0].?)), 0), "window name")); } test "vector casts" { @@ -625,9 +625,9 @@ test "vector casts" { var up3 = @as(@Vector(2, u64), up0); // Downcast (safety-checked) var down0 = up3; - var down1 = @intCast(@Vector(2, u32), down0); - var down2 = @intCast(@Vector(2, u16), down0); - var down3 = @intCast(@Vector(2, u8), down0); + var down1 = @as(@Vector(2, u32), @intCast(down0)); + var down2 = @as(@Vector(2, u16), @intCast(down0)); + var down3 = @as(@Vector(2, u8), @intCast(down0)); try expect(mem.eql(u16, &@as([2]u16, up1), &[2]u16{ 0x55, 0xaa })); try expect(mem.eql(u32, &@as([2]u32, up2), &[2]u32{ 0x55, 0xaa })); @@ -660,12 +660,12 @@ test "@floatCast cast down" { { var double: f64 = 0.001534; - var single = @floatCast(f32, double); + var single = @as(f32, @floatCast(double)); try expect(single == 0.001534); } { const double: f64 = 0.001534; - const single = @floatCast(f32, double); + const single = @as(f32, @floatCast(double)); try expect(single == 0.001534); } } @@ -1041,7 +1041,7 @@ test "cast between C pointer with different but compatible types" { } fn doTheTest() !void { var x = [_]u16{ 4, 2, 1, 3 }; - try expect(foo(@ptrCast([*]u16, &x)) == 4); + try expect(foo(@as([*]u16, @ptrCast(&x))) == 4); } }; try S.doTheTest(); @@ -1093,10 +1093,10 @@ test "peer type resolve array pointer and unknown pointer" { test "comptime float casts" { if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; - const a = @floatFromInt(comptime_float, 1); + const a = @as(comptime_float, @floatFromInt(1)); try expect(a == 1); try expect(@TypeOf(a) == comptime_float); - const b = @intFromFloat(comptime_int, 2); + const b = @as(comptime_int, @intFromFloat(2)); try expect(b == 2); try expect(@TypeOf(b) == comptime_int); @@ -1111,7 +1111,7 @@ test "pointer reinterpret const float to int" { // The hex representation is 0x3fe3333333333303. const float: f64 = 5.99999999999994648725e-01; const float_ptr = &float; - const int_ptr = @ptrCast(*const i32, float_ptr); + const int_ptr = @as(*const i32, @ptrCast(float_ptr)); const int_val = int_ptr.*; if (native_endian == .Little) try expect(int_val == 0x33333303) @@ -1134,7 +1134,7 @@ test "implicit cast from [*]T to ?*anyopaque" { fn incrementVoidPtrArray(array: ?*anyopaque, len: usize) void { var n: usize = 0; while (n < len) : (n += 1) { - @ptrCast([*]u8, array.?)[n] += 1; + @as([*]u8, @ptrCast(array.?))[n] += 1; } } @@ -1146,7 +1146,7 @@ test "compile time int to ptr of function" { // On some architectures function pointers must be aligned. const hardcoded_fn_addr = maxInt(usize) & ~@as(usize, 0xf); -pub const FUNCTION_CONSTANT = @ptrFromInt(PFN_void, hardcoded_fn_addr); +pub const FUNCTION_CONSTANT = @as(PFN_void, @ptrFromInt(hardcoded_fn_addr)); pub const PFN_void = *const fn (*anyopaque) callconv(.C) void; fn foobar(func: PFN_void) !void { @@ -1161,10 +1161,10 @@ test "implicit ptr to *anyopaque" { var a: u32 = 1; var ptr: *align(@alignOf(u32)) anyopaque = &a; - var b: *u32 = @ptrCast(*u32, ptr); + var b: *u32 = @as(*u32, @ptrCast(ptr)); try expect(b.* == 1); var ptr2: ?*align(@alignOf(u32)) anyopaque = &a; - var c: *u32 = @ptrCast(*u32, ptr2.?); + var c: *u32 = @as(*u32, @ptrCast(ptr2.?)); try expect(c.* == 1); } @@ -1235,11 +1235,11 @@ fn testCast128() !void { } fn cast128Int(x: f128) u128 { - return @bitCast(u128, x); + return @as(u128, @bitCast(x)); } fn cast128Float(x: u128) f128 { - return @bitCast(f128, x); + return @as(f128, @bitCast(x)); } test "implicit cast from *[N]T to ?[*]T" { @@ -1270,7 +1270,7 @@ test "implicit cast from *T to ?*anyopaque" { } fn incrementVoidPtrValue(value: ?*anyopaque) void { - @ptrCast(*u8, value.?).* += 1; + @as(*u8, @ptrCast(value.?)).* += 1; } test "implicit cast *[0]T to E![]const u8" { @@ -1284,11 +1284,11 @@ test "implicit cast *[0]T to E![]const u8" { var global_array: [4]u8 = undefined; test "cast from array reference to fn: comptime fn ptr" { - const f = @ptrCast(*align(1) const fn () callconv(.C) void, &global_array); + const f = @as(*align(1) const fn () callconv(.C) void, @ptrCast(&global_array)); try expect(@intFromPtr(f) == @intFromPtr(&global_array)); } test "cast from array reference to fn: runtime fn ptr" { - var f = @ptrCast(*align(1) const fn () callconv(.C) void, &global_array); + var f = @as(*align(1) const fn () callconv(.C) void, @ptrCast(&global_array)); try expect(@intFromPtr(f) == @intFromPtr(&global_array)); } @@ -1337,7 +1337,7 @@ test "assignment to optional pointer result loc" { if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; var foo: struct { ptr: ?*anyopaque } = .{ .ptr = &global_struct }; - try expect(foo.ptr.? == @ptrCast(*anyopaque, &global_struct)); + try expect(foo.ptr.? == @as(*anyopaque, @ptrCast(&global_struct))); } test "cast between *[N]void and []void" { @@ -1393,9 +1393,9 @@ test "cast f128 to narrower types" { const S = struct { fn doTheTest() !void { var x: f128 = 1234.0; - try expect(@as(f16, 1234.0) == @floatCast(f16, x)); - try expect(@as(f32, 1234.0) == @floatCast(f32, x)); - try expect(@as(f64, 1234.0) == @floatCast(f64, x)); + try expect(@as(f16, 1234.0) == @as(f16, @floatCast(x))); + try expect(@as(f32, 1234.0) == @as(f32, @floatCast(x))); + try expect(@as(f64, 1234.0) == @as(f64, @floatCast(x))); } }; try S.doTheTest(); @@ -1500,8 +1500,8 @@ test "coerce between pointers of compatible differently-named floats" { } test "peer type resolution of const and non-const pointer to array" { - const a = @ptrFromInt(*[1024]u8, 42); - const b = @ptrFromInt(*const [1024]u8, 42); + const a = @as(*[1024]u8, @ptrFromInt(42)); + const b = @as(*const [1024]u8, @ptrFromInt(42)); try std.testing.expect(@TypeOf(a, b) == *const [1024]u8); try std.testing.expect(a == b); } @@ -1512,7 +1512,7 @@ test "intFromFloat to zero-bit int" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO const a: f32 = 0.0; - try comptime std.testing.expect(@intFromFloat(u0, a) == 0); + try comptime std.testing.expect(@as(u0, @intFromFloat(a)) == 0); } test "peer type resolution of function pointer and function body" { @@ -1547,10 +1547,10 @@ test "bitcast packed struct with u0" { if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO const S = packed struct(u2) { a: u0, b: u2 }; - const s = @bitCast(S, @as(u2, 2)); + const s = @as(S, @bitCast(@as(u2, 2))); try expect(s.a == 0); try expect(s.b == 2); - const i = @bitCast(u2, s); + const i = @as(u2, @bitCast(s)); try expect(i == 2); } @@ -1560,7 +1560,7 @@ test "optional pointer coerced to optional allowzero pointer" { var p: ?*u32 = undefined; var q: ?*allowzero u32 = undefined; - p = @ptrFromInt(*u32, 4); + p = @as(*u32, @ptrFromInt(4)); q = p; try expect(@intFromPtr(q.?) == 4); } @@ -1583,7 +1583,7 @@ test "peer type resolution forms error union" { 0 => unreachable, 42 => error.AccessDenied, else => unreachable, - } else @intCast(u32, foo); + } else @as(u32, @intCast(foo)); try expect(try result == 123); } @@ -1623,8 +1623,8 @@ test "peer type resolution: const sentinel slice and mutable non-sentinel slice" const S = struct { fn doTheTest(comptime T: type, comptime s: T) !void { - var a: [:s]const T = @ptrFromInt(*const [2:s]T, 0x1000); - var b: []T = @ptrFromInt(*[3]T, 0x2000); + var a: [:s]const T = @as(*const [2:s]T, @ptrFromInt(0x1000)); + var b: []T = @as(*[3]T, @ptrFromInt(0x2000)); comptime assert(@TypeOf(a, b) == []const T); comptime assert(@TypeOf(b, a) == []const T); @@ -1634,8 +1634,8 @@ test "peer type resolution: const sentinel slice and mutable non-sentinel slice" const R = @TypeOf(r1); - try expectEqual(@as(R, @ptrFromInt(*const [2:s]T, 0x1000)), r1); - try expectEqual(@as(R, @ptrFromInt(*const [3]T, 0x2000)), r2); + try expectEqual(@as(R, @as(*const [2:s]T, @ptrFromInt(0x1000))), r1); + try expectEqual(@as(R, @as(*const [3]T, @ptrFromInt(0x2000))), r2); } }; @@ -1815,7 +1815,7 @@ test "peer type resolution: three-way resolution combines error set and optional const E = error{Foo}; var a: E = error.Foo; - var b: *const [5:0]u8 = @ptrFromInt(*const [5:0]u8, 0x1000); + var b: *const [5:0]u8 = @as(*const [5:0]u8, @ptrFromInt(0x1000)); var c: ?[*:0]u8 = null; comptime assert(@TypeOf(a, b, c) == E!?[*:0]const u8); comptime assert(@TypeOf(a, c, b) == E!?[*:0]const u8); @@ -1844,7 +1844,7 @@ test "peer type resolution: three-way resolution combines error set and optional const T = @TypeOf(r1); try expectEqual(@as(T, error.Foo), r1); - try expectEqual(@as(T, @ptrFromInt([*:0]u8, 0x1000)), r2); + try expectEqual(@as(T, @as([*:0]u8, @ptrFromInt(0x1000))), r2); try expectEqual(@as(T, null), r3); } @@ -2114,7 +2114,7 @@ test "peer type resolution: many compatible pointers" { 4 => "foo-4", else => unreachable, }; - try expectEqualSlices(u8, expected, std.mem.span(@ptrCast([*:0]const u8, r))); + try expectEqualSlices(u8, expected, std.mem.span(@as([*:0]const u8, @ptrCast(r)))); } } diff --git a/test/behavior/cast_int.zig b/test/behavior/cast_int.zig index 041ee193e8..6d4f530409 100644 --- a/test/behavior/cast_int.zig +++ b/test/behavior/cast_int.zig @@ -11,6 +11,6 @@ test "@intCast i32 to u7" { var x: u128 = maxInt(u128); var y: i32 = 120; - var z = x >> @intCast(u7, y); + var z = x >> @as(u7, @intCast(y)); try expect(z == 0xff); } diff --git a/test/behavior/comptime_memory.zig b/test/behavior/comptime_memory.zig index d327afb783..b0c5e9c91e 100644 --- a/test/behavior/comptime_memory.zig +++ b/test/behavior/comptime_memory.zig @@ -6,7 +6,7 @@ const ptr_size = @sizeOf(usize); test "type pun signed and unsigned as single pointer" { comptime { var x: u32 = 0; - const y = @ptrCast(*i32, &x); + const y = @as(*i32, @ptrCast(&x)); y.* = -1; try testing.expectEqual(@as(u32, 0xFFFFFFFF), x); } @@ -15,7 +15,7 @@ test "type pun signed and unsigned as single pointer" { test "type pun signed and unsigned as many pointer" { comptime { var x: u32 = 0; - const y = @ptrCast([*]i32, &x); + const y = @as([*]i32, @ptrCast(&x)); y[0] = -1; try testing.expectEqual(@as(u32, 0xFFFFFFFF), x); } @@ -24,7 +24,7 @@ test "type pun signed and unsigned as many pointer" { test "type pun signed and unsigned as array pointer" { comptime { var x: u32 = 0; - const y = @ptrCast(*[1]i32, &x); + const y = @as(*[1]i32, @ptrCast(&x)); y[0] = -1; try testing.expectEqual(@as(u32, 0xFFFFFFFF), x); } @@ -38,7 +38,7 @@ test "type pun signed and unsigned as offset many pointer" { comptime { var x: u32 = 0; - var y = @ptrCast([*]i32, &x); + var y = @as([*]i32, @ptrCast(&x)); y -= 10; y[10] = -1; try testing.expectEqual(@as(u32, 0xFFFFFFFF), x); @@ -53,7 +53,7 @@ test "type pun signed and unsigned as array pointer with pointer arithemtic" { comptime { var x: u32 = 0; - const y = @ptrCast([*]i32, &x) - 10; + const y = @as([*]i32, @ptrCast(&x)) - 10; const z: *[15]i32 = y[0..15]; z[10] = -1; try testing.expectEqual(@as(u32, 0xFFFFFFFF), x); @@ -64,9 +64,9 @@ test "type pun value and struct" { comptime { const StructOfU32 = extern struct { x: u32 }; var inst: StructOfU32 = .{ .x = 0 }; - @ptrCast(*i32, &inst.x).* = -1; + @as(*i32, @ptrCast(&inst.x)).* = -1; try testing.expectEqual(@as(u32, 0xFFFFFFFF), inst.x); - @ptrCast(*i32, &inst).* = -2; + @as(*i32, @ptrCast(&inst)).* = -2; try testing.expectEqual(@as(u32, 0xFFFFFFFE), inst.x); } } @@ -81,8 +81,8 @@ test "type pun endianness" { comptime { const StructOfBytes = extern struct { x: [4]u8 }; var inst: StructOfBytes = .{ .x = [4]u8{ 0, 0, 0, 0 } }; - const structPtr = @ptrCast(*align(1) u32, &inst); - const arrayPtr = @ptrCast(*align(1) u32, &inst.x); + const structPtr = @as(*align(1) u32, @ptrCast(&inst)); + const arrayPtr = @as(*align(1) u32, @ptrCast(&inst.x)); inst.x[0] = 0xFE; inst.x[2] = 0xBE; try testing.expectEqual(bigToNativeEndian(u32, 0xFE00BE00), structPtr.*); @@ -124,8 +124,8 @@ fn shuffle(ptr: usize, comptime From: type, comptime To: type) usize { @compileError("Mismatched sizes! " ++ @typeName(From) ++ " and " ++ @typeName(To) ++ " must have the same size!"); const array_len = @divExact(ptr_size, @sizeOf(From)); var result: usize = 0; - const pSource = @ptrCast(*align(1) const [array_len]From, &ptr); - const pResult = @ptrCast(*align(1) [array_len]To, &result); + const pSource = @as(*align(1) const [array_len]From, @ptrCast(&ptr)); + const pResult = @as(*align(1) [array_len]To, @ptrCast(&result)); var i: usize = 0; while (i < array_len) : (i += 1) { inline for (@typeInfo(To).Struct.fields) |f| { @@ -136,8 +136,8 @@ fn shuffle(ptr: usize, comptime From: type, comptime To: type) usize { } fn doTypePunBitsTest(as_bits: *Bits) !void { - const as_u32 = @ptrCast(*align(1) u32, as_bits); - const as_bytes = @ptrCast(*[4]u8, as_bits); + const as_u32 = @as(*align(1) u32, @ptrCast(as_bits)); + const as_bytes = @as(*[4]u8, @ptrCast(as_bits)); as_u32.* = bigToNativeEndian(u32, 0xB0A7DEED); try testing.expectEqual(@as(u1, 0x00), as_bits.p0); try testing.expectEqual(@as(u4, 0x08), as_bits.p1); @@ -176,7 +176,7 @@ test "type pun bits" { comptime { var v: u32 = undefined; - try doTypePunBitsTest(@ptrCast(*Bits, &v)); + try doTypePunBitsTest(@as(*Bits, @ptrCast(&v))); } } @@ -194,7 +194,7 @@ test "basic pointer preservation" { comptime { const lazy_address = @intFromPtr(&imports.global_u32); try testing.expectEqual(@intFromPtr(&imports.global_u32), lazy_address); - try testing.expectEqual(&imports.global_u32, @ptrFromInt(*u32, lazy_address)); + try testing.expectEqual(&imports.global_u32, @as(*u32, @ptrFromInt(lazy_address))); } } @@ -207,8 +207,8 @@ test "byte copy preserves linker value" { const ct_value = comptime blk: { const lazy = &imports.global_u32; var result: *u32 = undefined; - const pSource = @ptrCast(*const [ptr_size]u8, &lazy); - const pResult = @ptrCast(*[ptr_size]u8, &result); + const pSource = @as(*const [ptr_size]u8, @ptrCast(&lazy)); + const pResult = @as(*[ptr_size]u8, @ptrCast(&result)); var i: usize = 0; while (i < ptr_size) : (i += 1) { pResult[i] = pSource[i]; @@ -230,8 +230,8 @@ test "unordered byte copy preserves linker value" { const ct_value = comptime blk: { const lazy = &imports.global_u32; var result: *u32 = undefined; - const pSource = @ptrCast(*const [ptr_size]u8, &lazy); - const pResult = @ptrCast(*[ptr_size]u8, &result); + const pSource = @as(*const [ptr_size]u8, @ptrCast(&lazy)); + const pResult = @as(*[ptr_size]u8, @ptrCast(&result)); if (ptr_size > 8) @compileError("This array needs to be expanded for platform with very big pointers"); const shuffled_indices = [_]usize{ 4, 5, 2, 6, 1, 3, 0, 7 }; for (shuffled_indices) |i| { @@ -274,12 +274,12 @@ test "dance on linker values" { arr[0] = @intFromPtr(&imports.global_u32); arr[1] = @intFromPtr(&imports.global_u32); - const weird_ptr = @ptrCast([*]Bits, @ptrCast([*]u8, &arr) + @sizeOf(usize) - 3); + const weird_ptr = @as([*]Bits, @ptrCast(@as([*]u8, @ptrCast(&arr)) + @sizeOf(usize) - 3)); try doTypePunBitsTest(&weird_ptr[0]); if (ptr_size > @sizeOf(Bits)) try doTypePunBitsTest(&weird_ptr[1]); - var arr_bytes = @ptrCast(*[2][ptr_size]u8, &arr); + var arr_bytes = @as(*[2][ptr_size]u8, @ptrCast(&arr)); var rebuilt_bytes: [ptr_size]u8 = undefined; var i: usize = 0; @@ -290,7 +290,7 @@ test "dance on linker values" { rebuilt_bytes[i] = arr_bytes[1][i]; } - try testing.expectEqual(&imports.global_u32, @ptrFromInt(*u32, @bitCast(usize, rebuilt_bytes))); + try testing.expectEqual(&imports.global_u32, @as(*u32, @ptrFromInt(@as(usize, @bitCast(rebuilt_bytes))))); } } @@ -316,7 +316,7 @@ test "offset array ptr by element size" { try testing.expectEqual(@intFromPtr(&arr[2]), address + 2 * @sizeOf(VirtualStruct)); try testing.expectEqual(@intFromPtr(&arr[3]), address + @sizeOf(VirtualStruct) * 3); - const secondElement = @ptrFromInt(*VirtualStruct, @intFromPtr(&arr[0]) + 2 * @sizeOf(VirtualStruct)); + const secondElement = @as(*VirtualStruct, @ptrFromInt(@intFromPtr(&arr[0]) + 2 * @sizeOf(VirtualStruct))); try testing.expectEqual(bigToNativeEndian(u32, 0x02060a0e), secondElement.x); } } @@ -334,15 +334,15 @@ test "offset instance by field size" { var ptr = @intFromPtr(&inst); ptr -= 4; ptr += @offsetOf(VirtualStruct, "x"); - try testing.expectEqual(@as(u32, 0), @ptrFromInt([*]u32, ptr)[1]); + try testing.expectEqual(@as(u32, 0), @as([*]u32, @ptrFromInt(ptr))[1]); ptr -= @offsetOf(VirtualStruct, "x"); ptr += @offsetOf(VirtualStruct, "y"); - try testing.expectEqual(@as(u32, 1), @ptrFromInt([*]u32, ptr)[1]); + try testing.expectEqual(@as(u32, 1), @as([*]u32, @ptrFromInt(ptr))[1]); ptr = ptr - @offsetOf(VirtualStruct, "y") + @offsetOf(VirtualStruct, "z"); - try testing.expectEqual(@as(u32, 2), @ptrFromInt([*]u32, ptr)[1]); + try testing.expectEqual(@as(u32, 2), @as([*]u32, @ptrFromInt(ptr))[1]); ptr = @intFromPtr(&inst.z) - 4 - @offsetOf(VirtualStruct, "z"); ptr += @offsetOf(VirtualStruct, "w"); - try testing.expectEqual(@as(u32, 3), @ptrFromInt(*u32, ptr + 4).*); + try testing.expectEqual(@as(u32, 3), @as(*u32, @ptrFromInt(ptr + 4)).*); } } @@ -363,13 +363,13 @@ test "offset field ptr by enclosing array element size" { var i: usize = 0; while (i < 4) : (i += 1) { - var ptr: [*]u8 = @ptrCast([*]u8, &arr[0]); + var ptr: [*]u8 = @as([*]u8, @ptrCast(&arr[0])); ptr += i; ptr += @offsetOf(VirtualStruct, "x"); var j: usize = 0; while (j < 4) : (j += 1) { const base = ptr + j * @sizeOf(VirtualStruct); - try testing.expectEqual(@intCast(u8, i * 4 + j), base[0]); + try testing.expectEqual(@as(u8, @intCast(i * 4 + j)), base[0]); } } } @@ -393,7 +393,7 @@ test "accessing reinterpreted memory of parent object" { .c = 2.6, }; const ptr = &x.b[0]; - const b = @ptrCast([*c]const u8, ptr)[5]; + const b = @as([*c]const u8, @ptrCast(ptr))[5]; try testing.expect(b == expected); } } @@ -407,11 +407,11 @@ test "bitcast packed union to integer" { comptime { const a = U{ .x = 1 }; const b = U{ .y = 2 }; - const cast_a = @bitCast(u2, a); - const cast_b = @bitCast(u2, b); + const cast_a = @as(u2, @bitCast(a)); + const cast_b = @as(u2, @bitCast(b)); // truncated because the upper bit is garbage memory that we don't care about - try testing.expectEqual(@as(u1, 1), @truncate(u1, cast_a)); + try testing.expectEqual(@as(u1, 1), @as(u1, @truncate(cast_a))); try testing.expectEqual(@as(u2, 2), cast_b); } } @@ -435,6 +435,6 @@ test "dereference undefined pointer to zero-bit type" { test "type pun extern struct" { const S = extern struct { f: u8 }; comptime var s = S{ .f = 123 }; - @ptrCast(*u8, &s).* = 72; + @as(*u8, @ptrCast(&s)).* = 72; try testing.expectEqual(@as(u8, 72), s.f); } diff --git a/test/behavior/enum.zig b/test/behavior/enum.zig index 1076f5e3ea..ffb254f765 100644 --- a/test/behavior/enum.zig +++ b/test/behavior/enum.zig @@ -20,7 +20,7 @@ test "enum to int" { } fn testIntToEnumEval(x: i32) !void { - try expect(@enumFromInt(IntToEnumNumber, x) == IntToEnumNumber.Three); + try expect(@as(IntToEnumNumber, @enumFromInt(x)) == IntToEnumNumber.Three); } const IntToEnumNumber = enum { Zero, One, Two, Three, Four }; @@ -629,7 +629,7 @@ test "non-exhaustive enum" { .b => true, _ => false, }); - e = @enumFromInt(E, 12); + e = @as(E, @enumFromInt(12)); try expect(switch (e) { .a => false, .b => false, @@ -648,9 +648,9 @@ test "non-exhaustive enum" { }); try expect(@typeInfo(E).Enum.fields.len == 2); - e = @enumFromInt(E, 12); + e = @as(E, @enumFromInt(12)); try expect(@intFromEnum(e) == 12); - e = @enumFromInt(E, y); + e = @as(E, @enumFromInt(y)); try expect(@intFromEnum(e) == 52); try expect(@typeInfo(E).Enum.is_exhaustive == false); } @@ -666,7 +666,7 @@ test "empty non-exhaustive enum" { const E = enum(u8) { _ }; fn doTheTest(y: u8) !void { - var e = @enumFromInt(E, y); + var e = @as(E, @enumFromInt(y)); try expect(switch (e) { _ => true, }); @@ -693,7 +693,7 @@ test "single field non-exhaustive enum" { .a => true, _ => false, }); - e = @enumFromInt(E, 12); + e = @as(E, @enumFromInt(12)); try expect(switch (e) { .a => false, _ => true, @@ -709,7 +709,7 @@ test "single field non-exhaustive enum" { else => false, }); - try expect(@intFromEnum(@enumFromInt(E, y)) == y); + try expect(@intFromEnum(@as(E, @enumFromInt(y))) == y); try expect(@typeInfo(E).Enum.fields.len == 1); try expect(@typeInfo(E).Enum.is_exhaustive == false); } @@ -741,8 +741,8 @@ const MultipleChoice2 = enum(u32) { }; test "cast integer literal to enum" { - try expect(@enumFromInt(MultipleChoice2, 0) == MultipleChoice2.Unspecified1); - try expect(@enumFromInt(MultipleChoice2, 40) == MultipleChoice2.B); + try expect(@as(MultipleChoice2, @enumFromInt(0)) == MultipleChoice2.Unspecified1); + try expect(@as(MultipleChoice2, @enumFromInt(40)) == MultipleChoice2.B); } test "enum with specified and unspecified tag values" { @@ -1155,7 +1155,7 @@ test "size of enum with only one tag which has explicit integer tag type" { var s1: S1 = undefined; s1.e = .nope; try expect(s1.e == .nope); - const ptr = @ptrCast(*u8, &s1); + const ptr = @as(*u8, @ptrCast(&s1)); try expect(ptr.* == 10); var s0: S0 = undefined; @@ -1183,7 +1183,7 @@ test "Non-exhaustive enum with nonstandard int size behaves correctly" { test "runtime int to enum with one possible value" { const E = enum { one }; var runtime: usize = 0; - if (@enumFromInt(E, runtime) != .one) { + if (@as(E, @enumFromInt(runtime)) != .one) { @compileError("test failed"); } } @@ -1194,7 +1194,7 @@ test "enum tag from a local variable" { return enum(Inner) { _ }; } }; - const i = @enumFromInt(S.Int(u32), 0); + const i = @as(S.Int(u32), @enumFromInt(0)); try std.testing.expect(@intFromEnum(i) == 0); } @@ -1203,12 +1203,12 @@ test "auto-numbered enum with signed tag type" { try std.testing.expectEqual(@as(i32, 0), @intFromEnum(E.a)); try std.testing.expectEqual(@as(i32, 1), @intFromEnum(E.b)); - try std.testing.expectEqual(E.a, @enumFromInt(E, 0)); - try std.testing.expectEqual(E.b, @enumFromInt(E, 1)); - try std.testing.expectEqual(E.a, @enumFromInt(E, @as(i32, 0))); - try std.testing.expectEqual(E.b, @enumFromInt(E, @as(i32, 1))); - try std.testing.expectEqual(E.a, @enumFromInt(E, @as(u32, 0))); - try std.testing.expectEqual(E.b, @enumFromInt(E, @as(u32, 1))); + try std.testing.expectEqual(E.a, @as(E, @enumFromInt(0))); + try std.testing.expectEqual(E.b, @as(E, @enumFromInt(1))); + try std.testing.expectEqual(E.a, @as(E, @enumFromInt(@as(i32, 0)))); + try std.testing.expectEqual(E.b, @as(E, @enumFromInt(@as(i32, 1)))); + try std.testing.expectEqual(E.a, @as(E, @enumFromInt(@as(u32, 0)))); + try std.testing.expectEqual(E.b, @as(E, @enumFromInt(@as(u32, 1)))); try std.testing.expectEqualStrings("a", @tagName(E.a)); try std.testing.expectEqualStrings("b", @tagName(E.b)); } diff --git a/test/behavior/error.zig b/test/behavior/error.zig index 14b0eca030..06062ac66c 100644 --- a/test/behavior/error.zig +++ b/test/behavior/error.zig @@ -234,9 +234,9 @@ const Set1 = error{ A, B }; const Set2 = error{ A, C }; fn testExplicitErrorSetCast(set1: Set1) !void { - var x = @errSetCast(Set2, set1); + var x = @as(Set2, @errSetCast(set1)); try expect(@TypeOf(x) == Set2); - var y = @errSetCast(Set1, x); + var y = @as(Set1, @errSetCast(x)); try expect(@TypeOf(y) == Set1); try expect(y == error.A); } diff --git a/test/behavior/eval.zig b/test/behavior/eval.zig index 85dc5e29b5..f2b91e66ac 100644 --- a/test/behavior/eval.zig +++ b/test/behavior/eval.zig @@ -9,7 +9,7 @@ test "compile time recursion" { try expect(some_data.len == 21); } -var some_data: [@intCast(usize, fibonacci(7))]u8 = undefined; +var some_data: [@as(usize, @intCast(fibonacci(7)))]u8 = undefined; fn fibonacci(x: i32) i32 { if (x <= 1) return 1; return fibonacci(x - 1) + fibonacci(x - 2); @@ -123,7 +123,7 @@ fn fnWithSetRuntimeSafety() i32 { test "compile-time downcast when the bits fit" { comptime { const spartan_count: u16 = 255; - const byte = @intCast(u8, spartan_count); + const byte = @as(u8, @intCast(spartan_count)); try expect(byte == 255); } } @@ -149,7 +149,7 @@ test "a type constructed in a global expression" { l.array[0] = 10; l.array[1] = 11; l.array[2] = 12; - const ptr = @ptrCast([*]u8, &l.array); + const ptr = @as([*]u8, @ptrCast(&l.array)); try expect(ptr[0] == 10); try expect(ptr[1] == 11); try expect(ptr[2] == 12); @@ -332,7 +332,7 @@ fn generateTable(comptime T: type) [1010]T { var res: [1010]T = undefined; var i: usize = 0; while (i < 1010) : (i += 1) { - res[i] = @intCast(T, i); + res[i] = @as(T, @intCast(i)); } return res; } @@ -460,7 +460,7 @@ test "binary math operator in partially inlined function" { var b: [16]u8 = undefined; for (&b, 0..) |*r, i| - r.* = @intCast(u8, i + 1); + r.* = @as(u8, @intCast(i + 1)); copyWithPartialInline(s[0..], b[0..]); try expect(s[0] == 0x1020304); @@ -942,7 +942,7 @@ test "comptime pointer load through elem_ptr" { .x = i, }; } - var ptr = @ptrCast([*]S, &array); + var ptr = @as([*]S, @ptrCast(&array)); var x = ptr[0].x; assert(x == 0); ptr += 1; @@ -1281,9 +1281,9 @@ test "comptime write through extern struct reinterpreted as array" { c: u8, }; var s: S = undefined; - @ptrCast(*[3]u8, &s)[0] = 1; - @ptrCast(*[3]u8, &s)[1] = 2; - @ptrCast(*[3]u8, &s)[2] = 3; + @as(*[3]u8, @ptrCast(&s))[0] = 1; + @as(*[3]u8, @ptrCast(&s))[1] = 2; + @as(*[3]u8, @ptrCast(&s))[2] = 3; assert(s.a == 1); assert(s.b == 2); assert(s.c == 3); @@ -1371,7 +1371,7 @@ test "lazy value is resolved as slice operand" { var a: [512]u64 = undefined; const ptr1 = a[0..@sizeOf(A)]; - const ptr2 = @ptrCast([*]u8, &a)[0..@sizeOf(A)]; + const ptr2 = @as([*]u8, @ptrCast(&a))[0..@sizeOf(A)]; try expect(@intFromPtr(ptr1) == @intFromPtr(ptr2)); try expect(ptr1.len == ptr2.len); } diff --git a/test/behavior/export.zig b/test/behavior/export.zig index 4928e86725..4751ccafe5 100644 --- a/test/behavior/export.zig +++ b/test/behavior/export.zig @@ -7,7 +7,7 @@ const builtin = @import("builtin"); // can't really run this test but we can make sure it has no compile error // and generates code -const vram = @ptrFromInt([*]volatile u8, 0x20000000)[0..0x8000]; +const vram = @as([*]volatile u8, @ptrFromInt(0x20000000))[0..0x8000]; export fn writeToVRam() void { vram[0] = 'X'; } diff --git a/test/behavior/floatop.zig b/test/behavior/floatop.zig index 56f3885a4a..e21645ae8f 100644 --- a/test/behavior/floatop.zig +++ b/test/behavior/floatop.zig @@ -94,7 +94,7 @@ test "negative f128 intFromFloat at compile-time" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO const a: f128 = -2; - var b = @intFromFloat(i64, a); + var b = @as(i64, @intFromFloat(a)); try expect(@as(i64, -2) == b); } @@ -387,11 +387,11 @@ fn testLog() !void { } { var a: f32 = e; - try expect(@log(a) == 1 or @log(a) == @bitCast(f32, @as(u32, 0x3f7fffff))); + try expect(@log(a) == 1 or @log(a) == @as(f32, @bitCast(@as(u32, 0x3f7fffff)))); } { var a: f64 = e; - try expect(@log(a) == 1 or @log(a) == @bitCast(f64, @as(u64, 0x3ff0000000000000))); + try expect(@log(a) == 1 or @log(a) == @as(f64, @bitCast(@as(u64, 0x3ff0000000000000)))); } inline for ([_]type{ f16, f32, f64 }) |ty| { const eps = epsForType(ty); diff --git a/test/behavior/fn.zig b/test/behavior/fn.zig index 6c7e127964..e7b7e63e33 100644 --- a/test/behavior/fn.zig +++ b/test/behavior/fn.zig @@ -326,7 +326,7 @@ test "function pointers" { &fn4, }; for (fns, 0..) |f, i| { - try expect(f() == @intCast(u32, i) + 5); + try expect(f() == @as(u32, @intCast(i)) + 5); } } fn fn1() u32 { @@ -512,8 +512,8 @@ test "using @ptrCast on function pointers" { fn run() !void { const a = A{ .data = "abcd".* }; - const casted_fn = @ptrCast(*const fn (*const anyopaque, usize) *const u8, &at); - const casted_impl = @ptrCast(*const anyopaque, &a); + const casted_fn = @as(*const fn (*const anyopaque, usize) *const u8, @ptrCast(&at)); + const casted_impl = @as(*const anyopaque, @ptrCast(&a)); const ptr = casted_fn(casted_impl, 2); try expect(ptr.* == 'c'); } @@ -575,7 +575,7 @@ test "lazy values passed to anytype parameter" { try B.foo(.{ .x = @sizeOf(B) }); const C = struct {}; - try expect(@truncate(u32, @sizeOf(C)) == 0); + try expect(@as(u32, @truncate(@sizeOf(C))) == 0); const D = struct {}; try expect(@sizeOf(D) << 1 == 0); diff --git a/test/behavior/fn_in_struct_in_comptime.zig b/test/behavior/fn_in_struct_in_comptime.zig index b31b377c04..0acadbc5ea 100644 --- a/test/behavior/fn_in_struct_in_comptime.zig +++ b/test/behavior/fn_in_struct_in_comptime.zig @@ -14,5 +14,5 @@ fn get_foo() fn (*u8) usize { test "define a function in an anonymous struct in comptime" { const foo = get_foo(); - try expect(foo(@ptrFromInt(*u8, 12345)) == 12345); + try expect(foo(@as(*u8, @ptrFromInt(12345))) == 12345); } diff --git a/test/behavior/for.zig b/test/behavior/for.zig index 12b82c44a4..f751d35d96 100644 --- a/test/behavior/for.zig +++ b/test/behavior/for.zig @@ -84,7 +84,7 @@ test "basic for loop" { } for (array, 0..) |item, index| { _ = item; - buffer[buf_index] = @intCast(u8, index); + buffer[buf_index] = @as(u8, @intCast(index)); buf_index += 1; } const array_ptr = &array; @@ -94,7 +94,7 @@ test "basic for loop" { } for (array_ptr, 0..) |item, index| { _ = item; - buffer[buf_index] = @intCast(u8, index); + buffer[buf_index] = @as(u8, @intCast(index)); buf_index += 1; } const unknown_size: []const u8 = &array; @@ -103,7 +103,7 @@ test "basic for loop" { buf_index += 1; } for (unknown_size, 0..) |_, index| { - buffer[buf_index] = @intCast(u8, index); + buffer[buf_index] = @as(u8, @intCast(index)); buf_index += 1; } @@ -208,7 +208,7 @@ 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]; + var ptr = @as([*]allowzero const u8, @ptrCast(slice.ptr))[0..slice.len]; for (ptr, 0..) |x, i| try expect(x == i + 1); for (ptr, 0..) |*x, i| try expect(x.* == i + 1); } @@ -393,7 +393,7 @@ test "raw pointer and counter" { const ptr: [*]u8 = &buf; for (ptr, 0..4) |*a, b| { - a.* = @intCast(u8, 'A' + b); + a.* = @as(u8, @intCast('A' + b)); } try expect(buf[0] == 'A'); diff --git a/test/behavior/generics.zig b/test/behavior/generics.zig index f0c8516f67..7d4a841a62 100644 --- a/test/behavior/generics.zig +++ b/test/behavior/generics.zig @@ -97,7 +97,7 @@ test "type constructed by comptime function call" { l.array[0] = 10; l.array[1] = 11; l.array[2] = 12; - const ptr = @ptrCast([*]u8, &l.array); + const ptr = @as([*]u8, @ptrCast(&l.array)); try expect(ptr[0] == 10); try expect(ptr[1] == 11); try expect(ptr[2] == 12); @@ -171,7 +171,7 @@ fn getByte(ptr: ?*const u8) u8 { return ptr.?.*; } fn getFirstByte(comptime T: type, mem: []const T) u8 { - return getByte(@ptrCast(*const u8, &mem[0])); + return getByte(@as(*const u8, @ptrCast(&mem[0]))); } test "generic fn keeps non-generic parameter types" { @@ -428,7 +428,7 @@ test "null sentinel pointer passed as generic argument" { try std.testing.expect(@intFromPtr(a) == 8); } }; - try S.doTheTest((@ptrFromInt([*:null]const [*c]const u8, 8))); + try S.doTheTest((@as([*:null]const [*c]const u8, @ptrFromInt(8)))); } test "generic function passed as comptime argument" { diff --git a/test/behavior/int128.zig b/test/behavior/int128.zig index 6fd2c192a2..42f0b00922 100644 --- a/test/behavior/int128.zig +++ b/test/behavior/int128.zig @@ -38,7 +38,7 @@ test "undefined 128 bit int" { var undef: u128 = undefined; var undef_signed: i128 = undefined; - try expect(undef == 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa and @bitCast(u128, undef_signed) == undef); + try expect(undef == 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa and @as(u128, @bitCast(undef_signed)) == undef); } test "int128" { @@ -49,7 +49,7 @@ test "int128" { var buff: i128 = -1; try expect(buff < 0 and (buff + 1) == 0); - try expect(@intCast(i8, buff) == @as(i8, -1)); + try expect(@as(i8, @intCast(buff)) == @as(i8, -1)); buff = minInt(i128); try expect(buff < 0); @@ -73,16 +73,16 @@ test "truncate int128" { { var buff: u128 = maxInt(u128); - try expect(@truncate(u64, buff) == maxInt(u64)); - try expect(@truncate(u90, buff) == maxInt(u90)); - try expect(@truncate(u128, buff) == maxInt(u128)); + try expect(@as(u64, @truncate(buff)) == maxInt(u64)); + try expect(@as(u90, @truncate(buff)) == maxInt(u90)); + try expect(@as(u128, @truncate(buff)) == maxInt(u128)); } { var buff: i128 = maxInt(i128); - try expect(@truncate(i64, buff) == -1); - try expect(@truncate(i90, buff) == -1); - try expect(@truncate(i128, buff) == maxInt(i128)); + try expect(@as(i64, @truncate(buff)) == -1); + try expect(@as(i90, @truncate(buff)) == -1); + try expect(@as(i128, @truncate(buff)) == maxInt(i128)); } } diff --git a/test/behavior/math.zig b/test/behavior/math.zig index 42c328c7d4..3b5d4876fd 100644 --- a/test/behavior/math.zig +++ b/test/behavior/math.zig @@ -391,11 +391,11 @@ test "binary not 128-bit" { break :x ~@as(u128, 0x55555555_55555555_55555555_55555555) == 0xaaaaaaaa_aaaaaaaa_aaaaaaaa_aaaaaaaa; }); try expect(comptime x: { - break :x ~@as(i128, 0x55555555_55555555_55555555_55555555) == @bitCast(i128, @as(u128, 0xaaaaaaaa_aaaaaaaa_aaaaaaaa_aaaaaaaa)); + break :x ~@as(i128, 0x55555555_55555555_55555555_55555555) == @as(i128, @bitCast(@as(u128, 0xaaaaaaaa_aaaaaaaa_aaaaaaaa_aaaaaaaa))); }); try testBinaryNot128(u128, 0xaaaaaaaa_aaaaaaaa_aaaaaaaa_aaaaaaaa); - try testBinaryNot128(i128, @bitCast(i128, @as(u128, 0xaaaaaaaa_aaaaaaaa_aaaaaaaa_aaaaaaaa))); + try testBinaryNot128(i128, @as(i128, @bitCast(@as(u128, 0xaaaaaaaa_aaaaaaaa_aaaaaaaa_aaaaaaaa)))); } fn testBinaryNot128(comptime Type: type, x: Type) !void { @@ -1156,29 +1156,29 @@ test "quad hex float literal parsing accurate" { // implied 1 is dropped, with an exponent of 0 (0x3fff) after biasing. const expected: u128 = 0x3fff1111222233334444555566667777; - try expect(@bitCast(u128, a) == expected); + try expect(@as(u128, @bitCast(a)) == expected); // non-normalized const b: f128 = 0x11.111222233334444555566667777p-4; - try expect(@bitCast(u128, b) == expected); + try expect(@as(u128, @bitCast(b)) == expected); const S = struct { fn doTheTest() !void { { var f: f128 = 0x1.2eab345678439abcdefea56782346p+5; - try expect(@bitCast(u128, f) == 0x40042eab345678439abcdefea5678234); + try expect(@as(u128, @bitCast(f)) == 0x40042eab345678439abcdefea5678234); } { var f: f128 = 0x1.edcb34a235253948765432134674fp-1; - try expect(@bitCast(u128, f) == 0x3ffeedcb34a235253948765432134675); // round-to-even + try expect(@as(u128, @bitCast(f)) == 0x3ffeedcb34a235253948765432134675); // round-to-even } { var f: f128 = 0x1.353e45674d89abacc3a2ebf3ff4ffp-50; - try expect(@bitCast(u128, f) == 0x3fcd353e45674d89abacc3a2ebf3ff50); + try expect(@as(u128, @bitCast(f)) == 0x3fcd353e45674d89abacc3a2ebf3ff50); } { var f: f128 = 0x1.ed8764648369535adf4be3214567fp-9; - try expect(@bitCast(u128, f) == 0x3ff6ed8764648369535adf4be3214568); + try expect(@as(u128, @bitCast(f)) == 0x3ff6ed8764648369535adf4be3214568); } const exp2ft = [_]f64{ 0x1.6a09e667f3bcdp-1, @@ -1233,7 +1233,7 @@ test "quad hex float literal parsing accurate" { }; for (exp2ft, 0..) |x, i| { - try expect(@bitCast(u64, x) == answers[i]); + try expect(@as(u64, @bitCast(x)) == answers[i]); } } }; @@ -1586,7 +1586,7 @@ test "signed zeros are represented properly" { fn testOne(comptime T: type) !void { const ST = std.meta.Int(.unsigned, @typeInfo(T).Float.bits); var as_fp_val = -@as(T, 0.0); - var as_uint_val = @bitCast(ST, as_fp_val); + var as_uint_val = @as(ST, @bitCast(as_fp_val)); // Ensure the sign bit is set. try expect(as_uint_val >> (@typeInfo(T).Float.bits - 1) == 1); } diff --git a/test/behavior/memcpy.zig b/test/behavior/memcpy.zig index 3a87b66fb1..f1776dfe57 100644 --- a/test/behavior/memcpy.zig +++ b/test/behavior/memcpy.zig @@ -59,7 +59,7 @@ fn testMemcpyDestManyPtr() !void { var str = "hello".*; var buf: [5]u8 = undefined; var len: usize = 5; - @memcpy(@ptrCast([*]u8, &buf), @ptrCast([*]const u8, &str)[0..len]); + @memcpy(@as([*]u8, @ptrCast(&buf)), @as([*]const u8, @ptrCast(&str))[0..len]); try expect(buf[0] == 'h'); try expect(buf[1] == 'e'); try expect(buf[2] == 'l'); diff --git a/test/behavior/packed-struct.zig b/test/behavior/packed-struct.zig index 037fee74ee..12cc027ef4 100644 --- a/test/behavior/packed-struct.zig +++ b/test/behavior/packed-struct.zig @@ -166,7 +166,7 @@ test "correct sizeOf and offsets in packed structs" { try expectEqual(4, @sizeOf(PStruct)); if (native_endian == .Little) { - const s1 = @bitCast(PStruct, @as(u32, 0x12345678)); + const s1 = @as(PStruct, @bitCast(@as(u32, 0x12345678))); try expectEqual(false, s1.bool_a); try expectEqual(false, s1.bool_b); try expectEqual(false, s1.bool_c); @@ -180,7 +180,7 @@ test "correct sizeOf and offsets in packed structs" { try expectEqual(@as(u10, 0b1101000101), s1.u10_a); try expectEqual(@as(u10, 0b0001001000), s1.u10_b); - const s2 = @bitCast(packed struct { x: u1, y: u7, z: u24 }, @as(u32, 0xd5c71ff4)); + const s2 = @as(packed struct { x: u1, y: u7, z: u24 }, @bitCast(@as(u32, 0xd5c71ff4))); try expectEqual(@as(u1, 0), s2.x); try expectEqual(@as(u7, 0b1111010), s2.y); try expectEqual(@as(u24, 0xd5c71f), s2.z); @@ -207,7 +207,7 @@ test "nested packed structs" { try expectEqual(24, @bitOffsetOf(S3, "y")); if (native_endian == .Little) { - const s3 = @bitCast(S3Padded, @as(u64, 0xe952d5c71ff4)).s3; + const s3 = @as(S3Padded, @bitCast(@as(u64, 0xe952d5c71ff4))).s3; try expectEqual(@as(u8, 0xf4), s3.x.a); try expectEqual(@as(u8, 0x1f), s3.x.b); try expectEqual(@as(u8, 0xc7), s3.x.c); @@ -600,7 +600,7 @@ test "packed struct initialized in bitcast" { const T = packed struct { val: u8 }; var val: u8 = 123; - const t = @bitCast(u8, T{ .val = val }); + const t = @as(u8, @bitCast(T{ .val = val })); try expect(t == val); } @@ -627,7 +627,7 @@ test "pointer to container level packed struct field" { }, var arr = [_]u32{0} ** 2; }; - @ptrCast(*S, &S.arr[0]).other_bits.enable_3 = true; + @as(*S, @ptrCast(&S.arr[0])).other_bits.enable_3 = true; try expect(S.arr[0] == 0x10000000); } diff --git a/test/behavior/packed_struct_explicit_backing_int.zig b/test/behavior/packed_struct_explicit_backing_int.zig index 62dd178fd5..9e476572ab 100644 --- a/test/behavior/packed_struct_explicit_backing_int.zig +++ b/test/behavior/packed_struct_explicit_backing_int.zig @@ -25,7 +25,7 @@ test "packed struct explicit backing integer" { try expectEqual(24, @bitOffsetOf(S3, "y")); if (native_endian == .Little) { - const s3 = @bitCast(S3Padded, @as(u64, 0xe952d5c71ff4)).s3; + const s3 = @as(S3Padded, @bitCast(@as(u64, 0xe952d5c71ff4))).s3; try expectEqual(@as(u8, 0xf4), s3.x.a); try expectEqual(@as(u8, 0x1f), s3.x.b); try expectEqual(@as(u8, 0xc7), s3.x.c); diff --git a/test/behavior/pointers.zig b/test/behavior/pointers.zig index 4e04fe580c..d007e7b480 100644 --- a/test/behavior/pointers.zig +++ b/test/behavior/pointers.zig @@ -184,8 +184,8 @@ test "implicit cast error unions with non-optional to optional pointer" { } test "compare equality of optional and non-optional pointer" { - const a = @ptrFromInt(*const usize, 0x12345678); - const b = @ptrFromInt(?*usize, 0x12345678); + const a = @as(*const usize, @ptrFromInt(0x12345678)); + const b = @as(?*usize, @ptrFromInt(0x12345678)); try expect(a == b); try expect(b == a); } @@ -197,7 +197,7 @@ test "allowzero pointer and slice" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; - var ptr = @ptrFromInt([*]allowzero i32, 0); + var ptr = @as([*]allowzero i32, @ptrFromInt(0)); var opt_ptr: ?[*]allowzero i32 = ptr; try expect(opt_ptr != null); try expect(@intFromPtr(ptr) == 0); @@ -286,9 +286,9 @@ test "null terminated pointer" { const S = struct { fn doTheTest() !void { var array_with_zero = [_:0]u8{ 'h', 'e', 'l', 'l', 'o' }; - var zero_ptr: [*:0]const u8 = @ptrCast([*:0]const u8, &array_with_zero); + var zero_ptr: [*:0]const u8 = @as([*:0]const u8, @ptrCast(&array_with_zero)); var no_zero_ptr: [*]const u8 = zero_ptr; - var zero_ptr_again = @ptrCast([*:0]const u8, no_zero_ptr); + var zero_ptr_again = @as([*:0]const u8, @ptrCast(no_zero_ptr)); try expect(std.mem.eql(u8, std.mem.sliceTo(zero_ptr_again, 0), "hello")); } }; @@ -367,7 +367,7 @@ test "pointer sentinel with +inf" { } test "pointer to array at fixed address" { - const array = @ptrFromInt(*volatile [2]u32, 0x10); + const array = @as(*volatile [2]u32, @ptrFromInt(0x10)); // Silly check just to reference `array` try expect(@intFromPtr(&array[0]) == 0x10); try expect(@intFromPtr(&array[1]) == 0x14); @@ -406,13 +406,13 @@ test "pointer arithmetic affects the alignment" { test "@intFromPtr on null optional at comptime" { { - const pointer = @ptrFromInt(?*u8, 0x000); + const pointer = @as(?*u8, @ptrFromInt(0x000)); const x = @intFromPtr(pointer); _ = x; try comptime expect(0 == @intFromPtr(pointer)); } { - const pointer = @ptrFromInt(?*u8, 0xf00); + const pointer = @as(?*u8, @ptrFromInt(0xf00)); try comptime expect(0xf00 == @intFromPtr(pointer)); } } @@ -463,8 +463,8 @@ test "element pointer arithmetic to slice" { }; const elem_ptr = &cases[0]; // *[2]i32 - const many = @ptrCast([*][2]i32, elem_ptr); - const many_elem = @ptrCast(*[2]i32, &many[1]); + const many = @as([*][2]i32, @ptrCast(elem_ptr)); + const many_elem = @as(*[2]i32, @ptrCast(&many[1])); const items: []i32 = many_elem; try testing.expect(items.len == 2); try testing.expect(items[1] == 3); @@ -512,7 +512,7 @@ test "ptrCast comptime known slice to C pointer" { if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const s: [:0]const u8 = "foo"; - var p = @ptrCast([*c]const u8, s); + var p = @as([*c]const u8, @ptrCast(s)); try std.testing.expectEqualStrings(s, std.mem.sliceTo(p, 0)); } @@ -550,7 +550,7 @@ test "pointer to array has explicit alignment" { const Base = extern struct { a: u8 }; const Base2 = extern struct { a: u8 }; fn func(ptr: *[4]Base) *align(1) [4]Base2 { - return @alignCast(1, @ptrCast(*[4]Base2, ptr)); + return @alignCast(@as(*[4]Base2, @ptrCast(ptr))); } }; var bases = [_]S.Base{.{ .a = 2 }} ** 4; diff --git a/test/behavior/popcount.zig b/test/behavior/popcount.zig index 51146b14c8..da152d4dc5 100644 --- a/test/behavior/popcount.zig +++ b/test/behavior/popcount.zig @@ -63,7 +63,7 @@ fn testPopCountIntegers() !void { try expect(@popCount(x) == 2); } comptime { - try expect(@popCount(@bitCast(u8, @as(i8, -120))) == 2); + try expect(@popCount(@as(u8, @bitCast(@as(i8, -120)))) == 2); } } diff --git a/test/behavior/ptrcast.zig b/test/behavior/ptrcast.zig index aadae132d9..3a2ec9db19 100644 --- a/test/behavior/ptrcast.zig +++ b/test/behavior/ptrcast.zig @@ -16,7 +16,7 @@ fn testReinterpretBytesAsInteger() !void { .Little => 0xab785634, .Big => 0x345678ab, }; - try expect(@ptrCast(*align(1) const u32, bytes[1..5]).* == expected); + try expect(@as(*align(1) const u32, @ptrCast(bytes[1..5])).* == expected); } test "reinterpret an array over multiple elements, with no well-defined layout" { @@ -32,7 +32,7 @@ test "reinterpret an array over multiple elements, with no well-defined layout" fn testReinterpretWithOffsetAndNoWellDefinedLayout() !void { const bytes: ?[5]?u8 = [5]?u8{ 0x12, 0x34, 0x56, 0x78, 0x9a }; const ptr = &bytes.?[1]; - const copy: [4]?u8 = @ptrCast(*const [4]?u8, ptr).*; + const copy: [4]?u8 = @as(*const [4]?u8, @ptrCast(ptr)).*; _ = copy; //try expect(@ptrCast(*align(1)?u8, bytes[1..5]).* == ); } @@ -51,7 +51,7 @@ fn testReinterpretStructWrappedBytesAsInteger() !void { .Little => 0xab785634, .Big => 0x345678ab, }; - try expect(@ptrCast(*align(1) const u32, obj.bytes[1..5]).* == expected); + try expect(@as(*align(1) const u32, @ptrCast(obj.bytes[1..5])).* == expected); } test "reinterpret bytes of an array into an extern struct" { @@ -71,7 +71,7 @@ fn testReinterpretBytesAsExternStruct() !void { c: u8, }; - var ptr = @ptrCast(*const S, &bytes); + var ptr = @as(*const S, @ptrCast(&bytes)); var val = ptr.c; try expect(val == 5); } @@ -95,7 +95,7 @@ fn testReinterpretExternStructAsExternStruct() !void { a: u32 align(2), c: u8, }; - var ptr = @ptrCast(*const S2, &bytes); + var ptr = @as(*const S2, @ptrCast(&bytes)); var val = ptr.c; try expect(val == 5); } @@ -121,7 +121,7 @@ fn testReinterpretOverAlignedExternStructAsExternStruct() !void { a2: u16, c: u8, }; - var ptr = @ptrCast(*const S2, &bytes); + var ptr = @as(*const S2, @ptrCast(&bytes)); var val = ptr.c; try expect(val == 5); } @@ -138,13 +138,13 @@ test "lower reinterpreted comptime field ptr (with under-aligned fields)" { a: u32 align(2), c: u8, }; - comptime var ptr = @ptrCast(*const S, &bytes); + comptime var ptr = @as(*const S, @ptrCast(&bytes)); var val = &ptr.c; try expect(val.* == 5); // Test lowering an elem ptr comptime var src_value = S{ .a = 15, .c = 5 }; - comptime var ptr2 = @ptrCast(*[@sizeOf(S)]u8, &src_value); + comptime var ptr2 = @as(*[@sizeOf(S)]u8, @ptrCast(&src_value)); var val2 = &ptr2[4]; try expect(val2.* == 5); } @@ -161,13 +161,13 @@ test "lower reinterpreted comptime field ptr" { a: u32, c: u8, }; - comptime var ptr = @ptrCast(*const S, &bytes); + comptime var ptr = @as(*const S, @ptrCast(&bytes)); var val = &ptr.c; try expect(val.* == 5); // Test lowering an elem ptr comptime var src_value = S{ .a = 15, .c = 5 }; - comptime var ptr2 = @ptrCast(*[@sizeOf(S)]u8, &src_value); + comptime var ptr2 = @as(*[@sizeOf(S)]u8, @ptrCast(&src_value)); var val2 = &ptr2[4]; try expect(val2.* == 5); } @@ -190,27 +190,17 @@ const Bytes = struct { pub fn init(v: u32) Bytes { var res: Bytes = undefined; - @ptrCast(*align(1) u32, &res.bytes).* = v; + @as(*align(1) u32, @ptrCast(&res.bytes)).* = v; return res; } }; -test "comptime ptrcast keeps larger alignment" { - if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO - - comptime { - const a: u32 = 1234; - const p = @ptrCast([*]const u8, &a); - try expect(@TypeOf(p) == [*]align(@alignOf(u32)) const u8); - } -} - test "ptrcast of const integer has the correct object size" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO - const is_value = ~@intCast(isize, std.math.minInt(isize)); - const is_bytes = @ptrCast([*]const u8, &is_value)[0..@sizeOf(isize)]; + const is_value = ~@as(isize, @intCast(std.math.minInt(isize))); + const is_bytes = @as([*]const u8, @ptrCast(&is_value))[0..@sizeOf(isize)]; if (@sizeOf(isize) == 8) { switch (native_endian) { .Little => { @@ -248,7 +238,7 @@ test "implicit optional pointer to optional anyopaque pointer" { var buf: [4]u8 = "aoeu".*; var x: ?[*]u8 = &buf; var y: ?*anyopaque = x; - var z = @ptrCast(*[4]u8, y); + var z = @as(*[4]u8, @ptrCast(y)); try expect(std.mem.eql(u8, z, "aoeu")); } @@ -260,7 +250,7 @@ test "@ptrCast slice to slice" { const S = struct { fn foo(slice: []u32) []i32 { - return @ptrCast([]i32, slice); + return @as([]i32, @ptrCast(slice)); } }; var buf: [4]u32 = .{ 0, 0, 0, 0 }; @@ -277,7 +267,7 @@ test "comptime @ptrCast a subset of an array, then write through it" { comptime { var buff: [16]u8 align(4) = undefined; - const len_bytes = @ptrCast(*u32, &buff); + const len_bytes = @as(*u32, @ptrCast(&buff)); len_bytes.* = 16; std.mem.copy(u8, buff[4..], "abcdef"); } @@ -286,7 +276,7 @@ test "comptime @ptrCast a subset of an array, then write through it" { test "@ptrCast undefined value at comptime" { const S = struct { fn transmute(comptime T: type, comptime U: type, value: T) U { - return @ptrCast(*const U, &value).*; + return @as(*const U, @ptrCast(&value)).*; } }; comptime { diff --git a/test/behavior/ptrfromint.zig b/test/behavior/ptrfromint.zig index c07a6df834..72244aa7d1 100644 --- a/test/behavior/ptrfromint.zig +++ b/test/behavior/ptrfromint.zig @@ -9,7 +9,7 @@ test "casting integer address to function pointer" { fn addressToFunction() void { var addr: usize = 0xdeadbee0; - _ = @ptrFromInt(*const fn () void, addr); + _ = @as(*const fn () void, @ptrFromInt(addr)); } test "mutate through ptr initialized with constant ptrFromInt value" { @@ -21,7 +21,7 @@ test "mutate through ptr initialized with constant ptrFromInt value" { } fn forceCompilerAnalyzeBranchHardCodedPtrDereference(x: bool) void { - const hardCodedP = @ptrFromInt(*volatile u8, 0xdeadbeef); + const hardCodedP = @as(*volatile u8, @ptrFromInt(0xdeadbeef)); if (x) { hardCodedP.* = hardCodedP.* | 10; } else { @@ -34,7 +34,7 @@ test "@ptrFromInt creates null pointer" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO - const ptr = @ptrFromInt(?*u32, 0); + const ptr = @as(?*u32, @ptrFromInt(0)); try expectEqual(@as(?*u32, null), ptr); } @@ -43,6 +43,6 @@ test "@ptrFromInt creates allowzero zero pointer" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO - const ptr = @ptrFromInt(*allowzero u32, 0); + const ptr = @as(*allowzero u32, @ptrFromInt(0)); try expectEqual(@as(usize, 0), @intFromPtr(ptr)); } diff --git a/test/behavior/sizeof_and_typeof.zig b/test/behavior/sizeof_and_typeof.zig index 3657e77e50..a161be66eb 100644 --- a/test/behavior/sizeof_and_typeof.zig +++ b/test/behavior/sizeof_and_typeof.zig @@ -231,7 +231,7 @@ test "@sizeOf comparison against zero" { test "hardcoded address in typeof expression" { const S = struct { - fn func() @TypeOf(@ptrFromInt(*[]u8, 0x10).*[0]) { + fn func() @TypeOf(@as(*[]u8, @ptrFromInt(0x10)).*[0]) { return 0; } }; @@ -252,7 +252,7 @@ test "array access of generic param in typeof expression" { test "lazy size cast to float" { { const S = struct { a: u8 }; - try expect(@floatFromInt(f32, @sizeOf(S)) == 1.0); + try expect(@as(f32, @floatFromInt(@sizeOf(S))) == 1.0); } { const S = struct { a: u8 }; diff --git a/test/behavior/slice.zig b/test/behavior/slice.zig index fcbae214ac..4316aca34f 100644 --- a/test/behavior/slice.zig +++ b/test/behavior/slice.zig @@ -129,7 +129,7 @@ test "generic malloc free" { } var some_mem: [100]u8 = undefined; fn memAlloc(comptime T: type, n: usize) anyerror![]T { - return @ptrCast([*]T, &some_mem[0])[0..n]; + return @as([*]T, @ptrCast(&some_mem[0]))[0..n]; } fn memFree(comptime T: type, memory: []T) void { _ = memory; @@ -138,7 +138,7 @@ fn memFree(comptime T: type, memory: []T) void { test "slice of hardcoded address to pointer" { const S = struct { fn doTheTest() !void { - const pointer = @ptrFromInt([*]u8, 0x04)[0..2]; + const pointer = @as([*]u8, @ptrFromInt(0x04))[0..2]; try comptime expect(@TypeOf(pointer) == *[2]u8); const slice: []const u8 = pointer; try expect(@intFromPtr(slice.ptr) == 4); @@ -152,7 +152,7 @@ test "slice of hardcoded address to pointer" { test "comptime slice of pointer preserves comptime var" { comptime { var buff: [10]u8 = undefined; - var a = @ptrCast([*]u8, &buff); + var a = @as([*]u8, @ptrCast(&buff)); a[0..1][0] = 1; try expect(buff[0..][0..][0] == 1); } @@ -161,7 +161,7 @@ test "comptime slice of pointer preserves comptime var" { test "comptime pointer cast array and then slice" { const array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8 }; - const ptrA: [*]const u8 = @ptrCast([*]const u8, &array); + const ptrA: [*]const u8 = @as([*]const u8, @ptrCast(&array)); const sliceA: []const u8 = ptrA[0..2]; const ptrB: [*]const u8 = &array; @@ -188,7 +188,7 @@ test "slicing pointer by length" { if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8 }; - const ptr: [*]const u8 = @ptrCast([*]const u8, &array); + const ptr: [*]const u8 = @as([*]const u8, @ptrCast(&array)); const slice = ptr[1..][0..5]; try expect(slice.len == 5); var i: usize = 0; @@ -197,7 +197,7 @@ test "slicing pointer by length" { } } -const x = @ptrFromInt([*]i32, 0x1000)[0..0x500]; +const x = @as([*]i32, @ptrFromInt(0x1000))[0..0x500]; const y = x[0x100..]; test "compile time slice of pointer to hard coded address" { try expect(@intFromPtr(x) == 0x1000); @@ -262,7 +262,7 @@ test "C pointer slice access" { if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; var buf: [10]u32 = [1]u32{42} ** 10; - const c_ptr = @ptrCast([*c]const u32, &buf); + const c_ptr = @as([*c]const u32, @ptrCast(&buf)); var runtime_zero: usize = 0; try comptime expectEqual([]const u32, @TypeOf(c_ptr[runtime_zero..1])); @@ -352,7 +352,7 @@ test "@ptrCast slice to pointer" { fn doTheTest() !void { var array align(@alignOf(u16)) = [5]u8{ 0xff, 0xff, 0xff, 0xff, 0xff }; var slice: []align(@alignOf(u16)) u8 = &array; - var ptr = @ptrCast(*u16, slice); + var ptr = @as(*u16, @ptrCast(slice)); try expect(ptr.* == 65535); } }; @@ -837,13 +837,13 @@ test "empty slice ptr is non null" { { const empty_slice: []u8 = &[_]u8{}; const p: [*]u8 = empty_slice.ptr + 0; - const t = @ptrCast([*]i8, p); + const t = @as([*]i8, @ptrCast(p)); try expect(@intFromPtr(t) == @intFromPtr(empty_slice.ptr)); } { const empty_slice: []u8 = &.{}; const p: [*]u8 = empty_slice.ptr + 0; - const t = @ptrCast([*]i8, p); + const t = @as([*]i8, @ptrCast(p)); try expect(@intFromPtr(t) == @intFromPtr(empty_slice.ptr)); } } diff --git a/test/behavior/slice_sentinel_comptime.zig b/test/behavior/slice_sentinel_comptime.zig index 368860547e..31b7e2349e 100644 --- a/test/behavior/slice_sentinel_comptime.zig +++ b/test/behavior/slice_sentinel_comptime.zig @@ -25,7 +25,7 @@ test "comptime slice-sentinel in bounds (unterminated)" { // vector_ConstPtrSpecialRef comptime { var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10; - var target: [*]u8 = @ptrCast([*]u8, &buf); + var target: [*]u8 = @as([*]u8, @ptrCast(&buf)); const slice = target[0..3 :'d']; _ = slice; } @@ -41,7 +41,7 @@ test "comptime slice-sentinel in bounds (unterminated)" { // cvector_ConstPtrSpecialRef comptime { var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10; - var target: [*c]u8 = @ptrCast([*c]u8, &buf); + var target: [*c]u8 = @as([*c]u8, @ptrCast(&buf)); const slice = target[0..3 :'d']; _ = slice; } @@ -82,7 +82,7 @@ test "comptime slice-sentinel in bounds (end,unterminated)" { // vector_ConstPtrSpecialRef comptime { var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{0xff} ** 10; - var target: [*]u8 = @ptrCast([*]u8, &buf); + var target: [*]u8 = @as([*]u8, @ptrCast(&buf)); const slice = target[0..13 :0xff]; _ = slice; } @@ -98,7 +98,7 @@ test "comptime slice-sentinel in bounds (end,unterminated)" { // cvector_ConstPtrSpecialRef comptime { var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{0xff} ** 10; - var target: [*c]u8 = @ptrCast([*c]u8, &buf); + var target: [*c]u8 = @as([*c]u8, @ptrCast(&buf)); const slice = target[0..13 :0xff]; _ = slice; } @@ -139,7 +139,7 @@ test "comptime slice-sentinel in bounds (terminated)" { // vector_ConstPtrSpecialRef comptime { var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10; - var target: [*]u8 = @ptrCast([*]u8, &buf); + var target: [*]u8 = @as([*]u8, @ptrCast(&buf)); const slice = target[0..3 :'d']; _ = slice; } @@ -155,7 +155,7 @@ test "comptime slice-sentinel in bounds (terminated)" { // cvector_ConstPtrSpecialRef comptime { var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10; - var target: [*c]u8 = @ptrCast([*c]u8, &buf); + var target: [*c]u8 = @as([*c]u8, @ptrCast(&buf)); const slice = target[0..3 :'d']; _ = slice; } @@ -196,7 +196,7 @@ test "comptime slice-sentinel in bounds (on target sentinel)" { // vector_ConstPtrSpecialRef comptime { var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10; - var target: [*]u8 = @ptrCast([*]u8, &buf); + var target: [*]u8 = @as([*]u8, @ptrCast(&buf)); const slice = target[0..14 :0]; _ = slice; } @@ -212,7 +212,7 @@ test "comptime slice-sentinel in bounds (on target sentinel)" { // cvector_ConstPtrSpecialRef comptime { var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10; - var target: [*c]u8 = @ptrCast([*c]u8, &buf); + var target: [*c]u8 = @as([*c]u8, @ptrCast(&buf)); const slice = target[0..14 :0]; _ = slice; } diff --git a/test/behavior/struct.zig b/test/behavior/struct.zig index 6ced42998e..95b2718efd 100644 --- a/test/behavior/struct.zig +++ b/test/behavior/struct.zig @@ -92,7 +92,7 @@ test "structs" { if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; var foo: StructFoo = undefined; - @memset(@ptrCast([*]u8, &foo)[0..@sizeOf(StructFoo)], 0); + @memset(@as([*]u8, @ptrCast(&foo))[0..@sizeOf(StructFoo)], 0); foo.a += 1; foo.b = foo.a == 1; try testFoo(foo); @@ -479,14 +479,14 @@ test "runtime struct initialization of bitfield" { .y = x1, }; const s2 = Nibbles{ - .x = @intCast(u4, x2), - .y = @intCast(u4, x2), + .x = @as(u4, @intCast(x2)), + .y = @as(u4, @intCast(x2)), }; try expect(s1.x == x1); try expect(s1.y == x1); - try expect(s2.x == @intCast(u4, x2)); - try expect(s2.y == @intCast(u4, x2)); + try expect(s2.x == @as(u4, @intCast(x2))); + try expect(s2.y == @as(u4, @intCast(x2))); } var x1 = @as(u4, 1); @@ -515,8 +515,8 @@ test "packed struct fields are ordered from LSB to MSB" { var all: u64 = 0x7765443322221111; var bytes: [8]u8 align(@alignOf(Bitfields)) = undefined; - @memcpy(bytes[0..8], @ptrCast([*]u8, &all)); - var bitfields = @ptrCast(*Bitfields, &bytes).*; + @memcpy(bytes[0..8], @as([*]u8, @ptrCast(&all))); + var bitfields = @as(*Bitfields, @ptrCast(&bytes)).*; try expect(bitfields.f1 == 0x1111); try expect(bitfields.f2 == 0x2222); @@ -1281,7 +1281,7 @@ test "packed struct aggregate init" { const S = struct { fn foo(a: i2, b: i6) u8 { - return @bitCast(u8, P{ .a = a, .b = b }); + return @as(u8, @bitCast(P{ .a = a, .b = b })); } const P = packed struct { @@ -1289,7 +1289,7 @@ test "packed struct aggregate init" { b: i6, }; }; - const result = @bitCast(u8, S.foo(1, 2)); + const result = @as(u8, @bitCast(S.foo(1, 2))); try expect(result == 9); } @@ -1365,7 +1365,7 @@ test "under-aligned struct field" { }; var runtime: usize = 1234; const ptr = &S{ .events = 0, .data = .{ .u64 = runtime } }; - const array = @ptrCast(*const [12]u8, ptr); + const array = @as(*const [12]u8, @ptrCast(ptr)); const result = std.mem.readIntNative(u64, array[4..12]); try expect(result == 1234); } diff --git a/test/behavior/switch.zig b/test/behavior/switch.zig index bcbfc81ed4..0ae7c510ef 100644 --- a/test/behavior/switch.zig +++ b/test/behavior/switch.zig @@ -590,9 +590,9 @@ test "switch on pointer type" { field: u32, }; - const P1 = @ptrFromInt(*X, 0x400); - const P2 = @ptrFromInt(*X, 0x800); - const P3 = @ptrFromInt(*X, 0xC00); + const P1 = @as(*X, @ptrFromInt(0x400)); + const P2 = @as(*X, @ptrFromInt(0x800)); + const P3 = @as(*X, @ptrFromInt(0xC00)); fn doTheTest(arg: *X) i32 { switch (arg) { @@ -682,9 +682,9 @@ test "enum value without tag name used as switch item" { b = 2, _, }; - var e: E = @enumFromInt(E, 0); + var e: E = @as(E, @enumFromInt(0)); switch (e) { - @enumFromInt(E, 0) => {}, + @as(E, @enumFromInt(0)) => {}, .a => return error.TestFailed, .b => return error.TestFailed, _ => return error.TestFailed, diff --git a/test/behavior/translate_c_macros.zig b/test/behavior/translate_c_macros.zig index a69396c203..68e91bfa58 100644 --- a/test/behavior/translate_c_macros.zig +++ b/test/behavior/translate_c_macros.zig @@ -60,7 +60,7 @@ test "cast negative integer to pointer" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; - try expectEqual(@ptrFromInt(?*anyopaque, @bitCast(usize, @as(isize, -1))), h.MAP_FAILED); + try expectEqual(@as(?*anyopaque, @ptrFromInt(@as(usize, @bitCast(@as(isize, -1))))), h.MAP_FAILED); } test "casting to union with a macro" { @@ -89,7 +89,7 @@ test "casting or calling a value with a paren-surrounded macro" { const l: c_long = 42; const casted = h.CAST_OR_CALL_WITH_PARENS(c_int, l); - try expect(casted == @intCast(c_int, l)); + try expect(casted == @as(c_int, @intCast(l))); const Helper = struct { fn foo(n: c_int) !void { diff --git a/test/behavior/truncate.zig b/test/behavior/truncate.zig index 3ea979009e..4fc095b66c 100644 --- a/test/behavior/truncate.zig +++ b/test/behavior/truncate.zig @@ -4,58 +4,58 @@ const expect = std.testing.expect; test "truncate u0 to larger integer allowed and has comptime-known result" { var x: u0 = 0; - const y = @truncate(u8, x); + const y = @as(u8, @truncate(x)); try comptime expect(y == 0); } test "truncate.u0.literal" { - var z = @truncate(u0, 0); + var z = @as(u0, @truncate(0)); try expect(z == 0); } test "truncate.u0.const" { const c0: usize = 0; - var z = @truncate(u0, c0); + var z = @as(u0, @truncate(c0)); try expect(z == 0); } test "truncate.u0.var" { var d: u8 = 2; - var z = @truncate(u0, d); + var z = @as(u0, @truncate(d)); try expect(z == 0); } test "truncate i0 to larger integer allowed and has comptime-known result" { var x: i0 = 0; - const y = @truncate(i8, x); + const y = @as(i8, @truncate(x)); try comptime expect(y == 0); } test "truncate.i0.literal" { - var z = @truncate(i0, 0); + var z = @as(i0, @truncate(0)); try expect(z == 0); } test "truncate.i0.const" { const c0: isize = 0; - var z = @truncate(i0, c0); + var z = @as(i0, @truncate(c0)); try expect(z == 0); } test "truncate.i0.var" { var d: i8 = 2; - var z = @truncate(i0, d); + var z = @as(i0, @truncate(d)); try expect(z == 0); } test "truncate on comptime integer" { - var x = @truncate(u16, 9999); + var x = @as(u16, @truncate(9999)); try expect(x == 9999); - var y = @truncate(u16, -21555); + var y = @as(u16, @truncate(-21555)); try expect(y == 0xabcd); - var z = @truncate(i16, -65537); + var z = @as(i16, @truncate(-65537)); try expect(z == -1); - var w = @truncate(u1, 1 << 100); + var w = @as(u1, @truncate(1 << 100)); try expect(w == 0); } @@ -69,7 +69,7 @@ test "truncate on vectors" { const S = struct { fn doTheTest() !void { var v1: @Vector(4, u16) = .{ 0xaabb, 0xccdd, 0xeeff, 0x1122 }; - var v2 = @truncate(u8, v1); + var v2: @Vector(4, u8) = @truncate(v1); try expect(std.mem.eql(u8, &@as([4]u8, v2), &[4]u8{ 0xbb, 0xdd, 0xff, 0x22 })); } }; diff --git a/test/behavior/tuple.zig b/test/behavior/tuple.zig index ee414365c3..e9d3fcd0aa 100644 --- a/test/behavior/tuple.zig +++ b/test/behavior/tuple.zig @@ -403,7 +403,7 @@ test "nested runtime conditionals in tuple initializer" { var data: u8 = 0; const x = .{ - if (data != 0) "" else switch (@truncate(u1, data)) { + if (data != 0) "" else switch (@as(u1, @truncate(data))) { 0 => "up", 1 => "down", }, diff --git a/test/behavior/tuple_declarations.zig b/test/behavior/tuple_declarations.zig index c053447ccc..84b04d3e53 100644 --- a/test/behavior/tuple_declarations.zig +++ b/test/behavior/tuple_declarations.zig @@ -21,7 +21,7 @@ test "tuple declaration type info" { try expectEqualStrings(info.fields[0].name, "0"); try expect(info.fields[0].type == u32); - try expect(@ptrCast(*const u32, @alignCast(@alignOf(u32), info.fields[0].default_value)).* == 1); + try expect(@as(*const u32, @ptrCast(@alignCast(info.fields[0].default_value))).* == 1); try expect(info.fields[0].is_comptime); try expect(info.fields[0].alignment == 2); diff --git a/test/behavior/type.zig b/test/behavior/type.zig index 9420b5d2fd..a2ede838b2 100644 --- a/test/behavior/type.zig +++ b/test/behavior/type.zig @@ -289,7 +289,7 @@ test "Type.Struct" { try testing.expectEqual(@as(?*const anyopaque, null), infoB.fields[0].default_value); try testing.expectEqualSlices(u8, "y", infoB.fields[1].name); try testing.expectEqual(u32, infoB.fields[1].type); - try testing.expectEqual(@as(u32, 5), @ptrCast(*align(1) const u32, infoB.fields[1].default_value.?).*); + try testing.expectEqual(@as(u32, 5), @as(*align(1) const u32, @ptrCast(infoB.fields[1].default_value.?)).*); try testing.expectEqual(@as(usize, 0), infoB.decls.len); try testing.expectEqual(@as(bool, false), infoB.is_tuple); @@ -298,10 +298,10 @@ test "Type.Struct" { try testing.expectEqual(Type.ContainerLayout.Packed, infoC.layout); try testing.expectEqualSlices(u8, "x", infoC.fields[0].name); try testing.expectEqual(u8, infoC.fields[0].type); - try testing.expectEqual(@as(u8, 3), @ptrCast(*const u8, infoC.fields[0].default_value.?).*); + try testing.expectEqual(@as(u8, 3), @as(*const u8, @ptrCast(infoC.fields[0].default_value.?)).*); try testing.expectEqualSlices(u8, "y", infoC.fields[1].name); try testing.expectEqual(u32, infoC.fields[1].type); - try testing.expectEqual(@as(u32, 5), @ptrCast(*align(1) const u32, infoC.fields[1].default_value.?).*); + try testing.expectEqual(@as(u32, 5), @as(*align(1) const u32, @ptrCast(infoC.fields[1].default_value.?)).*); try testing.expectEqual(@as(usize, 0), infoC.decls.len); try testing.expectEqual(@as(bool, false), infoC.is_tuple); @@ -311,10 +311,10 @@ test "Type.Struct" { try testing.expectEqual(Type.ContainerLayout.Auto, infoD.layout); try testing.expectEqualSlices(u8, "x", infoD.fields[0].name); try testing.expectEqual(comptime_int, infoD.fields[0].type); - try testing.expectEqual(@as(comptime_int, 3), @ptrCast(*const comptime_int, infoD.fields[0].default_value.?).*); + try testing.expectEqual(@as(comptime_int, 3), @as(*const comptime_int, @ptrCast(infoD.fields[0].default_value.?)).*); try testing.expectEqualSlices(u8, "y", infoD.fields[1].name); try testing.expectEqual(comptime_int, infoD.fields[1].type); - try testing.expectEqual(@as(comptime_int, 5), @ptrCast(*const comptime_int, infoD.fields[1].default_value.?).*); + try testing.expectEqual(@as(comptime_int, 5), @as(*const comptime_int, @ptrCast(infoD.fields[1].default_value.?)).*); try testing.expectEqual(@as(usize, 0), infoD.decls.len); try testing.expectEqual(@as(bool, false), infoD.is_tuple); @@ -324,10 +324,10 @@ test "Type.Struct" { try testing.expectEqual(Type.ContainerLayout.Auto, infoE.layout); try testing.expectEqualSlices(u8, "0", infoE.fields[0].name); try testing.expectEqual(comptime_int, infoE.fields[0].type); - try testing.expectEqual(@as(comptime_int, 1), @ptrCast(*const comptime_int, infoE.fields[0].default_value.?).*); + try testing.expectEqual(@as(comptime_int, 1), @as(*const comptime_int, @ptrCast(infoE.fields[0].default_value.?)).*); try testing.expectEqualSlices(u8, "1", infoE.fields[1].name); try testing.expectEqual(comptime_int, infoE.fields[1].type); - try testing.expectEqual(@as(comptime_int, 2), @ptrCast(*const comptime_int, infoE.fields[1].default_value.?).*); + try testing.expectEqual(@as(comptime_int, 2), @as(*const comptime_int, @ptrCast(infoE.fields[1].default_value.?)).*); try testing.expectEqual(@as(usize, 0), infoE.decls.len); try testing.expectEqual(@as(bool, true), infoE.is_tuple); @@ -379,7 +379,7 @@ test "Type.Enum" { try testing.expectEqual(false, @typeInfo(Bar).Enum.is_exhaustive); try testing.expectEqual(@as(u32, 1), @intFromEnum(Bar.a)); try testing.expectEqual(@as(u32, 5), @intFromEnum(Bar.b)); - try testing.expectEqual(@as(u32, 6), @intFromEnum(@enumFromInt(Bar, 6))); + try testing.expectEqual(@as(u32, 6), @intFromEnum(@as(Bar, @enumFromInt(6)))); } test "Type.Union" { diff --git a/test/behavior/type_info.zig b/test/behavior/type_info.zig index 87ae96768a..0d026c0078 100644 --- a/test/behavior/type_info.zig +++ b/test/behavior/type_info.zig @@ -113,7 +113,7 @@ fn testNullTerminatedPtr() !void { try expect(ptr_info.Pointer.size == .Many); try expect(ptr_info.Pointer.is_const == false); try expect(ptr_info.Pointer.is_volatile == false); - try expect(@ptrCast(*const u8, ptr_info.Pointer.sentinel.?).* == 0); + try expect(@as(*const u8, @ptrCast(ptr_info.Pointer.sentinel.?)).* == 0); try expect(@typeInfo([:0]u8).Pointer.sentinel != null); } @@ -151,7 +151,7 @@ fn testArray() !void { const info = @typeInfo([10:0]u8); try expect(info.Array.len == 10); try expect(info.Array.child == u8); - try expect(@ptrCast(*const u8, info.Array.sentinel.?).* == @as(u8, 0)); + try expect(@as(*const u8, @ptrCast(info.Array.sentinel.?)).* == @as(u8, 0)); try expect(@sizeOf([10:0]u8) == info.Array.len + 1); } } @@ -295,8 +295,8 @@ fn testStruct() !void { try expect(unpacked_struct_info.Struct.is_tuple == false); try expect(unpacked_struct_info.Struct.backing_integer == null); try expect(unpacked_struct_info.Struct.fields[0].alignment == @alignOf(u32)); - try expect(@ptrCast(*align(1) const u32, unpacked_struct_info.Struct.fields[0].default_value.?).* == 4); - try expect(mem.eql(u8, "foobar", @ptrCast(*align(1) const *const [6:0]u8, unpacked_struct_info.Struct.fields[1].default_value.?).*)); + try expect(@as(*align(1) const u32, @ptrCast(unpacked_struct_info.Struct.fields[0].default_value.?)).* == 4); + try expect(mem.eql(u8, "foobar", @as(*align(1) const *const [6:0]u8, @ptrCast(unpacked_struct_info.Struct.fields[1].default_value.?)).*)); } const TestStruct = struct { @@ -319,7 +319,7 @@ fn testPackedStruct() !void { try expect(struct_info.Struct.fields[0].alignment == 0); try expect(struct_info.Struct.fields[2].type == f32); try expect(struct_info.Struct.fields[2].default_value == null); - try expect(@ptrCast(*align(1) const u32, struct_info.Struct.fields[3].default_value.?).* == 4); + try expect(@as(*align(1) const u32, @ptrCast(struct_info.Struct.fields[3].default_value.?)).* == 4); try expect(struct_info.Struct.fields[3].alignment == 0); try expect(struct_info.Struct.decls.len == 2); try expect(struct_info.Struct.decls[0].is_pub); @@ -504,7 +504,7 @@ test "type info for async frames" { switch (@typeInfo(@Frame(add))) { .Frame => |frame| { - try expect(@ptrCast(@TypeOf(add), frame.function) == add); + try expect(@as(@TypeOf(add), @ptrCast(frame.function)) == add); }, else => unreachable, } @@ -564,7 +564,7 @@ test "typeInfo resolves usingnamespace declarations" { test "value from struct @typeInfo default_value can be loaded at comptime" { comptime { const a = @typeInfo(@TypeOf(.{ .foo = @as(u8, 1) })).Struct.fields[0].default_value; - try expect(@ptrCast(*const u8, a).* == 1); + try expect(@as(*const u8, @ptrCast(a)).* == 1); } } @@ -607,6 +607,6 @@ test "@typeInfo decls ignore dependency loops" { test "type info of tuple of string literal default value" { const struct_field = @typeInfo(@TypeOf(.{"hi"})).Struct.fields[0]; - const value = @ptrCast(*align(1) const *const [2:0]u8, struct_field.default_value.?).*; + const value = @as(*align(1) const *const [2:0]u8, @ptrCast(struct_field.default_value.?)).*; comptime std.debug.assert(value[0] == 'h'); } diff --git a/test/behavior/vector.zig b/test/behavior/vector.zig index 47864a83c9..2aab98ea72 100644 --- a/test/behavior/vector.zig +++ b/test/behavior/vector.zig @@ -1244,7 +1244,7 @@ test "@intCast to u0" { if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; var zeros = @Vector(2, u32){ 0, 0 }; - const casted = @intCast(@Vector(2, u0), zeros); + const casted = @as(@Vector(2, u0), @intCast(zeros)); _ = casted[0]; } |
