Skip to content

Nullness of generics is lost for "new Foo<@Nullable Object>().bar()"Β #1155

Closed

Description

@agrieve

This code:

import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
public class Foo {
  private static class Inner<T extends @Nullable Object> {
    Inner<T> identity() { return this; }
  }

  Inner<@Nullable Object> mThing = new Inner<@Nullable Object>().identity();
}

Gives:

Foo.java:12: warning: [NullAway] Cannot assign from type Inner<Object> to type Inner<@Nullable Object> due to mismatched nullability of type parameters
  Inner<@Nullable Object> mThing = new Inner<@Nullable Object>().identity();
                          ^
    (see http://t.uber.com/nullaway )

If I change the assignment to two lines, it compiles without warning:

import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
public class Foo {
  static class Inner<T extends @Nullable Object> {
    Inner<T> identity() { return this; }
  }

  Inner<@Nullable Object> mThing;

  Foo() {
    mThing = new Inner<@Nullable Object>();
    mThing = mThing.identity();
  }
}

Activity

  1. msridhar commented on Mar 16, 2025

    @msridhar
    Collaborator

    We have some workarounds for missing annotations in types of new expressions, but we don't handle this case yet. I think openjdk/jdk#23946 might eventually solve this problem for us, assuming it lands and once it makes it into a released javac version. Otherwise, we'll have to add another workaround.

  2. msridhar commented on Apr 20, 2025

    @msridhar
    Collaborator

    It seems that in the latest JDK 25 early access build this bug is fixed, probably due to openjdk/jdk#23946 landing. We may hold off a bit on rolling our own fix for this, to see how this plays out and whether the JDK fix might get backported.

  3. agrieve commented on Apr 22, 2025

    @agrieve
    ContributorAuthor

    Progress!

    Just ran into another bug that I think is an instance of this:

    @NullMarked
    class Foo {
        interface Callback<T extends @Nullable Object> {
            void onResult(T result);
        }
    
        public void main() {
            Callback<@Nullable Integer> thing = new Callback<@Nullable Integer>() {
                @Override
                public void onResult(@Nullable Integer value) {
                    Callback<@Nullable Integer> self = this;
                }
            };
        }
    }
    Foo.java:16: warning: [NullAway] Cannot assign from type Foo. to type Callback<@Nullable Integer> due to mismatched nullability of type parameters
                    Callback<@Nullable Integer> self = this;
    

    The wrong name I filed as #1202, but the warning is triggered because it thinks "this" is Callback<Integer> rather than Callback<@Nullable Integer> (which is why I'm guessing it's due to this javac bug)

  4. added
    jspecifyRelated to support for jspecify standard (see jspecify.dev)
    on Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    false positivejspecifyRelated to support for jspecify standard (see jspecify.dev)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions