@@ -1025,8 +1025,9 @@ private void visitLoop(CtLoop loop, Consumer<LoopTypestates> iteration) {
10251025 *
10261026 * <p>
10271027 * An earlier check starts from fewer states and more invariants than the last one, so an error that stops it is
1028- * also an error of the last one. Only the last check keeps the errors recorded without stopping it (e.g. in a
1029- * lambda).
1028+ * also an error of the last one. It is only reported once the invariants it assumes are proved, with the statement
1029+ * where it stopped skipped (see {@link LoopInvariants}). Only the last check keeps the errors recorded without
1030+ * stopping it (e.g. in a lambda).
10301031 */
10311032 private void checkUntilFixpoint (LoopTypestates states , LoopInvariants loopInvariants ,
10321033 Consumer <LoopTypestates > iteration ) {
@@ -1041,12 +1042,17 @@ private void checkUntilFixpoint(LoopTypestates states, LoopInvariants loopInvari
10411042 iteration .accept (states );
10421043 states .recordNextIteration ();
10431044 loopInvariants .recordNextIteration ();
1045+ } catch (LJError e ) {
1046+ if (!loopInvariants .checkAgainAfterError (statement -> canSkip (statement , states )))
1047+ throw e ;
1048+ diagnostics .getErrors ().retainAll (errors );
1049+ continue ;
10441050 } finally {
10451051 context .exitContext ();
10461052 vcChecker .replacePathVariables (pathVariables );
10471053 }
10481054 boolean typestatesStable = states .reachedFixpoint ();
1049- if (loopInvariants .reachedFixpoint () && typestatesStable )
1055+ if (loopInvariants .reachedFixpoint () && typestatesStable && ! loopInvariants . checkSkippedAgain () )
10501056 return ;
10511057 diagnostics .getErrors ().retainAll (errors );
10521058 }
@@ -1091,6 +1097,60 @@ private void recordExit(LoopTypestates states, CtExpression<Boolean> condition)
10911097 vcChecker .removePathVariable (conditionIsFalse );
10921098 }
10931099
1100+ @ Override
1101+ public void scan (CtElement element ) {
1102+ LoopInvariants loopInvariants = element instanceof CtStatement statement ? invariantsOfBodyWith (statement )
1103+ : null ;
1104+ if (loopInvariants == null ) {
1105+ super .scan (element );
1106+ return ;
1107+ }
1108+ CtStatement statement = (CtStatement ) element ;
1109+ if (loopInvariants .skips (statement )) {
1110+ skip (statement );
1111+ return ;
1112+ }
1113+ try {
1114+ super .scan (statement );
1115+ } catch (LJError e ) {
1116+ loopInvariants .stoppedAt (statement );
1117+ throw e ;
1118+ }
1119+ }
1120+
1121+ /** The invariants of the loop being checked whose body is {@code statement} or a block of it, if any */
1122+ private LoopInvariants invariantsOfBodyWith (CtStatement statement ) {
1123+ if (invariants .isEmpty () || !statement .isParentInitialized ())
1124+ return null ;
1125+ CtElement parent = statement .getParent ();
1126+ if (parent instanceof CtBlock <?> && parent .isParentInitialized () && parent .getParent ()instanceof CtLoop loop
1127+ && loop .getBody () == parent )
1128+ return invariants .get (loop );
1129+ return parent instanceof CtLoop loop && loop .getBody () == statement ? invariants .get (loop ) : null ;
1130+ }
1131+
1132+ /**
1133+ * Whether a statement of a loop body can be skipped, as if it changed anything it may change: it is not a local
1134+ * class, continues no loop outside it (whose next iteration would be missed), and changes no object whose
1135+ * typestates the loop follows
1136+ */
1137+ private boolean canSkip (CtStatement statement , LoopTypestates states ) {
1138+ return !(statement instanceof CtClass <?>)
1139+ && statement .getElements (new TypeFilter <>(CtContinue .class )).stream ().allMatch (jump -> {
1140+ CtElement target = jumpTarget (jump );
1141+ return target == statement || target .hasParent (statement );
1142+ }) && changedIn (statement , statement ).stream ().noneMatch (states .tracked ()::contains );
1143+ }
1144+
1145+ /** Instead of checking a statement, forgets what it may change, and declares the variable it declares, if any */
1146+ private void skip (CtStatement statement ) {
1147+ for (String name : changedIn (statement , statement ))
1148+ forgetChanges (name , new Predicate (), statement );
1149+ if (statement instanceof CtLocalVariable <?> localVariable )
1150+ context .addVarToContext (localVariable .getSimpleName (), localVariable .getType (),
1151+ getRefinementFromAnnotation (localVariable ).orElse (new Predicate ()), localVariable );
1152+ }
1153+
10941154 /** Checks the body of a loop, up to the point after it where its continues join */
10951155 private void scanBody (CtLoop loop , LoopTypestates states ) {
10961156 List <RefinedVariable > pathVariables = vcChecker .getPathVariables ();
0 commit comments