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

Annotations on any type arguments of a method receiver are ignored #104

Open
zcai1 opened this issue Sep 30, 2021 · 2 comments · May be fixed by #793
Open

Annotations on any type arguments of a method receiver are ignored #104

zcai1 opened this issue Sep 30, 2021 · 2 comments · May be fixed by #793
Assignees

Comments

@zcai1
Copy link
Collaborator

zcai1 commented Sep 30, 2021

For example, we have the following test case that throws an NPE:

public class ReceiverTypeArgs {
    static class Box<T> {
        T item;

        public Box(T item) {
            this.item = item;
        }

        void test(Box<@NonNull T> this) {
            System.out.println(this.item.toString());
        }
    }

    private static void foo(Box<@Nullable String> box) {
        // expected: invalid method receiver
        box.test();
    }

    public static void main(String[] args) {
        foo(new Box<>(null));
    }
}

Note that the method receiver parameter Box<@NonNull T> this should not accept the argument Box<@Nullable String> box since generics are invariant. However, the Checker Framework doesn't have any warnings for this test case.

@cpovirk
Copy link

cpovirk commented Jan 5, 2022

(Upstream has issue 3203 for this.)

@wmdietl
Copy link
Member

wmdietl commented May 7, 2024

I just ran into this again:

import org.checkerframework.checker.nullness.qual.Nullable;

class MyBox<E extends @Nullable Object> {
  E f;

  MyBox(E in) {
    f = in;
  }

  void set(E in) {
    f = in;
  }
  
  void nullOut(MyBox<@Nullable E> this) {
    set(null);
  }

  void demo(MyBox<String> mbs, MyBox<@Nullable String> mbns) {
    mbns.set(null); // ok
    mbs.set(null); // error
  }
  
  void demoNullout(MyBox<String> mbs, MyBox<@Nullable String> mbns) {
    mbns.nullOut(); // ok
    mbs.nullOut(); // should error, but none raised
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants