typetools / typetools/checker-framework

Refine the types of literals

Open
#2,618 0 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

Compile the below file with:
javac -g RefineLiteralType.java -processor nullness -ArequirePrefixInWarningSuppressions -AwarnUnneededSuppressions

More warnings are issued when using manifest literals than a constant variable (m5 vs m6). This is a limitation of the implementation of dataflow: literals have no declared type and thus cannot be refined.

Here is the code:

// Compile this file with:
// javac -g RefineLiteralType.java -processor nullness -ArequirePrefixInWarningSuppressions

import java.util.HashMap;
import java.util.Map;
import org.checkerframework.checker.nullness.qual.EnsuresKeyForIf;
import org.checkerframework.checker.nullness.qual.KeyFor;
import org.checkerframework.dataflow.qual.Pure;

public class RefineLiteralType {

  void m5(Aux aux1, Aux aux2) {
    if (aux1.hasValue(Aux.MINIMUM_VALUE) && aux2.hasValue(Aux.MINIMUM_VALUE)) {
      int minA = aux1.getInt(Aux.MINIMUM_VALUE);
      @SuppressWarnings("keyfor:argument.type.incompatible")
      int minB = aux2.getInt(Aux.MINIMUM_VALUE);
    }
  }

  void m6(Aux aux1, Aux aux2) {
    if (aux1.hasValue("minvalue") && aux2.hasValue("minvalue")) {
      @SuppressWarnings("keyfor:argument.type.incompatible")
      int minA = aux1.getInt("minvalue");
      @SuppressWarnings("keyfor:argument.type.incompatible")
      int minB = aux2.getInt("minvalue");
    }
  }

}

class Aux {

  public Map<String, String> map = new HashMap<>();

  public static final String MINIMUM_VALUE = "minvalue";

  @Pure
  @EnsuresKeyForIf(result = true, expression = "#1", map = "map")
  public boolean hasValue(String key) {
    return map.containsKey(key);
  }

  @Pure
  public int getInt(@KeyFor("this.map") String key) {
    return 22;
  }
}

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 compiling the inline RefineLiteralType.java example with the supplied javac command and compare warnings in m5 and m6. Trace the dataflow implementation responsible for refining literal types. Done means manifest literals receive the same refinement as the equivalent constant variable and the extra warnings no longer occur.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.