From e2a9f2ef988e6ff4ec9f943ead9285c67c56ffd7 Mon Sep 17 00:00:00 2001 From: raulgrell Date: Mon, 27 Aug 2018 16:37:37 +0100 Subject: Allow implicit cast from *T and [*]T to ?*c_void --- src/ir.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src/ir.cpp') diff --git a/src/ir.cpp b/src/ir.cpp index 710456179c..b4af7f42e2 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -60,7 +60,7 @@ enum ConstCastResultId { ConstCastResultIdType, ConstCastResultIdUnresolvedInferredErrSet, ConstCastResultIdAsyncAllocatorType, - ConstCastResultIdNullWrapPtr, + ConstCastResultIdNullWrapPtr }; struct ConstCastOnly; @@ -8471,9 +8471,9 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, TypeTableEntry if (wanted_type == actual_type) return result; - // * and [*] can do a const-cast-only to ?* and ?[*], respectively - // but not if there is a mutable parent pointer - // and not if the pointer is zero bits + // *T and [*]T may const-cast-only to ?*U and ?[*]U, respectively + // but not if we want a mutable pointer + // and not if the actual pointer has zero bits if (!wanted_is_mutable && wanted_type->id == TypeTableEntryIdOptional && wanted_type->data.maybe.child_type->id == TypeTableEntryIdPointer && actual_type->id == TypeTableEntryIdPointer && type_has_bits(actual_type)) @@ -8488,6 +8488,18 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, TypeTableEntry return result; } + // *T and [*]T can always cast to ?*c_void + if (wanted_type->id == TypeTableEntryIdPointer && + wanted_type->data.pointer.ptr_len == PtrLenSingle && + wanted_type->data.pointer.child_type == g->builtin_types.entry_c_void && + actual_type->id == TypeTableEntryIdPointer && + (!actual_type->data.pointer.is_const || wanted_type->data.pointer.is_const) && + (!actual_type->data.pointer.is_volatile || wanted_type->data.pointer.is_volatile)) + { + assert(actual_type->data.pointer.alignment >= wanted_type->data.pointer.alignment); + return result; + } + // pointer const if (wanted_type->id == TypeTableEntryIdPointer && actual_type->id == TypeTableEntryIdPointer) { ConstCastOnly child = types_match_const_cast_only(ira, wanted_type->data.pointer.child_type, -- cgit v1.2.3 From 45d9d9f953df2167b35878ea02551d9c86ad8a0f Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 27 Aug 2018 18:31:28 -0400 Subject: minor fixups --- src/ir.cpp | 2 +- test/cases/cast.zig | 31 +++++++++++++++---------------- 2 files changed, 16 insertions(+), 17 deletions(-) (limited to 'src/ir.cpp') diff --git a/src/ir.cpp b/src/ir.cpp index b4af7f42e2..3117410cd2 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -8488,7 +8488,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, TypeTableEntry return result; } - // *T and [*]T can always cast to ?*c_void + // *T and [*]T can always cast to *c_void if (wanted_type->id == TypeTableEntryIdPointer && wanted_type->data.pointer.ptr_len == PtrLenSingle && wanted_type->data.pointer.child_type == g->builtin_types.entry_c_void && diff --git a/test/cases/cast.zig b/test/cases/cast.zig index dde714b2be..cc5e4b4394 100644 --- a/test/cases/cast.zig +++ b/test/cases/cast.zig @@ -487,20 +487,20 @@ fn MakeType(comptime T: type) type { } test "implicit cast from *[N]T to ?[*]T" { - var x: ?[*]u16 = null; - var y: [4]u16 = [4]u16 {0, 1, 2, 3}; + var x: ?[*]u16 = null; + var y: [4]u16 = [4]u16{ 0, 1, 2, 3 }; - x = &y; - assert(std.mem.eql(u16, x.?[0..4], y[0..4])); - x.?[0] = 8; - y[3] = 6; - assert(std.mem.eql(u16, x.?[0..4], y[0..4])); + x = &y; + assert(std.mem.eql(u16, x.?[0..4], y[0..4])); + x.?[0] = 8; + y[3] = 6; + assert(std.mem.eql(u16, x.?[0..4], y[0..4])); } test "implicit cast from *T to ?*c_void" { - var a: u8 = 1; - incrementVoidPtrValue(&a); - std.debug.assert(a == 2); + var a: u8 = 1; + incrementVoidPtrValue(&a); + std.debug.assert(a == 2); } fn incrementVoidPtrValue(value: ?*c_void) void { @@ -508,15 +508,14 @@ fn incrementVoidPtrValue(value: ?*c_void) void { } test "implicit cast from [*]T to ?*c_void" { - var a = []u8{3, 2, 1}; - incrementVoidPtrArray(a[0..].ptr, 3); - std.debug.assert(std.mem.eql(u8, a, []u8{4, 3, 2})); + var a = []u8{ 3, 2, 1 }; + incrementVoidPtrArray(a[0..].ptr, 3); + assert(std.mem.eql(u8, a, []u8{ 4, 3, 2 })); } fn incrementVoidPtrArray(array: ?*c_void, len: usize) void { var n: usize = 0; - while(n < len) : (n += 1) { - std.debug.warn("{}", n); + while (n < len) : (n += 1) { @ptrCast([*]u8, array.?)[n] += 1; } -} \ No newline at end of file +} -- cgit v1.2.3