zig/test/behavior/inttoptr.zig
mlugg 11d0dfb882 Sema: fix @intToPtr of zero value to optional pointer
Calling into coercion logic here is a little opaque, and more to the
point wholly unnecessary. Instead, the (very short) logic is now
implemented directly in Sema.

Resolves: #16033
2023-06-15 02:43:06 -07:00

49 lines
1.6 KiB
Zig

const std = @import("std");
const builtin = @import("builtin");
const expectEqual = std.testing.expectEqual;
test "casting integer address to function pointer" {
addressToFunction();
comptime addressToFunction();
}
fn addressToFunction() void {
var addr: usize = 0xdeadbee0;
_ = @intToPtr(*const fn () void, addr);
}
test "mutate through ptr initialized with constant intToPtr value" {
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
forceCompilerAnalyzeBranchHardCodedPtrDereference(false);
}
fn forceCompilerAnalyzeBranchHardCodedPtrDereference(x: bool) void {
const hardCodedP = @intToPtr(*volatile u8, 0xdeadbeef);
if (x) {
hardCodedP.* = hardCodedP.* | 10;
} else {
return;
}
}
test "@intToPtr creates null pointer" {
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
const ptr = @intToPtr(?*u32, 0);
try expectEqual(@as(?*u32, null), ptr);
}
test "@intToPtr creates allowzero zero pointer" {
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
const ptr = @intToPtr(*allowzero u32, 0);
try expectEqual(@as(usize, 0), @ptrToInt(ptr));
}