@@ -458,7 +458,9 @@ private void handleSwitch(JCTree tree,
458
458
newCases .add (c .head );
459
459
appendBreakIfNeeded (tree , cases , c .head );
460
460
}
461
- cases = processCases (tree , newCases .toList ());
461
+ cases = newCases .toList ();
462
+ patchCompletingNormallyCases (cases );
463
+ cases = processCases (tree , cases );
462
464
ListBuffer <JCStatement > statements = new ListBuffer <>();
463
465
VarSymbol temp = new VarSymbol (Flags .SYNTHETIC ,
464
466
names .fromString ("selector" + variableIndex ++ + target .syntheticNameChar () + "temp" ),
@@ -523,8 +525,6 @@ private void handleSwitch(JCTree tree,
523
525
boolean previousCompletesNormally = false ;
524
526
boolean hasDefault = false ;
525
527
526
- patchCompletingNormallyCases (cases );
527
-
528
528
for (var c : cases ) {
529
529
List <JCCaseLabel > clearedPatterns = c .labels ;
530
530
boolean hasJoinedNull =
@@ -685,7 +685,7 @@ private static void patchCompletingNormallyCases(List<JCCase> cases) {
685
685
if (currentCase .caseKind == CaseKind .STATEMENT &&
686
686
currentCase .completesNormally &&
687
687
cases .tail .nonEmpty () &&
688
- cases .tail .head .guard != null ) {
688
+ ( cases .tail .head .guard != null || cases . tail . head . labels . stream (). anyMatch ( cl -> cl instanceof JCPatternCaseLabel p && p . syntheticGuard != null )) ) {
689
689
ListBuffer <JCStatement > newStatements = new ListBuffer <>();
690
690
List <JCCase > copyFrom = cases ;
691
691
@@ -700,6 +700,7 @@ private static void patchCompletingNormallyCases(List<JCCase> cases) {
700
700
};
701
701
702
702
currentCase .stats = newStatements .toList ();
703
+ currentCase .completesNormally = false ;
703
704
}
704
705
705
706
cases = cases .tail ;
@@ -948,10 +949,12 @@ public void resolve(VarSymbol commonBinding,
948
949
JCExpression commonNestedExpression = null ;
949
950
VarSymbol commonNestedBinding = null ;
950
951
boolean previousNullable = false ;
952
+ boolean previousCompletesNormally = false ;
951
953
952
954
for (List <JCCase > c = inputCases ; c .nonEmpty (); c = c .tail ) {
953
955
VarSymbol currentBinding = null ;
954
956
boolean currentNullable = false ;
957
+ boolean currentCompletesNormally = c .head .completesNormally ;
955
958
JCExpression currentNestedExpression = null ;
956
959
VarSymbol currentNestedBinding = null ;
957
960
@@ -986,6 +989,8 @@ public void resolve(VarSymbol commonBinding,
986
989
commonBinding .isUnnamedVariable () == currentBinding .isUnnamedVariable () &&
987
990
!previousNullable &&
988
991
!currentNullable &&
992
+ !previousCompletesNormally &&
993
+ !currentCompletesNormally &&
989
994
new TreeDiffer (List .of (commonBinding ), List .of (currentBinding ))
990
995
.scan (commonNestedExpression , currentNestedExpression )) {
991
996
accummulator .add (c .head );
@@ -1001,6 +1006,7 @@ public void resolve(VarSymbol commonBinding,
1001
1006
commonNestedBinding = currentNestedBinding ;
1002
1007
}
1003
1008
previousNullable = currentNullable ;
1009
+ previousCompletesNormally = currentCompletesNormally ;
1004
1010
}
1005
1011
resolveAccummulator .resolve (commonBinding , commonNestedExpression , commonNestedBinding );
1006
1012
return result .toList ();
@@ -1473,8 +1479,8 @@ List<JCStatement> bindingVars(int diagPos) {
1473
1479
ListBuffer <JCStatement > stats = new ListBuffer <>();
1474
1480
for (Entry <BindingSymbol , VarSymbol > e : hoistedVarMap .entrySet ()) {
1475
1481
JCVariableDecl decl = makeHoistedVarDecl (diagPos , e .getValue ());
1476
- if (!e .getKey ().isPreserved () ||
1477
- ! parent .tryPrepend (e .getKey (), decl )) {
1482
+ if (!e .getValue ().isUnnamedVariable () &&
1483
+ (! e . getKey (). isPreserved () || ! parent .tryPrepend (e .getKey (), decl ) )) {
1478
1484
stats .add (decl );
1479
1485
}
1480
1486
}
0 commit comments