switch expression type with 'null' return expression

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Fri Oct 9 09:42:53 UTC 2020


Hi Anna,
yes, this looks like a compiler bug.

Behavior should be the same as:

<Z> Z pick(Z z1, Z z2, Z z3) { .... }

pick(i1, null, i2)

--> lub(I1, I2) --> I

Seems like the `null` is tripping javac up

Maurizio

On 30/09/2020 10:50, Anna Kozlova wrote:
> Hi all,
>
> The following code doesn't compile (infers i_ to Object)
> interface I {
>      void m();
> }
> interface I1 extends I {}
> interface I2 extends I {}
>
> static void n(I1 i1,I2 i2,int s) {
>      var i_ =switch (s) {
>          case 1 -> i1;
>          case 2 ->null;
>          default -> i2;
>      };
>      if (i_ !=null) {
>          i_.m(); //cannot find symbol m()
>      }
> }
>
> If you permute "case 1" and "case 2" switch rules, the code starts to 
> compile (infers i_ to I).
>
> Looks like a bug?
>
> Thanks,
> Anna
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20201009/85b8468f/attachment-0001.htm>


More information about the compiler-dev mailing list