typetools / typetools/checker-framework

Permit return type of @MonotonicNonNull

Open
#2,216 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.1k
Forks
440
Avg merge
1d 12h
Merged PRs (30d)
134

Description

It should be permitted to write @MonotonicNonNull on a return type.

A method call to such a method behaves analogously to a field of type @MonotonicNonNull. After the client tests the expression against null, the checker can assume that subsequent uses of the expression are non-null (until the receiver might be re-assigned, for a field or zero-arity method; rules for non-nullary methods would be different and might require purity).

To type-check implementations of methods with @MonotonicNonNull return type, the rules on assignment of @MonotonicNonNull to @MonotonicNonNull need to be relaxed.
Currently such an assignment is forbidden, because the two might be in different states.
However, it is acceptable when a method has a single return statement. Consider the following code, which currently does not type-check but should:

import org.checkerframework.checker.nullness.qual.MonotonicNonNull;

public class MonotonicNonNullReturn {

  @MonotonicNonNull Object field;

  @MonotonicNonNull Object getter() {
    return field;
  }

}

A less restrictive rule would be that if the lhs has only one assignment (statically), then the assignment is permitted (even though lhs and rhs are both @MonotonicNonNull). When a routine has only one return statement, this is satisfied.

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

Start by locating the nullness checker rules for @MonotonicNonNull return types and assignments, then use the MonotonicNonNullReturn example in the issue as the initial reproduction. Done means the example type-checks and the checker permits the stated single-return assignment behavior while preserving the nullness rules for other cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.