Support for multi-catch-clauses in try-statements#3919
Conversation
There was a problem hiding this comment.
Thanks.
Remaining issues:
-
MultiCatchReduceris not added to the pipeline. It is defined under…/transformations/pipeline/but not registered inKeYJavaPipeline.createDefault(...), so it never runs during loading -
Two or more multi-catch clauses in the same
tryare handled incorrectly.rewrite(...)iterates a snapshot (originalClauses) but mutates the livecatchClausesviaremove(i)/add(j++, …); after the first multi-catch expands, the snapshot index no longer matches the live list. As a result one clause is silently dropped and an un-expandedUnionTypeis left behind (which then fails to load). Reproducer:public class T { void m() {} int f() { try { m(); } catch (IllegalArgumentException | NullPointerException e) { return 1; } catch (ArithmeticException | ArrayStoreException e) { return 2; } return 0; } }
With the reducer registered this fails to load with
Unsupported element … UnionTypeon the leftoverArithmeticException | ArrayStoreException, and the transformer output shows a clause from the first multi-catch is dropped. A single multi-catch (even mixed with single-type catches) works. Fix: track the live insertion position instead of removing by the snapshot index (e.g. rebuild the clause list, or remove the specific node). -
The bundled example does not load.
key.ui/examples/Java/MultiCatch/MultiCatch.javausesjava.sql.SQLException, which is not in KeY's default classpath (JavaRedux) -
Nit: the Javadoc note about ordering catch clauses by subtype relationship doesn't apply to a multi-catch's own alternatives, they must be disjoint (JLS §14.20), so there's no "more specific first" among them.
|
Intended Change
This PR implements a new transformer for the
KeYJavaTransformationPipelinethat desugars modern Java exception handling constructs into their equivalent basic forms:MultiCatchReducer transforms multi-catch clauses into multiple single catch clauses (per JLS 14.20).
These transformations enable KeY to verify code using these Java 7+ features by reducing them to the core try/catch/finally constructs already supported by the verification infrastructure.
Technical Details
For a multi-catch:
Produces:
Supports:
finally-blocksType of pull request
Ensuring quality
MultiCatchReducerTest.java- Tests for 2-3 exception types, mixed catches, and finally blockskey.ui/examples/Java/MultiCatch/