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

False negative with diamond syntax #820

Open
cpovirk opened this issue Jul 25, 2024 · 0 comments
Open

False negative with diamond syntax #820

cpovirk opened this issue Jul 25, 2024 · 0 comments

Comments

@cpovirk
Copy link

cpovirk commented Jul 25, 2024

==> Foo.java <==
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.NullMarked;

@NullMarked
class Foo<V extends @NonNull Object> extends FooParent<V> {}

==> FooParent.java <==
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
class FooParent<V extends @Nullable Object> {}

==> TestClass.java <==
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
class TestClass {
  static <V extends @Nullable Object> FooParent<V> shouldFailButDoesNot() {
    return new Foo<>();
  }
}

(I included the redundant extends @NonNull Object above so that I could make a fairer comparison to typetools, which AFAIK doesn't yet recognize the effect of @NullMarked on type-parameter declarations. The issue reproduces even without that (and also with just extends Object).)

checker-framework-3.42.0-eisop4 $ checker/bin/javac -cp $HOME/.m2/repository/org/jspecify/jspecify/1.0.0/jspecify-1.0.0.jar -processor nullness Foo.java FooParent.java TestClass.java -AconservativeUninferredTypeArguments
Note: The Checker Framework is tested with JDK 8, 11, 17, and 21. You are using version 22.

This happens only when using the diamond operator: If we instead specify an explicit type argument V, we get an error (even without -AconservativeUninferredTypeArguments):

TestClass.java:7: error: [type.argument.type.incompatible] incompatible type argument for type parameter V of Foo.
    return new Foo<V>();
                   ^
  found   : V extends @Nullable Object
  required: @NonNull Object
1 error

typetools likewise gets this wrong at 3.42.0. It gets it right by 3.45.0 (I didn't check versions in between), I assume as a result of the type-inference revamp:

TestClass.java:7: error: [type.arguments.not.inferred] Could not infer type arguments for Foo constructor
    return new Foo<>();
           ^
  unsatisfiable constraint: V extends @Initialized @Nullable Object <: @Initialized @NonNull Object
1 error

My expectation would be that eisop would start working once it picks up the type-inference changes. I am filing this issue anyway just in case it's of interest. (I at least found it surprising that -AconservativeUninferredTypeArguments didn't help, though again, that's the case in typetools, too.)

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

1 participant