master
 1const std = @import("std");
 2const builtin = @import("builtin");
 3const expectEqual = std.testing.expectEqual;
 4
 5test "casting integer address to function pointer" {
 6    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
 7
 8    addressToFunction();
 9    comptime addressToFunction();
10}
11
12fn addressToFunction() void {
13    var addr: usize = 0xdeadbee0;
14    _ = &addr;
15    _ = @as(*const fn () void, @ptrFromInt(addr));
16}
17
18test "mutate through ptr initialized with constant ptrFromInt value" {
19    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
20    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
21    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
22
23    forceCompilerAnalyzeBranchHardCodedPtrDereference(false);
24}
25
26fn forceCompilerAnalyzeBranchHardCodedPtrDereference(x: bool) void {
27    const hardCodedP = @as(*volatile u8, @ptrFromInt(0xdeadbeef));
28    if (x) {
29        hardCodedP.* = hardCodedP.* | 10;
30    } else {
31        return;
32    }
33}
34
35test "@ptrFromInt creates null pointer" {
36    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
37    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
38    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
39
40    const ptr = @as(?*u32, @ptrFromInt(0));
41    try expectEqual(@as(?*u32, null), ptr);
42}
43
44test "@ptrFromInt creates allowzero zero pointer" {
45    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
46    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
47    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
48
49    const ptr = @as(*allowzero u32, @ptrFromInt(0));
50    try expectEqual(@as(usize, 0), @intFromPtr(ptr));
51}