Bug 575652 - [17][codegen][switch pattern] Unnecessary casts in switch
Summary: [17][codegen][switch pattern] Unnecessary casts in switch
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.21   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 4.24   Edit
Assignee: Srikanth Sankaran CLA
QA Contact: Manoj N Palat CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-08-26 19:04 EDT by Manoj N Palat CLA
Modified: 2024-04-26 06:34 EDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Manoj N Palat CLA 2021-08-26 19:04:22 EDT
Issue by Remi Forax

import static java.util.stream.IntStream.rangeClosed;

public interface PatternFizzBuzz {
  static String fizzbuzz(int i) {
    return switch((Integer) i) {
      case Integer v && v % 15 == 0 -> "FizzBuzz";
      case Integer v && v % 5 == 0 -> "Buzz";
      case Integer v && v % 3 == 0 -> "Fizz";
      case Integer v -> "" + v;
    };
  }

  static void main(String[] args) {
    rangeClosed(1, 100).mapToObj(i -> fizzbuzz(i)).forEach(System.out::println);
  }
}

the code generated by eclipse is full of unnecessary casts
(see the <-- here)

public static java.lang.String fizzbuzz(int);
    Code:
       0: iload_0
       1: invokestatic  #8                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       4: astore_1
       5: iconst_0
       6: istore_2
       7: aload_1
       8: iload_2
       9: invokedynamic #14,  0             // InvokeDynamic #0:typeSwitch:(Ljava/lang/Object;I)I
      14: tableswitch   { // -1 to 2
                    -1: 121
                     0: 44
                     1: 69
                     2: 95
               default: 121
          }
      44: aload_1
      45: checkcast     #9                  // class java/lang/Integer   <-- here
      48: astore_3
      49: aload_3
      50: invokevirtual #18                 // Method java/lang/Integer.intValue:()I
      53: bipush        15
      55: irem
      56: ifeq          64
      59: iconst_1
      60: istore_2
      61: goto          7
      64: ldc           #22                 // String FizzBuzz
      66: goto          139
      69: aload_1
      70: checkcast     #9                  // class java/lang/Integer  <-- here
      73: astore        4
      75: aload         4
      77: invokevirtual #18                 // Method java/lang/Integer.intValue:()I
      80: iconst_5
      81: irem
      82: ifeq          90
      85: iconst_2
      86: istore_2
      87: goto          7
      90: ldc           #24                 // String Buzz
      92: goto          139
      95: aload_1
      96: checkcast     #9                  // class java/lang/Integer  <-- here
      99: astore        5
     101: aload         5
     103: invokevirtual #18                 // Method java/lang/Integer.intValue:()I
     106: iconst_3
     107: irem
     108: ifeq          116
     111: iconst_3
     112: istore_2
     113: goto          7
     116: ldc           #26                 // String Fizz
     118: goto          139
     121: aload_1
     122: astore        6
     124: new           #28                 // class java/lang/StringBuilder
     127: dup
     128: invokespecial #30                 // Method java/lang/StringBuilder."<init>":()V
     131: aload         6
     133: invokevirtual #34                 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
     136: invokevirtual #38                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     139: areturn


if you compare with the one generated by javac

public interface PatternFizzBuzz {
  public static java.lang.String fizzbuzz(int);
    Code:
       0: iload_0
       1: invokestatic  #1                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       4: astore_1
       5: iconst_0
       6: istore_2
       7: aload_1
       8: iload_2
       9: invokedynamic #7,  0              // InvokeDynamic #0:typeSwitch:(Ljava/lang/Object;I)I
      14: tableswitch   { // -1 to 2
                    -1: 112
                     0: 44
                     1: 66
                     2: 89
               default: 112
          }
      44: aload_1
      45: astore_3
      46: aload_3
      47: invokevirtual #11                 // Method java/lang/Integer.intValue:()I
      50: bipush        15
      52: irem
      53: ifeq          61
      56: iconst_1
      57: istore_2
      58: goto          7
      61: ldc           #15                 // String FizzBuzz
      63: goto          125
      66: aload_1
      67: astore        4
      69: aload         4
      71: invokevirtual #11                 // Method java/lang/Integer.intValue:()I
      74: iconst_5
      75: irem
      76: ifeq          84
      79: iconst_2
      80: istore_2
      81: goto          7
      84: ldc           #17                 // String Buzz
      86: goto          125
      89: aload_1
      90: astore        5
      92: aload         5
      94: invokevirtual #11                 // Method java/lang/Integer.intValue:()I
      97: iconst_3
      98: irem
      99: ifeq          107
     102: iconst_3
     103: istore_2
     104: goto          7
     107: ldc           #19                 // String Fizz
     109: goto          125
     112: aload_1
     113: astore        6
     115: aload         6
     117: invokedynamic #21,  0             // InvokeDynamic #1:makeConcatWithConstants:(Ljava/lang/Integer;)Ljava/lang/String;
     122: goto          125
     125: areturn
Comment 1 Jay Arthanareeswaran CLA 2021-11-19 00:45:47 EST
I presume no progress here so far and I don't see this making it to 4.22. So, moving out.
Comment 2 Manoj N Palat CLA 2022-02-16 05:09:57 EST
Bulk move out of 4.23
Comment 3 Manoj N Palat CLA 2022-02-16 05:10:10 EST
Bulk move out of 4.23
Comment 4 Manoj N Palat CLA 2022-02-16 05:10:16 EST
Bulk move out of 4.23
Comment 5 Manoj N Palat CLA 2022-02-16 05:10:27 EST
Bulk move out of 4.23
Comment 6 Eclipse Genie CLA 2024-02-08 13:11:51 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 7 Rémi Forax CLA 2024-02-26 03:52:40 EST
As far as i know, this bug still exists
Comment 8 Andrey Loskutov CLA 2024-02-26 05:30:49 EST
(In reply to Rémi Forax from comment #7)
> As far as i know, this bug still exists

Remi, may I ask you to create a ticket at https://github.com/eclipse-jdt/eclipse.jdt.core/issues?

Srikanth plans to work on major switch code refactoring soon.
Comment 9 Srikanth Sankaran CLA 2024-04-26 06:34:58 EDT
I'll follow up