diff options
| author | Ben Noordhuis <info@bnoordhuis.nl> | 2018-02-23 15:25:42 +0100 |
|---|---|---|
| committer | Ben Noordhuis <info@bnoordhuis.nl> | 2018-02-23 15:55:57 +0100 |
| commit | f11b9480192dea44acb92cb9edd7a91c7c73cd2f (patch) | |
| tree | 5172a83b0a23842d661d4e2d8bf74587e902913d /test | |
| parent | b66547e98c9034e52c5647735b47dc24939c8d15 (diff) | |
| download | zig-f11b9480192dea44acb92cb9edd7a91c7c73cd2f.tar.gz zig-f11b9480192dea44acb92cb9edd7a91c7c73cd2f.zip | |
allow implicit cast from `S` to `?&const S`
Allow implicit casts from container types to nullable const pointers to
said container type. That is:
fn f() void {
const s = S {};
g(s); // Works.
g(&s); // So does this.
}
fn g(_: ?&const S) void { // Nullable const pointer.
}
Fixes #731.
Diffstat (limited to 'test')
| -rw-r--r-- | test/cases/cast.zig | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/test/cases/cast.zig b/test/cases/cast.zig index 6ffb558174..dabf97a799 100644 --- a/test/cases/cast.zig +++ b/test/cases/cast.zig @@ -32,6 +32,77 @@ fn funcWithConstPtrPtr(x: &const &i32) void { **x += 1; } +test "implicitly cast a container to a const pointer of it" { + const z = Struct(void) { .x = void{} }; + assert(0 == @sizeOf(@typeOf(z))); + assert(void{} == Struct(void).pointer(z).x); + assert(void{} == Struct(void).pointer(&z).x); + assert(void{} == Struct(void).maybePointer(z).x); + assert(void{} == Struct(void).maybePointer(&z).x); + assert(void{} == Struct(void).maybePointer(null).x); + const s = Struct(u8) { .x = 42 }; + assert(0 != @sizeOf(@typeOf(s))); + assert(42 == Struct(u8).pointer(s).x); + assert(42 == Struct(u8).pointer(&s).x); + assert(42 == Struct(u8).maybePointer(s).x); + assert(42 == Struct(u8).maybePointer(&s).x); + assert(0 == Struct(u8).maybePointer(null).x); + const u = Union { .x = 42 }; + assert(42 == Union.pointer(u).x); + assert(42 == Union.pointer(&u).x); + assert(42 == Union.maybePointer(u).x); + assert(42 == Union.maybePointer(&u).x); + assert(0 == Union.maybePointer(null).x); + const e = Enum.Some; + assert(Enum.Some == Enum.pointer(e)); + assert(Enum.Some == Enum.pointer(&e)); + assert(Enum.Some == Enum.maybePointer(e)); + assert(Enum.Some == Enum.maybePointer(&e)); + assert(Enum.None == Enum.maybePointer(null)); +} + +fn Struct(comptime T: type) type { + return struct { + const Self = this; + x: T, + + fn pointer(self: &const Self) Self { + return *self; + } + + fn maybePointer(self: ?&const Self) Self { + const none = Self { .x = if (T == void) void{} else 0 }; + return *(self ?? &none); + } + }; +} + +const Union = union { + x: u8, + + fn pointer(self: &const Union) Union { + return *self; + } + + fn maybePointer(self: ?&const Union) Union { + const none = Union { .x = 0 }; + return *(self ?? &none); + } +}; + +const Enum = enum { + None, + Some, + + fn pointer(self: &const Enum) Enum { + return *self; + } + + fn maybePointer(self: ?&const Enum) Enum { + return *(self ?? &Enum.None); + } +}; + test "explicit cast from integer to error type" { testCastIntToErr(error.ItBroke); comptime testCastIntToErr(error.ItBroke); |
