typetools / typetools/checker-framework

Restrict the instantiation type on a constructor

Open
#1,330 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement False Positive (false warning or imprecision)
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.