optimize logical and/or, initial null checking in flows

This commit is contained in:
dcode
2019-04-09 03:04:45 +02:00
parent c16c19e18d
commit da4a7751fd
73 changed files with 5920 additions and 6866 deletions

View File

@ -0,0 +1,98 @@
class Ref {}
// the following makes use of the fact that branches that can be eliminated statically
// won't become compiled, hence the ERROR statement is never executed.
export function testTrue(a: Ref | null): void {
if (a) {
if (isNullable(a)) ERROR("should be non-nullable");
}
}
export function testFalseElse(a: Ref | null): void {
if (!a) return;
else {
if (isNullable(a)) ERROR("should be non-nullable");
}
}
export function testFalseContinuation(a: Ref | null): void {
if (!a) return;
if (isNullable(a)) ERROR("should be non-nullable");
}
export function testNeNull(a: Ref | null): void {
if (a != null) {
if (isNullable(a)) ERROR("should be non-nullable");
}
}
export function testEqNullElse(a: Ref | null): void {
if (a == null) return;
else {
if (isNullable(a)) ERROR("should be non-nullable");
}
}
export function testEqNullContinuation(a: Ref | null): void {
if (a == null) return;
if (isNullable(a)) ERROR("should be non-nullable");
}
export function testNotEqNull(a: Ref | null): void {
if (!(a == null)) {
if (isNullable(a)) ERROR("should be non-nullable");
}
}
export function testNotNeNullElse(a: Ref | null): void {
if (!(a != null)) return;
else {
if (isNullable(a)) ERROR("should be non-nullable");
}
}
export function testNotNeNullContinuation(a: Ref | null): void {
if (!(a != null)) return;
if (isNullable(a)) ERROR("should be non-nullable");
}
export function testWhile(a: Ref | null): void {
while (a) {
if (isNullable(a)) ERROR("should be non-nullable");
a = null;
if (!isNullable(a)) ERROR("should be nullable again");
}
}
export function testWhile2(a: Ref | null, b: Ref | null): void {
while (a) {
if (isNullable(a)) ERROR("should be non-nullable");
a = b;
if (!isNullable(a)) ERROR("should be nullable again");
}
}
export function testWhile3(a: Ref | null, b: Ref | null): void {
while (a) {
if (isNullable(a)) ERROR("should be non-nullable");
if (b) {
a = b;
if (isNullable(a)) ERROR("should be non-nullable still");
}
}
}
// TODO:
// function requireNonNull(a: Ref): Ref {
// return a;
// }
// export function testLogicalAnd(a: Ref | null): void {
// a && requireNonNull(a);
// }
// export function testLogicalOr(a: Ref | null): void {
// !a || requireNonNull(a);
// }