@@ -4577,15 +4577,26 @@ export class Compiler extends DiagnosticEmitter {
45774577
45784578 // simplify if only interested in true or false
45794579 if ( contextualType == Type . bool || contextualType == Type . void ) {
4580- rightExpr = this . compileExpression ( right , leftType , inheritedConstraints ) ;
4581- rightType = this . currentType ;
4582- rightFlow . freeScopedLocals ( ) ;
4580+ leftExpr = this . makeIsTrueish ( leftExpr , leftType , left ) ;
4581+
4582+ // shortcut if lhs is always false
4583+ let condKind = this . evaluateCondition ( leftExpr ) ;
4584+ if ( condKind == ConditionKind . FALSE ) {
4585+ expr = leftExpr ;
4586+ } else {
4587+ rightExpr = this . compileExpression ( right , leftType , inheritedConstraints ) ;
4588+ rightType = this . currentType ;
4589+ rightFlow . freeScopedLocals ( ) ;
4590+ rightExpr = this . makeIsTrueish ( rightExpr , rightType , right ) ;
4591+
4592+ // simplify if lhs is always true
4593+ if ( condKind == ConditionKind . TRUE ) {
4594+ expr = rightExpr ;
4595+ } else {
4596+ expr = module . if ( leftExpr , rightExpr , module . i32 ( 0 ) ) ;
4597+ }
4598+ }
45834599 this . currentFlow = flow ;
4584- expr = module . if (
4585- this . makeIsTrueish ( leftExpr , leftType , left ) ,
4586- this . makeIsTrueish ( rightExpr , rightType , right ) ,
4587- module . i32 ( 0 )
4588- ) ;
45894600 this . currentType = Type . bool ;
45904601
45914602 } else {
@@ -4630,15 +4641,26 @@ export class Compiler extends DiagnosticEmitter {
46304641
46314642 // simplify if only interested in true or false
46324643 if ( contextualType == Type . bool || contextualType == Type . void ) {
4633- rightExpr = this . compileExpression ( right , leftType , inheritedConstraints ) ;
4634- rightType = this . currentType ;
4635- rightFlow . freeScopedLocals ( ) ;
4644+ leftExpr = this . makeIsTrueish ( leftExpr , leftType , left ) ;
4645+
4646+ // shortcut if lhs is always true
4647+ let condKind = this . evaluateCondition ( leftExpr ) ;
4648+ if ( condKind == ConditionKind . TRUE ) {
4649+ expr = leftExpr ;
4650+ } else {
4651+ rightExpr = this . compileExpression ( right , leftType , inheritedConstraints ) ;
4652+ rightType = this . currentType ;
4653+ rightFlow . freeScopedLocals ( ) ;
4654+ rightExpr = this . makeIsTrueish ( rightExpr , rightType , right ) ;
4655+
4656+ // simplify if lhs is always false
4657+ if ( condKind == ConditionKind . FALSE ) {
4658+ expr = rightExpr ;
4659+ } else {
4660+ expr = module . if ( leftExpr , module . i32 ( 1 ) , rightExpr ) ;
4661+ }
4662+ }
46364663 this . currentFlow = flow ;
4637- expr = module . if (
4638- this . makeIsTrueish ( leftExpr , leftType , left ) ,
4639- module . i32 ( 1 ) ,
4640- this . makeIsTrueish ( rightExpr , rightType , right )
4641- ) ;
46424664 this . currentType = Type . bool ;
46434665
46444666 } else {
@@ -10037,6 +10059,7 @@ export class Compiler extends DiagnosticEmitter {
1003710059
1003810060 /** Evaluates a boolean condition, determining whether it is TRUE, FALSE or UNKNOWN. */
1003910061 evaluateCondition ( expr : ExpressionRef ) : ConditionKind {
10062+ assert ( getExpressionType ( expr ) == TypeRef . I32 ) ;
1004010063 var module = this . module ;
1004110064 var evaled = module . runExpression ( expr , ExpressionRunnerFlags . Default ) ;
1004210065 if ( evaled ) {
0 commit comments