| ... | ... | @@ -32,6 +32,77 @@ fn funcWithConstPtrPtr(x: &const &i32) void { |
| 32 | 32 | **x += 1; |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | test "implicitly cast a container to a const pointer of it" { |
| 36 | const z = Struct(void) { .x = void{} }; |
| 37 | assert(0 == @sizeOf(@typeOf(z))); |
| 38 | assert(void{} == Struct(void).pointer(z).x); |
| 39 | assert(void{} == Struct(void).pointer(&z).x); |
| 40 | assert(void{} == Struct(void).maybePointer(z).x); |
| 41 | assert(void{} == Struct(void).maybePointer(&z).x); |
| 42 | assert(void{} == Struct(void).maybePointer(null).x); |
| 43 | const s = Struct(u8) { .x = 42 }; |
| 44 | assert(0 != @sizeOf(@typeOf(s))); |
| 45 | assert(42 == Struct(u8).pointer(s).x); |
| 46 | assert(42 == Struct(u8).pointer(&s).x); |
| 47 | assert(42 == Struct(u8).maybePointer(s).x); |
| 48 | assert(42 == Struct(u8).maybePointer(&s).x); |
| 49 | assert(0 == Struct(u8).maybePointer(null).x); |
| 50 | const u = Union { .x = 42 }; |
| 51 | assert(42 == Union.pointer(u).x); |
| 52 | assert(42 == Union.pointer(&u).x); |
| 53 | assert(42 == Union.maybePointer(u).x); |
| 54 | assert(42 == Union.maybePointer(&u).x); |
| 55 | assert(0 == Union.maybePointer(null).x); |
| 56 | const e = Enum.Some; |
| 57 | assert(Enum.Some == Enum.pointer(e)); |
| 58 | assert(Enum.Some == Enum.pointer(&e)); |
| 59 | assert(Enum.Some == Enum.maybePointer(e)); |
| 60 | assert(Enum.Some == Enum.maybePointer(&e)); |
| 61 | assert(Enum.None == Enum.maybePointer(null)); |
| 62 | } |
| 63 | |
| 64 | fn Struct(comptime T: type) type { |
| 65 | return struct { |
| 66 | const Self = this; |
| 67 | x: T, |
| 68 | |
| 69 | fn pointer(self: &const Self) Self { |
| 70 | return *self; |
| 71 | } |
| 72 | |
| 73 | fn maybePointer(self: ?&const Self) Self { |
| 74 | const none = Self { .x = if (T == void) void{} else 0 }; |
| 75 | return *(self ?? &none); |
| 76 | } |
| 77 | }; |
| 78 | } |
| 79 | |
| 80 | const Union = union { |
| 81 | x: u8, |
| 82 | |
| 83 | fn pointer(self: &const Union) Union { |
| 84 | return *self; |
| 85 | } |
| 86 | |
| 87 | fn maybePointer(self: ?&const Union) Union { |
| 88 | const none = Union { .x = 0 }; |
| 89 | return *(self ?? &none); |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | const Enum = enum { |
| 94 | None, |
| 95 | Some, |
| 96 | |
| 97 | fn pointer(self: &const Enum) Enum { |
| 98 | return *self; |
| 99 | } |
| 100 | |
| 101 | fn maybePointer(self: ?&const Enum) Enum { |
| 102 | return *(self ?? &Enum.None); |
| 103 | } |
| 104 | }; |
| 105 | |
| 35 | 106 | test "explicit cast from integer to error type" { |
| 36 | 107 | testCastIntToErr(error.ItBroke); |
| 37 | 108 | comptime testCastIntToErr(error.ItBroke); |