authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-06-25 02:52:58+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-06-25 13:28:32+01:00
logd249629ef1345e6f1ff03ff1381fa526da97e9e5
treeeed603548cee6ccf2406c5bd87976ac596b141d9
parent569ae762e1025f83ae7d2c6ae2ca6b015b289991
signaturelock-open Commit is signed but in an unrecognized format.

cases: add tests for errors introduced by cast builtin result type inference


4 files changed, 100 insertions(+), 0 deletions(-)

test/cases/compile_errors/cast_without_result_type.zig created+28
......@@ -0,0 +1,28 @@
1export fn a() void {
2 _ = @ptrFromInt(123);
3}
4export fn b() void {
5 const x = @ptrCast(@alignCast(@as(*u8, undefined)));
6 _ = x;
7}
8export fn c() void {
9 _ = &@intCast(@as(u64, 123));
10 _ = S;
11}
12export fn d() void {
13 var x: f32 = 0;
14 _ = x + @floatFromInt(123);
15}
16
17// error
18// backend=stage2
19// target=native
20//
21// :2:9: error: @ptrFromInt must have a known result type
22// :2:9: note: use @as to provide explicit result type
23// :5:15: error: @ptrCast must have a known result type
24// :5:15: note: use @as to provide explicit result type
25// :9:10: error: @intCast must have a known result type
26// :9:10: note: use @as to provide explicit result type
27// :14:13: error: @floatFromInt must have a known result type
28// :14:13: note: use @as to provide explicit result type
test/cases/compile_errors/cast_without_result_type_due_to_generic_parameter.zig created+31
......@@ -0,0 +1,31 @@
1export fn a() void {
2 bar(@ptrFromInt(123));
3}
4export fn b() void {
5 bar(@ptrCast(@alignCast(@as(*u8, undefined))));
6}
7export fn c() void {
8 bar(@intCast(@as(u64, 123)));
9}
10export fn d() void {
11 bar(@floatFromInt(123));
12}
13
14fn bar(_: anytype) void {}
15
16// error
17// backend=stage2
18// target=native
19//
20// :2:9: error: @ptrFromInt must have a known result type
21// :2:9: note: result type is unknown due to anytype parameter
22// :2:9: note: use @as to provide explicit result type
23// :5:9: error: @ptrCast must have a known result type
24// :5:9: note: result type is unknown due to anytype parameter
25// :5:9: note: use @as to provide explicit result type
26// :8:9: error: @intCast must have a known result type
27// :8:9: note: result type is unknown due to anytype parameter
28// :8:9: note: use @as to provide explicit result type
29// :11:9: error: @floatFromInt must have a known result type
30// :11:9: note: result type is unknown due to anytype parameter
31// :11:9: note: use @as to provide explicit result type
test/cases/compile_errors/nested_ptr_cast_bad_operand.zig created+22
......@@ -0,0 +1,22 @@
1const p: ?*const u8 = null;
2export fn a() void {
3 _ = @as(*const u32, @ptrCast(@alignCast(p)));
4}
5export fn b() void {
6 _ = @constCast(@volatileCast(123));
7}
8export fn c() void {
9 const x: ?*f32 = @constCast(@ptrCast(@addrSpaceCast(@volatileCast(p))));
10 _ = x;
11}
12
13// error
14// backend=stage2
15// target=native
16//
17// :3:45: error: null pointer casted to type '*const u32'
18// :6:34: error: expected pointer type, found 'comptime_int'
19// :9:22: error: cast increases pointer alignment
20// :9:71: note: '?*const u8' has alignment '1'
21// :9:22: note: '?*f32' has alignment '4'
22// :9:22: note: use @alignCast to assert pointer alignment
test/cases/compile_errors/redundant_ptr_cast.zig created+19
......@@ -0,0 +1,19 @@
1const p: *anyopaque = undefined;
2export fn a() void {
3 _ = @ptrCast(@ptrCast(p));
4}
5export fn b() void {
6 const ptr1: *u32 = @alignCast(@ptrCast(@alignCast(p)));
7 _ = ptr1;
8}
9export fn c() void {
10 _ = @constCast(@alignCast(@ptrCast(@constCast(@volatileCast(p)))));
11}
12
13// error
14// backend=stage2
15// target=native
16//
17// :3:18: error: redundant @ptrCast
18// :6:44: error: redundant @alignCast
19// :10:40: error: redundant @constCast