Sema: fix UB in error reporting

And add test coverage for the compile error in question.
This commit is contained in:
mlugg 2025-01-12 23:34:42 +00:00 committed by Matthew Lugg
parent 27274d4fde
commit 5322459a0b
2 changed files with 16 additions and 6 deletions

View File

@ -23353,7 +23353,7 @@ fn ptrCastFull(
if (src_info.flags.size == .C) break :check_size; if (src_info.flags.size == .C) break :check_size;
if (dest_info.flags.size == .C) break :check_size; if (dest_info.flags.size == .C) break :check_size;
return sema.failWithOwnedErrorMsg(block, msg: { return sema.failWithOwnedErrorMsg(block, msg: {
const msg = try sema.errMsg(src, "cannot implicitly convert {s} pointer to {s} pointer", .{ const msg = try sema.errMsg(src, "cannot implicitly convert {s} to {s}", .{
pointerSizeString(src_info.flags.size), pointerSizeString(src_info.flags.size),
pointerSizeString(dest_info.flags.size), pointerSizeString(dest_info.flags.size),
}); });
@ -30145,7 +30145,7 @@ const InMemoryCoercionResult = union(enum) {
break; break;
}, },
.ptr_size => |size| { .ptr_size => |size| {
try sema.errNote(src, msg, "a {s} pointer cannot cast into a {s} pointer", .{ pointerSizeString(size.actual), pointerSizeString(size.wanted) }); try sema.errNote(src, msg, "a {s} cannot cast into a {s}", .{ pointerSizeString(size.actual), pointerSizeString(size.wanted) });
break; break;
}, },
.ptr_allowzero => |pair| { .ptr_allowzero => |pair| {
@ -30224,10 +30224,10 @@ const InMemoryCoercionResult = union(enum) {
fn pointerSizeString(size: std.builtin.Type.Pointer.Size) []const u8 { fn pointerSizeString(size: std.builtin.Type.Pointer.Size) []const u8 {
return switch (size) { return switch (size) {
.One => "single", .One => "single pointer",
.Many => "many", .Many => "many pointer",
.C => "C", .C => "C pointer",
.Slice => unreachable, .Slice => "slice",
}; };
} }

View File

@ -0,0 +1,10 @@
export fn foo() void {
const slice: []const u8 = &.{ 1, 2, 3 };
const result: [*]const u8 = @alignCast(slice);
_ = result;
}
// error
//
// :3:33: error: cannot implicitly convert slice to many pointer
// :3:33: note: use 'ptr' field to convert slice to many pointer