mirror of
https://github.com/ziglang/zig.git
synced 2026-02-12 20:37:54 +00:00
cases: add tests for errors introduced by cast builtin result type inference
This commit is contained in:
parent
569ae762e1
commit
d249629ef1
28
test/cases/compile_errors/cast_without_result_type.zig
Normal file
28
test/cases/compile_errors/cast_without_result_type.zig
Normal file
@ -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
|
||||
@ -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
|
||||
22
test/cases/compile_errors/nested_ptr_cast_bad_operand.zig
Normal file
22
test/cases/compile_errors/nested_ptr_cast_bad_operand.zig
Normal file
@ -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
|
||||
19
test/cases/compile_errors/redundant_ptr_cast.zig
Normal file
19
test/cases/compile_errors/redundant_ptr_cast.zig
Normal file
@ -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
|
||||
Loading…
x
Reference in New Issue
Block a user