2017-01-05 00:57:48 -08:00
|
|
|
const assert = @import("std").debug.assert;
|
|
|
|
|
2016-12-21 22:20:08 -08:00
|
|
|
fn nullableType() {
|
|
|
|
@setFnTest(this);
|
|
|
|
|
2017-01-08 19:25:38 -08:00
|
|
|
const x : ?bool = @generatedCode(true);
|
2016-12-21 22:20:08 -08:00
|
|
|
|
|
|
|
if (const y ?= x) {
|
|
|
|
if (y) {
|
|
|
|
// OK
|
|
|
|
} else {
|
|
|
|
@unreachable();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
@unreachable();
|
|
|
|
}
|
|
|
|
|
2017-01-08 19:25:38 -08:00
|
|
|
const next_x : ?i32 = @generatedCode(null);
|
2016-12-21 22:20:08 -08:00
|
|
|
|
|
|
|
const z = next_x ?? 1234;
|
|
|
|
|
|
|
|
assert(z == 1234);
|
|
|
|
|
2017-01-08 19:25:38 -08:00
|
|
|
const final_x : ?i32 = @generatedCode(13);
|
2016-12-21 22:20:08 -08:00
|
|
|
|
|
|
|
const num = final_x ?? @unreachable();
|
|
|
|
|
|
|
|
assert(num == 13);
|
|
|
|
}
|
|
|
|
|
2016-12-21 22:42:30 -08:00
|
|
|
fn assignToIfVarPtr() {
|
|
|
|
@setFnTest(this);
|
|
|
|
|
|
|
|
var maybe_bool: ?bool = true;
|
|
|
|
|
|
|
|
if (const *b ?= maybe_bool) {
|
|
|
|
*b = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(??maybe_bool == false);
|
|
|
|
}
|
|
|
|
|
2016-12-22 07:09:53 -08:00
|
|
|
fn rhsMaybeUnwrapReturn() {
|
|
|
|
@setFnTest(this);
|
|
|
|
|
2017-01-08 19:25:38 -08:00
|
|
|
const x: ?bool = @generatedCode(true);
|
2016-12-22 07:09:53 -08:00
|
|
|
const y = x ?? return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn maybeReturn() {
|
|
|
|
@setFnTest(this);
|
|
|
|
|
|
|
|
assert(??foo(1235));
|
|
|
|
assert(if (const _ ?= foo(null)) false else true);
|
|
|
|
assert(!??foo(1234));
|
|
|
|
}
|
|
|
|
|
2016-12-26 00:44:59 -08:00
|
|
|
|
|
|
|
fn ifVarMaybePointer() {
|
|
|
|
@setFnTest(this);
|
|
|
|
|
|
|
|
assert(shouldBeAPlus1(Particle {.a = 14, .b = 1, .c = 1, .d = 1}) == 15);
|
|
|
|
}
|
|
|
|
fn shouldBeAPlus1(p: Particle) -> u64 {
|
|
|
|
var maybe_particle: ?Particle = p;
|
|
|
|
if (const *particle ?= maybe_particle) {
|
|
|
|
particle.a += 1;
|
|
|
|
}
|
|
|
|
if (const particle ?= maybe_particle) {
|
|
|
|
return particle.a;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const Particle = struct {
|
|
|
|
a: u64,
|
|
|
|
b: u64,
|
|
|
|
c: u64,
|
|
|
|
d: u64,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
fn nullLiteralOutsideFunction() {
|
|
|
|
@setFnTest(this);
|
|
|
|
|
2017-01-16 13:39:31 -08:00
|
|
|
const is_null = here_is_a_null_literal.context == null;
|
2016-12-26 00:44:59 -08:00
|
|
|
assert(is_null);
|
2017-01-16 13:39:31 -08:00
|
|
|
|
|
|
|
const is_non_null = here_is_a_null_literal.context != null;
|
|
|
|
assert(!is_non_null);
|
2016-12-26 00:44:59 -08:00
|
|
|
}
|
|
|
|
const SillyStruct = struct {
|
|
|
|
context: ?i32,
|
|
|
|
};
|
|
|
|
const here_is_a_null_literal = SillyStruct {
|
|
|
|
.context = null,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-12-22 07:09:53 -08:00
|
|
|
// TODO test static eval maybe return
|
|
|
|
fn foo(x: ?i32) -> ?bool {
|
|
|
|
const value = ?return x;
|
|
|
|
return value > 1234;
|
|
|
|
}
|
2017-01-16 13:39:31 -08:00
|
|
|
|
|
|
|
fn testNullRuntime() {
|
|
|
|
@setFnTest(this);
|
|
|
|
|
|
|
|
testTestNullRuntime(null);
|
|
|
|
}
|
|
|
|
fn testTestNullRuntime(x: ?i32) {
|
|
|
|
assert(x == null);
|
|
|
|
assert(!(x != null));
|
|
|
|
}
|