| 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; |
| 3 | const builtin = @import("builtin"); |
| 4 | |
| 5 | const module = @This(); |
| 6 | |
| 7 | fn Point(comptime T: type) type { |
| 8 | return struct { |
| 9 | const Self = @This(); |
| 10 | x: T, |
| 11 | y: T, |
| 12 | |
| 13 | fn addOne(self: *Self) void { |
| 14 | self.x += 1; |
| 15 | self.y += 1; |
| 16 | } |
| 17 | }; |
| 18 | } |
| 19 | |
| 20 | fn add(x: i32, y: i32) i32 { |
| 21 | return x + y; |
| 22 | } |
| 23 | |
| 24 | test "this refer to module call private fn" { |
| 25 | try expect(module.add(1, 2) == 3); |
| 26 | } |
| 27 | |
| 28 | test "this refer to container" { |
| 29 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 30 | |
| 31 | var pt: Point(i32) = undefined; |
| 32 | pt.x = 12; |
| 33 | pt.y = 34; |
| 34 | Point(i32).addOne(&pt); |
| 35 | try expect(pt.x == 13); |
| 36 | try expect(pt.y == 35); |
| 37 | } |
| 38 | |
| 39 | const State = struct { |
| 40 | const Self = @This(); |
| 41 | enter: *const fn (previous: ?Self) void, |
| 42 | }; |
| 43 | |
| 44 | fn prev(p: ?State) void { |
| 45 | expect(p == null) catch @panic("test failure"); |
| 46 | } |
| 47 | |
| 48 | test "this used as optional function parameter" { |
| 49 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; |
| 50 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 51 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 52 | |
| 53 | var global: State = undefined; |
| 54 | global.enter = prev; |
| 55 | global.enter(null); |
| 56 | } |
| 57 | |
| 58 | test "@This() in opaque" { |
| 59 | const T = opaque { |
| 60 | const Self = @This(); |
| 61 | }; |
| 62 | comptime std.debug.assert(T.Self == T); |
| 63 | } |