Commit ec6ffaa1e4

kcbanner <kcbanner@gmail.com>
2023-04-28 03:42:32
sema: improve the error message when coercing to []anyopaque
1 parent 1b43207
src/Sema.zig
@@ -25335,7 +25335,7 @@ fn coerceExtra(
 
             // cast from *T and [*]T to *anyopaque
             // but don't do it if the source type is a double pointer
-            if (dest_info.pointee_type.tag() == .anyopaque and inst_ty.zigTypeTag() == .Pointer) {
+            if (dest_info.pointee_type.tag() == .anyopaque and inst_ty.zigTypeTag() == .Pointer) to_anyopaque: {
                 if (!sema.checkPtrAttributes(dest_ty, inst_ty, &in_memory_result)) break :pointer;
                 const elem_ty = inst_ty.elemType2();
                 if (elem_ty.zigTypeTag() == .Pointer or elem_ty.isPtrLikeOptional()) {
@@ -25345,6 +25345,7 @@ fn coerceExtra(
                     } };
                     break :pointer;
                 }
+                if (dest_ty.isSlice()) break :to_anyopaque;
                 if (inst_ty.isSlice()) {
                     in_memory_result = .{ .slice_to_anyopaque = .{
                         .actual = inst_ty,
test/cases/compile_errors/double_pointer_to_anyopaque_pointer.zig
@@ -15,6 +15,11 @@ pub export fn entry3() void {
     const ptr: *const anyopaque = x;
     _ = ptr;
 }
+export fn entry4() void {
+    var a: []*u32 = undefined;
+    var b: []anyopaque = undefined;
+    b = a;
+}
 
 // error
 // backend=stage2
@@ -27,3 +32,5 @@ pub export fn entry3() void {
 // :11:12: note: parameter type declared here
 // :15:35: error: expected type '*const anyopaque', found '*?*usize'
 // :15:35: note: cannot implicitly cast double pointer '*?*usize' to anyopaque pointer '*const anyopaque'
+// :21:9: error: expected type '[]anyopaque', found '[]*u32'
+// :21:9: note: cannot implicitly cast double pointer '[]*u32' to anyopaque pointer '[]anyopaque'
test/cases/compile_errors/pointer_to_anyopaque_slice.zig
@@ -0,0 +1,11 @@
+export fn x() void {
+    var a: *u32 = undefined;
+    var b: []anyopaque = undefined;
+    b = a;
+}
+
+// error
+// backend=stage2
+// target=native
+//
+// :4:9: error: expected type '[]anyopaque', found '*u32'