Skip to content

Wrong class name used in error message when "this" is used in an anonymous inner class #1202

Closed

Description

@agrieve
@NullMarked
class Foo {
    interface Callback<T extends @Nullable Object> {
        void onResult(T result);
    }

    static void addCallback(Callback<@Nullable Integer> cb) {
    }
    static void removeCallback(Callback<@Nullable Integer> cb) {
    }

    public void main() {
        addCallback(new Callback<>() {
            @Override
            public void onResult(Integer value) {
                removeCallback(this);
            }
        });
    }
}

This results in:

Foo.java:21: warning: [NullAway] Cannot pass parameter of type Foo., as formal parameter has type Callback<@Nullable Integer>, which has mismatched type parameter nullability
                removeCallback(this);
                               ^
    (see http://t.uber.com/nullaway )

Note that "Foo" is the outer class, but the class it's complaining about is the anonymous Callback.

If you change main() to be static, it shows:

Foo.java:21: warning: [NullAway] Cannot pass parameter of type , as formal parameter has type Callback<@Nullable Integer>, which has mismatched type parameter nullability
                removeCallback(this);
                               ^
    (see http://t.uber.com/nullaway )

Activity

  1. agrieve commented on Oct 1, 2025

    @agrieve
    ContributorAuthor

    Just hit this again. Note that there's a second more impactful bug here, which is that the nullness of "this" is incorrect.

    For

       removeCallback(this);
    

    It's saying that "this" is non-nullable, when it's nullable.

  2. added
    jspecify
    on Oct 1, 2025
  3. msridhar commented on Oct 1, 2025

    @msridhar
    Contributor

    This is unrelated to the diamond operator, which we still don't support well. Even this version results in the same error:

    @NullMarked
    class Test {
      interface Callback<T extends @Nullable Object> {
        void onResult(T result);
      }
    
      static void addCallback(Callback<@Nullable Integer> cb) {
      }
      static void removeCallback(Callback<@Nullable Integer> cb) {
      }
    
      public void main() {
        addCallback(new Callback<@Nullable Integer>() {
          @Override
          public void onResult(@Nullable Integer value) {
            removeCallback(this);
          }
        });
      }
    }
  4. added a commit that references this issue on Oct 5, 2025
    24f3675
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

    jspecify

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions