Commit d249629ef1
Changed files (4)
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
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
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
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