Record Pattern with swich has something wrong

kishida naoki naokikishida at gmail.com
Wed Mar 22 18:13:47 UTC 2023


On running Nested Record+Switch code, java command throws error.
condition is:
  switch has 2 case clauses. 1 case + default has no problem.
  The `Type` places 2nd position. `Product(Type type, String name)` has no
problem.
  sealed does not affect.

The code is below.

```
public class PatternSample {
  sealed interface Type {
    record Bulk(int price) implements Type {}
    record Packed(int price) implements Type {}
  }

  record Product(String name, Type type) {}

  public static void main(String[] args) {
    Product item = new Product("meat", new Type.Bulk(250));

    int total = switch(item) {
        case Product(var n, Type.Packed(int price))
                 -> price;
        case Product(var n, Type.Bulk(int price))
                 -> price * 2;
      };
    System.out.println(total);
  }
}
```

The error is below.

```
>java --enable-preview PatternSample
Error: Unable to initialize main class PatternSample
Caused by: java.lang.VerifyError: Bad local variable type
Exception Details:
  Location:
    PatternSample.main([Ljava/lang/String;)V @155: iload
  Reason:
    Type top (current frame, locals[6]) is not assignable to integer
  Current Frame:
    bci: @155
    flags: { }
    locals: { '[Ljava/lang/String;', 'PatternSample$Product', top,
'PatternSample$Product', integer, 'PatternSample$Product', top, top, top,
'java/lang/String', integer }
    stack: { }
  Bytecode:
    0000000: bb00 0759 1209 bb00 0b59 1100 fab7 000d
    0000010: b700 104c 2b59 b800 1357 4e03 3604 2d15
    0000020: 04ba 0019 0000 ab00 0000 007c 0000 0001
    0000030: 0000 0000 0000 0012 2d3a 0519 05b6 001d
    0000040: 3a09 0336 0a19 0915 0aba 0021 0000 ab00
    0000050: 0000 004d 0000 0002 ffff ffff 0000 004d
    0000060: 0000 0000 0000 001a 1909 3a06 1905 b600
    0000070: 223a 0b19 0bc1 0026 9900 1819 0bc0 0026
    0000080: 3a07 1907 b600 2836 0b15 0b36 08a7 0009
    0000090: 0436 0aa7 ffb2 1508 a700 0b15 0605 68a7
    00000a0: 0004 083d b200 2c1c b600 32a7 0011 4ebb
    00000b0: 0039 592d b600 3b2d b700 3ebf b1
  Exception Handler Table:
    bci [61, 64] => handler: 174
    bci [110, 113] => handler: 174
    bci [132, 135] => handler: 174
  Stackmap Table:
    full_frame(@30,{Object[#72],Object[#7],Top,Object[#7],Integer},{})
    same_frame(@56)

full_frame(@69,{Object[#72],Object[#7],Top,Object[#7],Integer,Object[#7],Top,Top,Top,Object[#74],Integer},{})
    same_frame(@104)

full_frame(@144,{Object[#72],Object[#7],Top,Object[#7],Integer,Object[#7],Object[#74],Top,Top,Object[#74],Integer},{})

full_frame(@150,{Object[#72],Object[#7],Top,Object[#7],Integer,Object[#7],Object[#74],Object[#38],Integer,Object[#74],Integer},{})

full_frame(@155,{Object[#72],Object[#7],Top,Object[#7],Integer,Object[#7],Top,Top,Top,Object[#74],Integer},{})
    full_frame(@162,{Object[#72],Object[#7],Top,Object[#7],Integer},{})
    full_frame(@163,{Object[#72],Object[#7]},{Integer})
    full_frame(@174,{Object[#72]},{Object[#55]})
    append_frame(@188,Object[#7],Integer)
```

-- 
Naoki Kishida
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230323/32dbdd95/attachment.htm>


More information about the amber-dev mailing list