Sema: disallow C pointer to slice coercion

Resolves: #16719
This commit is contained in:
mlugg 2023-08-21 02:07:40 +01:00 committed by Andrew Kelley
parent 82c8e45a7e
commit 6a5463951f
2 changed files with 21 additions and 0 deletions

View File

@ -27289,6 +27289,7 @@ fn coerceExtra(
// coercion from C pointer
if (inst_ty.isCPtr(mod)) src_c_ptr: {
if (dest_info.flags.size == .Slice) break :src_c_ptr;
if (!sema.checkPtrAttributes(dest_ty, inst_ty, &in_memory_result)) break :src_c_ptr;
// In this case we must add a safety check because the C pointer
// could be null.

View File

@ -0,0 +1,20 @@
export fn foo() void {
const ptr: [*]const u8 = "abc";
_ = @as([]const u8, ptr);
}
export fn bar() void {
const ptr: [*c]const u8 = "def";
_ = @as([]const u8, ptr);
}
export fn baz() void {
const ptr: *const u8 = &@as(u8, 123);
_ = @as([]const u8, ptr);
}
// error
// backend=stage2
// target=native
//
// :3:25: error: expected type '[]const u8', found '[*]const u8'
// :7:25: error: expected type '[]const u8', found '[*c]const u8'
// :11:25: error: expected type '[]const u8', found '*const u8'