authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-04-24 02:20:44-04:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-04-26 00:53:09+03:00
log295b8ca467da36cd1066395e7f50b6245f456573
tree5a0ec69841ede820eec4b3194729cc591648292a
parent61236c2aa1610e0ce470bd64ba930a6430ad6de2

sema: add error for coercing a slice to an anyopaque pointer


2 files changed, 28 insertions(+), 0 deletions(-)

src/Sema.zig+15
...@@ -25093,6 +25093,13 @@ fn coerceExtra(...@@ -25093,6 +25093,13 @@ fn coerceExtra(
25093 } };25093 } };
25094 break :pointer;25094 break :pointer;
25095 }25095 }
25096 if (inst_ty.isSlice()) {
25097 in_memory_result = .{ .slice_to_anyopaque = .{
25098 .actual = inst_ty,
25099 .wanted = dest_ty,
25100 } };
25101 break :pointer;
25102 }
25096 return sema.coerceCompatiblePtrs(block, dest_ty, inst, inst_src);25103 return sema.coerceCompatiblePtrs(block, dest_ty, inst, inst_src);
25097 }25104 }
2509825105
...@@ -25603,6 +25610,7 @@ const InMemoryCoercionResult = union(enum) {...@@ -25603,6 +25610,7 @@ const InMemoryCoercionResult = union(enum) {
25603 ptr_bit_range: BitRange,25610 ptr_bit_range: BitRange,
25604 ptr_alignment: IntPair,25611 ptr_alignment: IntPair,
25605 double_ptr_to_anyopaque: Pair,25612 double_ptr_to_anyopaque: Pair,
25613 slice_to_anyopaque: Pair,
2560625614
25607 const Pair = struct {25615 const Pair = struct {
25608 actual: Type,25616 actual: Type,
...@@ -25901,6 +25909,13 @@ const InMemoryCoercionResult = union(enum) {...@@ -25901,6 +25909,13 @@ const InMemoryCoercionResult = union(enum) {
25901 });25909 });
25902 break;25910 break;
25903 },25911 },
25912 .slice_to_anyopaque => |pair| {
25913 try sema.errNote(block, src, msg, "cannot implicitly cast slice '{}' to anyopaque pointer '{}'", .{
25914 pair.actual.fmt(sema.mod), pair.wanted.fmt(sema.mod),
25915 });
25916 try sema.errNote(block, src, msg, "consider using '.ptr'", .{});
25917 break;
25918 },
25904 };25919 };
25905 }25920 }
25906};25921};
test/cases/compile_errors/slice_to_anyopaque_pointer.zig created+13
...@@ -0,0 +1,13 @@
1export fn entry() void {
2 const slice: []const u8 = "foo";
3 const x = @as(*const anyopaque, slice);
4 _ = x;
5}
6
7// error
8// backend=stage2
9// target=native
10//
11// :3:37: error: expected type '*const anyopaque', found '[]const u8'
12// :3:37: note: cannot implicitly cast slice '[]const u8' to anyopaque pointer '*const anyopaque'
13// :3:37: note: consider using '.ptr'