authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-06-23 19:37:50+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-24 16:56:39-07:00
loga84a8953257ccfb70567a75017c98830eca250e3
tree6af3dcc5132a64767001287f13715bdb33ea6e83
parentf26dda21171e26f44aeec8c59a75bbb3331eeb2e

langref: update to new cast builtin syntax


1 files changed, 65 insertions(+), 59 deletions(-)

doc/langref.html.in+65-59
...@@ -2410,7 +2410,7 @@ var some_integers: [100]i32 = undefined;...@@ -2410,7 +2410,7 @@ var some_integers: [100]i32 = undefined;
24102410
2411test "modify an array" {2411test "modify an array" {
2412 for (&some_integers, 0..) |*item, i| {2412 for (&some_integers, 0..) |*item, i| {
2413 item.* = @intCast(i32, i);2413 item.* = @intCast(i);
2414 }2414 }
2415 try expect(some_integers[10] == 10);2415 try expect(some_integers[10] == 10);
2416 try expect(some_integers[99] == 99);2416 try expect(some_integers[99] == 99);
...@@ -2452,8 +2452,8 @@ var fancy_array = init: {...@@ -2452,8 +2452,8 @@ var fancy_array = init: {
2452 var initial_value: [10]Point = undefined;2452 var initial_value: [10]Point = undefined;
2453 for (&initial_value, 0..) |*pt, i| {2453 for (&initial_value, 0..) |*pt, i| {
2454 pt.* = Point{2454 pt.* = Point{
2455 .x = @intCast(i32, i),2455 .x = @intCast(i),
2456 .y = @intCast(i32, i) * 2,2456 .y = @intCast(i * 2),
2457 };2457 };
2458 }2458 }
2459 break :init initial_value;2459 break :init initial_value;
...@@ -2769,7 +2769,7 @@ test "comptime pointers" {...@@ -2769,7 +2769,7 @@ test "comptime pointers" {
2769const expect = @import("std").testing.expect;2769const expect = @import("std").testing.expect;
27702770
2771test "@intFromPtr and @ptrFromInt" {2771test "@intFromPtr and @ptrFromInt" {
2772 const ptr = @ptrFromInt(*i32, 0xdeadbee0);2772 const ptr: *i32 = @ptrFromInt(0xdeadbee0);
2773 const addr = @intFromPtr(ptr);2773 const addr = @intFromPtr(ptr);
2774 try expect(@TypeOf(addr) == usize);2774 try expect(@TypeOf(addr) == usize);
2775 try expect(addr == 0xdeadbee0);2775 try expect(addr == 0xdeadbee0);
...@@ -2784,7 +2784,7 @@ test "comptime @ptrFromInt" {...@@ -2784,7 +2784,7 @@ test "comptime @ptrFromInt" {
2784 comptime {2784 comptime {
2785 // Zig is able to do this at compile-time, as long as2785 // Zig is able to do this at compile-time, as long as
2786 // ptr is never dereferenced.2786 // ptr is never dereferenced.
2787 const ptr = @ptrFromInt(*i32, 0xdeadbee0);2787 const ptr: *i32 = @ptrFromInt(0xdeadbee0);
2788 const addr = @intFromPtr(ptr);2788 const addr = @intFromPtr(ptr);
2789 try expect(@TypeOf(addr) == usize);2789 try expect(@TypeOf(addr) == usize);
2790 try expect(addr == 0xdeadbee0);2790 try expect(addr == 0xdeadbee0);
...@@ -2801,7 +2801,7 @@ test "comptime @ptrFromInt" {...@@ -2801,7 +2801,7 @@ test "comptime @ptrFromInt" {
2801const expect = @import("std").testing.expect;2801const expect = @import("std").testing.expect;
28022802
2803test "volatile" {2803test "volatile" {
2804 const mmio_ptr = @ptrFromInt(*volatile u8, 0x12345678);2804 const mmio_ptr: *volatile u8 = @ptrFromInt(0x12345678);
2805 try expect(@TypeOf(mmio_ptr) == *volatile u8);2805 try expect(@TypeOf(mmio_ptr) == *volatile u8);
2806}2806}
2807 {#code_end#}2807 {#code_end#}
...@@ -2822,7 +2822,7 @@ const expect = std.testing.expect;...@@ -2822,7 +2822,7 @@ const expect = std.testing.expect;
28222822
2823test "pointer casting" {2823test "pointer casting" {
2824 const bytes align(@alignOf(u32)) = [_]u8{ 0x12, 0x12, 0x12, 0x12 };2824 const bytes align(@alignOf(u32)) = [_]u8{ 0x12, 0x12, 0x12, 0x12 };
2825 const u32_ptr = @ptrCast(*const u32, &bytes);2825 const u32_ptr: *const u32 = @ptrCast(&bytes);
2826 try expect(u32_ptr.* == 0x12121212);2826 try expect(u32_ptr.* == 0x12121212);
28272827
2828 // Even this example is contrived - there are better ways to do the above than2828 // Even this example is contrived - there are better ways to do the above than
...@@ -2831,7 +2831,7 @@ test "pointer casting" {...@@ -2831,7 +2831,7 @@ test "pointer casting" {
2831 try expect(u32_value == 0x12121212);2831 try expect(u32_value == 0x12121212);
28322832
2833 // And even another way, the most straightforward way to do it:2833 // And even another way, the most straightforward way to do it:
2834 try expect(@bitCast(u32, bytes) == 0x12121212);2834 try expect(@as(u32, @bitCast(bytes)) == 0x12121212);
2835}2835}
28362836
2837test "pointer child type" {2837test "pointer child type" {
...@@ -2921,7 +2921,7 @@ test "pointer alignment safety" {...@@ -2921,7 +2921,7 @@ test "pointer alignment safety" {
2921}2921}
2922fn foo(bytes: []u8) u32 {2922fn foo(bytes: []u8) u32 {
2923 const slice4 = bytes[1..5];2923 const slice4 = bytes[1..5];
2924 const int_slice = std.mem.bytesAsSlice(u32, @alignCast(4, slice4));2924 const int_slice = std.mem.bytesAsSlice(u32, @as([]align(4) u8, @alignCast(slice4)));
2925 return int_slice[0];2925 return int_slice[0];
2926}2926}
2927 {#code_end#}2927 {#code_end#}
...@@ -2942,7 +2942,7 @@ const expect = std.testing.expect;...@@ -2942,7 +2942,7 @@ const expect = std.testing.expect;
29422942
2943test "allowzero" {2943test "allowzero" {
2944 var zero: usize = 0;2944 var zero: usize = 0;
2945 var ptr = @ptrFromInt(*allowzero i32, zero);2945 var ptr: *allowzero i32 = @ptrFromInt(zero);
2946 try expect(@intFromPtr(ptr) == 0);2946 try expect(@intFromPtr(ptr) == 0);
2947}2947}
2948 {#code_end#}2948 {#code_end#}
...@@ -3354,12 +3354,12 @@ fn doTheTest() !void {...@@ -3354,12 +3354,12 @@ fn doTheTest() !void {
3354 try expect(@sizeOf(Full) == 2);3354 try expect(@sizeOf(Full) == 2);
3355 try expect(@sizeOf(Divided) == 2);3355 try expect(@sizeOf(Divided) == 2);
3356 var full = Full{ .number = 0x1234 };3356 var full = Full{ .number = 0x1234 };
3357 var divided = @bitCast(Divided, full);3357 var divided: Divided = @bitCast(full);
3358 try expect(divided.half1 == 0x34);3358 try expect(divided.half1 == 0x34);
3359 try expect(divided.quarter3 == 0x2);3359 try expect(divided.quarter3 == 0x2);
3360 try expect(divided.quarter4 == 0x1);3360 try expect(divided.quarter4 == 0x1);
33613361
3362 var ordered = @bitCast([2]u8, full);3362 var ordered: [2]u8 = @bitCast(full);
3363 switch (native_endian) {3363 switch (native_endian) {
3364 .Big => {3364 .Big => {
3365 try expect(ordered[0] == 0x12);3365 try expect(ordered[0] == 0x12);
...@@ -4428,7 +4428,7 @@ fn getNum(u: U) u32 {...@@ -4428,7 +4428,7 @@ fn getNum(u: U) u32 {
4428 // `u.a` or `u.b` and `tag` is `u`'s comptime-known tag value.4428 // `u.a` or `u.b` and `tag` is `u`'s comptime-known tag value.
4429 inline else => |num, tag| {4429 inline else => |num, tag| {
4430 if (tag == .b) {4430 if (tag == .b) {
4431 return @intFromFloat(u32, num);4431 return @intFromFloat(num);
4432 }4432 }
4433 return num;4433 return num;
4434 }4434 }
...@@ -4714,7 +4714,7 @@ test "for basics" {...@@ -4714,7 +4714,7 @@ test "for basics" {
4714 var sum2: i32 = 0;4714 var sum2: i32 = 0;
4715 for (items, 0..) |_, i| {4715 for (items, 0..) |_, i| {
4716 try expect(@TypeOf(i) == usize);4716 try expect(@TypeOf(i) == usize);
4717 sum2 += @intCast(i32, i);4717 sum2 += @as(i32, @intCast(i));
4718 }4718 }
4719 try expect(sum2 == 10);4719 try expect(sum2 == 10);
47204720
...@@ -6363,7 +6363,7 @@ const mem = std.mem;...@@ -6363,7 +6363,7 @@ const mem = std.mem;
6363test "cast *[1][*]const u8 to [*]const ?[*]const u8" {6363test "cast *[1][*]const u8 to [*]const ?[*]const u8" {
6364 const window_name = [1][*]const u8{"window name"};6364 const window_name = [1][*]const u8{"window name"};
6365 const x: [*]const ?[*]const u8 = &window_name;6365 const x: [*]const ?[*]const u8 = &window_name;
6366 try expect(mem.eql(u8, std.mem.sliceTo(@ptrCast([*:0]const u8, x[0].?), 0), "window name"));6366 try expect(mem.eql(u8, std.mem.sliceTo(@as([*:0]const u8, @ptrCast(x[0].?)), 0), "window name"));
6367}6367}
6368 {#code_end#}6368 {#code_end#}
6369 {#header_close#}6369 {#header_close#}
...@@ -6760,8 +6760,8 @@ fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 {...@@ -6760,8 +6760,8 @@ fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 {
6760}6760}
67616761
6762test "peer type resolution: *const T and ?*T" {6762test "peer type resolution: *const T and ?*T" {
6763 const a = @ptrFromInt(*const usize, 0x123456780);6763 const a: *const usize = @ptrFromInt(0x123456780);
6764 const b = @ptrFromInt(?*usize, 0x123456780);6764 const b: ?*usize = @ptrFromInt(0x123456780);
6765 try expect(a == b);6765 try expect(a == b);
6766 try expect(b == a);6766 try expect(b == a);
6767}6767}
...@@ -7762,12 +7762,13 @@ test "global assembly" {...@@ -7762,12 +7762,13 @@ test "global assembly" {
7762 at compile time.7762 at compile time.
7763 </p>7763 </p>
7764 {#header_open|@addrSpaceCast#}7764 {#header_open|@addrSpaceCast#}
7765 <pre>{#syntax#}@addrSpaceCast(comptime addrspace: std.builtin.AddressSpace, ptr: anytype) anytype{#endsyntax#}</pre>7765 <pre>{#syntax#}@addrSpaceCast(ptr: anytype) anytype{#endsyntax#}</pre>
7766 <p>7766 <p>
7767 Converts a pointer from one address space to another. Depending on the current target and7767 Converts a pointer from one address space to another. The new address space is inferred
7768 address spaces, this cast may be a no-op, a complex operation, or illegal. If the cast is7768 based on the result type. Depending on the current target and address spaces, this cast
7769 legal, then the resulting pointer points to the same memory location as the pointer operand.7769 may be a no-op, a complex operation, or illegal. If the cast is legal, then the resulting
7770 It is always valid to cast a pointer between the same address spaces.7770 pointer points to the same memory location as the pointer operand. It is always valid to
7771 cast a pointer between the same address spaces.
7771 </p>7772 </p>
7772 {#header_close#}7773 {#header_close#}
7773 {#header_open|@addWithOverflow#}7774 {#header_open|@addWithOverflow#}
...@@ -7777,10 +7778,10 @@ test "global assembly" {...@@ -7777,10 +7778,10 @@ test "global assembly" {
7777 </p>7778 </p>
7778 {#header_close#}7779 {#header_close#}
7779 {#header_open|@alignCast#}7780 {#header_open|@alignCast#}
7780 <pre>{#syntax#}@alignCast(comptime alignment: u29, ptr: anytype) anytype{#endsyntax#}</pre>7781 <pre>{#syntax#}@alignCast(ptr: anytype) anytype{#endsyntax#}</pre>
7781 <p>7782 <p>
7782 {#syntax#}ptr{#endsyntax#} can be {#syntax#}*T{#endsyntax#}, {#syntax#}?*T{#endsyntax#}, or {#syntax#}[]T{#endsyntax#}.7783 {#syntax#}ptr{#endsyntax#} can be {#syntax#}*T{#endsyntax#}, {#syntax#}?*T{#endsyntax#}, or {#syntax#}[]T{#endsyntax#}.
7783 It returns the same type as {#syntax#}ptr{#endsyntax#} except with the alignment adjusted to the new value.7784 Changes the alignment of a pointer. The alignment to use is inferred based on the result type.
7784 </p>7785 </p>
7785 <p>A {#link|pointer alignment safety check|Incorrect Pointer Alignment#} is added7786 <p>A {#link|pointer alignment safety check|Incorrect Pointer Alignment#} is added
7786 to the generated code to make sure the pointer is aligned as promised.</p>7787 to the generated code to make sure the pointer is aligned as promised.</p>
...@@ -7865,9 +7866,10 @@ comptime {...@@ -7865,9 +7866,10 @@ comptime {
7865 {#header_close#}7866 {#header_close#}
78667867
7867 {#header_open|@bitCast#}7868 {#header_open|@bitCast#}
7868 <pre>{#syntax#}@bitCast(comptime DestType: type, value: anytype) DestType{#endsyntax#}</pre>7869 <pre>{#syntax#}@bitCast(value: anytype) anytype{#endsyntax#}</pre>
7869 <p>7870 <p>
7870 Converts a value of one type to another type.7871 Converts a value of one type to another type. The return type is the
7872 inferred result type.
7871 </p>7873 </p>
7872 <p>7874 <p>
7873 Asserts that {#syntax#}@sizeOf(@TypeOf(value)) == @sizeOf(DestType){#endsyntax#}.7875 Asserts that {#syntax#}@sizeOf(@TypeOf(value)) == @sizeOf(DestType){#endsyntax#}.
...@@ -8420,10 +8422,11 @@ test "main" {...@@ -8420,10 +8422,11 @@ test "main" {
8420 {#header_close#}8422 {#header_close#}
84218423
8422 {#header_open|@errSetCast#}8424 {#header_open|@errSetCast#}
8423 <pre>{#syntax#}@errSetCast(comptime T: DestType, value: anytype) DestType{#endsyntax#}</pre>8425 <pre>{#syntax#}@errSetCast(value: anytype) anytype{#endsyntax#}</pre>
8424 <p>8426 <p>
8425 Converts an error value from one error set to another error set. Attempting to convert an error8427 Converts an error value from one error set to another error set. The return type is the
8426 which is not in the destination error set results in safety-protected {#link|Undefined Behavior#}.8428 inferred result type. Attempting to convert an error which is not in the destination error
8429 set results in safety-protected {#link|Undefined Behavior#}.
8427 </p>8430 </p>
8428 {#header_close#}8431 {#header_close#}
84298432
...@@ -8535,17 +8538,17 @@ test "decl access by string" {...@@ -8535,17 +8538,17 @@ test "decl access by string" {
8535 {#header_close#}8538 {#header_close#}
85368539
8537 {#header_open|@floatCast#}8540 {#header_open|@floatCast#}
8538 <pre>{#syntax#}@floatCast(comptime DestType: type, value: anytype) DestType{#endsyntax#}</pre>8541 <pre>{#syntax#}@floatCast(value: anytype) anytype{#endsyntax#}</pre>
8539 <p>8542 <p>
8540 Convert from one float type to another. This cast is safe, but may cause the8543 Convert from one float type to another. This cast is safe, but may cause the
8541 numeric value to lose precision.8544 numeric value to lose precision. The return type is the inferred result type.
8542 </p>8545 </p>
8543 {#header_close#}8546 {#header_close#}
85448547
8545 {#header_open|@intFromFloat#}8548 {#header_open|@intFromFloat#}
8546 <pre>{#syntax#}@intFromFloat(comptime DestType: type, float: anytype) DestType{#endsyntax#}</pre>8549 <pre>{#syntax#}@intFromFloat(float: anytype) anytype{#endsyntax#}</pre>
8547 <p>8550 <p>
8548 Converts the integer part of a floating point number to the destination type.8551 Converts the integer part of a floating point number to the inferred result type.
8549 </p>8552 </p>
8550 <p>8553 <p>
8551 If the integer part of the floating point number cannot fit in the destination type,8554 If the integer part of the floating point number cannot fit in the destination type,
...@@ -8660,16 +8663,17 @@ test "@hasDecl" {...@@ -8660,16 +8663,17 @@ test "@hasDecl" {
8660 {#header_close#}8663 {#header_close#}
86618664
8662 {#header_open|@intCast#}8665 {#header_open|@intCast#}
8663 <pre>{#syntax#}@intCast(comptime DestType: type, int: anytype) DestType{#endsyntax#}</pre>8666 <pre>{#syntax#}@intCast(int: anytype) anytype{#endsyntax#}</pre>
8664 <p>8667 <p>
8665 Converts an integer to another integer while keeping the same numerical value.8668 Converts an integer to another integer while keeping the same numerical value.
8669 The return type is the inferred result type.
8666 Attempting to convert a number which is out of range of the destination type results in8670 Attempting to convert a number which is out of range of the destination type results in
8667 safety-protected {#link|Undefined Behavior#}.8671 safety-protected {#link|Undefined Behavior#}.
8668 </p>8672 </p>
8669 {#code_begin|test_err|test_intCast_builtin|cast truncated bits#}8673 {#code_begin|test_err|test_intCast_builtin|cast truncated bits#}
8670test "integer cast panic" {8674test "integer cast panic" {
8671 var a: u16 = 0xabcd;8675 var a: u16 = 0xabcd;
8672 var b: u8 = @intCast(u8, a);8676 var b: u8 = @intCast(a);
8673 _ = b;8677 _ = b;
8674}8678}
8675 {#code_end#}8679 {#code_end#}
...@@ -8683,9 +8687,9 @@ test "integer cast panic" {...@@ -8683,9 +8687,9 @@ test "integer cast panic" {
8683 {#header_close#}8687 {#header_close#}
86848688
8685 {#header_open|@enumFromInt#}8689 {#header_open|@enumFromInt#}
8686 <pre>{#syntax#}@enumFromInt(comptime DestType: type, integer: anytype) DestType{#endsyntax#}</pre>8690 <pre>{#syntax#}@enumFromInt(integer: anytype) anytype{#endsyntax#}</pre>
8687 <p>8691 <p>
8688 Converts an integer into an {#link|enum#} value.8692 Converts an integer into an {#link|enum#} value. The return type is the inferred result type.
8689 </p>8693 </p>
8690 <p>8694 <p>
8691 Attempting to convert an integer which represents no value in the chosen enum type invokes8695 Attempting to convert an integer which represents no value in the chosen enum type invokes
...@@ -8711,16 +8715,18 @@ test "integer cast panic" {...@@ -8711,16 +8715,18 @@ test "integer cast panic" {
8711 {#header_close#}8715 {#header_close#}
87128716
8713 {#header_open|@floatFromInt#}8717 {#header_open|@floatFromInt#}
8714 <pre>{#syntax#}@floatFromInt(comptime DestType: type, int: anytype) DestType{#endsyntax#}</pre>8718 <pre>{#syntax#}@floatFromInt(int: anytype) anytype{#endsyntax#}</pre>
8715 <p>8719 <p>
8716 Converts an integer to the closest floating point representation. To convert the other way, use {#link|@intFromFloat#}. This cast is always safe.8720 Converts an integer to the closest floating point representation. The return type is the inferred result type.
8721 To convert the other way, use {#link|@intFromFloat#}. This cast is always safe.
8717 </p>8722 </p>
8718 {#header_close#}8723 {#header_close#}
87198724
8720 {#header_open|@ptrFromInt#}8725 {#header_open|@ptrFromInt#}
8721 <pre>{#syntax#}@ptrFromInt(comptime DestType: type, address: usize) DestType{#endsyntax#}</pre>8726 <pre>{#syntax#}@ptrFromInt(address: usize) anytype{#endsyntax#}</pre>
8722 <p>8727 <p>
8723 Converts an integer to a {#link|pointer|Pointers#}. To convert the other way, use {#link|@intFromPtr#}. Casting an address of 0 to a destination type8728 Converts an integer to a {#link|pointer|Pointers#}. The return type is the inferred result type.
8729 To convert the other way, use {#link|@intFromPtr#}. Casting an address of 0 to a destination type
8724 which in not {#link|optional|Optional Pointers#} and does not have the {#syntax#}allowzero{#endsyntax#} attribute will result in a8730 which in not {#link|optional|Optional Pointers#} and does not have the {#syntax#}allowzero{#endsyntax#} attribute will result in a
8725 {#link|Pointer Cast Invalid Null#} panic when runtime safety checks are enabled.8731 {#link|Pointer Cast Invalid Null#} panic when runtime safety checks are enabled.
8726 </p>8732 </p>
...@@ -8924,9 +8930,9 @@ pub const PrefetchOptions = struct {...@@ -8924,9 +8930,9 @@ pub const PrefetchOptions = struct {
8924 {#header_close#}8930 {#header_close#}
89258931
8926 {#header_open|@ptrCast#}8932 {#header_open|@ptrCast#}
8927 <pre>{#syntax#}@ptrCast(comptime DestType: type, value: anytype) DestType{#endsyntax#}</pre>8933 <pre>{#syntax#}@ptrCast(value: anytype) anytype{#endsyntax#}</pre>
8928 <p>8934 <p>
8929 Converts a pointer of one type to a pointer of another type.8935 Converts a pointer of one type to a pointer of another type. The return type is the inferred result type.
8930 </p>8936 </p>
8931 <p>8937 <p>
8932 {#link|Optional Pointers#} are allowed. Casting an optional pointer which is {#link|null#}8938 {#link|Optional Pointers#} are allowed. Casting an optional pointer which is {#link|null#}
...@@ -9522,10 +9528,10 @@ fn List(comptime T: type) type {...@@ -9522,10 +9528,10 @@ fn List(comptime T: type) type {
9522 {#header_close#}9528 {#header_close#}
95239529
9524 {#header_open|@truncate#}9530 {#header_open|@truncate#}
9525 <pre>{#syntax#}@truncate(comptime T: type, integer: anytype) T{#endsyntax#}</pre>9531 <pre>{#syntax#}@truncate(integer: anytype) anytype{#endsyntax#}</pre>
9526 <p>9532 <p>
9527 This function truncates bits from an integer type, resulting in a smaller9533 This function truncates bits from an integer type, resulting in a smaller
9528 or same-sized integer type.9534 or same-sized integer type. The return type is the inferred result type.
9529 </p>9535 </p>
9530 <p>9536 <p>
9531 This function always truncates the significant bits of the integer, regardless9537 This function always truncates the significant bits of the integer, regardless
...@@ -9540,7 +9546,7 @@ const expect = std.testing.expect;...@@ -9540,7 +9546,7 @@ const expect = std.testing.expect;
95409546
9541test "integer truncation" {9547test "integer truncation" {
9542 var a: u16 = 0xabcd;9548 var a: u16 = 0xabcd;
9543 var b: u8 = @truncate(u8, a);9549 var b: u8 = @truncate(a);
9544 try expect(b == 0xcd);9550 try expect(b == 0xcd);
9545}9551}
9546 {#code_end#}9552 {#code_end#}
...@@ -9838,7 +9844,7 @@ fn foo(x: []const u8) u8 {...@@ -9838,7 +9844,7 @@ fn foo(x: []const u8) u8 {
9838 {#code_begin|test_err|test_comptime_invalid_cast|type 'u32' cannot represent integer value '-1'#}9844 {#code_begin|test_err|test_comptime_invalid_cast|type 'u32' cannot represent integer value '-1'#}
9839comptime {9845comptime {
9840 var value: i32 = -1;9846 var value: i32 = -1;
9841 const unsigned = @intCast(u32, value);9847 const unsigned: u32 = @intCast(value);
9842 _ = unsigned;9848 _ = unsigned;
9843}9849}
9844 {#code_end#}9850 {#code_end#}
...@@ -9848,7 +9854,7 @@ const std = @import("std");...@@ -9848,7 +9854,7 @@ const std = @import("std");
98489854
9849pub fn main() void {9855pub fn main() void {
9850 var value: i32 = -1;9856 var value: i32 = -1;
9851 var unsigned = @intCast(u32, value);9857 var unsigned: u32 = @intCast(value);
9852 std.debug.print("value: {}\n", .{unsigned});9858 std.debug.print("value: {}\n", .{unsigned});
9853}9859}
9854 {#code_end#}9860 {#code_end#}
...@@ -9861,7 +9867,7 @@ pub fn main() void {...@@ -9861,7 +9867,7 @@ pub fn main() void {
9861 {#code_begin|test_err|test_comptime_invalid_cast_truncate|type 'u8' cannot represent integer value '300'#}9867 {#code_begin|test_err|test_comptime_invalid_cast_truncate|type 'u8' cannot represent integer value '300'#}
9862comptime {9868comptime {
9863 const spartan_count: u16 = 300;9869 const spartan_count: u16 = 300;
9864 const byte = @intCast(u8, spartan_count);9870 const byte: u8 = @intCast(spartan_count);
9865 _ = byte;9871 _ = byte;
9866}9872}
9867 {#code_end#}9873 {#code_end#}
...@@ -9871,7 +9877,7 @@ const std = @import("std");...@@ -9871,7 +9877,7 @@ const std = @import("std");
98719877
9872pub fn main() void {9878pub fn main() void {
9873 var spartan_count: u16 = 300;9879 var spartan_count: u16 = 300;
9874 const byte = @intCast(u8, spartan_count);9880 const byte: u8 = @intCast(spartan_count);
9875 std.debug.print("value: {}\n", .{byte});9881 std.debug.print("value: {}\n", .{byte});
9876}9882}
9877 {#code_end#}9883 {#code_end#}
...@@ -10208,7 +10214,7 @@ const Foo = enum {...@@ -10208,7 +10214,7 @@ const Foo = enum {
10208};10214};
10209comptime {10215comptime {
10210 const a: u2 = 3;10216 const a: u2 = 3;
10211 const b = @enumFromInt(Foo, a);10217 const b: Foo = @enumFromInt(a);
10212 _ = b;10218 _ = b;
10213}10219}
10214 {#code_end#}10220 {#code_end#}
...@@ -10224,7 +10230,7 @@ const Foo = enum {...@@ -10224,7 +10230,7 @@ const Foo = enum {
1022410230
10225pub fn main() void {10231pub fn main() void {
10226 var a: u2 = 3;10232 var a: u2 = 3;
10227 var b = @enumFromInt(Foo, a);10233 var b: Foo = @enumFromInt(a);
10228 std.debug.print("value: {s}\n", .{@tagName(b)});10234 std.debug.print("value: {s}\n", .{@tagName(b)});
10229}10235}
10230 {#code_end#}10236 {#code_end#}
...@@ -10242,7 +10248,7 @@ const Set2 = error{...@@ -10242,7 +10248,7 @@ const Set2 = error{
10242 C,10248 C,
10243};10249};
10244comptime {10250comptime {
10245 _ = @errSetCast(Set2, Set1.B);10251 _ = @as(Set2, @errSetCast(Set1.B));
10246}10252}
10247 {#code_end#}10253 {#code_end#}
10248 <p>At runtime:</p>10254 <p>At runtime:</p>
...@@ -10261,7 +10267,7 @@ pub fn main() void {...@@ -10261,7 +10267,7 @@ pub fn main() void {
10261 foo(Set1.B);10267 foo(Set1.B);
10262}10268}
10263fn foo(set1: Set1) void {10269fn foo(set1: Set1) void {
10264 const x = @errSetCast(Set2, set1);10270 const x = @as(Set2, @errSetCast(set1));
10265 std.debug.print("value: {}\n", .{x});10271 std.debug.print("value: {}\n", .{x});
10266}10272}
10267 {#code_end#}10273 {#code_end#}
...@@ -10271,8 +10277,8 @@ fn foo(set1: Set1) void {...@@ -10271,8 +10277,8 @@ fn foo(set1: Set1) void {
10271 <p>At compile-time:</p>10277 <p>At compile-time:</p>
10272 {#code_begin|test_err|test_comptime_incorrect_pointer_alignment|pointer address 0x1 is not aligned to 4 bytes#}10278 {#code_begin|test_err|test_comptime_incorrect_pointer_alignment|pointer address 0x1 is not aligned to 4 bytes#}
10273comptime {10279comptime {
10274 const ptr = @ptrFromInt(*align(1) i32, 0x1);10280 const ptr: *align(1) i32 = @ptrFromInt(0x1);
10275 const aligned = @alignCast(4, ptr);10281 const aligned: *align(4) i32 = @alignCast(ptr);
10276 _ = aligned;10282 _ = aligned;
10277}10283}
10278 {#code_end#}10284 {#code_end#}
...@@ -10286,7 +10292,7 @@ pub fn main() !void {...@@ -10286,7 +10292,7 @@ pub fn main() !void {
10286}10292}
10287fn foo(bytes: []u8) u32 {10293fn foo(bytes: []u8) u32 {
10288 const slice4 = bytes[1..5];10294 const slice4 = bytes[1..5];
10289 const int_slice = mem.bytesAsSlice(u32, @alignCast(4, slice4));10295 const int_slice = mem.bytesAsSlice(u32, @as([]align(4) u8, @alignCast(slice4)));
10290 return int_slice[0];10296 return int_slice[0];
10291}10297}
10292 {#code_end#}10298 {#code_end#}
...@@ -10387,7 +10393,7 @@ fn bar(f: *Foo) void {...@@ -10387,7 +10393,7 @@ fn bar(f: *Foo) void {
10387 {#code_begin|test_err|test_comptime_invalid_null_pointer_cast|null pointer casted to type#}10393 {#code_begin|test_err|test_comptime_invalid_null_pointer_cast|null pointer casted to type#}
10388comptime {10394comptime {
10389 const opt_ptr: ?*i32 = null;10395 const opt_ptr: ?*i32 = null;
10390 const ptr = @ptrCast(*i32, opt_ptr);10396 const ptr: *i32 = @ptrCast(opt_ptr);
10391 _ = ptr;10397 _ = ptr;
10392}10398}
10393 {#code_end#}10399 {#code_end#}
...@@ -10395,7 +10401,7 @@ comptime {...@@ -10395,7 +10401,7 @@ comptime {
10395 {#code_begin|exe_err|runtime_invalid_null_pointer_cast#}10401 {#code_begin|exe_err|runtime_invalid_null_pointer_cast#}
10396pub fn main() void {10402pub fn main() void {
10397 var opt_ptr: ?*i32 = null;10403 var opt_ptr: ?*i32 = null;
10398 var ptr = @ptrCast(*i32, opt_ptr);10404 var ptr: *i32 = @ptrCast(opt_ptr);
10399 _ = ptr;10405 _ = ptr;
10400}10406}
10401 {#code_end#}10407 {#code_end#}