Skip to content

Propagation of nullability info from overridden methods in supertypes #14

Closed

Description

@dzharkov

If there's an unannotated method having an annotated overridden method in a supertype, should we treat the former one as having the same nullability annotations as the latter? I.e, propagating the nullability info

In Kotlin, we already do this for supported annotations

Activity

  1. changed the title [-]Propagation of nullability info from overridden[/-] [+]Propagation of nullability info from overridden methods in supertypes[/+] on Dec 3, 2018
  2. wmdietlGC commented on Jan 10, 2019

    @wmdietlGC
    Collaborator

    We discussed this issue some more and preferred explicit annotations.

    class Super {
      @Nullable Object foo(@Nullable Object param) ...
    }
    
    class Sub extends Super {
      @Override
      Object foo(Object param) ...
    }

    Class Super is either explicitly annotated or comes from a nullable default context.
    Class Sub is in a not-null-by-default context.
    The reference implementation should issue an error for the invalid parameter type in the override and for violations of behavioral subtyping in general. (That is, parameters can only get wider, returns only narrower.)

    Note that specializing the return type to be NotNull would be sound. The following would work ok:

    class Sub extends Super {
      @Override
      Object foo(@Nullable Object param) ...
    }

    Making the Nullable annotation on the parameter explicit simplifies reading code.
    That is, there are only two places to look at to determine an effective type:

    • Explicit annotations
    • A NotNullDefault annotation on the enclosing top-level type or directly enclosing package
  3. added
    nullness
    on Jan 10, 2019
  4. kevinb9n commented on Jun 17, 2019

    @kevinb9n
    Collaborator

    Agreed, no inheriting nullability annotations.

  5. stephan-herrmann commented on Jul 27, 2019

    @stephan-herrmann

    I generally agree with the decision made here. Just want to let you know that some Eclipse users were very persistent in requesting an option to propagate nullness down from super signatures. Maybe s.t. that could be reconsidered as an option after a 1.0 Release.

  6. added
    design
    on Mar 13, 2024
  7. added this to the 0.3 milestone on Mar 27, 2024
  8. kevinb9n commented on Aug 12, 2024

    @kevinb9n
    Collaborator

    Today, as long as you understand whether you're in a null-marked context or not, you can read code and know what the nullness of every type usage is. At most, you may have to scroll up to where a variable, type variable, etc. was declared to know it.

    Any kind of automatic propagation/inheritance defeats that. If we did that even in a few odd cases, users would lose their ability to trust their own eyes, and nullness semantics would become mysterious.

    Put simply: "String means String." Full stop. This is a core value of the project.

    Here's another, more minor (but still real) issue: suppose we have, in null-marked context:

    interface Foo<T extends @Nullable Object> {
        @Nullable T method(@NonNull T t);
    }
    
    interface Bar<T extends @Nullable Object> extends Foo<T> {
        T method(T t);
    }
    

    If either of the nullness annotations were automatically inherited from Foo.method to Bar.method ... how would Bar.method express that it doesn't want that inheritance in this case - that it wants T to be parametric? It's perfectly valid contractually (it is expanding the set of what it accepts, and refining the type of what it returns).

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

    designnullness

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions