aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorLemonBoy <thatlemon@gmail.com>2020-02-25 13:10:29 +0100
committerAndrew Kelley <andrew@ziglang.org>2020-02-25 17:38:56 -0500
commit55ea855e2cefe8dcdb5fab9be66aa6ca3acd0370 (patch)
tree8b08c867108b6e589ea5e6504728199a4482fbd5 /test
parent89812217b4e5fee7e2851266c17c9d47204a1573 (diff)
downloadzig-55ea855e2cefe8dcdb5fab9be66aa6ca3acd0370.tar.gz
zig-55ea855e2cefe8dcdb5fab9be66aa6ca3acd0370.zip
ir: Various fixes for comptime ptr handling
* Correctly fold ptrToInt on optional types * Generate null as ConstPtrSpecialNull in intToPtr * Correctly stop ptrToInt on ?*T where T is zero-sized Closes #4535
Diffstat (limited to 'test')
-rw-r--r--test/compile_errors.zig9
-rw-r--r--test/stage1/behavior/pointers.zig12
2 files changed, 21 insertions, 0 deletions
diff --git a/test/compile_errors.zig b/test/compile_errors.zig
index 56df006e82..138f7a7633 100644
--- a/test/compile_errors.zig
+++ b/test/compile_errors.zig
@@ -3,6 +3,15 @@ const builtin = @import("builtin");
const Target = @import("std").Target;
pub fn addCases(cases: *tests.CompileErrorContext) void {
+ cases.addTest("@ptrToInt with pointer to zero-sized type",
+ \\export fn entry() void {
+ \\ var pointer: ?*u0 = null;
+ \\ var x = @ptrToInt(pointer);
+ \\}
+ , &[_][]const u8{
+ "tmp.zig:3:23: error: pointer to size 0 type has no address",
+ });
+
cases.addTest("slice to pointer conversion mismatch",
\\pub fn bytesAsSlice(bytes: var) [*]align(1) const u16 {
\\ return @ptrCast([*]align(1) const u16, bytes.ptr)[0..1];
diff --git a/test/stage1/behavior/pointers.zig b/test/stage1/behavior/pointers.zig
index fdaa5867d7..bcc1d62df3 100644
--- a/test/stage1/behavior/pointers.zig
+++ b/test/stage1/behavior/pointers.zig
@@ -318,3 +318,15 @@ test "pointer arithmetic affects the alignment" {
expect(@typeInfo(@TypeOf(ptr4)).Pointer.alignment == 4);
}
}
+
+test "@ptrToInt on null optional at comptime" {
+ {
+ const pointer = @intToPtr(?*u8, 0x000);
+ const x = @ptrToInt(pointer);
+ comptime expect(0 == @ptrToInt(pointer));
+ }
+ {
+ const pointer = @intToPtr(?*u8, 0xf00);
+ comptime expect(0xf00 == @ptrToInt(pointer));
+ }
+}