typetools / typetools/checker-framework
@RequiresNonNull seems to be forgotten depending on order of initialization
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.1k
- Forks
- 440
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 134
Description
I would expect the following to check successfully:
MonotonicNonNullConstructorTest.java
import org.checkerframework.checker.nullness.qual.*;
public class MonotonicNonNullConstructorTest {
static class Data {
@MonotonicNonNull Object field;
}
Data data;
Object object;
@RequiresNonNull("#1.field")
MonotonicNonNullConstructorTest(final Data data) {
this.data = data;
this.object = data.field;
}
}
However I get the following error:
$ java -jar checker.jar -processor org.checkerframework.checker.nullness.NullnessChecker -version MonotonicNonNullConstructorTest.java
javac 1.8.0-jsr308-1.9.10
MonotonicNonNullConstructorTest.java:14: error: [assignment.type.incompatible] incompatible types in assignment.
this.object = data.field;
^
found : @Initialized @MonotonicNonNull Object
required: @Initialized @NonNull Object
1 error
However, if the order of the two lines in the constructor are reversed so that 'object' is initialized before 'data', the file checks successfully. Is it possible that the checker is confusing the instance field 'data' with the constructor parameter 'data'? I would expect that because the constructor parameter is final, it shouldn't matter where in the constructor its field is accessed; it must continue to be non-null because of the combination of @RequiresNonNull and @MonotonicNonNull.
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
Start by reproducing the report with the shown MonotonicNonNullConstructorTest.java and the NullnessChecker command. Trace handling of @RequiresNonNull and @MonotonicNonNull, then verify that both constructor statement orders check successfully without the reported assignment error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100