Skip to content

Commit d67941b

Browse files
authored
Handle lambdas and method references in switch expressions
1 parent ef69228 commit d67941b

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

framework/src/main/java/org/checkerframework/framework/type/AnnotatedTypeFactory.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.sun.source.tree.NewClassTree;
2020
import com.sun.source.tree.ReturnTree;
2121
import com.sun.source.tree.Tree;
22+
import com.sun.source.tree.Tree.Kind;
2223
import com.sun.source.tree.TypeCastTree;
2324
import com.sun.source.tree.VariableTree;
2425
import com.sun.source.util.TreePath;
@@ -4601,8 +4602,8 @@ public Pair<AnnotatedTypeMirror, AnnotatedExecutableType> getFnInterfaceFromTree
46014602
* @return the functional interface type or an uninferred type argument
46024603
*/
46034604
private AnnotatedTypeMirror getFunctionalInterfaceType(Tree tree) {
4604-
4605-
Tree parentTree = getPath(tree).getParentPath().getLeaf();
4605+
TreePath parentPath = getPath(tree).getParentPath();
4606+
Tree parentTree = parentPath.getLeaf();
46064607
switch (parentTree.getKind()) {
46074608
case PARENTHESIZED:
46084609
return getFunctionalInterfaceType(parentTree);
@@ -4713,8 +4714,16 @@ private AnnotatedTypeMirror getFunctionalInterfaceType(Tree tree) {
47134714
AnnotatedTypes.leastUpperBound(this, trueType, falseType);
47144715
assertIsFunctionalInterface(conditionalType.getUnderlyingType(), parentTree, tree);
47154716
return conditionalType;
4717+
case CASE:
4718+
// Get the functional interface type of the whole switch expression.
4719+
Tree switchTree = parentPath.getParentPath().getLeaf();
4720+
return getFunctionalInterfaceType(switchTree);
47164721

47174722
default:
4723+
if (parentTree.getKind().toString().equals("YIELD")) {
4724+
TreePath pathToCase = TreePathUtil.pathTillOfKind(parentPath, Kind.CASE);
4725+
return getFunctionalInterfaceType(pathToCase.getParentPath().getLeaf());
4726+
}
47184727
throw new BugInCF(
47194728
"Could not find functional interface from assignment context. "
47204729
+ "Unexpected tree type: "
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.function.Supplier;
2+
3+
// @below-java17-jdk-skip-test
4+
public final class Issue5930 {
5+
enum TestEnum {
6+
FIRST,
7+
SECOND
8+
};
9+
10+
public static void main(String[] args) {
11+
TestEnum testEnum = TestEnum.FIRST;
12+
Supplier<Integer> supplier =
13+
switch (testEnum) {
14+
case FIRST -> () -> 1;
15+
case SECOND -> () -> 2;
16+
};
17+
System.out.println(supplier.get());
18+
}
19+
}

0 commit comments

Comments
 (0)