mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +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
12
src/Sema.zig
12
src/Sema.zig
@ -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",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
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…
x
Reference in New Issue
Block a user