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

Type parameter nullability mismatch when using @Nullable T with ? extends @Nullable T #1126

Open
sdeleuze opened this issue Jan 13, 2025 · 1 comment

Comments

@sdeleuze
Copy link

I am working on enabling JSpecify mode in Spring Framework, and so far the most frequent source of issues has been the handling of ? extends @Nullable T versus @Nullable of T.

See for example this simple code sample:

Supplier<? extends @Nullable String> stringSupplier = () -> null;

Which generates the following error:

error: [NullAway] Cannot assign from type Supplier<@Nullable String> to type Supplier<? extends @Nullable String> due to mismatched nullability of type parameters

It looks like a value use case to me, if not, please let me know.I have also the same issue with nullable method reference.

@sdeleuze sdeleuze changed the title Cannot assign from type Supplier<@Nullable String> to type Supplier<? extends @Nullable String> Cannot use generic type @Nullable T with ? extends @Nullable T Jan 13, 2025
@sdeleuze
Copy link
Author

I have also this use case where I have a isEmpty method defined as:

@Contract("null -> true")
public static boolean isEmpty(@Nullable Map<?, ? extends @Nullable Object> map) {
	return (map == null || map.isEmpty());
}

That I try to use like:

Map<String, @Nullable Object> variables = new HashMap<>();
if (CollectionUtils.isEmpty(variables)) {
	// ...
}

Which generates the following error:

error: [NullAway] Cannot pass parameter of type @Nullable Map<String, @Nullable Object>, as formal parameter has type @Nullable Map<?, ? extends @Nullable Object>, which has mismatched type parameter nullability

@sdeleuze sdeleuze changed the title Cannot use generic type @Nullable T with ? extends @Nullable T Type parameter nullability mismatch when using @Nullable T with ? extends @Nullable T Jan 13, 2025
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