sema: improve the error message when coercing to []anyopaque

This commit is contained in:
kcbanner 2023-04-27 21:42:32 -04:00 committed by Andrew Kelley
parent 1b432072b5
commit ec6ffaa1e4
3 changed files with 20 additions and 1 deletions

View File

@ -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,

View File

@ -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'

View File

@ -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'