Skip to content

Support for try-with-resources by reduction #3918

Open
wadoon wants to merge 5 commits into
mainfrom
weigl/trywithresources
Open

Support for try-with-resources by reduction #3918
wadoon wants to merge 5 commits into
mainfrom
weigl/trywithresources

Conversation

@wadoon

@wadoon wadoon commented Jul 12, 2026

Copy link
Copy Markdown
Member

Pull Request Description

Related Issue

This pull request resolves # [add issue number if applicable]

Intended Change

This PR implements the complete desugaring transformation for try-with-resources statements in the KeY Java transformation pipeline. The TryWithResourceReducer class now transforms every try-with-resources statement into the equivalent nested try/finally form that javac generates, enabling KeY to reason about code without requiring native support for try-with-resources syntax.

Technical Details

For a simple try-with-resources:

try (Resource r = open()) {
    body;
}

The transformation produces:

try {
    Resource r = open();
    Throwable primaryExc$1 = null;
    try {
        body;
    } catch (Throwable t$1) {
        primaryExc$1 = t$1;
        throw t$1;
    } finally {
        if (r != null) {
            if (primaryExc$1 != null) {
                try { r.close(); } catch (Throwable suppressedExc$1) {
                    primaryExc$1.addSuppressed(suppressedExc$1);
                }
            } else { 
                r.close(); 
            }
        }
    }
}

Key features:

  • Multiple resources are handled by nesting (last-declared resource closes first)
  • Any catch/finally clauses attached to the original try are preserved on an outer wrapper
  • Supports both Java 8 style try (Resource r = expr) and Java 9+ style try (existingVar) forms
  • Properly handles suppressed exceptions per JLS 14.20.3
  • Uses unique counter-based variable names to avoid collisions

Type of pull request

  • Refactoring (behavior should not change or only minimally change)
  • There are changes to the Java code

Ensuring quality

  • I made sure that introduced/changed code is well documented (Javadoc and inline comments).
  • I added new test case(s) for new functionality.
  • I have tested the feature as follows: ...
  • I have checked that runtime performance has not deteriorated.

Additional information and contact(s)

Author: @weigl, with contributions from Claude Sonnet

This transformation is part of the broader effort to normalize Java constructs for formal verification in KeY. The implementation follows the exact semantics specified in JLS 14.20.3 and mirrors the bytecode-level transformation performed by javac.


Contributions within this pull request are licensed under GPLv2 (only) for inclusion in KeY.

@wadoon wadoon changed the title weigl/trywithresources Support for try-with-resources by reduction Jul 12, 2026
@wadoon wadoon requested a review from unp1 July 12, 2026 12:26
@wadoon wadoon self-assigned this Jul 12, 2026
@wadoon wadoon added Java Parser Java Pull requests that update Java code labels Jul 12, 2026

@unp1 unp1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only small things:

  • Throwable.addSuppressed(...) is not in KeY's Throwable stub, the desugaring emits primaryExc.addSuppressed(...).

  • java.lang.AutoCloseable is absent from ReduX (only java.io.Closeable exists), so a resource that implements AutoCloseable directly won't resolve.

-try (this.r) (field-access resource) fails in KeY: "Cannot resolve 'this.r'", because the transform stringified the field access into a single NameExpr

Thanks!

@wadoon wadoon added this to the v3.0.0 milestone Jul 12, 2026
@wadoon wadoon requested a review from unp1 July 12, 2026 20:17
@wadoon wadoon force-pushed the weigl/trywithresources branch from 8d830cc to f636b77 Compare July 12, 2026 20:20
@wadoon wadoon force-pushed the weigl/trywithresources branch from f636b77 to 861a8c3 Compare July 12, 2026 20:28

@unp1 unp1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.
One remaining issue: Closeable needs to extend AutoCloseable in Redux

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Java Parser Java Pull requests that update Java code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants