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

Closed
sdeleuze opened this issue Jan 13, 2025 · 8 comments · Fixed by #1137
Closed

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
@agrieve
Copy link

agrieve commented Jan 28, 2025

I think this might be another example of the same:

@NullMarked
class Foo {
    static abstract class MyClass<T extends @Nullable Object>  {
        abstract T doThing(T value);
    }

    static void repro() {
      new MyClass<@Nullable Object>() {
        @Override
        @Nullable Object doThing(@Nullable Object value) {
            return value;
        }
      }.doThing(null);
    }
}
Foo.java:14: warning: [NullAway] method returns @Nullable, but superclass method Foo.MyClass.doThing(T) returns @NonNull
        @Nullable Object doThing(@Nullable Object value) {
                         ^
    (see http://t.uber.com/nullaway )

@msridhar
Copy link
Collaborator

I think this might be another example of the same:

@NullMarked
class Foo {
static abstract class MyClass<T extends @nullable Object> {
abstract T doThing(T value);
}

static void repro() {
  new MyClass<@Nullable Object>() {
    @Override
    @Nullable Object doThing(@Nullable Object value) {
        return value;
    }
  }.doThing(null);
}

}

Foo.java:14: warning: [NullAway] method returns @Nullable, but superclass method Foo.MyClass.doThing(T) returns @NonNull
       @Nullable Object doThing(@Nullable Object value) {
                        ^
   (see http://t.uber.com/nullaway )

@agrieve do you get this error with JSpecify mode enabled? Without JSpecify mode it is expected as NullAway treats all type variables as @NonNull. In any case I think it's a different issue than the one above.

@agrieve
Copy link

agrieve commented Jan 30, 2025

Yes, this is in jspecify mode.

@msridhar
Copy link
Collaborator

Yes, this is in jspecify mode.

Ok opened #1139 for that one

@sdeleuze
Copy link
Author

To be sure we are all on the same page, this issue is also with the JSpecify mode enabled.

@msridhar
Copy link
Collaborator

Yes @sdeleuze I have a fix PR up for the original issue you reported in #1137

msridhar added a commit that referenced this issue Jan 31, 2025
We need to handle wildcards eventually, but in the meantime, avoid
reporting false positives.

Fixes #1126
@msridhar
Copy link
Collaborator

@sdeleuze NullAway 0.12.4 is now released with the fix for this issue and a couple of the other issues you reported

aarongable pushed a commit to chromium/chromium that referenced this issue Mar 6, 2025
Includes fixes to warnings introduced by the change. Main fixes are:

uber/NullAway#1126
uber/NullAway#1139
uber/NullAway#1148
uber/NullAway#1151

No-try due to flaky optional windows bot.

No-Try: true
Bug: 389129271
Change-Id: Ia2abd8482ffabba7885b616048996f97da856b9a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6325293
Reviewed-by: Mohamed Heikal <mheikal@chromium.org>
Commit-Queue: Andrew Grieve <agrieve@chromium.org>
Owners-Override: Andrew Grieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1429012}
github-actions bot pushed a commit to kaidokert/chrome_base_mirror that referenced this issue Mar 7, 2025
Includes fixes to warnings introduced by the change. Main fixes are:

uber/NullAway#1126
uber/NullAway#1139
uber/NullAway#1148
uber/NullAway#1151

No-try due to flaky optional windows bot.

No-Try: true
Bug: 389129271
Change-Id: Ia2abd8482ffabba7885b616048996f97da856b9a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6325293
Reviewed-by: Mohamed Heikal <mheikal@chromium.org>
Commit-Queue: Andrew Grieve <agrieve@chromium.org>
Owners-Override: Andrew Grieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1429012}
NOKEYCHECK=True
GitOrigin-RevId: 4c17934359abc12d6cf5aef20a073285a25a2d50
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

Successfully merging a pull request may close this issue.

3 participants