Capture conversion and switch selectors

Liam Miller-Cushon cushon at google.com
Tue Jan 28 18:51:22 UTC 2025


Hi,

Chris Povirk and I were trying to understand why examples like the
following are rejected:

sealed interface A<T> {
  enum B implements A<String> {
    C;
  }

  static void d(A<?> a) {
    switch (a) {
      case B.C:
    }
  }
}

A.java:8: error: incompatible types: B cannot be converted to A<CAP#1>
      case B.C:
            ^
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Object from capture of ?

I think this is a consequence of JLS 6.5.6.1, which says that the type of
the switch selector expression is subject to capture conversion, and then
the captured type is not convertible.

In the implementation, this is happening here [1], where attribExpr uses
KindSelector.VAL, which causes the capture to happen.

Has consideration been given to treating selector expressions differently,
to avoid the capture conversion, and allow examples like this to compile? I
couldn't find test coverage for this specific combination of features,
which made me wonder if this was a corner case that hadn't been explored,
rather than something that had definitely been ruled out.

Liam

[1]
https://github.com/openjdk/jdk/blob/a224f12cb701b45df4706a403a05c66de2d623af/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java#L1702
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20250128/1b497948/attachment.htm>


More information about the compiler-dev mailing list