mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 05:44:20 +00:00
sema: add error for coercing a slice to an anyopaque pointer
This commit is contained in:
parent
61236c2aa1
commit
295b8ca467
2 changed files with 28 additions and 0 deletions
15
src/Sema.zig
15
src/Sema.zig
|
|
@ -25093,6 +25093,13 @@ fn coerceExtra(
|
|||
} };
|
||||
break :pointer;
|
||||
}
|
||||
if (inst_ty.isSlice()) {
|
||||
in_memory_result = .{ .slice_to_anyopaque = .{
|
||||
.actual = inst_ty,
|
||||
.wanted = dest_ty,
|
||||
} };
|
||||
break :pointer;
|
||||
}
|
||||
return sema.coerceCompatiblePtrs(block, dest_ty, inst, inst_src);
|
||||
}
|
||||
|
||||
|
|
@ -25603,6 +25610,7 @@ const InMemoryCoercionResult = union(enum) {
|
|||
ptr_bit_range: BitRange,
|
||||
ptr_alignment: IntPair,
|
||||
double_ptr_to_anyopaque: Pair,
|
||||
slice_to_anyopaque: Pair,
|
||||
|
||||
const Pair = struct {
|
||||
actual: Type,
|
||||
|
|
@ -25901,6 +25909,13 @@ const InMemoryCoercionResult = union(enum) {
|
|||
});
|
||||
break;
|
||||
},
|
||||
.slice_to_anyopaque => |pair| {
|
||||
try sema.errNote(block, src, msg, "cannot implicitly cast slice '{}' to anyopaque pointer '{}'", .{
|
||||
pair.actual.fmt(sema.mod), pair.wanted.fmt(sema.mod),
|
||||
});
|
||||
try sema.errNote(block, src, msg, "consider using '.ptr'", .{});
|
||||
break;
|
||||
},
|
||||
};
|
||||
}
|
||||
};
|
||||
|
|
|
|||
13
test/cases/compile_errors/slice_to_anyopaque_pointer.zig
Normal file
13
test/cases/compile_errors/slice_to_anyopaque_pointer.zig
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
export fn entry() void {
|
||||
const slice: []const u8 = "foo";
|
||||
const x = @as(*const anyopaque, slice);
|
||||
_ = x;
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :3:37: error: expected type '*const anyopaque', found '[]const u8'
|
||||
// :3:37: note: cannot implicitly cast slice '[]const u8' to anyopaque pointer '*const anyopaque'
|
||||
// :3:37: note: consider using '.ptr'
|
||||
Loading…
Add table
Reference in a new issue