authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-02-13 16:19:17+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-02-15 01:43:57+02:00
log7199d7c77715fe06606c5c89595e6852b3fa8c20
tree7ffd59e66cc2074d2da5284c9c22d69ddfc92968
parent4e6f21e2cb2c557b5c019f4acf445665a26edcba

split `@qualCast` into `@constCast` and `@volatileCast`


18 files changed, 126 insertions(+), 87 deletions(-)

doc/langref.html.in+16-8
......@@ -8129,6 +8129,13 @@ test "main" {
81298129 {#code_end#}
81308130 {#header_close#}
81318131
8132 {#header_open|@constCast#}
8133 <pre>{#syntax#}@constCast(value: anytype) DestType{#endsyntax#}</pre>
8134 <p>
8135 Remove {#syntax#}const{#endsyntax#} qualifier from a pointer.
8136 </p>
8137 {#header_close#}
8138
81328139 {#header_open|@ctz#}
81338140 <pre>{#syntax#}@ctz(operand: anytype){#endsyntax#}</pre>
81348141 <p>{#syntax#}@TypeOf(operand){#endsyntax#} must be an integer type or an integer vector type.</p>
......@@ -8814,7 +8821,8 @@ pub const PrefetchOptions = struct {
88148821 {#syntax#}@ptrCast{#endsyntax#} cannot be used for:
88158822 </p>
88168823 <ul>
8817 <li>Removing {#syntax#}const{#endsyntax#} or {#syntax#}volatile{#endsyntax#} qualifier, use {#link|@qualCast#}.</li>
8824 <li>Removing {#syntax#}const{#endsyntax#} qualifier, use {#link|@constCast#}.</li>
8825 <li>Removing {#syntax#}volatile{#endsyntax#} qualifier, use {#link|@volatileCast#}.</li>
88188826 <li>Changing pointer address space, use {#link|@addrSpaceCast#}.</li>
88198827 <li>Increasing pointer alignment, use {#link|@alignCast#}.</li>
88208828 <li>Casting a non-slice pointer to a slice, use slicing syntax {#syntax#}ptr[start..end]{#endsyntax#}.</li>
......@@ -8831,13 +8839,6 @@ pub const PrefetchOptions = struct {
88318839
88328840 {#header_close#}
88338841
8834 {#header_open|@qualCast#}
8835 <pre>{#syntax#}@qualCast(comptime DestType: type, value: anytype) DestType{#endsyntax#}</pre>
8836 <p>
8837 Remove {#syntax#}const{#endsyntax#} or {#syntax#}volatile{#endsyntax#} qualifier from a pointer.
8838 </p>
8839 {#header_close#}
8840
88418842 {#header_open|@rem#}
88428843 <pre>{#syntax#}@rem(numerator: T, denominator: T) T{#endsyntax#}</pre>
88438844 <p>
......@@ -9525,6 +9526,13 @@ fn foo(comptime T: type, ptr: *T) T {
95259526 <pre>{#syntax#}@Vector(len: comptime_int, Element: type) type{#endsyntax#}</pre>
95269527 <p>Creates {#link|Vectors#}.</p>
95279528 {#header_close#}
9529
9530 {#header_open|@volatileCast#}
9531 <pre>{#syntax#}@volatileCast(value: anytype) DestType{#endsyntax#}</pre>
9532 <p>
9533 Remove {#syntax#}volatile{#endsyntax#} qualifier from a pointer.
9534 </p>
9535 {#header_close#}
95289536 {#header_close#}
95299537
95309538 {#header_open|Build Mode#}
lib/docs/main.js+6-2
......@@ -1354,8 +1354,12 @@ const NAV_MODES = {
13541354 payloadHtml += "ptrCast";
13551355 break;
13561356 }
1357 case "qual_cast": {
1358 payloadHtml += "qualCast";
1357 case "const_cast": {
1358 payloadHtml += "constCast";
1359 break;
1360 }
1361 case "volatile_cast": {
1362 payloadHtml += "volatileCast";
13591363 break;
13601364 }
13611365 case "truncate": {
lib/std/child_process.zig+2-2
......@@ -1164,7 +1164,7 @@ fn windowsCreateProcessPathExt(
11641164 var app_name_unicode_string = windows.UNICODE_STRING{
11651165 .Length = app_name_len_bytes,
11661166 .MaximumLength = app_name_len_bytes,
1167 .Buffer = @qualCast([*:0]u16, app_name_wildcard.ptr),
1167 .Buffer = @constCast(app_name_wildcard.ptr),
11681168 };
11691169 const rc = windows.ntdll.NtQueryDirectoryFile(
11701170 dir.fd,
......@@ -1261,7 +1261,7 @@ fn windowsCreateProcessPathExt(
12611261 var app_name_unicode_string = windows.UNICODE_STRING{
12621262 .Length = app_name_len_bytes,
12631263 .MaximumLength = app_name_len_bytes,
1264 .Buffer = @qualCast([*:0]u16, app_name_appended.ptr),
1264 .Buffer = @constCast(app_name_appended.ptr),
12651265 };
12661266
12671267 // Re-use the directory handle but this time we call with the appended app name
lib/std/fs.zig+1-1
......@@ -1763,7 +1763,7 @@ pub const Dir = struct {
17631763 var nt_name = w.UNICODE_STRING{
17641764 .Length = path_len_bytes,
17651765 .MaximumLength = path_len_bytes,
1766 .Buffer = @qualCast([*:0]u16, sub_path_w),
1766 .Buffer = @constCast(sub_path_w),
17671767 };
17681768 var attr = w.OBJECT_ATTRIBUTES{
17691769 .Length = @sizeOf(w.OBJECT_ATTRIBUTES),
lib/std/mem/Allocator.zig+2-2
......@@ -112,7 +112,7 @@ pub fn destroy(self: Allocator, ptr: anytype) void {
112112 const info = @typeInfo(@TypeOf(ptr)).Pointer;
113113 const T = info.child;
114114 if (@sizeOf(T) == 0) return;
115 const non_const_ptr = @intToPtr([*]u8, @ptrToInt(ptr));
115 const non_const_ptr = @ptrCast([*]u8, @constCast(ptr));
116116 self.rawFree(non_const_ptr[0..@sizeOf(T)], math.log2(info.alignment), @returnAddress());
117117}
118118
......@@ -297,7 +297,7 @@ pub fn free(self: Allocator, memory: anytype) void {
297297 const bytes = mem.sliceAsBytes(memory);
298298 const bytes_len = bytes.len + if (Slice.sentinel != null) @sizeOf(Slice.child) else 0;
299299 if (bytes_len == 0) return;
300 const non_const_ptr = @intToPtr([*]u8, @ptrToInt(bytes.ptr));
300 const non_const_ptr = @constCast(bytes.ptr);
301301 // TODO: https://github.com/ziglang/zig/issues/4298
302302 @memset(non_const_ptr, undefined, bytes_len);
303303 self.rawFree(non_const_ptr[0..bytes_len], log2a(Slice.alignment), @returnAddress());
lib/std/os.zig+1-1
......@@ -4513,7 +4513,7 @@ pub fn faccessatW(dirfd: fd_t, sub_path_w: [*:0]const u16, mode: u32, flags: u32
45134513 var nt_name = windows.UNICODE_STRING{
45144514 .Length = path_len_bytes,
45154515 .MaximumLength = path_len_bytes,
4516 .Buffer = @qualCast([*:0]u16, sub_path_w),
4516 .Buffer = @constCast(sub_path_w),
45174517 };
45184518 var attr = windows.OBJECT_ATTRIBUTES{
45194519 .Length = @sizeOf(windows.OBJECT_ATTRIBUTES),
lib/std/os/windows.zig+7-7
......@@ -85,7 +85,7 @@ pub fn OpenFile(sub_path_w: []const u16, options: OpenFileOptions) OpenError!HAN
8585 var nt_name = UNICODE_STRING{
8686 .Length = path_len_bytes,
8787 .MaximumLength = path_len_bytes,
88 .Buffer = @qualCast([*]u16, sub_path_w.ptr),
88 .Buffer = @constCast(sub_path_w.ptr),
8989 };
9090 var attr = OBJECT_ATTRIBUTES{
9191 .Length = @sizeOf(OBJECT_ATTRIBUTES),
......@@ -634,7 +634,7 @@ pub fn SetCurrentDirectory(path_name: []const u16) SetCurrentDirectoryError!void
634634 var nt_name = UNICODE_STRING{
635635 .Length = path_len_bytes,
636636 .MaximumLength = path_len_bytes,
637 .Buffer = @qualCast([*]u16, path_name.ptr),
637 .Buffer = @constCast(path_name.ptr),
638638 };
639639
640640 const rc = ntdll.RtlSetCurrentDirectory_U(&nt_name);
......@@ -766,7 +766,7 @@ pub fn ReadLink(dir: ?HANDLE, sub_path_w: []const u16, out_buffer: []u8) ReadLin
766766 var nt_name = UNICODE_STRING{
767767 .Length = path_len_bytes,
768768 .MaximumLength = path_len_bytes,
769 .Buffer = @qualCast([*]u16, sub_path_w.ptr),
769 .Buffer = @constCast(sub_path_w.ptr),
770770 };
771771 var attr = OBJECT_ATTRIBUTES{
772772 .Length = @sizeOf(OBJECT_ATTRIBUTES),
......@@ -876,7 +876,7 @@ pub fn DeleteFile(sub_path_w: []const u16, options: DeleteFileOptions) DeleteFil
876876 .Length = path_len_bytes,
877877 .MaximumLength = path_len_bytes,
878878 // The Windows API makes this mutable, but it will not mutate here.
879 .Buffer = @qualCast([*]u16, sub_path_w.ptr),
879 .Buffer = @constCast(sub_path_w.ptr),
880880 };
881881
882882 if (sub_path_w[0] == '.' and sub_path_w[1] == 0) {
......@@ -1414,7 +1414,7 @@ pub fn sendmsg(
14141414}
14151415
14161416pub fn sendto(s: ws2_32.SOCKET, buf: [*]const u8, len: usize, flags: u32, to: ?*const ws2_32.sockaddr, to_len: ws2_32.socklen_t) i32 {
1417 var buffer = ws2_32.WSABUF{ .len = @truncate(u31, len), .buf = @qualCast([*]u8, buf) };
1417 var buffer = ws2_32.WSABUF{ .len = @truncate(u31, len), .buf = @constCast(buf) };
14181418 var bytes_send: DWORD = undefined;
14191419 if (ws2_32.WSASendTo(s, @ptrCast([*]ws2_32.WSABUF, &buffer), 1, &bytes_send, flags, to, @intCast(i32, to_len), null, null) == ws2_32.SOCKET_ERROR) {
14201420 return ws2_32.SOCKET_ERROR;
......@@ -1876,13 +1876,13 @@ pub fn eqlIgnoreCaseWTF16(a: []const u16, b: []const u16) bool {
18761876 const a_string = UNICODE_STRING{
18771877 .Length = a_bytes,
18781878 .MaximumLength = a_bytes,
1879 .Buffer = @qualCast([*]u16, a.ptr),
1879 .Buffer = @constCast(a.ptr),
18801880 };
18811881 const b_bytes = @intCast(u16, b.len * 2);
18821882 const b_string = UNICODE_STRING{
18831883 .Length = b_bytes,
18841884 .MaximumLength = b_bytes,
1885 .Buffer = @qualCast([*]u16, b.ptr),
1885 .Buffer = @constCast(b.ptr),
18861886 };
18871887 return ntdll.RtlEqualUnicodeString(&a_string, &b_string, TRUE) == TRUE;
18881888}
lib/std/zig/c_translation.zig+4-2
......@@ -74,8 +74,10 @@ fn castPtr(comptime DestType: type, target: anytype) DestType {
7474 const dest = ptrInfo(DestType);
7575 const source = ptrInfo(@TypeOf(target));
7676
77 if (source.is_const and !dest.is_const or source.is_volatile and !dest.is_volatile)
78 return @qualCast(DestType, target)
77 if (source.is_const and !dest.is_const)
78 return @constCast(target)
79 else if (source.is_volatile and !dest.is_volatile)
80 return @volatileCast(target)
7981 else if (@typeInfo(dest.child) == .Opaque)
8082 // dest.alignment would error out
8183 return @ptrCast(DestType, target)
src/AstGen.zig+16-2
......@@ -2531,7 +2531,6 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
25312531 .bit_size_of,
25322532 .typeof_log2_int_type,
25332533 .ptr_to_int,
2534 .qual_cast,
25352534 .align_of,
25362535 .bool_to_int,
25372536 .embed_file,
......@@ -8039,7 +8038,6 @@ fn builtinCall(
80398038 .float_cast => return typeCast(gz, scope, ri, node, params[0], params[1], .float_cast),
80408039 .int_cast => return typeCast(gz, scope, ri, node, params[0], params[1], .int_cast),
80418040 .ptr_cast => return typeCast(gz, scope, ri, node, params[0], params[1], .ptr_cast),
8042 .qual_cast => return typeCast(gz, scope, ri, node, params[0], params[1], .qual_cast),
80438041 .truncate => return typeCast(gz, scope, ri, node, params[0], params[1], .truncate),
80448042 // zig fmt: on
80458043
......@@ -8115,6 +8113,22 @@ fn builtinCall(
81158113 });
81168114 return rvalue(gz, ri, result, node);
81178115 },
8116 .const_cast => {
8117 const operand = try expr(gz, scope, .{ .rl = .none }, params[0]);
8118 const result = try gz.addExtendedPayload(.const_cast, Zir.Inst.UnNode{
8119 .node = gz.nodeIndexToRelative(node),
8120 .operand = operand,
8121 });
8122 return rvalue(gz, ri, result, node);
8123 },
8124 .volatile_cast => {
8125 const operand = try expr(gz, scope, .{ .rl = .none }, params[0]);
8126 const result = try gz.addExtendedPayload(.volatile_cast, Zir.Inst.UnNode{
8127 .node = gz.nodeIndexToRelative(node),
8128 .operand = operand,
8129 });
8130 return rvalue(gz, ri, result, node);
8131 },
81188132
81198133 // zig fmt: off
81208134 .has_decl => return hasDeclOrField(gz, scope, ri, node, params[0], params[1], .has_decl),
src/Autodoc.zig+2-1
......@@ -1400,7 +1400,6 @@ fn walkInstruction(
14001400 .float_cast,
14011401 .int_cast,
14021402 .ptr_cast,
1403 .qual_cast,
14041403 .truncate,
14051404 .align_cast,
14061405 .has_decl,
......@@ -2983,6 +2982,8 @@ fn walkInstruction(
29832982 .error_to_int,
29842983 .int_to_error,
29852984 .reify,
2985 .const_cast,
2986 .volatile_cast,
29862987 => {
29872988 const extra = file.zir.extraData(Zir.Inst.UnNode, extended.operand).data;
29882989 const bin_index = self.exprs.items.len;
src/BuiltinFn.zig+16-8
......@@ -28,6 +28,7 @@ pub const Tag = enum {
2828 cmpxchg_weak,
2929 compile_error,
3030 compile_log,
31 const_cast,
3132 ctz,
3233 c_undef,
3334 c_va_arg,
......@@ -75,7 +76,6 @@ pub const Tag = enum {
7576 prefetch,
7677 ptr_cast,
7778 ptr_to_int,
78 qual_cast,
7979 rem,
8080 return_address,
8181 select,
......@@ -116,6 +116,7 @@ pub const Tag = enum {
116116 TypeOf,
117117 union_init,
118118 Vector,
119 volatile_cast,
119120};
120121
121122pub const MemLocRequirement = enum {
......@@ -345,6 +346,13 @@ pub const list = list: {
345346 .param_count = null,
346347 },
347348 },
349 .{
350 "@constCast",
351 .{
352 .tag = .const_cast,
353 .param_count = 1,
354 },
355 },
348356 .{
349357 "@ctz",
350358 .{
......@@ -675,13 +683,6 @@ pub const list = list: {
675683 .param_count = 1,
676684 },
677685 },
678 .{
679 "@qualCast",
680 .{
681 .tag = .qual_cast,
682 .param_count = 2,
683 },
684 },
685686 .{
686687 "@rem",
687688 .{
......@@ -964,5 +965,12 @@ pub const list = list: {
964965 .param_count = 2,
965966 },
966967 },
968 .{
969 "@volatileCast",
970 .{
971 .tag = .volatile_cast,
972 .param_count = 1,
973 },
974 },
967975 });
968976};
src/Sema.zig+29-31
......@@ -1015,7 +1015,6 @@ fn analyzeBodyInner(
10151015 .float_cast => try sema.zirFloatCast(block, inst),
10161016 .int_cast => try sema.zirIntCast(block, inst),
10171017 .ptr_cast => try sema.zirPtrCast(block, inst),
1018 .qual_cast => try sema.zirQualCast(block, inst),
10191018 .truncate => try sema.zirTruncate(block, inst),
10201019 .align_cast => try sema.zirAlignCast(block, inst),
10211020 .has_decl => try sema.zirHasDecl(block, inst),
......@@ -1147,6 +1146,8 @@ fn analyzeBodyInner(
11471146 .c_va_copy => try sema.zirCVaCopy( block, extended),
11481147 .c_va_end => try sema.zirCVaEnd( block, extended),
11491148 .c_va_start => try sema.zirCVaStart( block, extended),
1149 .const_cast, => try sema.zirConstCast( block, extended),
1150 .volatile_cast, => try sema.zirVolatileCast( block, extended),
11501151 // zig fmt: on
11511152
11521153 .fence => {
......@@ -19545,7 +19546,7 @@ fn zirPtrCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1954519546 const msg = try sema.errMsg(block, src, "cast discards const qualifier", .{});
1954619547 errdefer msg.destroy(sema.gpa);
1954719548
19548 try sema.errNote(block, src, msg, "consider using '@qualCast'", .{});
19549 try sema.errNote(block, src, msg, "consider using '@constCast'", .{});
1954919550 break :msg msg;
1955019551 };
1955119552 return sema.failWithOwnedErrorMsg(msg);
......@@ -19555,7 +19556,7 @@ fn zirPtrCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1955519556 const msg = try sema.errMsg(block, src, "cast discards volatile qualifier", .{});
1955619557 errdefer msg.destroy(sema.gpa);
1955719558
19558 try sema.errNote(block, src, msg, "consider using '@qualCast'", .{});
19559 try sema.errNote(block, src, msg, "consider using '@volatileCast'", .{});
1955919560 break :msg msg;
1956019561 };
1956119562 return sema.failWithOwnedErrorMsg(msg);
......@@ -19660,40 +19661,37 @@ fn zirPtrCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1966019661 return block.addBitCast(aligned_dest_ty, ptr);
1966119662}
1966219663
19663fn zirQualCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
19664 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
19665 const src = inst_data.src();
19666 const dest_ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
19667 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
19668 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
19669 const dest_ty = try sema.resolveType(block, dest_ty_src, extra.lhs);
19670 const operand = try sema.resolveInst(extra.rhs);
19664fn zirConstCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
19665 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
19666 const src = LazySrcLoc.nodeOffset(extra.node);
19667 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
19668 const operand = try sema.resolveInst(extra.operand);
1967119669 const operand_ty = sema.typeOf(operand);
19672
19673 try sema.checkPtrType(block, dest_ty_src, dest_ty);
1967419670 try sema.checkPtrOperand(block, operand_src, operand_ty);
1967519671
19676 var operand_payload = operand_ty.ptrInfo();
19677 var dest_info = dest_ty.ptrInfo();
19672 var ptr_info = operand_ty.ptrInfo().data;
19673 ptr_info.mutable = true;
19674 const dest_ty = try Type.ptr(sema.arena, sema.mod, ptr_info);
19675
19676 if (try sema.resolveMaybeUndefVal(operand)) |operand_val| {
19677 return sema.addConstant(dest_ty, operand_val);
19678 }
1967819679
19679 operand_payload.data.mutable = dest_info.data.mutable;
19680 operand_payload.data.@"volatile" = dest_info.data.@"volatile";
19680 try sema.requireRuntimeBlock(block, src, null);
19681 return block.addBitCast(dest_ty, operand);
19682}
1968119683
19682 const altered_operand_ty = Type.initPayload(&operand_payload.base);
19683 if (!altered_operand_ty.eql(dest_ty, sema.mod)) {
19684 const msg = msg: {
19685 const msg = try sema.errMsg(block, src, "'@qualCast' can only modify 'const' and 'volatile' qualifiers", .{});
19686 errdefer msg.destroy(sema.gpa);
19684fn zirVolatileCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
19685 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
19686 const src = LazySrcLoc.nodeOffset(extra.node);
19687 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
19688 const operand = try sema.resolveInst(extra.operand);
19689 const operand_ty = sema.typeOf(operand);
19690 try sema.checkPtrOperand(block, operand_src, operand_ty);
1968719691
19688 dest_info.data.mutable = !operand_ty.isConstPtr();
19689 dest_info.data.@"volatile" = operand_ty.isVolatilePtr();
19690 const altered_dest_ty = Type.initPayload(&dest_info.base);
19691 try sema.errNote(block, src, msg, "expected type '{}'", .{altered_dest_ty.fmt(sema.mod)});
19692 try sema.errNote(block, src, msg, "got type '{}'", .{operand_ty.fmt(sema.mod)});
19693 break :msg msg;
19694 };
19695 return sema.failWithOwnedErrorMsg(msg);
19696 }
19692 var ptr_info = operand_ty.ptrInfo().data;
19693 ptr_info.@"volatile" = false;
19694 const dest_ty = try Type.ptr(sema.arena, sema.mod, ptr_info);
1969719695
1969819696 if (try sema.resolveMaybeUndefVal(operand)) |operand_val| {
1969919697 return sema.addConstant(dest_ty, operand_val);
src/Zir.zig+6-6
......@@ -857,9 +857,6 @@ pub const Inst = struct {
857857 /// Implements the `@ptrCast` builtin.
858858 /// Uses `pl_node` with payload `Bin`. `lhs` is dest type, `rhs` is operand.
859859 ptr_cast,
860 /// Implements the `@qualCast` builtin.
861 /// Uses `pl_node` with payload `Bin`. `lhs` is dest type, `rhs` is operand.
862 qual_cast,
863860 /// Implements the `@truncate` builtin.
864861 /// Uses `pl_node` with payload `Bin`. `lhs` is dest type, `rhs` is operand.
865862 truncate,
......@@ -1198,7 +1195,6 @@ pub const Inst = struct {
11981195 .float_cast,
11991196 .int_cast,
12001197 .ptr_cast,
1201 .qual_cast,
12021198 .truncate,
12031199 .align_cast,
12041200 .has_field,
......@@ -1488,7 +1484,6 @@ pub const Inst = struct {
14881484 .float_cast,
14891485 .int_cast,
14901486 .ptr_cast,
1491 .qual_cast,
14921487 .truncate,
14931488 .align_cast,
14941489 .has_field,
......@@ -1760,7 +1755,6 @@ pub const Inst = struct {
17601755 .float_cast = .pl_node,
17611756 .int_cast = .pl_node,
17621757 .ptr_cast = .pl_node,
1763 .qual_cast = .pl_node,
17641758 .truncate = .pl_node,
17651759 .align_cast = .pl_node,
17661760 .typeof_builtin = .pl_node,
......@@ -2004,6 +1998,12 @@ pub const Inst = struct {
20041998 /// Implement builtin `@cVaStart`.
20051999 /// `operand` is `src_node: i32`.
20062000 c_va_start,
2001 /// Implements the `@constCast` builtin.
2002 /// `operand` is payload index to `UnNode`.
2003 const_cast,
2004 /// Implements the `@volatileCast` builtin.
2005 /// `operand` is payload index to `UnNode`.
2006 volatile_cast,
20072007
20082008 pub const InstData = struct {
20092009 opcode: Extended,
src/print_zir.zig+2-1
......@@ -332,7 +332,6 @@ const Writer = struct {
332332 .float_cast,
333333 .int_cast,
334334 .ptr_cast,
335 .qual_cast,
336335 .truncate,
337336 .align_cast,
338337 .div_exact,
......@@ -507,6 +506,8 @@ const Writer = struct {
507506 .reify,
508507 .c_va_copy,
509508 .c_va_end,
509 .const_cast,
510 .volatile_cast,
510511 => {
511512 const inst_data = self.code.extraData(Zir.Inst.UnNode, extended.operand).data;
512513 const src = LazySrcLoc.nodeOffset(inst_data.node);
stage1/zig1.wasm
Binary files a/stage1/zig1.wasm and b/stage1/zig1.wasm differ
test/behavior/cast.zig+15
......@@ -1553,3 +1553,18 @@ test "peer type resolution forms error union" {
15531553 } else @intCast(u32, foo);
15541554 try expect(try result == 123);
15551555}
1556
1557test "@constCast without a result location" {
1558 const x: i32 = 1234;
1559 const y = @constCast(&x);
1560 try expect(@TypeOf(y) == *i32);
1561 try expect(y.* == 1234);
1562}
1563
1564test "@volatileCast without a result location" {
1565 var x: i32 = 1234;
1566 var y: *volatile i32 = &x;
1567 const z = @volatileCast(y);
1568 try expect(@TypeOf(z) == *i32);
1569 try expect(z.* == 1234);
1570}
test/cases/compile_errors/invalid_qualcast.zig deleted-12
......@@ -1,12 +0,0 @@
1pub export fn entry() void {
2 var a: [*:0]const volatile u16 = undefined;
3 _ = @qualCast([*]u16, a);
4}
5
6// error
7// backend=stage2
8// target=native
9//
10// :3:9: error: '@qualCast' can only modify 'const' and 'volatile' qualifiers
11// :3:9: note: expected type '[*]const volatile u16'
12// :3:9: note: got type '[*:0]const volatile u16'
test/cases/compile_errors/ptrCast_discards_const_qualifier.zig+1-1
......@@ -9,4 +9,4 @@ export fn entry() void {
99// target=native
1010//
1111// :3:15: error: cast discards const qualifier
12// :3:15: note: consider using '@qualCast'
12// :3:15: note: consider using '@constCast'