|
25 | 25 | import spoon.reflect.code.CtAssignment; |
26 | 26 | import spoon.reflect.code.CtBinaryOperator; |
27 | 27 | import spoon.reflect.code.CtBlock; |
| 28 | +import spoon.reflect.code.CtBreak; |
28 | 29 | import spoon.reflect.code.CtConditional; |
| 30 | +import spoon.reflect.code.CtContinue; |
29 | 31 | import spoon.reflect.code.CtConstructorCall; |
30 | 32 | import spoon.reflect.code.CtExpression; |
31 | 33 | import spoon.reflect.code.CtFieldRead; |
|
39 | 41 | import spoon.reflect.code.CtReturn; |
40 | 42 | import spoon.reflect.code.CtStatement; |
41 | 43 | import spoon.reflect.code.CtThisAccess; |
| 44 | +import spoon.reflect.code.CtThrow; |
42 | 45 | import spoon.reflect.code.CtUnaryOperator; |
43 | 46 | import spoon.reflect.code.CtVariableAccess; |
44 | 47 | import spoon.reflect.code.CtVariableRead; |
@@ -399,37 +402,36 @@ public void visitCtIf(CtIf ifElement) { |
399 | 402 | } |
400 | 403 |
|
401 | 404 | /** |
402 | | - * Returns true when a condition's refinement gives no useful information about which branch may run. |
| 405 | + * A condition is uninformative when its refinement is the trivial {@code true} predicate yet the expression itself |
| 406 | + * is not a boolean literal — i.e. the verifier has no symbolic information to relate the branch to. Treating such a |
| 407 | + * condition as {@code true} would force every if-then to be taken, producing spurious state-refinement errors. |
403 | 408 | */ |
404 | 409 | private boolean isUninformativeCondition(Predicate conditionRefinement, CtExpression<Boolean> condition) { |
405 | | - if (!conditionRefinement.isBooleanTrue()) { |
| 410 | + if (!conditionRefinement.isBooleanTrue()) |
406 | 411 | return false; |
407 | | - } |
408 | | - if (condition instanceof CtLiteral<?> literal && literal.getValue() instanceof Boolean) { |
409 | | - return false; |
410 | | - } |
411 | | - return true; |
| 412 | + return !(condition instanceof CtLiteral<?> literal && literal.getValue() instanceof Boolean); |
412 | 413 | } |
413 | 414 |
|
414 | 415 | /** |
415 | | - * Best-effort normal-completion check for branch joins. Branches that end in {@code return} cannot contribute state |
416 | | - * to the code following the {@code if}. |
| 416 | + * Best-effort normal-completion check (JLS §14.21): branches that always {@code return} or {@code throw} cannot |
| 417 | + * contribute state to code following the {@code if}, so their post-context must be discarded at the join. |
417 | 418 | */ |
418 | 419 | private boolean canCompleteNormally(CtStatement statement) { |
419 | | - if (statement == null) { |
| 420 | + if (statement == null) |
420 | 421 | return true; |
421 | | - } |
422 | | - if (statement instanceof CtReturn<?>) { |
| 422 | + if (statement instanceof CtReturn<?> || statement instanceof CtThrow || statement instanceof CtBreak |
| 423 | + || statement instanceof CtContinue) |
423 | 424 | return false; |
424 | | - } |
425 | 425 | if (statement instanceof CtBlock<?> block) { |
426 | 426 | List<CtStatement> statements = block.getStatements(); |
427 | 427 | return statements.isEmpty() || canCompleteNormally(statements.get(statements.size() - 1)); |
428 | 428 | } |
429 | 429 | if (statement instanceof CtIf nestedIf) { |
430 | 430 | CtStatement elseStatement = nestedIf.getElseStatement(); |
431 | | - return canCompleteNormally(nestedIf.getThenStatement()) || elseStatement == null |
432 | | - || canCompleteNormally(elseStatement); |
| 431 | + // No else means the false path always falls through. |
| 432 | + if (elseStatement == null) |
| 433 | + return true; |
| 434 | + return canCompleteNormally(nestedIf.getThenStatement()) || canCompleteNormally(elseStatement); |
433 | 435 | } |
434 | 436 | return true; |
435 | 437 | } |
|
0 commit comments