mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 05:44:20 +00:00
Sema: fix UB in error reporting
And add test coverage for the compile error in question.
This commit is contained in:
parent
27274d4fde
commit
5322459a0b
2 changed files with 16 additions and 6 deletions
12
src/Sema.zig
12
src/Sema.zig
|
|
@ -23353,7 +23353,7 @@ fn ptrCastFull(
|
|||
if (src_info.flags.size == .C) break :check_size;
|
||||
if (dest_info.flags.size == .C) break :check_size;
|
||||
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(dest_info.flags.size),
|
||||
});
|
||||
|
|
@ -30145,7 +30145,7 @@ const InMemoryCoercionResult = union(enum) {
|
|||
break;
|
||||
},
|
||||
.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;
|
||||
},
|
||||
.ptr_allowzero => |pair| {
|
||||
|
|
@ -30224,10 +30224,10 @@ const InMemoryCoercionResult = union(enum) {
|
|||
|
||||
fn pointerSizeString(size: std.builtin.Type.Pointer.Size) []const u8 {
|
||||
return switch (size) {
|
||||
.One => "single",
|
||||
.Many => "many",
|
||||
.C => "C",
|
||||
.Slice => unreachable,
|
||||
.One => "single pointer",
|
||||
.Many => "many pointer",
|
||||
.C => "C pointer",
|
||||
.Slice => "slice",
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
10
test/cases/compile_errors/invalid_pointer_cast.zig
Normal file
10
test/cases/compile_errors/invalid_pointer_cast.zig
Normal 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
|
||||
Loading…
Add table
Reference in a new issue