This is an automated email from the ASF dual-hosted git repository.

joshtynjala pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-compiler.git

commit f20248f4f5545018865822adfffdaae5c7cc4258
Author: Josh Tynjala <[email protected]>
AuthorDate: Thu Sep 19 14:40:05 2024 -0700

    MethodBodySemanticChecker: NonBooleanUsedWhereBooleanExpectedProblem is not 
reported when Object is assigned to Boolean
    
    My original implementation was too strict, and did not match the Flex SDK 
compiler. Similar to no warning for the * type, the Object type is also 
excluded.
---
 .../semantics/MethodBodySemanticChecker.java       | 33 ++++++++++++++--------
 1 file changed, 21 insertions(+), 12 deletions(-)

diff --git 
a/compiler/src/main/java/org/apache/royale/compiler/internal/semantics/MethodBodySemanticChecker.java
 
b/compiler/src/main/java/org/apache/royale/compiler/internal/semantics/MethodBodySemanticChecker.java
index 013985adb..ba82f399a 100644
--- 
a/compiler/src/main/java/org/apache/royale/compiler/internal/semantics/MethodBodySemanticChecker.java
+++ 
b/compiler/src/main/java/org/apache/royale/compiler/internal/semantics/MethodBodySemanticChecker.java
@@ -543,12 +543,7 @@ public class MethodBodySemanticChecker
                         new NullUsedWhereOtherExpectedProblem(rightNode, 
leftType.getBaseName()));
             }
              
-            final boolean leftIsBoolean = SemanticUtils.isBuiltin(leftType, 
BuiltinType.BOOLEAN, project);
-            final boolean rightIsBoolean = SemanticUtils.isBuiltin(rightType, 
BuiltinType.BOOLEAN, project);
-            final boolean rightIsAny = rightType == null || 
SemanticUtils.isBuiltin(rightType, BuiltinType.ANY_TYPE, project);
-            
-            if (leftIsBoolean && !rightIsBoolean && !rightIsAny)
-            {
+            if (findNonBooleanAssignedToBoolean(leftType, rightNode)) {
                 String rightTypeName = rightType != null ? 
rightType.getBaseName() : "Non-Boolean value";
                 addProblem(new 
NonBooleanUsedWhereBooleanExpectedProblem(rightNode, rightTypeName));
             }
@@ -2653,13 +2648,8 @@ public class MethodBodySemanticChecker
                 {
                     addProblem(new 
NullUsedWhereOtherExpectedProblem(returnExpression, return_type.getBaseName()));
                 }
-             
-                final boolean leftIsBoolean = 
SemanticUtils.isBuiltin(return_type, BuiltinType.BOOLEAN, project);
-                final boolean rightIsBoolean = 
SemanticUtils.isBuiltin(rightType, BuiltinType.BOOLEAN, project);
-                final boolean rightIsAny = rightType == null || 
SemanticUtils.isBuiltin(rightType, BuiltinType.ANY_TYPE, project);
                 
-                if (leftIsBoolean && !rightIsBoolean && !rightIsAny)
-                {
+                if (findNonBooleanAssignedToBoolean(return_type, 
returnExpression)) {
                     String rightTypeName = rightType != null ? 
rightType.getBaseName() : "Non-Boolean value";
                     addProblem(new 
NonBooleanUsedWhereBooleanExpectedProblem(returnExpression, rightTypeName));
                 }
@@ -3695,5 +3685,24 @@ public class MethodBodySemanticChecker
 
         return false;
     }
+
+    private boolean findNonBooleanAssignedToBoolean(IDefinition leftType, 
IASNode rightNode) {
+        IDefinition rightType = 
((IExpressionNode)rightNode).resolveType(project);
+        final boolean leftIsBoolean = SemanticUtils.isBuiltin(leftType, 
BuiltinType.BOOLEAN, project);
+        final boolean rightIsBoolean = SemanticUtils.isBuiltin(rightType, 
BuiltinType.BOOLEAN, project);
+        // the Flex SDK compiler allowed setting a Boolean variable to either
+        // Object or * without a warning. probably because it would add way too
+        // much noise. for instance, a logical operator like && or || is
+        // expected to result in a Boolean value. It actually doesn't and
+        // returns either the left or right value as-is, and coerces it to
+        // Boolean only when necessary, such as as part of the assignment to a
+        // Boolean variable.
+        final boolean rightIsAny = rightType == null || 
SemanticUtils.isBuiltin(rightType, BuiltinType.ANY_TYPE, project);
+        final boolean rightIsObject = rightType == null || 
SemanticUtils.isBuiltin(rightType, BuiltinType.OBJECT, project);
+        return leftIsBoolean
+                && !rightIsBoolean
+                && !rightIsAny
+                && !rightIsObject;
+    }
 }
 

Reply via email to