mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-04-28 08:22:15 +00:00
null checking throughout logical and/or
This commit is contained in:
parent
da4a7751fd
commit
cd79376101
@ -4755,8 +4755,14 @@ export class Compiler extends DiagnosticEmitter {
|
|||||||
case Token.AMPERSAND_AMPERSAND: { // left && right
|
case Token.AMPERSAND_AMPERSAND: { // left && right
|
||||||
leftExpr = this.compileExpressionRetainType(left, contextualType, WrapMode.NONE);
|
leftExpr = this.compileExpressionRetainType(left, contextualType, WrapMode.NONE);
|
||||||
leftType = this.currentType;
|
leftType = this.currentType;
|
||||||
|
|
||||||
|
let previousFlow = this.currentFlow;
|
||||||
|
let rightFlow = previousFlow.fork();
|
||||||
|
this.currentFlow = rightFlow;
|
||||||
|
rightFlow.inheritNonnullIf(leftExpr);
|
||||||
rightExpr = this.compileExpression(right, leftType, ConversionKind.IMPLICIT, WrapMode.NONE);
|
rightExpr = this.compileExpression(right, leftType, ConversionKind.IMPLICIT, WrapMode.NONE);
|
||||||
rightType = leftType;
|
rightType = leftType;
|
||||||
|
this.currentFlow = previousFlow;
|
||||||
|
|
||||||
// simplify if only interested in true or false
|
// simplify if only interested in true or false
|
||||||
if (contextualType == Type.bool || contextualType == Type.void) {
|
if (contextualType == Type.bool || contextualType == Type.void) {
|
||||||
@ -4799,8 +4805,14 @@ export class Compiler extends DiagnosticEmitter {
|
|||||||
case Token.BAR_BAR: { // left || right
|
case Token.BAR_BAR: { // left || right
|
||||||
leftExpr = this.compileExpressionRetainType(left, contextualType, WrapMode.NONE);
|
leftExpr = this.compileExpressionRetainType(left, contextualType, WrapMode.NONE);
|
||||||
leftType = this.currentType;
|
leftType = this.currentType;
|
||||||
|
|
||||||
|
let previousFlow = this.currentFlow;
|
||||||
|
let rightFlow = previousFlow.fork();
|
||||||
|
this.currentFlow = rightFlow;
|
||||||
|
rightFlow.inheritNonnullIfNot(leftExpr);
|
||||||
rightExpr = this.compileExpression(right, leftType, ConversionKind.IMPLICIT, WrapMode.NONE);
|
rightExpr = this.compileExpression(right, leftType, ConversionKind.IMPLICIT, WrapMode.NONE);
|
||||||
rightType = leftType;
|
rightType = leftType;
|
||||||
|
this.currentFlow = previousFlow;
|
||||||
|
|
||||||
// simplify if only interested in true or false
|
// simplify if only interested in true or false
|
||||||
if (contextualType == Type.bool || contextualType == Type.void) {
|
if (contextualType == Type.bool || contextualType == Type.void) {
|
||||||
|
@ -16,6 +16,10 @@
|
|||||||
(export "testWhile" (func $possibly-null/testWhile))
|
(export "testWhile" (func $possibly-null/testWhile))
|
||||||
(export "testWhile2" (func $possibly-null/testWhile2))
|
(export "testWhile2" (func $possibly-null/testWhile2))
|
||||||
(export "testWhile3" (func $possibly-null/testWhile3))
|
(export "testWhile3" (func $possibly-null/testWhile3))
|
||||||
|
(export "testLogicalAnd" (func $possibly-null/testTrue))
|
||||||
|
(export "testLogicalOr" (func $possibly-null/testTrue))
|
||||||
|
(export "testLogicalAndMulti" (func $possibly-null/testLogicalAndMulti))
|
||||||
|
(export "testLogicalOrMulti" (func $possibly-null/testLogicalAndMulti))
|
||||||
(func $possibly-null/testTrue (; 0 ;) (type $FUNCSIG$vi) (param $0 i32)
|
(func $possibly-null/testTrue (; 0 ;) (type $FUNCSIG$vi) (param $0 i32)
|
||||||
nop
|
nop
|
||||||
)
|
)
|
||||||
@ -52,7 +56,10 @@
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
(func $null (; 4 ;) (type $FUNCSIG$v)
|
(func $possibly-null/testLogicalAndMulti (; 4 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32)
|
||||||
|
nop
|
||||||
|
)
|
||||||
|
(func $null (; 5 ;) (type $FUNCSIG$v)
|
||||||
nop
|
nop
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -83,16 +83,30 @@ export function testWhile3(a: Ref | null, b: Ref | null): void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO:
|
function requireNonNull(a: Ref): Ref {
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
// function requireNonNull(a: Ref): Ref {
|
export function testLogicalAnd(a: Ref | null): void {
|
||||||
// return a;
|
a && requireNonNull(a);
|
||||||
// }
|
}
|
||||||
|
|
||||||
// export function testLogicalAnd(a: Ref | null): void {
|
export function testLogicalOr(a: Ref | null): void {
|
||||||
// a && requireNonNull(a);
|
!a || requireNonNull(a) != null;
|
||||||
// }
|
}
|
||||||
|
|
||||||
// export function testLogicalOr(a: Ref | null): void {
|
export function testLogicalAndMulti(a: Ref | null, b: Ref | null): void {
|
||||||
// !a || requireNonNull(a);
|
if (a && b) {
|
||||||
// }
|
if (isNullable(a)) ERROR("should be non-nullable");
|
||||||
|
if (isNullable(b)) ERROR("should be non-nullable");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function testLogicalOrMulti(a: Ref | null, b: Ref | null): void {
|
||||||
|
if (!a || !b) {
|
||||||
|
// something
|
||||||
|
} else {
|
||||||
|
if (isNullable(a)) ERROR("should be non-nullable");
|
||||||
|
if (isNullable(b)) ERROR("should be non-nullable");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
(module
|
(module
|
||||||
(type $FUNCSIG$vi (func (param i32)))
|
(type $FUNCSIG$vi (func (param i32)))
|
||||||
(type $FUNCSIG$vii (func (param i32 i32)))
|
(type $FUNCSIG$vii (func (param i32 i32)))
|
||||||
|
(type $FUNCSIG$ii (func (param i32) (result i32)))
|
||||||
(type $FUNCSIG$v (func))
|
(type $FUNCSIG$v (func))
|
||||||
(memory $0 0)
|
(memory $0 0)
|
||||||
(table $0 1 funcref)
|
(table $0 1 funcref)
|
||||||
@ -18,6 +19,10 @@
|
|||||||
(export "testWhile" (func $possibly-null/testWhile))
|
(export "testWhile" (func $possibly-null/testWhile))
|
||||||
(export "testWhile2" (func $possibly-null/testWhile2))
|
(export "testWhile2" (func $possibly-null/testWhile2))
|
||||||
(export "testWhile3" (func $possibly-null/testWhile3))
|
(export "testWhile3" (func $possibly-null/testWhile3))
|
||||||
|
(export "testLogicalAnd" (func $possibly-null/testLogicalAnd))
|
||||||
|
(export "testLogicalOr" (func $possibly-null/testLogicalOr))
|
||||||
|
(export "testLogicalAndMulti" (func $possibly-null/testLogicalAndMulti))
|
||||||
|
(export "testLogicalOrMulti" (func $possibly-null/testLogicalOrMulti))
|
||||||
(func $possibly-null/testTrue (; 0 ;) (type $FUNCSIG$vi) (param $0 i32)
|
(func $possibly-null/testTrue (; 0 ;) (type $FUNCSIG$vi) (param $0 i32)
|
||||||
local.get $0
|
local.get $0
|
||||||
if
|
if
|
||||||
@ -128,6 +133,58 @@
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
(func $null (; 12 ;) (type $FUNCSIG$v)
|
(func $possibly-null/requireNonNull (; 12 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
|
||||||
|
local.get $0
|
||||||
|
)
|
||||||
|
(func $possibly-null/testLogicalAnd (; 13 ;) (type $FUNCSIG$vi) (param $0 i32)
|
||||||
|
local.get $0
|
||||||
|
if (result i32)
|
||||||
|
local.get $0
|
||||||
|
call $possibly-null/requireNonNull
|
||||||
|
else
|
||||||
|
i32.const 0
|
||||||
|
end
|
||||||
|
drop
|
||||||
|
)
|
||||||
|
(func $possibly-null/testLogicalOr (; 14 ;) (type $FUNCSIG$vi) (param $0 i32)
|
||||||
|
local.get $0
|
||||||
|
i32.eqz
|
||||||
|
if (result i32)
|
||||||
|
i32.const 1
|
||||||
|
else
|
||||||
|
local.get $0
|
||||||
|
call $possibly-null/requireNonNull
|
||||||
|
i32.const 0
|
||||||
|
i32.ne
|
||||||
|
end
|
||||||
|
drop
|
||||||
|
)
|
||||||
|
(func $possibly-null/testLogicalAndMulti (; 15 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32)
|
||||||
|
local.get $0
|
||||||
|
if (result i32)
|
||||||
|
local.get $1
|
||||||
|
else
|
||||||
|
i32.const 0
|
||||||
|
end
|
||||||
|
if
|
||||||
|
nop
|
||||||
|
end
|
||||||
|
)
|
||||||
|
(func $possibly-null/testLogicalOrMulti (; 16 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32)
|
||||||
|
local.get $0
|
||||||
|
i32.eqz
|
||||||
|
if (result i32)
|
||||||
|
i32.const 1
|
||||||
|
else
|
||||||
|
local.get $1
|
||||||
|
i32.eqz
|
||||||
|
end
|
||||||
|
if
|
||||||
|
nop
|
||||||
|
else
|
||||||
|
nop
|
||||||
|
end
|
||||||
|
)
|
||||||
|
(func $null (; 17 ;) (type $FUNCSIG$v)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user