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 positive due to failure to capture full type information #6747

Open
MichalStehlikCz opened this issue Aug 8, 2024 · 0 comments
Open
Assignees

Comments

@MichalStehlikCz
Copy link
Contributor

Following example

class Test {

  static final class Expression<T> {
    Class<T> getType() {
      throw new RuntimeException("xxx");
    }
  }

  private static void validate(List<? extends Class<?>> argumentTypes) {
    throw new RuntimeException("xxx");
  }

  void test(Collection<? extends Expression<?>> arguments) {
    var argumentTypes = arguments.stream().map(Expression::getType).toList();
    validate(argumentTypes);
  }
}

reports

C:\Projects\pvyscomponents\java\libs\kernel\repository\api\src\main\java\com\provys\repository\Test.java:24:14
java: [argument] incompatible argument for parameter argumentTypes of Test.validate.
  found   : @UnknownKeyFor List<R[ extends @UnknownKeyFor Object super @KeyForBottom Void]>
  required: @UnknownKeyFor List<?[ extends @UnknownKeyFor Class<?[ extends @UnknownKeyFor Object super @UnknownKeyFor Void]> super @KeyForBottom Void]>
C:\Projects\pvyscomponents\java\libs\kernel\repository\api\src\main\java\com\provys\repository\Test.java:24:14
java: [argument] incompatible argument for parameter argumentTypes of Test.validate.
  found   : @Initialized @NonNull List<R extends @Initialized @Nullable Object>
  required: @Initialized @NonNull List<? extends @Initialized @NonNull Class<? extends @Initialized @Nullable Object>>

It seems checker is unable to infer correct type, JDK is able to compile the code and problem is not nullability, but missing information in checker type inference.

To be honest, I do not completely understand even JDK type inference here, as if I replace var with what I think should pass for actual type
List<Class<?>> argumentTypes = arguments.stream().map(Expression::getType).toList();
compilation fails with
java: incompatible types: java.util.List<java.lang.Class<capture#1 of ?>> cannot be converted to java.util.List<java.lang.Class<?>>

I get originally mentioned warning with
List<? extends Class<?>> argumentTypes = arguments.stream().map(Expression::getType).toList();

@smillst smillst self-assigned this Aug 8, 2024
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