Skip to content

Support for <expr> instanceof <Type> <var> - #3976

Draft
wadoon wants to merge 8 commits into
mainfrom
weigl/instanceof
Draft

wadoon wants to merge 8 commits into
mainfrom
weigl/instanceof

Conversation

@wadoon

@wadoon wadoon commented Aug 19, 2026 •

Copy link
Copy Markdown
Member

Intended Change

This PR adds support for the simple form of the instanceof pattern types that came in Java 16: <expr> instanceof <Type> <var>.

Please consult: JLS 6.3.1 for the binding rules.

Plan

  • Add AST and translation support
  • Add first rules
  • Make the rules parseable
  • Discuss flow analysis as varcond.
  • Add the more complex rules for while, for, do.
  • Example
  • Git history cleanup

Type of pull request

  • New feature (non-breaking change which adds functionality)
  • There are changes to the (Java) code
  • There are changes to the taclet rule base

Ensuring quality

  • I made sure that introduced/changed code is well documented (javadoc and inline comments).
  • I made sure that new/changed end-user features are well documented (https://github.com/KeYProject/key-docs).
  • I added new test case(s) for new functionality.

@wadoon wadoon self-assigned this Aug 19, 2026
@wadoon wadoon added Java Parser Java Pull requests that update Java code Completeness labels Aug 19, 2026
@wadoon wadoon changed the title Weigl/instanceof Support for <expr> instanceof <Type> <var> Aug 19, 2026
@wadoon

wadoon commented Aug 21, 2026 •

Copy link
Copy Markdown
Member Author

State of PR 3976 zu instanceof:

  • added varcond `isBindingExpr(#expression, #posBinding, #negBindings) (true iff one of the bindings is not empty)
  • added varcond alwaysAbnormallyTerminates(#stmts) (true iff #stmts are never reaches the natural end, e.g., if(x) { assert false; }) under-approximation
  • added rule: ifElseUnfoldPattern, ifUnfoldPatternUnreachable, ifElseUnfoldPattern
  • General-Idea: if(#bnse) #slist1 else #slist2 ~~~~~> boolean #boolv; {#boolv = #bnse} if(#bnse) { #posBinding; #slist1} else {#negBinding; #slist2}
  • added rules for #boolv = #nse instanceof #T #id which does not match.

Todo:

  • forbid \ifElseUnfold for binding expressions

@wadoon wadoon added this to the v3.1.0 milestone Aug 21, 2026
@unp1

unp1 commented Aug 21, 2026 •

Copy link
Copy Markdown
Member

Hi,

  • added rules for #boolv = #nse instanceof #T #id which does not match.

The rule matches for me in the example below:

 /*@ normal_behavior
      @ requires obj != null;
      @ ensures \result instanceof String;
      @*/
    public static Object getString(Object obj) {
        return "ha";
    }

    /*@ normal_behavior
      @ requires obj != null;
      @ ensures \result;
      @*/
    public static boolean isString(Object obj) {
        boolean result = getString(obj) instanceof String id;
        return result;
    }

I added the example and a minor fix (replacewith of the instance_eval_id taclet)

@wadoon
wadoon force-pushed the weigl/instanceof branch 2 times, most recently from 2654e43 to 73183af Compare August 23, 2026 13:32
- BindingsOfExpr ignored the `positive` flag and always emitted the
  whenTrue bindings; the negative meta construct now uses whenFalse().
- Align the transformer names with the dispatched constructs (#posBinding /
  #negBinding instead of #posBindings / #negBindings).
- alwaysAbnormallyTerminates used BindingExpressionCond (copy-paste bug);
  use AbnormallyTerminatesCond. Drop the dangling BINDING_EXPRESSION_FULL
  reference from the registry.
- Add BindingsOfExprTest covering positive/negative bindings for instanceof
  patterns, negation, && / || and non-binding expressions.
The six tests load the production rules from javaRules.key (LDT +
javaRules profile) and apply each rule to a goal with a single rule in
the index, asserting the successor succedent modulo bound renaming.

Until the jmlparser supports the #posBinding/#negBinding meta-constructs
the rule base cannot be loaded, so the tests skip via an assumption that
detects that specific parse error.

Also fixes javaRules.key, which otherwise would not load once the parser
supports the meta-constructs:
- declare the schema variables #b and #nse3 used by the rules
- add the missing semicolon after #negBinding(#nse) in
  conditionalPatternAssignment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Completeness Java Parser Java Pull requests that update Java code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants