diff --git a/src/compiler.ts b/src/compiler.ts index 98d9a42084..9d5b6c4b6e 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -4566,6 +4566,7 @@ export class Compiler extends DiagnosticEmitter { expr = module.if(leftExpr, rightExpr, module.i32(0)); } } + flow.inheritBranch(rightFlow, condKind); this.currentFlow = flow; this.currentType = Type.bool; @@ -4573,6 +4574,7 @@ export class Compiler extends DiagnosticEmitter { rightExpr = this.compileExpression(right, leftType, inheritedConstraints | Constraints.CONV_IMPLICIT); rightType = this.currentType; rightFlow.freeScopedLocals(); + flow.inheritBranch(rightFlow); this.currentFlow = flow; // simplify if copying left is trivial @@ -4630,6 +4632,13 @@ export class Compiler extends DiagnosticEmitter { expr = module.if(leftExpr, module.i32(1), rightExpr); } } + let inheritCondi = + condKind == ConditionKind.TRUE + ? ConditionKind.FALSE + : condKind == ConditionKind.FALSE + ? ConditionKind.TRUE + : ConditionKind.UNKNOWN; + flow.inheritBranch(rightFlow, inheritCondi); this.currentFlow = flow; this.currentType = Type.bool; @@ -4637,6 +4646,7 @@ export class Compiler extends DiagnosticEmitter { rightExpr = this.compileExpression(right, leftType, inheritedConstraints | Constraints.CONV_IMPLICIT); rightType = this.currentType; rightFlow.freeScopedLocals(); + flow.inheritBranch(rightFlow); this.currentFlow = flow; // simplify if copying left is trivial diff --git a/tests/compiler/nullable.json b/tests/compiler/nullable.json index fdef0a1a3b..4aaa25669e 100644 --- a/tests/compiler/nullable.json +++ b/tests/compiler/nullable.json @@ -2,6 +2,7 @@ "asc_flags": [ ], "stderr": [ + "TS2322: Type 'nullable/Example | null' is not assignable to type 'nullable/Example'.", "TS2322: Type 'nullable/Example | null' is not assignable to type 'nullable/Example'.", "EOF" ] diff --git a/tests/compiler/nullable.ts b/tests/compiler/nullable.ts index 3cffa68d67..9c2eda2744 100644 --- a/tests/compiler/nullable.ts +++ b/tests/compiler/nullable.ts @@ -4,4 +4,16 @@ function notNullable(a: Example): void {} notNullable(null); +function test(): void { + let value: Example | null = new Example(); + if (value != null) { + // value = null; + true && (value = null); + // "TS2322: Type 'nullable/Example | null' is not assignable to type 'nullable/Example'.", + notNullable(value); + } +} + +test(); + ERROR("EOF");