Skip to content

Type parameter deemed nullable in <? extends T> and nonnull in <T>? #6740

@MichalStehlikCz

Description

@MichalStehlikCz

Following use case

public class Test {

  @Nullable Object item;

  <T> @Nullable T getNullable(Class<T> type) {
    return type.cast(item);
  }

  <T> Optional<T> getOpt(int index, Class<? extends T> type) {
    return Optional.ofNullable(getNullable(type));
  }
}

fails compilation in getOpt method with

java: [argument] incompatible argument for parameter type of Test.getNullable.
  found   : @Initialized @NonNull Class<capture#01 extends T extends @Initialized @Nullable Object>
  required: @Initialized @NonNull Class<T extends @Initialized @NonNull Object>

I believe it should compile fine, as neither method makes any assumption about nullity constraints on type variable

It compiled fine with 3.42.0, fails with 3.46.0

Sub-issues

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions