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

inference of generic method's type argument leads to false negative #918

Open
theosotr opened this issue Sep 19, 2024 · 3 comments
Open

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-3.42.0-eisop4/checker/dist/checker.jar  Test.java

Program

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


public class Test {
    public static <T> T m(T x) { return x; }

    public static void main(String[] args) {
        @Nullable String p2 = null;
        var x = m(p2);
        @Nullable String y = x;
        y.replace("", "");
    }
}

Actual behavior

The code passes the checks, but I get NPE at runtime

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.replace(java.lang.CharSequence, java.lang.CharSequence)" because "<local3>" is null
        at Test.main(Test.java:11)

Expected behavior

The code should have been rejected.

@wmdietl
Copy link
Member

wmdietl commented Sep 19, 2024

Thanks for the report! I can reproduce the problem.

public class Test {
    public static <T> T m(T x) { return x; }

    public static void main(String[] args) {
        String p2 = null;
        var x = m(p2);
        String y = x;
        y.replace("", "");
    }
}

produces no warning. Changing the type of x from var to the supposedly equal String gives the expected warning.

@wmdietl
Copy link
Member

wmdietl commented Sep 19, 2024

As you didn't file this issue with typetools, I assume that it was fixed in a release we haven't pulled into eisop yet.
We'll verify this before working on a fix.

@theosotr
Copy link
Author

theosotr commented Sep 20, 2024

Yes, you are right! It seems it has been fixed in typetools, because typetools 3.42.0 fails to identify the error, but typetoools 3.47.0 detects it! Next time, I will proceed with this verification step before opening an issue here. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants