Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

failing to detect invalid assignment when using ? wildcard #6890

Open
theosotr opened this issue Nov 12, 2024 · 0 comments
Open

failing to detect invalid assignment when using ? wildcard #6890

theosotr opened this issue Nov 12, 2024 · 0 comments
Assignees

Comments

@theosotr
Copy link

Command

javac -J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED \
  -J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED \
-processor org.checkerframework.checker.nullness.NullnessChecker \
-cp checker-framework/checker/dist/checker.jar -Aignorejdkastub Test.java

File

import java.util.*;
import org.checkerframework.checker.nullness.qual.*;

class A<T> {
    T f;
    A(T f) {this.f = f;}
    T getF() { return this.f; }
}

public class Test {

  static public void main(String[] args) {
      Map<@Nullable String, String> map = new HashMap<@Nullable String, String>();
      map.put(null, "");
      A<Map<@Nullable String, String>> x = new A<Map<@Nullable String, String>>(map);
      Map<String, ?> y = x.getF();
      y.keySet().iterator().next().toString();
  }
}

Actual behavior

The code passes the check, but there's a NPE

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.toString()" because the return value of "java.util.Iterator.next()" is null
        at Test.main(Test.java:17)

Expected behavior

The code should have been rejected

Test.java:16: error: [assignment] incompatible types in assignment.
      Map<String, ?> y = x.getF(); // invalid assignment
                                    ^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants