daniellansun commented on code in PR #2784: URL: https://github.com/apache/groovy/pull/2784#discussion_r3790989174
########## src/test/groovy/org/codehaus/groovy/classgen/asm/SwitchExpressionBytecodeTest.groovy: ########## @@ -0,0 +1,154 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.codehaus.groovy.classgen.asm + +import org.junit.jupiter.api.Test + +/** + * GROOVY-12255: bytecode shape of first-class switch expressions — no closure + * wrapper, and tableswitch / lookupswitch when the selector and case labels + * permit it. + */ +final class SwitchExpressionBytecodeTest extends AbstractBytecodeTestCase { + + @Test + void noClosureAllocationForSwitchExpression() { + // Probe the current closure bytecode so the negative assertions + // below stay meaningful if closure codegen changes (indy, naming). + def closureBytecode = compile(method: 'run', '''\ + def c = { -> 'closure' } + c() + ''') + def closureText = closureBytecode.toString() + assert closureText.contains('$_run_closure') + + def switchBytecode = compile(method: 'run', '''\ + def r = switch (1) { + case 1 -> 'a' + default -> 'z' + } + ''') + def switchText = switchBytecode.toString() + assert !switchText.contains('$_run_closure') + if (closureText.contains('InnerClassNode')) { + assert !switchText.contains('InnerClassNode') + } + } + + @Test + void staticIntSwitchUsesTableSwitch() { + def bytecode = compile(method: 'm', '''\ + @groovy.transform.CompileStatic + int m(int n) { + switch (n) { + case 1 -> 10 + case 2 -> 20 + case 3 -> 30 + default -> 0 + } + } + ''') + assert bytecode.hasSequence(['TABLESWITCH']) || bytecode.hasSequence(['LOOKUPSWITCH']) Review Comment: Agreed. Dense `int` asserts `TABLESWITCH` only; sparse `int` asserts `LOOKUPSWITCH` only; `String` asserts the Java 7 pair (`LOOKUPSWITCH` on `hashCode`, then `TABLESWITCH` on the case index). Those tests live in `src/test/groovy/org/codehaus/groovy/classgen/asm/sc/SwitchExpressionStaticCompileTest.groovy`. -- 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]
