paulk-asert commented on code in PR #2922:
URL: https://github.com/apache/groovy/pull/2922#discussion_r3993837581
##########
src/main/java/org/codehaus/groovy/classgen/asm/SwitchExpressionWriter.java:
##########
@@ -213,12 +215,41 @@ protected void writeDefaultOrThrow(final SwitchExpression
expression, final int
if (defaultStatement != null && !defaultStatement.isEmpty()) {
defaultStatement.visit(controller.getAcg());
if (maybeFallsThrough(defaultStatement)) {
- throwUnmatchedSelector(selectorIndex, selectorType);
+ writeUnmatchedSelector(expression, selectorIndex,
selectorType);
}
return;
}
if (completeEnum) {
throwIncompatibleClassChangeError();
+ } else {
+ writeUnmatchedSelector(expression, selectorIndex, selectorType);
+ }
+ }
+
+ /**
+ * Whether an unmatched selector completes the expression with {@code null}
+ * rather than throwing: a switch in implicit-return position
(GROOVY-12399).
+ *
+ * @since 6.0.0
+ */
+ protected static boolean unmatchedYieldsNull(final SwitchExpression
expression) {
+ return
Boolean.TRUE.equals(expression.getNodeMetaData(UNMATCHED_YIELDS_NULL));
+ }
+
+ /**
+ * Completes the expression for an unmatched selector: {@code null} (cast
+ * to the result type) for a switch in implicit-return position, otherwise
+ * an {@code IllegalStateException} (GROOVY-12399).
+ */
+ private void writeUnmatchedSelector(final SwitchExpression expression,
final int selectorIndex, final ClassNode selectorType) {
+ if (unmatchedYieldsNull(expression)) {
+ CompileStack.SwitchExpressionContext context =
controller.getCompileStack().requireSwitchExpressionContext();
+ OperandStack operandStack = controller.getOperandStack();
+ controller.getMethodVisitor().visitInsn(ACONST_NULL);
+ operandStack.push(ClassHelper.OBJECT_TYPE);
+ operandStack.doGroovyCast(context.resultType);
Review Comment:
done
--
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]