Copilot commented on code in PR #2735:
URL: https://github.com/apache/groovy/pull/2735#discussion_r3649246451


##########
src/main/java/org/codehaus/groovy/transform/StaticTypesTransformation.java:
##########
@@ -73,6 +81,45 @@ public void visit(ASTNode[] nodes, SourceUnit source) {
         }
         if (visitor != null) {
             visitor.performSecondPass();
+            resolveEnumConstantsInSwitchCases(node, source);
+        }
+    }
+
+    /**
+     * GROOVY-8444, GROOVY-11614: replaces "CONST" expressions resolved by the
+     * type checker in enum switch case labels with "EnumType.CONST" 
expressions;
+     * without static compilation they would otherwise be dynamic property 
lookups
+     * on the enclosing class, failing at runtime.
+     */

Review Comment:
   The Javadoc for this helper mentions GROOVY-8444 and GROOVY-11614 only, but 
this change also implements GROOVY-12190 and is now invoked by the 
`@TypeChecked` transformation. Updating the comment makes it easier to track 
why this AST rewrite exists and when it applies.



##########
src/main/java/org/codehaus/groovy/transform/StaticTypesTransformation.java:
##########
@@ -73,6 +81,45 @@ public void visit(ASTNode[] nodes, SourceUnit source) {
         }
         if (visitor != null) {
             visitor.performSecondPass();
+            resolveEnumConstantsInSwitchCases(node, source);
+        }
+    }
+
+    /**
+     * GROOVY-8444, GROOVY-11614: replaces "CONST" expressions resolved by the
+     * type checker in enum switch case labels with "EnumType.CONST" 
expressions;
+     * without static compilation they would otherwise be dynamic property 
lookups
+     * on the enclosing class, failing at runtime.
+     */
+    private static void resolveEnumConstantsInSwitchCases(final AnnotatedNode 
node, final SourceUnit source) {
+        ClassCodeExpressionTransformer transformer = new 
ClassCodeExpressionTransformer() {
+            @Override
+            protected SourceUnit getSourceUnit() {
+                return source;
+            }
+
+            @Override
+            public Expression transform(final Expression expression) {
+                if (expression instanceof ClosureExpression) { // switch 
expressions are desugared to closures
+                    expression.visit(this);
+                    return expression;
+                }
+                if (expression instanceof VariableExpression) {
+                    ClassNode enumType = 
expression.getNodeMetaData(StaticTypesMarker.SWITCH_CONDITION_EXPRESSION_TYPE);
+                    if (enumType != null) {
+                        PropertyExpression pe = propX(classX(enumType), 
expression.getText());
+                        pe.getProperty().setSourcePosition(expression);
+                        return pe;

Review Comment:
   When rewriting the case-label VariableExpression to a PropertyExpression, 
the new node should preserve source positions (for accurate stack traces/debug 
info) and should mirror the existing static-compilation rewrite by tagging the 
inferred type. `ClassCodeExpressionTransformer#setSourcePosition` exists for 
this purpose.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to