diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2023-06-25 02:52:58 +0100 |
|---|---|---|
| committer | mlugg <mlugg@mlugg.co.uk> | 2023-06-25 13:28:32 +0100 |
| commit | d249629ef1345e6f1ff03ff1381fa526da97e9e5 (patch) | |
| tree | eed603548cee6ccf2406c5bd87976ac596b141d9 /test/cases/compile_errors | |
| parent | 569ae762e1025f83ae7d2c6ae2ca6b015b289991 (diff) | |
| download | zig-d249629ef1345e6f1ff03ff1381fa526da97e9e5.tar.gz zig-d249629ef1345e6f1ff03ff1381fa526da97e9e5.zip | |
cases: add tests for errors introduced by cast builtin result type inference
Diffstat (limited to 'test/cases/compile_errors')
4 files changed, 100 insertions, 0 deletions
diff --git a/test/cases/compile_errors/cast_without_result_type.zig b/test/cases/compile_errors/cast_without_result_type.zig new file mode 100644 index 0000000000..3d8a3d5412 --- /dev/null +++ b/test/cases/compile_errors/cast_without_result_type.zig @@ -0,0 +1,28 @@ +export fn a() void { + _ = @ptrFromInt(123); +} +export fn b() void { + const x = @ptrCast(@alignCast(@as(*u8, undefined))); + _ = x; +} +export fn c() void { + _ = &@intCast(@as(u64, 123)); + _ = S; +} +export fn d() void { + var x: f32 = 0; + _ = x + @floatFromInt(123); +} + +// error +// backend=stage2 +// target=native +// +// :2:9: error: @ptrFromInt must have a known result type +// :2:9: note: use @as to provide explicit result type +// :5:15: error: @ptrCast must have a known result type +// :5:15: note: use @as to provide explicit result type +// :9:10: error: @intCast must have a known result type +// :9:10: note: use @as to provide explicit result type +// :14:13: error: @floatFromInt must have a known result type +// :14:13: note: use @as to provide explicit result type diff --git a/test/cases/compile_errors/cast_without_result_type_due_to_generic_parameter.zig b/test/cases/compile_errors/cast_without_result_type_due_to_generic_parameter.zig new file mode 100644 index 0000000000..a9006c5352 --- /dev/null +++ b/test/cases/compile_errors/cast_without_result_type_due_to_generic_parameter.zig @@ -0,0 +1,31 @@ +export fn a() void { + bar(@ptrFromInt(123)); +} +export fn b() void { + bar(@ptrCast(@alignCast(@as(*u8, undefined)))); +} +export fn c() void { + bar(@intCast(@as(u64, 123))); +} +export fn d() void { + bar(@floatFromInt(123)); +} + +fn bar(_: anytype) void {} + +// error +// backend=stage2 +// target=native +// +// :2:9: error: @ptrFromInt must have a known result type +// :2:9: note: result type is unknown due to anytype parameter +// :2:9: note: use @as to provide explicit result type +// :5:9: error: @ptrCast must have a known result type +// :5:9: note: result type is unknown due to anytype parameter +// :5:9: note: use @as to provide explicit result type +// :8:9: error: @intCast must have a known result type +// :8:9: note: result type is unknown due to anytype parameter +// :8:9: note: use @as to provide explicit result type +// :11:9: error: @floatFromInt must have a known result type +// :11:9: note: result type is unknown due to anytype parameter +// :11:9: note: use @as to provide explicit result type diff --git a/test/cases/compile_errors/nested_ptr_cast_bad_operand.zig b/test/cases/compile_errors/nested_ptr_cast_bad_operand.zig new file mode 100644 index 0000000000..ec7ee3075c --- /dev/null +++ b/test/cases/compile_errors/nested_ptr_cast_bad_operand.zig @@ -0,0 +1,22 @@ +const p: ?*const u8 = null; +export fn a() void { + _ = @as(*const u32, @ptrCast(@alignCast(p))); +} +export fn b() void { + _ = @constCast(@volatileCast(123)); +} +export fn c() void { + const x: ?*f32 = @constCast(@ptrCast(@addrSpaceCast(@volatileCast(p)))); + _ = x; +} + +// error +// backend=stage2 +// target=native +// +// :3:45: error: null pointer casted to type '*const u32' +// :6:34: error: expected pointer type, found 'comptime_int' +// :9:22: error: cast increases pointer alignment +// :9:71: note: '?*const u8' has alignment '1' +// :9:22: note: '?*f32' has alignment '4' +// :9:22: note: use @alignCast to assert pointer alignment diff --git a/test/cases/compile_errors/redundant_ptr_cast.zig b/test/cases/compile_errors/redundant_ptr_cast.zig new file mode 100644 index 0000000000..933eeb2719 --- /dev/null +++ b/test/cases/compile_errors/redundant_ptr_cast.zig @@ -0,0 +1,19 @@ +const p: *anyopaque = undefined; +export fn a() void { + _ = @ptrCast(@ptrCast(p)); +} +export fn b() void { + const ptr1: *u32 = @alignCast(@ptrCast(@alignCast(p))); + _ = ptr1; +} +export fn c() void { + _ = @constCast(@alignCast(@ptrCast(@constCast(@volatileCast(p))))); +} + +// error +// backend=stage2 +// target=native +// +// :3:18: error: redundant @ptrCast +// :6:44: error: redundant @alignCast +// :10:40: error: redundant @constCast |
