@@ -422,7 +422,8 @@ private static boolean switchOnNullWouldImplicitlyThrow(
422422 * Analyzes the supplied case IRs for a switch statement for issues related default/unconditional
423423 * cases. If deemed necessary, this method injects a `default` and/or `case null` into the
424424 * supplied case IRs. If the supplied case IRs cannot be used to form a syntactically valid switch
425- * statement, returns `Optional.empty()`.
425+ * statement, returns `Optional.empty()`. Precondition: the list of supplied cases must not
426+ * contain any dominance violations.
426427 */
427428 private Optional <List <CaseIr >> maybeFixDefaultNullAndUnconditional (
428429 List <CaseIr > cases ,
@@ -608,7 +609,11 @@ && isSubtype(
608609 }
609610 }
610611
611- return recheckDominanceNeeded ? maybeFixDominance (cases , state , subject ) : Optional .of (cases );
612+ return recheckDominanceNeeded
613+ // If there is a dominance violation, then we must have caused it by adding a case, so
614+ // fixing it merely puts the new case in the correct position
615+ ? maybeFixDominance (cases , state , subject , /* canReorderCases= */ true )
616+ : Optional .of (cases );
612617 }
613618
614619 /**
@@ -705,12 +710,13 @@ private static Optional<List<CaseIr>> maybeDetectDuplicateConstants(List<CaseIr>
705710 }
706711
707712 /**
708- * Analyzes the supplied case IRs for dominance violations. If a dominance violation is detected,
709- * attempts to fix the problem by reordering them so that the violation does not occur. If the
710- * dominance violation cannot be corrected by this algorithm, returns {@code Optional.empty()}.
713+ * Analyzes the supplied case IRs for dominance violations. If a dominance violation is detected
714+ * and {@code canReorderCases} is {@code true}, attempts to fix the problem by reordering cases so
715+ * that no violation is present. If a dominance violation exists and cannot be corrected by this
716+ * algorithm, returns {@code Optional.empty()}.
711717 */
712718 private static Optional <List <CaseIr >> maybeFixDominance (
713- List <CaseIr > cases , VisitorState state , ExpressionTree subject ) {
719+ List <CaseIr > cases , VisitorState state , ExpressionTree subject , boolean canReorderCases ) {
714720
715721 List <CaseIr > casesCopy = new ArrayList <>(cases );
716722 List <CaseIr > casesToInsert = new ArrayList <>();
@@ -720,8 +726,12 @@ private static Optional<List<CaseIr>> maybeFixDominance(
720726 // they were encountered in the if-chain)
721727 casesToInsert .add (casesCopy .get (insert ));
722728
729+ // Violation found when trying to insert @insert?
723730 if (hasDominanceViolation (casesToInsert , state , subject )) {
724- // Violation found when trying to insert @insert
731+ if (!canReorderCases ) {
732+ // Dominance violation exists and not allowed to fix
733+ return Optional .empty ();
734+ }
725735 casesToInsert .remove (casesToInsert .size () - 1 );
726736
727737 // Slow path: try to clean up by moving insertion around. The idea is a bit like an
@@ -1380,7 +1390,9 @@ private List<SuggestedFix> deepAnalysisOfIfChain(
13801390 Optional <List <CaseIr >> fixedCasesOptional =
13811391 casesWithPullupMaybeApplied
13821392 .flatMap (caseList -> maybeDetectDuplicateConstants (caseList ))
1383- .flatMap (caseList -> maybeFixDominance (caseList , state , subject ))
1393+ .flatMap (
1394+ caseList ->
1395+ maybeFixDominance (caseList , state , subject , /* canReorderCases= */ !enableSafe ))
13841396 .flatMap (
13851397 x ->
13861398 maybeFixDefaultNullAndUnconditional (
@@ -1402,7 +1414,10 @@ private List<SuggestedFix> deepAnalysisOfIfChain(
14021414 fixedCasesOptional =
14031415 Optional .of (cases )
14041416 .flatMap (caseList -> maybeDetectDuplicateConstants (caseList ))
1405- .flatMap (caseList -> maybeFixDominance (caseList , state , subject ))
1417+ .flatMap (
1418+ caseList ->
1419+ maybeFixDominance (
1420+ caseList , state , subject , /* canReorderCases= */ !enableSafe ))
14061421 .flatMap (
14071422 caseList ->
14081423 maybeFixDefaultNullAndUnconditional (
@@ -1416,7 +1431,10 @@ private List<SuggestedFix> deepAnalysisOfIfChain(
14161431 switchType ,
14171432 ifTreeSourceRange ))
14181433 // Changing default/null can affect dominance
1419- .flatMap (caseList -> maybeFixDominance (caseList , state , subject ));
1434+ .flatMap (
1435+ caseList ->
1436+ maybeFixDominance (
1437+ caseList , state , subject , /* canReorderCases= */ !enableSafe ));
14201438 }
14211439
14221440 List <SuggestedFix > suggestedFixes = new ArrayList <>();
0 commit comments