typetools / typetools/checker-framework
Restrict the instantiation type on a constructor
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.1k
- Forks
- 440
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 134
Description
It is desirable to specify the result type (the "return type") of a constructor.
For example, new TreeSet<Integer>() has type TreeSet<@NonNull Integer>, but other treesets can permit nullable elements.
More generally, we wish to permit the first two constructor calls but forbid the last two.
new TreeSet<@Nullable Integer>(new IntegerOrNullComparator());
new TreeSet<Integer>();
//:: error: (type.argument.type.incompatible)
new TreeSet<@Nullable Integer>();
//:: error: (type.argument.type.incompatible)
new TreeSet<@Nullable Integer>(new IntegerComparator());
Java provides syntax for restricting the receiver type on an instance method, but no syntax for type parameter annotations on the constructor result. Only a type annotation, not a full type which would include type parameters, can precede a constructor declaration. So, we would need to make up a new annotation to express the constraint.
There are likely to be classes other than TreeSet that need to utilize this new mechanism.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue names no files, tests, or entry points. Start by locating the constructor type-checking and type-annotation handling in the Checker Framework, then compare it with receiver-type restrictions for instance methods. Done means defining a mechanism for constraining constructor result type arguments and checking the four TreeSet examples with the stated diagnostics.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100