mirror of
https://github.com/ziglang/zig.git
synced 2025-12-27 16:43:07 +00:00
sema: improve the error message when coercing to []anyopaque
This commit is contained in:
parent
1b432072b5
commit
ec6ffaa1e4
@ -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,
|
||||
|
||||
@ -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'
|
||||
|
||||
11
test/cases/compile_errors/pointer_to_anyopaque_slice.zig
Normal file
11
test/cases/compile_errors/pointer_to_anyopaque_slice.zig
Normal 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'
|
||||
Loading…
x
Reference in New Issue
Block a user