diff --git a/src/all_types.hpp b/src/all_types.hpp index 0d364915f98e..093bbdd36a77 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1363,7 +1363,7 @@ enum BuiltinFnId { BuiltinFnIdSetFloatMode, BuiltinFnIdTypeName, BuiltinFnIdPanic, - BuiltinFnIdPtrCast, + BuiltinFnIdElemCast, BuiltinFnIdBitCast, BuiltinFnIdIntToPtr, BuiltinFnIdPtrToInt, @@ -2060,7 +2060,7 @@ enum IrInstructionId { IrInstructionIdErrWrapPayload, IrInstructionIdFnProto, IrInstructionIdTestComptime, - IrInstructionIdPtrCast, + IrInstructionIdElemCast, IrInstructionIdBitCast, IrInstructionIdWidenOrShorten, IrInstructionIdIntToPtr, @@ -2786,7 +2786,7 @@ struct IrInstructionTestComptime { IrInstruction *value; }; -struct IrInstructionPtrCast { +struct IrInstructionElemCast { IrInstruction base; IrInstruction *dest_type; diff --git a/src/codegen.cpp b/src/codegen.cpp index d05bcba2ce89..e88d103897a4 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -2605,8 +2605,8 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable, zig_unreachable(); } -static LLVMValueRef ir_render_ptr_cast(CodeGen *g, IrExecutable *executable, - IrInstructionPtrCast *instruction) +static LLVMValueRef ir_render_elem_cast(CodeGen *g, IrExecutable *executable, + IrInstructionElemCast *instruction) { TypeTableEntry *wanted_type = instruction->base.value.type; LLVMValueRef ptr = ir_llvm_value(g, instruction->ptr); @@ -4833,8 +4833,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, return ir_render_struct_init(g, executable, (IrInstructionStructInit *)instruction); case IrInstructionIdUnionInit: return ir_render_union_init(g, executable, (IrInstructionUnionInit *)instruction); - case IrInstructionIdPtrCast: - return ir_render_ptr_cast(g, executable, (IrInstructionPtrCast *)instruction); + case IrInstructionIdElemCast: + return ir_render_elem_cast(g, executable, (IrInstructionElemCast *)instruction); case IrInstructionIdBitCast: return ir_render_bit_cast(g, executable, (IrInstructionBitCast *)instruction); case IrInstructionIdWidenOrShorten: @@ -6338,7 +6338,7 @@ static void define_builtin_fns(CodeGen *g) { create_builtin_fn(g, BuiltinFnIdSetRuntimeSafety, "setRuntimeSafety", 1); create_builtin_fn(g, BuiltinFnIdSetFloatMode, "setFloatMode", 2); create_builtin_fn(g, BuiltinFnIdPanic, "panic", 1); - create_builtin_fn(g, BuiltinFnIdPtrCast, "ptrCast", 2); + create_builtin_fn(g, BuiltinFnIdElemCast, "elemCast", 2); create_builtin_fn(g, BuiltinFnIdBitCast, "bitCast", 2); create_builtin_fn(g, BuiltinFnIdIntToPtr, "intToPtr", 2); create_builtin_fn(g, BuiltinFnIdPtrToInt, "ptrToInt", 1); diff --git a/src/ir.cpp b/src/ir.cpp index e5e8dcbb9dd0..ee7180d3134e 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -544,8 +544,8 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionTestComptime *) return IrInstructionIdTestComptime; } -static constexpr IrInstructionId ir_instruction_id(IrInstructionPtrCast *) { - return IrInstructionIdPtrCast; +static constexpr IrInstructionId ir_instruction_id(IrInstructionElemCast *) { + return IrInstructionIdElemCast; } static constexpr IrInstructionId ir_instruction_id(IrInstructionBitCast *) { @@ -2201,10 +2201,10 @@ static IrInstruction *ir_build_test_comptime(IrBuilder *irb, Scope *scope, AstNo return &instruction->base; } -static IrInstruction *ir_build_ptr_cast(IrBuilder *irb, Scope *scope, AstNode *source_node, +static IrInstruction *ir_build_elem_cast(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *dest_type, IrInstruction *ptr) { - IrInstructionPtrCast *instruction = ir_build_instruction( + IrInstructionElemCast *instruction = ir_build_instruction( irb, scope, source_node); instruction->dest_type = dest_type; instruction->ptr = ptr; @@ -4127,7 +4127,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo IrInstruction *panic = ir_build_panic(irb, scope, node, arg0_value); return ir_lval_wrap(irb, scope, panic, lval); } - case BuiltinFnIdPtrCast: + case BuiltinFnIdElemCast: { AstNode *arg0_node = node->data.fn_call_expr.params.at(0); IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope); @@ -4139,8 +4139,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg1_value == irb->codegen->invalid_instruction) return arg1_value; - IrInstruction *ptr_cast = ir_build_ptr_cast(irb, scope, node, arg0_value, arg1_value); - return ir_lval_wrap(irb, scope, ptr_cast, lval); + IrInstruction *elem_cast = ir_build_elem_cast(irb, scope, node, arg0_value, arg1_value); + return ir_lval_wrap(irb, scope, elem_cast, lval); } case BuiltinFnIdBitCast: { @@ -6641,10 +6641,11 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec // Entry block gets a reference because we enter it to begin. ir_ref_bb(irb->current_basic_block); + IrInstruction *u8_type = ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_u8); + FnTableEntry *fn_entry = exec_fn_entry(irb->exec); bool is_async = fn_entry != nullptr && fn_entry->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync; IrInstruction *coro_id; - IrInstruction *u8_ptr_type; IrInstruction *const_bool_false; IrInstruction *coro_promise_ptr; IrInstruction *err_ret_trace_ptr; @@ -6672,9 +6673,7 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec ir_build_var_decl(irb, coro_scope, node, await_handle_var, await_handle_type_val, nullptr, null_value); irb->exec->await_handle_var_ptr = ir_build_var_ptr(irb, coro_scope, node, await_handle_var); - u8_ptr_type = ir_build_const_type(irb, coro_scope, node, - get_pointer_to_type(irb->codegen, irb->codegen->builtin_types.entry_u8, false)); - IrInstruction *promise_as_u8_ptr = ir_build_ptr_cast(irb, coro_scope, node, u8_ptr_type, coro_promise_ptr); + IrInstruction *promise_as_u8_ptr = ir_build_elem_cast(irb, coro_scope, node, u8_type, coro_promise_ptr); coro_id = ir_build_coro_id(irb, coro_scope, node, promise_as_u8_ptr); coro_size_var = ir_create_var(irb, node, coro_scope, nullptr, false, false, true, const_bool_false); IrInstruction *coro_size = ir_build_coro_size(irb, coro_scope, node); @@ -6698,7 +6697,8 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec ir_build_return(irb, coro_scope, node, undef); ir_set_cursor_at_end_and_append_block(irb, alloc_ok_block); - IrInstruction *coro_mem_ptr = ir_build_ptr_cast(irb, coro_scope, node, u8_ptr_type, maybe_coro_mem_ptr); + + IrInstruction *coro_mem_ptr = ir_build_elem_cast(irb, coro_scope, node, u8_type, maybe_coro_mem_ptr); irb->exec->coro_handle = ir_build_coro_begin(irb, coro_scope, node, coro_id, coro_mem_ptr); Buf *awaiter_handle_field_name = buf_create_from_str(AWAITER_HANDLE_FIELD_NAME); @@ -6772,13 +6772,10 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec ir_set_cursor_at_end_and_append_block(irb, irb->exec->coro_normal_final); if (type_has_bits(return_type)) { - IrInstruction *u8_ptr_type_unknown_len = ir_build_const_type(irb, scope, node, - get_pointer_to_type_extra(irb->codegen, irb->codegen->builtin_types.entry_u8, - false, false, PtrLenUnknown, get_abi_alignment(irb->codegen, irb->codegen->builtin_types.entry_u8), - 0, 0)); IrInstruction *result_ptr = ir_build_load_ptr(irb, scope, node, irb->exec->coro_result_ptr_field_ptr); - IrInstruction *result_ptr_as_u8_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type_unknown_len, result_ptr); - IrInstruction *return_value_ptr_as_u8_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type_unknown_len, + + IrInstruction *result_ptr_as_u8_ptr = ir_build_elem_cast(irb, scope, node, u8_type, result_ptr); + IrInstruction *return_value_ptr_as_u8_ptr = ir_build_elem_cast(irb, scope, node, u8_type, irb->exec->coro_result_field_ptr); IrInstruction *return_type_inst = ir_build_const_type(irb, scope, node, fn_entry->type_entry->data.fn.fn_type_id.return_type); @@ -6812,11 +6809,7 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec IrInstruction *free_fn = ir_build_load_ptr(irb, scope, node, free_fn_ptr); IrInstruction *zero = ir_build_const_usize(irb, scope, node, 0); IrInstruction *coro_mem_ptr_maybe = ir_build_coro_free(irb, scope, node, coro_id, irb->exec->coro_handle); - IrInstruction *u8_ptr_type_unknown_len = ir_build_const_type(irb, scope, node, - get_pointer_to_type_extra(irb->codegen, irb->codegen->builtin_types.entry_u8, - false, false, PtrLenUnknown, get_abi_alignment(irb->codegen, irb->codegen->builtin_types.entry_u8), - 0, 0)); - IrInstruction *coro_mem_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type_unknown_len, coro_mem_ptr_maybe); + IrInstruction *coro_mem_ptr = ir_build_elem_cast(irb, scope, node, u8_type, coro_mem_ptr_maybe); IrInstruction *coro_mem_ptr_ref = ir_build_ref(irb, scope, node, coro_mem_ptr, true, false); IrInstruction *coro_size_ptr = ir_build_var_ptr(irb, scope, node, coro_size_var); IrInstruction *coro_size = ir_build_load_ptr(irb, scope, node, coro_size_ptr); @@ -18684,7 +18677,7 @@ static IrInstruction *ir_align_cast(IrAnalyze *ira, IrInstruction *target, uint3 return result; } -static TypeTableEntry *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstructionPtrCast *instruction) { +static TypeTableEntry *ir_analyze_instruction_elem_cast(IrAnalyze *ira, IrInstructionElemCast *instruction) { IrInstruction *dest_type_value = instruction->dest_type->other; TypeTableEntry *dest_type = ir_resolve_type(ira, dest_type_value); if (type_is_invalid(dest_type)) @@ -18692,25 +18685,63 @@ static TypeTableEntry *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstruc IrInstruction *ptr = instruction->ptr->other; TypeTableEntry *src_type = ptr->value.type; + if (type_is_invalid(src_type)) return ira->codegen->builtin_types.entry_invalid; - if (get_codegen_ptr_type(src_type) == nullptr) { - ir_add_error(ira, ptr, buf_sprintf("expected pointer, found '%s'", buf_ptr(&src_type->name))); - return ira->codegen->builtin_types.entry_invalid; + // @TODO + // Tests for error messages, tests for pointer and promise types. + + // Check wether we can cast the source type and wrap up the destination type in the same "template" as the source type. + bool is_optional = false; + + if (src_type->id == TypeTableEntryIdOptional) { + src_type = src_type->data.maybe.child_type; + is_optional = true; } - if (get_codegen_ptr_type(dest_type) == nullptr) { - ir_add_error(ira, dest_type_value, - buf_sprintf("expected pointer, found '%s'", buf_ptr(&dest_type->name))); - return ira->codegen->builtin_types.entry_invalid; + switch (src_type->id) { + case TypeTableEntryIdPointer: + { + const auto& pointer_type = src_type->data.pointer; + + auto ptr_len = pointer_type.ptr_len; + if (ptr_len == PtrLenUnknown && dest_type->id == TypeTableEntryIdOpaque) { + ptr_len = PtrLenSingle; + } else if (pointer_type.child_type->id == TypeTableEntryIdOpaque && + dest_type->id != TypeTableEntryIdOpaque) { + + ptr_len = PtrLenUnknown; + } + + dest_type = get_pointer_to_type_extra(ira->codegen, dest_type, pointer_type.is_const, + pointer_type.is_volatile, ptr_len, pointer_type.alignment, + pointer_type.bit_offset, pointer_type.unaligned_bit_count); + } break; + case TypeTableEntryIdPromise: + { + const auto& promise_type = src_type->data.promise; + + if (promise_type.result_type == nullptr) { + ir_add_error(ira, ptr, buf_create_from_str("Trying to cast non-returning promise.")); + return ira->codegen->builtin_types.entry_invalid; + } + + dest_type = get_promise_type(ira->codegen, dest_type); + } break; + default: + { + ir_add_error(ira, ptr, buf_sprintf("Expected pointer or promise type, found '%s'", buf_ptr(&src_type->name))); + return ira->codegen->builtin_types.entry_invalid; + } } - if (get_ptr_const(src_type) && !get_ptr_const(dest_type)) { - ir_add_error(ira, &instruction->base, buf_sprintf("cast discards const qualifier")); - return ira->codegen->builtin_types.entry_invalid; + if (is_optional) { + dest_type = get_maybe_type(ira->codegen, dest_type); } + assert(get_codegen_ptr_type(dest_type) != nullptr); + if (instr_is_comptime(ptr)) { ConstExprValue *val = ir_resolve_const(ira, ptr, UndefOk); if (!val) @@ -18734,7 +18765,7 @@ static TypeTableEntry *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstruc return ira->codegen->builtin_types.entry_invalid; } - IrInstruction *casted_ptr = ir_build_ptr_cast(&ira->new_irb, instruction->base.scope, + IrInstruction *casted_ptr = ir_build_elem_cast(&ira->new_irb, instruction->base.scope, instruction->base.source_node, nullptr, ptr); casted_ptr->value.type = dest_type; @@ -19932,8 +19963,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi return ir_analyze_instruction_decl_ref(ira, (IrInstructionDeclRef *)instruction); case IrInstructionIdPanic: return ir_analyze_instruction_panic(ira, (IrInstructionPanic *)instruction); - case IrInstructionIdPtrCast: - return ir_analyze_instruction_ptr_cast(ira, (IrInstructionPtrCast *)instruction); + case IrInstructionIdElemCast: + return ir_analyze_instruction_elem_cast(ira, (IrInstructionElemCast *)instruction); case IrInstructionIdBitCast: return ir_analyze_instruction_bit_cast(ira, (IrInstructionBitCast *)instruction); case IrInstructionIdIntToPtr: @@ -20200,7 +20231,7 @@ bool ir_has_side_effects(IrInstruction *instruction) { case IrInstructionIdErrWrapPayload: case IrInstructionIdFnProto: case IrInstructionIdTestComptime: - case IrInstructionIdPtrCast: + case IrInstructionIdElemCast: case IrInstructionIdBitCast: case IrInstructionIdWidenOrShorten: case IrInstructionIdPtrToInt: diff --git a/src/ir_print.cpp b/src/ir_print.cpp index 43907fa9d43b..6bdc4e6fc19f 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -822,8 +822,8 @@ static void ir_print_test_comptime(IrPrint *irp, IrInstructionTestComptime *inst fprintf(irp->f, ")"); } -static void ir_print_ptr_cast(IrPrint *irp, IrInstructionPtrCast *instruction) { - fprintf(irp->f, "@ptrCast("); +static void ir_print_elem_cast(IrPrint *irp, IrInstructionElemCast *instruction) { + fprintf(irp->f, "@elemCast("); if (instruction->dest_type) { ir_print_other_instruction(irp, instruction->dest_type); } @@ -1480,8 +1480,8 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) { case IrInstructionIdTestComptime: ir_print_test_comptime(irp, (IrInstructionTestComptime *)instruction); break; - case IrInstructionIdPtrCast: - ir_print_ptr_cast(irp, (IrInstructionPtrCast *)instruction); + case IrInstructionIdElemCast: + ir_print_elem_cast(irp, (IrInstructionElemCast *)instruction); break; case IrInstructionIdBitCast: ir_print_bit_cast(irp, (IrInstructionBitCast *)instruction); diff --git a/std/cstr.zig b/std/cstr.zig index d9106769c192..0970b8c8c7a1 100644 --- a/std/cstr.zig +++ b/std/cstr.zig @@ -97,12 +97,12 @@ pub const NullTerminated2DArray = struct { return NullTerminated2DArray{ .allocator = allocator, .byte_count = byte_count, - .ptr = @ptrCast(?[*]?[*]u8, buf.ptr), + .ptr = @elemCast(?[*]u8, buf.ptr), }; } pub fn deinit(self: *NullTerminated2DArray) void { - const buf = @ptrCast([*]u8, self.ptr); - self.allocator.free(buf[0..self.byte_count]); + const buf = @elemCast(u8, self.ptr); + self.allocator.free(buf.?[0..self.byte_count]); } }; diff --git a/std/heap.zig b/std/heap.zig index 172bc2411822..9c606bc62658 100644 --- a/std/heap.zig +++ b/std/heap.zig @@ -18,13 +18,13 @@ var c_allocator_state = Allocator{ fn cAlloc(self: *Allocator, n: usize, alignment: u29) ![]u8 { assert(alignment <= @alignOf(c_longdouble)); - return if (c.malloc(n)) |buf| @ptrCast([*]u8, buf)[0..n] else error.OutOfMemory; + return if (c.malloc(n)) |buf| @elemCast(u8, buf)[0..n] else error.OutOfMemory; } fn cRealloc(self: *Allocator, old_mem: []u8, new_size: usize, alignment: u29) ![]u8 { - const old_ptr = @ptrCast(*c_void, old_mem.ptr); + const old_ptr = @elemCast(c_void, old_mem.ptr); if (c.realloc(old_ptr, new_size)) |buf| { - return @ptrCast([*]u8, buf)[0..new_size]; + return @elemCast(u8, buf)[0..new_size]; } else if (new_size <= old_mem.len) { return old_mem[0..new_size]; } else { @@ -33,7 +33,7 @@ fn cRealloc(self: *Allocator, old_mem: []u8, new_size: usize, alignment: u29) ![ } fn cFree(self: *Allocator, old_mem: []u8) void { - const old_ptr = @ptrCast(*c_void, old_mem.ptr); + const old_ptr = @elemCast(c_void, old_mem.ptr); c.free(old_ptr); } diff --git a/std/math/index.zig b/std/math/index.zig index cc1b833a372f..51b4c8d2c04d 100644 --- a/std/math/index.zig +++ b/std/math/index.zig @@ -46,12 +46,12 @@ pub fn forceEval(value: var) void { switch (T) { f32 => { var x: f32 = undefined; - const p = @ptrCast(*volatile f32, &x); + const p = (*volatile f32)(&x); p.* = x; }, f64 => { var x: f64 = undefined; - const p = @ptrCast(*volatile f64, &x); + const p = (*volatile f64)(&x); p.* = x; }, else => { diff --git a/std/os/child_process.zig b/std/os/child_process.zig index 1e3a732498db..2de2f798a550 100644 --- a/std/os/child_process.zig +++ b/std/os/child_process.zig @@ -640,7 +640,7 @@ pub const ChildProcess = struct { }; fn windowsCreateProcess(app_name: [*]u8, cmd_line: [*]u8, envp_ptr: ?[*]u8, cwd_ptr: ?[*]u8, lpStartupInfo: *windows.STARTUPINFOA, lpProcessInformation: *windows.PROCESS_INFORMATION) !void { - if (windows.CreateProcessA(app_name, cmd_line, null, null, windows.TRUE, 0, @ptrCast(?*c_void, envp_ptr), cwd_ptr, lpStartupInfo, lpProcessInformation) == 0) { + if (windows.CreateProcessA(app_name, cmd_line, null, null, windows.TRUE, 0, @elemCast(c_void, envp_ptr), cwd_ptr, lpStartupInfo, lpProcessInformation) == 0) { const err = windows.GetLastError(); return switch (err) { windows.ERROR.FILE_NOT_FOUND, windows.ERROR.PATH_NOT_FOUND => error.FileNotFound, diff --git a/std/os/darwin.zig b/std/os/darwin.zig index a835959103dc..6823094506f3 100644 --- a/std/os/darwin.zig +++ b/std/os/darwin.zig @@ -327,7 +327,7 @@ pub fn raise(sig: i32) usize { } pub fn read(fd: i32, buf: [*]u8, nbyte: usize) usize { - return errnoWrap(c.read(fd, @ptrCast(*c_void, buf), nbyte)); + return errnoWrap(c.read(fd, @elemCast(c_void, buf), nbyte)); } pub fn stat(noalias path: [*]const u8, noalias buf: *stat) usize { @@ -335,11 +335,11 @@ pub fn stat(noalias path: [*]const u8, noalias buf: *stat) usize { } pub fn write(fd: i32, buf: [*]const u8, nbyte: usize) usize { - return errnoWrap(c.write(fd, @ptrCast(*const c_void, buf), nbyte)); + return errnoWrap(c.write(fd, @elemCast(c_void, buf), nbyte)); } pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize { - const ptr_result = c.mmap(@ptrCast(*c_void, address), length, @bitCast(c_int, c_uint(prot)), @bitCast(c_int, c_uint(flags)), fd, offset); + const ptr_result = c.mmap(@elemCast(c_void, address), length, @bitCast(c_int, c_uint(prot)), @bitCast(c_int, c_uint(flags)), fd, offset); const isize_result = @bitCast(isize, @ptrToInt(ptr_result)); return errnoWrap(isize_result); } @@ -358,7 +358,7 @@ pub fn getcwd(buf: [*]u8, size: usize) usize { pub fn waitpid(pid: i32, status: *i32, options: u32) usize { comptime assert(i32.bit_count == c_int.bit_count); - return errnoWrap(c.waitpid(pid, @ptrCast(*c_int, status), @bitCast(c_int, options))); + return errnoWrap(c.waitpid(pid, @elemCast(c_int, status), @bitCast(c_int, options))); } pub fn fork() usize { @@ -371,7 +371,7 @@ pub fn access(path: [*]const u8, mode: u32) usize { pub fn pipe(fds: *[2]i32) usize { comptime assert(i32.bit_count == c_int.bit_count); - return errnoWrap(c.pipe(@ptrCast(*[2]c_int, fds))); + return errnoWrap(c.pipe(@elemCast([2]c_int, fds))); } pub fn getdirentries64(fd: i32, buf_ptr: [*]u8, buf_len: usize, basep: *i64) usize { diff --git a/std/os/file.zig b/std/os/file.zig index 56da4f73a6eb..d447382038fb 100644 --- a/std/os/file.zig +++ b/std/os/file.zig @@ -347,7 +347,7 @@ pub const File = struct { while (index < buffer.len) { const want_read_count = windows.DWORD(math.min(windows.DWORD(@maxValue(windows.DWORD)), buffer.len - index)); var amt_read: windows.DWORD = undefined; - if (windows.ReadFile(self.handle, @ptrCast(*c_void, buffer.ptr + index), want_read_count, &amt_read, null) == 0) { + if (windows.ReadFile(self.handle, @elemCast(c_void, buffer.ptr + index), want_read_count, &amt_read, null) == 0) { const err = windows.GetLastError(); return switch (err) { windows.ERROR.OPERATION_ABORTED => continue, diff --git a/std/os/index.zig b/std/os/index.zig index 62eeb7e43e04..30b04b60e137 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -1336,11 +1336,11 @@ pub const Dir = struct { break; } } - const darwin_entry = @ptrCast(*align(1) posix.dirent, &self.handle.buf[self.handle.index]); + const darwin_entry = @elemCast(posix.dirent, &self.handle.buf[self.handle.index]); const next_index = self.handle.index + darwin_entry.d_reclen; self.handle.index = next_index; - const name = @ptrCast([*]u8, &darwin_entry.d_name)[0..darwin_entry.d_namlen]; + const name = @elemCast(u8, &darwin_entry.d_name)[0..darwin_entry.d_namlen]; if (mem.eql(u8, name, ".") or mem.eql(u8, name, "..")) { continue :start_over; @@ -1415,11 +1415,11 @@ pub const Dir = struct { break; } } - const linux_entry = @ptrCast(*align(1) posix.dirent, &self.handle.buf[self.handle.index]); + const linux_entry = @elemCast(posix.dirent, &self.handle.buf[self.handle.index]); const next_index = self.handle.index + linux_entry.d_reclen; self.handle.index = next_index; - const name = cstr.toSlice(@ptrCast([*]u8, &linux_entry.d_name)); + const name = cstr.toSlice(([*]u8)((*[1]u8)(&linux_entry.d_name))); // skip . and .. entries if (mem.eql(u8, name, ".") or mem.eql(u8, name, "..")) { @@ -1824,7 +1824,7 @@ pub fn argsFree(allocator: *mem.Allocator, args_alloc: []const []u8) void { for (args_alloc) |arg| { total_bytes += @sizeOf([]u8) + arg.len; } - const unaligned_allocated_buf = @ptrCast([*]const u8, args_alloc.ptr)[0..total_bytes]; + const unaligned_allocated_buf = @elemCast( u8, args_alloc.ptr)[0..total_bytes]; const aligned_allocated_buf = @alignCast(@alignOf([]u8), unaligned_allocated_buf); return allocator.free(aligned_allocated_buf); } @@ -2431,7 +2431,7 @@ pub fn posixConnectAsync(sockfd: i32, sockaddr: *const posix.sockaddr) PosixConn pub fn posixGetSockOptConnectError(sockfd: i32) PosixConnectError!void { var err_code: i32 = undefined; var size: u32 = @sizeOf(i32); - const rc = posix.getsockopt(sockfd, posix.SOL_SOCKET, posix.SO_ERROR, @ptrCast([*]u8, &err_code), &size); + const rc = posix.getsockopt(sockfd, posix.SOL_SOCKET, posix.SO_ERROR, @elemCast(u8, &err_code), &size); assert(size == 4); const err = posix.getErrno(rc); switch (err) { @@ -2572,7 +2572,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!*Thread if (@sizeOf(Context) == 0) { return startFn({}); } else { - return startFn(@ptrCast(*Context, @alignCast(@alignOf(Context), arg)).*); + return startFn(@elemCast(Context, @alignCast(@alignOf(Context), arg)).*); } } }; @@ -2581,13 +2581,13 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!*Thread const byte_count = @alignOf(WinThread.OuterContext) + @sizeOf(WinThread.OuterContext); const bytes_ptr = windows.HeapAlloc(heap_handle, 0, byte_count) orelse return SpawnThreadError.OutOfMemory; errdefer assert(windows.HeapFree(heap_handle, 0, bytes_ptr) != 0); - const bytes = @ptrCast([*]u8, bytes_ptr)[0..byte_count]; + const bytes = @elemCast(u8, bytes_ptr)[0..byte_count]; const outer_context = std.heap.FixedBufferAllocator.init(bytes).allocator.create(WinThread.OuterContext) catch unreachable; outer_context.inner = context; outer_context.thread.data.heap_handle = heap_handle; outer_context.thread.data.alloc_start = bytes_ptr; - const parameter = if (@sizeOf(Context) == 0) null else @ptrCast(*c_void, &outer_context.inner); + const parameter = if (@sizeOf(Context) == 0) null else @elemCast(c_void, &outer_context.inner); outer_context.thread.data.handle = windows.CreateThread(null, default_stack_size, WinThread.threadMain, parameter, 0, null) orelse { const err = windows.GetLastError(); return switch (err) { @@ -2610,7 +2610,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!*Thread _ = startFn({}); return null; } else { - _ = startFn(@ptrCast(*const Context, @alignCast(@alignOf(Context), ctx)).*); + _ = startFn(@elemCast(Context, @alignCast(@alignOf(Context), ctx)).*); return null; } } diff --git a/std/os/linux/index.zig b/std/os/linux/index.zig index 0e77371ec2e5..621496c626db 100644 --- a/std/os/linux/index.zig +++ b/std/os/linux/index.zig @@ -958,7 +958,7 @@ pub fn sigaction(sig: u6, noalias act: *const Sigaction, noalias oact: ?*Sigacti .restorer = @ptrCast(extern fn () void, restore_rt), }; var ksa_old: k_sigaction = undefined; - @memcpy(@ptrCast([*]u8, &ksa.mask), @ptrCast([*]const u8, &act.mask), 8); + @memcpy(@elemCast(u8, &ksa.mask), @elemCast( u8, &act.mask), 8); const result = syscall4(SYS_rt_sigaction, sig, @ptrToInt(&ksa), @ptrToInt(&ksa_old), @sizeOf(@typeOf(ksa.mask))); const err = getErrno(result); if (err != 0) { @@ -967,7 +967,7 @@ pub fn sigaction(sig: u6, noalias act: *const Sigaction, noalias oact: ?*Sigacti if (oact) |old| { old.handler = ksa_old.handler; old.flags = @truncate(u32, ksa_old.flags); - @memcpy(@ptrCast([*]u8, &old.mask), @ptrCast([*]const u8, &ksa_old.mask), @sizeOf(@typeOf(ksa_old.mask))); + @memcpy(@elemCast(u8, &old.mask), @elemCast( u8, &ksa_old.mask), @sizeOf(@typeOf(ksa_old.mask))); } return 0; } diff --git a/std/os/linux/test.zig b/std/os/linux/test.zig index 948a3ac96b81..d220709ce3a3 100644 --- a/std/os/linux/test.zig +++ b/std/os/linux/test.zig @@ -36,5 +36,5 @@ test "timer" { var events = []linux.epoll_event{events_one} ** 8; // TODO implicit cast from *[N]T to [*]T - err = linux.epoll_wait(i32(epoll_fd), @ptrCast([*]linux.epoll_event, &events), 8, -1); + err = linux.epoll_wait(i32(epoll_fd), @elemCast(linux.epoll_event, &events), 8, -1); } diff --git a/std/os/windows/util.zig b/std/os/windows/util.zig index 88a9e7952efa..0061d275bff1 100644 --- a/std/os/windows/util.zig +++ b/std/os/windows/util.zig @@ -42,7 +42,7 @@ pub const WriteError = error{ }; pub fn windowsWrite(handle: windows.HANDLE, bytes: []const u8) WriteError!void { - if (windows.WriteFile(handle, @ptrCast(*const c_void, bytes.ptr), u32(bytes.len), null, null) == 0) { + if (windows.WriteFile(handle, @elemCast(c_void, bytes.ptr), u32(bytes.len), null, null) == 0) { const err = windows.GetLastError(); return switch (err) { windows.ERROR.INVALID_USER_BUFFER => WriteError.SystemResources, @@ -68,11 +68,11 @@ pub fn windowsIsCygwinPty(handle: windows.HANDLE) bool { const size = @sizeOf(windows.FILE_NAME_INFO); var name_info_bytes align(@alignOf(windows.FILE_NAME_INFO)) = []u8{0} ** (size + windows.MAX_PATH); - if (windows.GetFileInformationByHandleEx(handle, windows.FileNameInfo, @ptrCast(*c_void, &name_info_bytes[0]), u32(name_info_bytes.len)) == 0) { + if (windows.GetFileInformationByHandleEx(handle, windows.FileNameInfo, @elemCast(c_void, &name_info_bytes[0]), u32(name_info_bytes.len)) == 0) { return true; } - const name_info = @ptrCast(*const windows.FILE_NAME_INFO, &name_info_bytes[0]); + const name_info = @elemCast(windows.FILE_NAME_INFO, &name_info_bytes[0]); const name_bytes = name_info_bytes[size .. size + usize(name_info.FileNameLength)]; const name_wide = ([]u16)(name_bytes); return mem.indexOf(u16, name_wide, []u16{ 'm', 's', 'y', 's', '-' }) != null or diff --git a/std/special/bootstrap.zig b/std/special/bootstrap.zig index dd37f1edb683..12946cdea22e 100644 --- a/std/special/bootstrap.zig +++ b/std/special/bootstrap.zig @@ -52,14 +52,14 @@ extern fn WinMainCRTStartup() noreturn { // TODO https://github.com/ziglang/zig/issues/265 fn posixCallMainAndExit() noreturn { const argc = argc_ptr[0]; - const argv = @ptrCast([*][*]u8, argc_ptr + 1); + const argv = @elemCast([*]u8, argc_ptr + 1); - const envp_optional = @ptrCast([*]?[*]u8, argv + argc + 1); + const envp_optional = @elemCast(?[*]u8, argv + argc + 1); var envp_count: usize = 0; while (envp_optional[envp_count]) |_| : (envp_count += 1) {} - const envp = @ptrCast([*][*]u8, envp_optional)[0..envp_count]; + const envp = @elemCast([*]u8, envp_optional)[0..envp_count]; if (builtin.os == builtin.Os.linux) { - const auxv = @ptrCast([*]usize, envp.ptr + envp_count + 1); + const auxv = @elemCast(usize, envp.ptr + envp_count + 1); var i: usize = 0; while (auxv[i] != 0) : (i += 2) { if (auxv[i] < std.os.linux_aux_raw.len) std.os.linux_aux_raw[auxv[i]] = auxv[i + 1]; @@ -79,7 +79,7 @@ fn callMainWithArgs(argc: usize, argv: [*][*]u8, envp: [][*]u8) u8 { extern fn main(c_argc: i32, c_argv: [*][*]u8, c_envp: [*]?[*]u8) i32 { var env_count: usize = 0; while (c_envp[env_count] != null) : (env_count += 1) {} - const envp = @ptrCast([*][*]u8, c_envp)[0..env_count]; + const envp = @elemCast([*]u8, c_envp)[0..env_count]; return callMainWithArgs(usize(c_argc), c_argv, envp); } diff --git a/std/special/compiler_rt/udivmod.zig b/std/special/compiler_rt/udivmod.zig index 894dd022393d..7ba72e68d839 100644 --- a/std/special/compiler_rt/udivmod.zig +++ b/std/special/compiler_rt/udivmod.zig @@ -14,8 +14,8 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem: const SignedDoubleInt = @IntType(true, DoubleInt.bit_count); const Log2SingleInt = @import("std").math.Log2Int(SingleInt); - const n = @ptrCast(*const [2]SingleInt, &a).*; // TODO issue #421 - const d = @ptrCast(*const [2]SingleInt, &b).*; // TODO issue #421 + const n = @elemCast([2]SingleInt, &a).*; // TODO issue #421 + const d = @elemCast([2]SingleInt, &b).*; // TODO issue #421 var q: [2]SingleInt = undefined; var r: [2]SingleInt = undefined; var sr: c_uint = undefined; @@ -57,7 +57,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem: if (maybe_rem) |rem| { r[high] = n[high] % d[high]; r[low] = 0; - rem.* = @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421 + rem.* = @elemCast(DoubleInt, &r[0]).*; // TODO issue #421 } return n[high] / d[high]; } @@ -69,7 +69,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem: if (maybe_rem) |rem| { r[low] = n[low]; r[high] = n[high] & (d[high] - 1); - rem.* = @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421 + rem.* = @elemCast(DoubleInt, &r[0]).*; // TODO issue #421 } return n[high] >> Log2SingleInt(@ctz(d[high])); } @@ -109,7 +109,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem: sr = @ctz(d[low]); q[high] = n[high] >> Log2SingleInt(sr); q[low] = (n[high] << Log2SingleInt(SingleInt.bit_count - sr)) | (n[low] >> Log2SingleInt(sr)); - return @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &q[0]).*; // TODO issue #421 + return @elemCast(DoubleInt, &q[0]).*; // TODO issue #421 } // K X // --- @@ -183,13 +183,13 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem: // r.all -= b; // carry = 1; // } - r_all = @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421 + r_all = @elemCast(DoubleInt, &r[0]).*; // TODO issue #421 const s: SignedDoubleInt = SignedDoubleInt(b -% r_all -% 1) >> (DoubleInt.bit_count - 1); carry = u32(s & 1); r_all -= b & @bitCast(DoubleInt, s); - r = @ptrCast(*[2]SingleInt, &r_all).*; // TODO issue #421 + r = @elemCast([2]SingleInt, &r_all).*; // TODO issue #421 } - const q_all = ((@ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &q[0]).*) << 1) | carry; // TODO issue #421 + const q_all = ((@elemCast(DoubleInt, &q[0]).*) << 1) | carry; // TODO issue #421 if (maybe_rem) |rem| { rem.* = r_all; }