typetools / typetools/checker-framework

KeyFor can't find variable inside local anonymous class

Open
#4,713 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

For the code below I would expect that checker should be able to find finalistsCount inside the anonymous class used for the comparator. However that doesn't appear to be the case.

package net.mtu.eggplant.checker;

import java.util.*;

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

public class KeyForSortedList {

  public static void function() {
    final Map<Integer, List<String>> finalistsCount = new HashMap<>();

    // code to populate finalistsCount

    final List<@KeyFor("finalistsCount") Integer> sortedTeams = new LinkedList<>(finalistsCount.keySet());
    Collections.sort(sortedTeams, new Comparator<@KeyFor("finalistsCount") Integer>() {
      public int compare(final @KeyFor("finalistsCount") Integer teamOne,
                         final @KeyFor("finalistsCount") Integer teamTwo) {
        final int numCatsOne = finalistsCount.get(teamOne).size();
        final int numCatsTwo = finalistsCount.get(teamTwo).size();
        if (numCatsOne == numCatsTwo) {
          return 0;
        } else if (numCatsOne < numCatsTwo) {
          return 1;
        } else {
          return -1;
        }
      }
    });

    for (final Integer team : sortedTeams) {
      for (final String category : finalistsCount.get(team)) {
        // do something with category, which should be non-null at this point
      }
    }

  }
    
}

This gives the warnings

/home/jpschewe/projects/checker-bugs/src/main/java/net/mtu/eggplant/checker/KeyForSortedList.java:16: error: [expression.unparsable] Expression invalid in dependent type annotation: [error for expression: finalistsCount; error: Invalid 'finalistsCount' because identifier not found]
      public int compare(final @KeyFor("finalistsCount") Integer teamOne,
                                                         ^
/home/jpschewe/projects/checker-bugs/src/main/java/net/mtu/eggplant/checker/KeyForSortedList.java:17: error: [expression.unparsable] Expression invalid in dependent type annotation: [error for expression: finalistsCount; error: Invalid 'finalistsCount' because identifier not found]
                         final @KeyFor("finalistsCount") Integer teamTwo) {
                                                         ^
/home/jpschewe/projects/checker-bugs/src/main/java/net/mtu/eggplant/checker/KeyForSortedList.java:18: error: [dereference.of.nullable] dereference of possibly-null reference finalistsCount.get(teamOne)
        final int numCatsOne = finalistsCount.get(teamOne).size();
                                                 ^
/home/jpschewe/projects/checker-bugs/src/main/java/net/mtu/eggplant/checker/KeyForSortedList.java:19: error: [dereference.of.nullable] dereference of possibly-null reference finalistsCount.get(teamTwo)
        final int numCatsTwo = finalistsCount.get(teamTwo).size();

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 with the reproducer at /home/jpschewe/projects/checker-bugs/src/main/java/net/mtu/eggplant/checker/KeyForSortedList.java and run the Checker Framework to confirm the reported expression.unparsable and dereference.of.nullable diagnostics. Trace how KeyFor resolves finalistsCount from the anonymous Comparator class; done means the variable is recognized there and the nullable dereference warnings are no longer reported.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.