Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

A generic constructor called without a diamond gets no inference for its own type variables #2191

Backlog

Description

@vlsi

A call of a generic constructor without a diamond, such as new NonGeneric(l, null) or new Gen<String>(l, null), reports two errors where the same call of a generic method reports none:

import java.util.List;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
@NullMarked
class Test {
  static class NonGeneric {
    <W extends @Nullable Object> NonGeneric(List<W> ws, W w) {}
  }
  static class Gen<E> {
    <W extends @Nullable Object> Gen(List<W> ws, W w) {}
  }
  static <W extends @Nullable Object> void method(List<W> ws, W w) {}
  void methodCall(List<@Nullable String> l) {
    method(l, null);
  }
  void nonDiamond(List<@Nullable String> l) {
    new NonGeneric(l, null);
  }
  void explicitClassTypeArgument(List<@Nullable String> l) {
    new Gen<String>(l, null);
  }
}

At master 594cf3d (a CompilationTestHelper test in nullaway/src/test), with -XepOpt:NullAway:JSpecifyMode=true, -XepOpt:NullAway:JSpecifyExperimental=true and -XepOpt:NullAway:OnlyNullMarked=true, method(l, null) passes, and each constructor call reports:

/Test.java:17: warning: [NullAway] incompatible types: List<@Nullable String> cannot be converted to List<W>
    new NonGeneric(l, null);
                   ^
    (see http://t.uber.com/nullaway )

/Test.java:17: warning: [NullAway] passing @Nullable parameter 'null' where @NonNull is required
    new NonGeneric(l, null);
                      ^
    (see http://t.uber.com/nullaway )

/Test.java:20: warning: [NullAway] incompatible types: List<@Nullable String> cannot be converted to List<W>
    new Gen<String>(l, null);
                    ^
    (see http://t.uber.com/nullaway )

/Test.java:20: warning: [NullAway] passing @Nullable parameter 'null' where @NonNull is required
    new Gen<String>(l, null);
                       ^
    (see http://t.uber.com/nullaway )

Expected: W is inferred as @Nullable String for both constructor calls, as it is for method(l, null), and none of the four errors is reported.

#1825 tracks the constructor's own type variables at a diamond call, new Gen<>(l, null); this one is the call without a diamond, where the constructor's type variables are not inferred at all. The same gap hides a report once #1955 lands: with <W> Wild(Box<? extends W> b) and a Box<T> whose T has a @Nullable bound, new Wild(b) passes while wild(b), with <W> void wild(Box<? extends W> b), reports inference failure: type variable W of Test.wild is constrained to be @Nullable, but its upper bound requires it to be @NonNull.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions