authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-01-12 23:34:42+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-01-14 21:17:46+00:00
log5322459a0bd346c78ba069262a5fd7073389a750
treeac9d7d985f5584359bf31d895a4d712bf8749106
parent27274d4fdea8061d7afacbb2b179fd49fbaca125

Sema: fix UB in error reporting

And add test coverage for the compile error in question.

2 files changed, 16 insertions(+), 6 deletions(-)

src/Sema.zig+6-6
......@@ -23353,7 +23353,7 @@ fn ptrCastFull(
2335323353 if (src_info.flags.size == .C) break :check_size;
2335423354 if (dest_info.flags.size == .C) break :check_size;
2335523355 return sema.failWithOwnedErrorMsg(block, msg: {
23356 const msg = try sema.errMsg(src, "cannot implicitly convert {s} pointer to {s} pointer", .{
23356 const msg = try sema.errMsg(src, "cannot implicitly convert {s} to {s}", .{
2335723357 pointerSizeString(src_info.flags.size),
2335823358 pointerSizeString(dest_info.flags.size),
2335923359 });
......@@ -30145,7 +30145,7 @@ const InMemoryCoercionResult = union(enum) {
3014530145 break;
3014630146 },
3014730147 .ptr_size => |size| {
30148 try sema.errNote(src, msg, "a {s} pointer cannot cast into a {s} pointer", .{ pointerSizeString(size.actual), pointerSizeString(size.wanted) });
30148 try sema.errNote(src, msg, "a {s} cannot cast into a {s}", .{ pointerSizeString(size.actual), pointerSizeString(size.wanted) });
3014930149 break;
3015030150 },
3015130151 .ptr_allowzero => |pair| {
......@@ -30224,10 +30224,10 @@ const InMemoryCoercionResult = union(enum) {
3022430224
3022530225fn pointerSizeString(size: std.builtin.Type.Pointer.Size) []const u8 {
3022630226 return switch (size) {
30227 .One => "single",
30228 .Many => "many",
30229 .C => "C",
30230 .Slice => unreachable,
30227 .One => "single pointer",
30228 .Many => "many pointer",
30229 .C => "C pointer",
30230 .Slice => "slice",
3023130231 };
3023230232}
3023330233
test/cases/compile_errors/invalid_pointer_cast.zig created+10
......@@ -0,0 +1,10 @@
1export fn foo() void {
2 const slice: []const u8 = &.{ 1, 2, 3 };
3 const result: [*]const u8 = @alignCast(slice);
4 _ = result;
5}
6
7// error
8//
9// :3:33: error: cannot implicitly convert slice to many pointer
10// :3:33: note: use 'ptr' field to convert slice to many pointer