mirror of
https://github.com/ziglang/zig.git
synced 2025-12-24 15:13:08 +00:00
* Sema: implement peer type resolution for optionals and null. * Rename `Module.optionalType` to `Type.optional`. * LLVM backend: re-use anonymous values. This is especially useful when isByRef()=true because it means re-using the same generated LLVM globals. * LLVM backend: rework the implementation of is_null and is_non_null AIR instructions. Generate slightly better LLVM code, and also fix the behavior for optionals whose payload type is 0-bit. * LLVM backend: improve `cmp` AIR instruction lowering to support pointer-like optionals. * `Value`: implement support for equality-checking optionals.
76 lines
1.6 KiB
Zig
76 lines
1.6 KiB
Zig
const std = @import("std");
|
|
const testing = std.testing;
|
|
const expect = testing.expect;
|
|
const expectEqual = testing.expectEqual;
|
|
|
|
test "passing an optional integer as a parameter" {
|
|
const S = struct {
|
|
fn entry() bool {
|
|
var x: i32 = 1234;
|
|
return foo(x);
|
|
}
|
|
|
|
fn foo(x: ?i32) bool {
|
|
return x.? == 1234;
|
|
}
|
|
};
|
|
try expect(S.entry());
|
|
comptime try expect(S.entry());
|
|
}
|
|
|
|
test "self-referential struct through a slice of optional" {
|
|
const S = struct {
|
|
const Node = struct {
|
|
children: []?Node,
|
|
data: ?u8,
|
|
|
|
fn new() Node {
|
|
return Node{
|
|
.children = undefined,
|
|
.data = null,
|
|
};
|
|
}
|
|
};
|
|
};
|
|
|
|
var n = S.Node.new();
|
|
try expect(n.data == null);
|
|
}
|
|
|
|
pub const EmptyStruct = struct {};
|
|
|
|
test "optional pointer to size zero struct" {
|
|
var e = EmptyStruct{};
|
|
var o: ?*EmptyStruct = &e;
|
|
try expect(o != null);
|
|
}
|
|
|
|
test "equality compare optional pointers" {
|
|
try testNullPtrsEql();
|
|
comptime try testNullPtrsEql();
|
|
}
|
|
|
|
fn testNullPtrsEql() !void {
|
|
var number: i32 = 1234;
|
|
|
|
var x: ?*i32 = null;
|
|
var y: ?*i32 = null;
|
|
try expect(x == y);
|
|
y = &number;
|
|
try expect(x != y);
|
|
try expect(x != &number);
|
|
try expect(&number != x);
|
|
x = &number;
|
|
try expect(x == y);
|
|
try expect(x == &number);
|
|
try expect(&number == x);
|
|
}
|
|
|
|
test "optional with void type" {
|
|
const Foo = struct {
|
|
x: ?void,
|
|
};
|
|
var x = Foo{ .x = null };
|
|
try expect(x.x == null);
|
|
}
|