You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by jo...@apache.org on 2023/06/20 20:48:20 UTC

[royale-compiler] 03/05: TernaryOperator: resolving type of ?: ternary operator now accounts for the common base type, using the same algorithm as type inference

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 c605ace5d32456e7ab1f0754363bea284aaa6aa0
Author: Josh Tynjala <jo...@apache.org>
AuthorDate: Tue Jun 20 13:09:20 2023 -0700

    TernaryOperator: resolving type of ?: ternary  operator now accounts for the common base type, using the same algorithm as type inference
---
 .../royale/compiler/internal/tree/as/TernaryOperatorNode.java | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/compiler/src/main/java/org/apache/royale/compiler/internal/tree/as/TernaryOperatorNode.java b/compiler/src/main/java/org/apache/royale/compiler/internal/tree/as/TernaryOperatorNode.java
index e8c9301a8..01fa93368 100644
--- a/compiler/src/main/java/org/apache/royale/compiler/internal/tree/as/TernaryOperatorNode.java
+++ b/compiler/src/main/java/org/apache/royale/compiler/internal/tree/as/TernaryOperatorNode.java
@@ -21,6 +21,7 @@ package org.apache.royale.compiler.internal.tree.as;
 
 import org.apache.royale.compiler.constants.IASLanguageConstants.BuiltinType;
 import org.apache.royale.compiler.definitions.ITypeDefinition;
+import org.apache.royale.compiler.internal.semantics.SemanticUtils;
 import org.apache.royale.compiler.parsing.IASToken;
 import org.apache.royale.compiler.projects.ICompilerProject;
 import org.apache.royale.compiler.tree.ASTNodeID;
@@ -145,11 +146,15 @@ public class TernaryOperatorNode extends BinaryOperatorNodeBase implements ITern
         // The old compiler simply considered the type of (a ? b : c) to be *.
         // We'll do a bit better: if b and c have identical type
         // then we'll consider (a ? b : c) to be that type.
-        // We should probably determine the common base type of b and c,
-        // but if either or both types are interfaces rather than classes,
-        // it isn't obvious what this means.
+        // If the types are not identical, we'll try to find a common base type,
+        // and only then, fall back to the * type.
         ITypeDefinition leftType = getLeftOperandNode().resolveType(project);
         ITypeDefinition rightType = getRightOperandNode().resolveType(project);
+        ITypeDefinition commonType = SemanticUtils.resolveCommonType(leftType, rightType, project);
+        if (commonType != null)
+        {
+            return commonType;
+        }
         if (leftType == rightType)
             return leftType;