diff options
| author | Wooster <wooster0@proton.me> | 2024-07-16 03:18:38 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-15 18:18:38 +0000 |
| commit | 888708ec8af9b60681ef14fb0a5c265f2a30b41f (patch) | |
| tree | 95da742d1d0082ae0150ea109f57e5ce2eebddb8 /test | |
| parent | 89942ebd03b2943cbbe84b575a024e156ca5bf52 (diff) | |
| download | zig-888708ec8af9b60681ef14fb0a5c265f2a30b41f.tar.gz zig-888708ec8af9b60681ef14fb0a5c265f2a30b41f.zip | |
Sema: support pointer subtraction
Diffstat (limited to 'test')
| -rw-r--r-- | test/behavior/pointers.zig | 60 | ||||
| -rw-r--r-- | test/cases/compile_errors/invalid_pointer_arithmetic.zig | 52 |
2 files changed, 110 insertions, 2 deletions
diff --git a/test/behavior/pointers.zig b/test/behavior/pointers.zig index c574f487b3..36152cf81a 100644 --- a/test/behavior/pointers.zig +++ b/test/behavior/pointers.zig @@ -17,7 +17,7 @@ fn testDerefPtr() !void { try expect(x == 1235); } -test "pointer arithmetic" { +test "pointer-integer arithmetic" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; @@ -43,6 +43,62 @@ test "pointer arithmetic" { try expect(ptr[0] == 'a'); } +test "pointer subtraction" { + if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; + + { + const a: *u8 = @ptrFromInt(100); + const b: *u8 = @ptrFromInt(50); + try expect(a - b == 50); + } + { + var ptr: [*]const u8 = "abc"; + try expect(&ptr[1] - &ptr[0] == 1); + try expect(&ptr[2] - &ptr[0] == 2); + } + { + const a: *[100]u16 = @ptrFromInt(100); + const b: *[100]u16 = @ptrFromInt(50); + try expect(a - b == 25); + } + { + var x: struct { a: u32, b: u32 } = undefined; + const a = &x.a; + const b = &x.b; + try expect(a - a == 0); + try expect(b - b == 0); + try expect(b - a == 1); + } + comptime { + var x: packed struct { a: u1, b: u1 } = undefined; + const a = &x.a; + const b = &x.b; + try expect(a - a == 0); + try expect(b - b == 0); + try expect(b - a == 0); + } + comptime { + var x: extern struct { a: u32, b: u32 } = undefined; + const a = &x.a; + const b = &x.b; + try expect(a - a == 0); + try expect(b - b == 0); + try expect(b - a == 1); + } + comptime { + const a: *const [3]u8 = "abc"; + const b: [*]const u8 = @ptrCast(a); + try expect(&a[1] - &b[0] == 1); + } + comptime { + var x: [64][64]u8 = undefined; + const a = &x[0][12]; + const b = &x[15][3]; + try expect(b - a == 951); + } +} + test "double pointer parsing" { comptime assert(PtrOf(PtrOf(i32)) == **i32); } @@ -382,7 +438,7 @@ test "pointer to array at fixed address" { try expect(@intFromPtr(&array[1]) == 0x14); } -test "pointer arithmetic affects the alignment" { +test "pointer-integer arithmetic affects the alignment" { { var ptr: [*]align(8) u32 = undefined; var x: usize = 1; diff --git a/test/cases/compile_errors/invalid_pointer_arithmetic.zig b/test/cases/compile_errors/invalid_pointer_arithmetic.zig new file mode 100644 index 0000000000..436a339204 --- /dev/null +++ b/test/cases/compile_errors/invalid_pointer_arithmetic.zig @@ -0,0 +1,52 @@ +export fn a(x: [*]u8) void { + _ = x * 1; +} + +export fn b(x: *u8) void { + _ = x * x; +} + +export fn c() void { + const x: []u8 = undefined; + const y: []u8 = undefined; + _ = x - y; +} + +export fn d() void { + var x: [*]u8 = undefined; + var y: [*]u16 = undefined; + _ = &x; + _ = &y; + _ = x - y; +} + +comptime { + const x: *u8 = @ptrFromInt(1); + const y: *u16 = @ptrFromInt(2); + _ = x - y; +} + +comptime { + const x: [*]u0 = @ptrFromInt(1); + _ = x + 1; +} + +comptime { + const x: *u0 = @ptrFromInt(1); + const y: *u0 = @ptrFromInt(2); + _ = x - y; +} + +// error +// backend=stage2 +// target=native +// +// :2:11: error: invalid pointer-integer arithmetic operator +// :2:11: note: pointer-integer arithmetic only supports addition and subtraction +// :6:11: error: invalid pointer-pointer arithmetic operator +// :6:11: note: pointer-pointer arithmetic only supports subtraction +// :12:11: error: invalid operands to binary expression: 'Pointer' and 'Pointer' +// :20:11: error: incompatible pointer arithmetic operands '[*]u8' and '[*]u16' +// :26:11: error: incompatible pointer arithmetic operands '*u8' and '*u16' +// :31:11: error: pointer arithmetic requires element type 'u0' to have runtime bits +// :37:11: error: pointer arithmetic requires element type 'u0' to have runtime bits |
