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

Unexpected assignment.type.incompatible error #1010

Open
micapolos-google opened this issue Dec 6, 2024 · 0 comments
Open

Unexpected assignment.type.incompatible error #1010

micapolos-google opened this issue Dec 6, 2024 · 0 comments

Comments

@micapolos-google
Copy link

This valid code:

interface Array<T extends @Nullable Object> {
  @Nullable T get();
}

interface Cell<C extends Cell<C>> {}

interface Table<C extends Cell<C>> extends Array<C> {}

public static void test(Table<? extends Cell<?>> table) {
  Cell<?> cell = table.get();
}

produces this error:

error: [assignment.type.incompatible] incompatible types in assignment.
    Cell<?> cell = table.get();
                            ^
  found   : capture#01 extends @Nullable Cell<capture#01>
  required: @Nullable Cell<? extends @NonNull Object>

Nullness checker incorrectly assumes that the result of table.get() method is Cell<@Nullable ?>, while in fact it is Cell<?>.

There are currently two ways to overcome this problem:

  • add @SuppressWarnings annotation to silence the nullness checker,
  • declare local variable as Cell<@Nullable ?> cell, which should not be necessary.

The problem is likely caused by the fact that Table extends Array, where Table has non-null bound, but Array has nullable bounds, so nullness checker confuses the return type of get() method.

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