|
| 1 | +fn Foo(T: type) type { |
| 2 | + return struct { |
| 3 | + fn bar(U: type, t: ?T, u: ?U) void { |
| 4 | + _ = .{ t, u }; |
| 5 | + } |
| 6 | + |
| 7 | + fn baz(U: type, t: T, u: U) T { |
| 8 | + return t + u; |
| 9 | + } |
| 10 | + |
| 11 | + fn qux(U: type, t: T, u: U) @TypeOf(t, u) { |
| 12 | + return t + u; |
| 13 | + } |
| 14 | + }; |
| 15 | +} |
| 16 | + |
| 17 | +const foo = Foo(u8){}; |
| 18 | +// ^^^ (Foo(u8))() |
| 19 | + |
| 20 | +// TODO this should be `fn (U: type, ?u8, ?U) void` |
| 21 | +const bar_fn = Foo(u8).bar; |
| 22 | +// ^^^^^^ (fn (type, ?u8, ?U) void)() |
| 23 | + |
| 24 | +const bar_call = Foo(u8).bar(i32, null, null); |
| 25 | +// ^^^^^^^^ (void)() |
| 26 | + |
| 27 | +// TODO this should be `fn (U: type, i32, U) i32` |
| 28 | +const baz_fn = Foo(i32).baz; |
| 29 | +// ^^^^^^ (fn (type, i32, U) i32)() |
| 30 | + |
| 31 | +const baz_call = Foo(i32).baz(u8, -42, 42); |
| 32 | +// ^^^^^^^^ (i32)() |
| 33 | + |
| 34 | +// TODO this should be `fn (U: type, u8, U) anytype` |
| 35 | +const qux_fn = Foo(u8).qux; |
| 36 | +// ^^^^^^ (fn (type, u8, U) u8)() |
| 37 | + |
| 38 | +// TODO this should be `i32` |
| 39 | +const qux_call = Foo(u8).qux(i32, 42, -42); |
| 40 | +// ^^^^^^^^ (u8)() |
| 41 | + |
| 42 | +fn fizz(T: type) ?fn () error{}!struct { ??T } { |
| 43 | + return null; |
| 44 | +} |
| 45 | + |
| 46 | +// TODO this should be `fn (T: type) ?fn () error{}!struct { ??T })()` |
| 47 | +const fizz_fn = fizz; |
| 48 | +// ^^^^^^^ (fn (type) ?fn () error{}!struct { ??T })() |
| 49 | + |
| 50 | +const fizz_call = fizz(u8); |
| 51 | +// ^^^^^^^^^ (?fn () error{}!struct { ??u8 })() |
| 52 | + |
| 53 | +comptime { |
| 54 | + // Use @compileLog to verify the expected type with the compiler: |
| 55 | + // @compileLog(foo); |
| 56 | +} |
| 57 | + |
| 58 | +fn Point1(comptime T: type) type { |
| 59 | + return struct { |
| 60 | + x: T, |
| 61 | + y: T, |
| 62 | + fn normSquared(self: Point1(T)) T { |
| 63 | + _ = self; |
| 64 | + // ^^^^ (Point1(T))() |
| 65 | + } |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +fn parameter(comptime T: type, in: T) void { |
| 70 | + _ = in; |
| 71 | + // ^^ (T)() |
| 72 | +} |
| 73 | + |
| 74 | +fn taggedUnion(comptime T: type, in: union(enum) { a: T, b: T }) void { |
| 75 | + switch (in) { |
| 76 | + .a => |a| { |
| 77 | + _ = a; |
| 78 | + // ^ (T)() |
| 79 | + }, |
| 80 | + .b => |b| { |
| 81 | + _ = b; |
| 82 | + // ^ (T)() |
| 83 | + }, |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +fn Option(comptime T: type) type { |
| 88 | + return struct { |
| 89 | + item: ?T, |
| 90 | + const none: @This() = undefined; |
| 91 | + const alias = none; |
| 92 | + const default = init(); |
| 93 | + fn init() @This() {} |
| 94 | + }; |
| 95 | +} |
| 96 | + |
| 97 | +const option_none: Option(u8) = .none; |
| 98 | +// ^^^^^ (Option(u8))() |
| 99 | + |
| 100 | +const option_alias: Option(u8) = .alias; |
| 101 | +// ^^^^^^ (Option(u8))() |
| 102 | + |
| 103 | +const option_default: Option(u8) = .default; |
| 104 | +// ^^^^^^^^ (Option(u8))() |
| 105 | + |
| 106 | +const option_init: Option(u8) = .init(); |
| 107 | +// ^^^^^ (fn () Option(u8))() |
| 108 | + |
| 109 | +fn GenericUnion(T: type) type { |
| 110 | + return union { |
| 111 | + field: T, |
| 112 | + const decl: T = undefined; |
| 113 | + }; |
| 114 | +} |
| 115 | + |
| 116 | +const generic_union_decl = GenericUnion(u8).decl; |
| 117 | +// ^^^^^^^^^^^^^^^^^^ (u8)() |
| 118 | + |
| 119 | +const generic_union: GenericUnion(u8) = .{ .field = 1 }; |
| 120 | +// ^^^^^^^^^^^^^ (GenericUnion(u8))() |
| 121 | + |
| 122 | +const generic_union_field = generic_union.field; |
| 123 | +// ^^^^^^^^^^^^^^^^^^^ (u8)() |
| 124 | + |
| 125 | +const generic_union_tag = GenericUnion(u8).field; |
| 126 | +// ^^^^^^^^^^^^^^^^^ (unknown)() |
| 127 | + |
| 128 | +fn GenericTaggedUnion(T: type) type { |
| 129 | + return union(enum) { |
| 130 | + field: T, |
| 131 | + const decl: T = undefined; |
| 132 | + }; |
| 133 | +} |
| 134 | + |
| 135 | +const generic_tagged_union_decl = GenericTaggedUnion(u8).decl; |
| 136 | +// ^^^^^^^^^^^^^^^^^^^^^^^^^ (u8)() |
| 137 | + |
| 138 | +const generic_tagged_union: GenericTaggedUnion(u8) = .{ .field = 1 }; |
| 139 | +// ^^^^^^^^^^^^^^^^^^^^ (GenericTaggedUnion(u8))() |
| 140 | + |
| 141 | +const generic_tagged_union_field = generic_tagged_union.field; |
| 142 | +// ^^^^^^^^^^^^^^^^^^^^^^^^^^ (u8)() |
| 143 | + |
| 144 | +const generic_tagged_union_tag = GenericTaggedUnion(u8).field; |
| 145 | +// ^^^^^^^^^^^^^^^^^^^^^^^^ (@typeInfo(GenericTaggedUnion(u8)).@"union".tag_type.?)() |
| 146 | + |
| 147 | +fn GenericEnum(T: type) type { |
| 148 | + return enum { |
| 149 | + field, |
| 150 | + const decl: T = undefined; |
| 151 | + }; |
| 152 | +} |
| 153 | + |
| 154 | +const generic_enum_decl = GenericEnum(u8).decl; |
| 155 | +// ^^^^^^^^^^^^^^^^^ (u8)() |
| 156 | + |
| 157 | +const generic_enum: GenericEnum(u8) = .field; |
| 158 | +// ^^^^^^^^^^^^ (GenericEnum(u8))() |
| 159 | + |
| 160 | +const generic_enum_field = generic_enum.field; |
| 161 | +// ^^^^^^^^^^^^^^^^^^ (unknown)() |
| 162 | + |
| 163 | +const generic_enum_tag = GenericEnum(u8).field; |
| 164 | +// ^^^^^^^^^^^^^^^^ (GenericEnum(u8))() |
| 165 | + |
| 166 | +fn GenericStruct(T: type) type { |
| 167 | + return struct { |
| 168 | + field: T, |
| 169 | + const decl: T = undefined; |
| 170 | + }; |
| 171 | +} |
| 172 | + |
| 173 | +const generic_struct_decl = GenericStruct(u8).decl; |
| 174 | +// ^^^^^^^^^^^^^^^^^^^ (u8)() |
| 175 | + |
| 176 | +const generic_struct: GenericStruct(u8) = .{ .field = 1 }; |
| 177 | +// ^^^^^^^^^^^^^^ (GenericStruct(u8))() |
| 178 | + |
| 179 | +const generic_struct_field = generic_struct.field; |
| 180 | +// ^^^^^^^^^^^^^^^^^^^^ (u8)() |
| 181 | + |
| 182 | +const generic_struct_tag = GenericStruct(u8).field; |
| 183 | +// ^^^^^^^^^^^^^^^^^^ (unknown)() |
| 184 | + |
| 185 | +fn Map(Context: type) type { |
| 186 | + return struct { |
| 187 | + unmanaged: MapUnmanaged(Context), |
| 188 | + ctx: Context, |
| 189 | + const Self = @This(); |
| 190 | + fn clone(self: Self) Self { |
| 191 | + const unmanaged = self.unmanaged.cloneContext(self.ctx); |
| 192 | + // ^^^^^^^^^ (MapUnmanaged(either type))() |
| 193 | + return .{ .unmanaged = unmanaged, .ctx = self.ctx }; |
| 194 | + } |
| 195 | + fn clone2(self: Self) Self { |
| 196 | + const unmanaged = self.unmanaged.cloneContext2(self.ctx); |
| 197 | + // ^^^^^^^^^ (MapUnmanaged(*either type))() |
| 198 | + return .{ .unmanaged = unmanaged, .ctx = self.ctx }; |
| 199 | + } |
| 200 | + }; |
| 201 | +} |
| 202 | + |
| 203 | +fn MapUnmanaged(Context: type) type { |
| 204 | + return struct { |
| 205 | + size: u32, |
| 206 | + const Self = @This(); |
| 207 | + fn clone(self: Self) Self { |
| 208 | + return self.cloneContext(@as(Context, undefined)); |
| 209 | + } |
| 210 | + fn cloneContext(self: Self, new_ctx: anytype) MapUnmanaged(@TypeOf(new_ctx)) { |
| 211 | + _ = self; |
| 212 | + } |
| 213 | + fn clone2(self: Self) Self { |
| 214 | + return self.cloneContext2(@as(Context, undefined)); |
| 215 | + } |
| 216 | + fn cloneContext2(self: Self, new_ctx: anytype) MapUnmanaged(*@TypeOf(new_ctx)) { |
| 217 | + _ = self; |
| 218 | + } |
| 219 | + }; |
| 220 | +} |
| 221 | + |
| 222 | +const some_list: std.ArrayListUnmanaged(u8) = .empty; |
| 223 | +// ^^^^^^^^^ (ArrayListAlignedUnmanaged(u8))() |
| 224 | + |
| 225 | +const some_list_items = some_list.items; |
| 226 | +// ^^^^^^^^^^^^^^^ ([]u8)() |
| 227 | + |
| 228 | +const std = @import("std"); |
0 commit comments