Skip to content

Add support for type Annotations - #3996

Merged
WolframPfeifer merged 5 commits into
KeYProject:mainfrom
PiIsRational:annotations
Oct 28, 2026
Merged

WolframPfeifer merged 5 commits into
KeYProject:mainfrom
PiIsRational:annotations

Conversation

@PiIsRational

@PiIsRational PiIsRational commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Intended Change

The Goal of this Pull Request is to be able to use Java Type Annotations in Taclets. To make this work the Type Annotations were added to the KeY Ast at the TypeReference level. The annotations were added to the type references, since they correspond to the syntactical version of the types. Since in most places KeYJavaTypes were used, there was the need to replace them.

The default matching behaviour was not altered and is compatible with the current master branch. This means that there is no semantic difference between Object oand @Rep Object o. Therefore, no taclets need to be altered and annotations are simply ignored per default.

To interact with annotations in taclets there are currently 3 variable conditions:

  • hasAnnotation(SchemaVariable variable, String annot):
    It checks that the type (TypeReference) of variable has an annotation whose fully qualified name matches annot. It supports ProgramVariables, New (simple constuctor calls) and Fields.
  • addAnnotation(SchemaVariable store, SchemaVariable read, String annot):
    It creates a copy of read and stores in store except that the last annotation of the type of store now corresponds to annot. If the given string annot does not match an annotation, the variable condition fails. It supports ProgramVariables and New (simple constructor calls).
  • removeAnnotation(SchemaVariable store, SchemaVariable read, String annot):
    It creates a copy of read where all annotations of the type of read with the fully qualified name matching annot are removed and stores it in store. This variable condition matches always and only supports New (simple constructor calls).

Currently only, Marker annotations so annotations with no arguments (such as @NonNull or @Rep) are supported.

Example

In the context of a Nullness type system (where the @Null annotation is type checked and means that the only value of a type with this annotation is null) we could have something like the following snippet:

class Nop {
    @Null Object o;

    /*@ ensures o == null; */
    void nop() {}
}

To be able to use the guarantee expressed by the annotation we could write the following Taclet:

selectNull {
    \schemaVar \term Object o;
    \schemaVar \term Field f;
    \schemaVar \term Heap h;

    \find(select<Object>(h, o, f))

    \varcond(\hasAnnotation(f, Null))

    \replacewith(null)
}

A further (similar) example can be found here.
The main use of this change is PR #3724 and can be found here (mainly in universeDefs.key).

Plan

  • Add tests for the variable conditions
  • Add documentation

Type of pull request

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

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.
  • I have tested the feature as follows:

Additional information and contact(s)

@WolframPfeifer is involved in this Pull Request

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

Previous Commits:
230f842 # add the ownership rules
6a195dd # added the new rules
415f9db # extend the javac extension
f37d143 # change the way the universe rules are treated
c337d49 # update to add the latest changes
82f0bcc # do some bugfixes in the recoder parsers
912cc8c # the last changes
94763f1 # update the match conditions
19f98e9 # add support for args and results in contracts
0d932a9 # some changes
a82c2c2 # the new rule files
2f35dcf # add the proofs
85a0e6d # update the rules
902c17c # remove unneeded assertion
3f0dea8 # remove logs in the default lemma generator
29dabd9 # all the universe lemmas have a corresponding runnable proof
ac9124d # remove factorypaths
88187a0 # add support for dom references
16e29d2 # update the heuristics
519a11c # update the heap simplification macro with universe rules
2cb864d # apply spotless
eae3d25 # refactoring
86d605e # a first try with the new parser
23d12f4 # a minor change
67995b6 # tried to get types to wor
8bc0407 # add annotations to type references
bbd5a1b # try to get Annotation Declarations to properly load
8a1112a # the Annotation Declarations now get loaded correctly
3c8bff8 # a first somewhat functional state
85fd964 # formatting
a1f56a6 # a few changes:
90c2968 # some small changes
26ca483 # remove special case, as it is already coeverd by the base case
26cf894 # remove incorrect proof rules
74e547e # completed the addAnnotation variable condition
76c00be # create the RemoveAnnotation varcond
1cc43d5 # fix a testcase
7b1e680 # formatting
6104e0a # have clean variable conditions
d3bf37c # reduced the various lemmas
228a069 # deactivated the universe rules
34da918 # remove universe rules
34a15df # remove automatic proofs
c2a059f # run spotless
6cd0ba1 # add back proof files
32a9fa5 # the conract adder works!
cf12853 # make the annotation interface member be a real member declaration
04eaa6b # move to ImmutableList
7efd1f4 # fix the lemma proofs
87ff5cd # remove unused axiom from the rules
6e737e4 # cleanup
c5a911e # move the annotation-declaration test and some cleanup
f9c48b7 # remove the limiting
fd803b1 # remove universe specific constructs
e48ebe6 # add the annotation mover
1fe8020 # fix the pipeline tests
d563cb1 # add the small annotation test
ade91f0 # cleanup
b0e956d # minor last changes
@wadoon

wadoon commented Oct 22, 2026

Copy link
Copy Markdown
Member

Stupid remarks:

@WolframPfeifer

Copy link
Copy Markdown
Member

For documentation: The above example does not work with the current implementation. The HasAnnotation varcond is not applicable (throws an error in the constructor). I think the issue is that at the time when the varcond is evaluated, the information about the annotation is already lost, so there is no possibility to access the annotations of parameters (of the current method under proving) in taclets. I think this is a general limitation and cannot be overcome with taclets.

That is the reason why for the branch that adds Universe Type support, a new pipeline step had to be added (UniverseMethodContractAdder), which adds the information from the annotations via the UT predicates.

The PR description needs to be changed, maybe with the working example from the test cases instead.
As far as I understood, the new varconds support annotations with field select, on new statements, and local variable declarations.

@WolframPfeifer
WolframPfeifer marked this pull request as ready for review October 28, 2026 14:14
@WolframPfeifer WolframPfeifer added this to the v3.1.0 milestone Oct 28, 2026
@WolframPfeifer
WolframPfeifer added this pull request to the merge queue Oct 28, 2026
Merged via the queue into KeYProject:main with commit 6ce13fe Oct 28, 2026
39 checks passed
wadoon added a commit that referenced this pull request Oct 28, 2026
The rebase onto main brings the merged type-annotations feature, which
changed the TypeReference API of TermBuilder (selfVar takes TypeRef now)
and the TypeDeclaration constructor surface. Adapt the JML front-end
takeover to it:

- AnnotationInterfaceDeclaration: drop the removed TextualJMLConstruct
  jmlAttachments parameter (branch deleted the carrier in Phase 1)
- JP2KeYConverter.visit(AnnotationDeclaration): no longer pass getClassSpec
- ClassAxiomComputer: selfVar(kjt) -> selfVar(new TypeRef(kjt), ...)
wadoon added a commit that referenced this pull request Nov 2, 2026
The rebase onto main brings the merged type-annotations feature, which
changed the TypeReference API of TermBuilder (selfVar takes TypeRef now)
and the TypeDeclaration constructor surface. Adapt the JML front-end
takeover to it:

- AnnotationInterfaceDeclaration: drop the removed TextualJMLConstruct
  jmlAttachments parameter (branch deleted the carrier in Phase 1)
- JP2KeYConverter.visit(AnnotationDeclaration): no longer pass getClassSpec
- ClassAxiomComputer: selfVar(kjt) -> selfVar(new TypeRef(kjt), ...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants