Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing cast for generic method invocation on raw parameterized class instance #408

Open
jorgsaa opened this issue Jul 21, 2024 · 0 comments
Labels
Priority: Medium Medium priority Subsystem: Variables Anything concerning variables, types, assignments, and casting Type: Bug Something isn't working

Comments

@jorgsaa
Copy link

jorgsaa commented Jul 21, 2024

Vineflower version

Reproduced on 1.9.3 1.10.1 and on current HEAD of develop/1.11.0

Describe the bug

Decompiling a method invocation with a generic return type on a raw parameterized class instance seems to not cast when assigning it (when it is required) There are checkcast instructions on the result before the assignment, so i believe the information should be there to be able to do the cast.

Additional information

Input:

public class Main {
    static String[] stringsField;

    public static void main(String[] args) {
        String[] strings = new String[]{"Hello", "World"};
        LinkedList list = new LinkedList();

        for (String string : strings) {
            list.add(string);
        }

        stringsField = (String[]) list.toArray(new String[0]);

        for (String string : stringsField) {
            System.out.println(string);
        }
    }
}

Output:

public class Main {
   static String[] stringsField;

   public static void main(String[] args) {
      String[] strings = new String[]{"Hello", "World"};
      LinkedList list = new LinkedList();

      for (String string : strings) {
         list.add(string);
      }

      stringsField = list.toArray(new String[0]); // Incompatible types. Found: 'java.lang.Object[]', required: 'java.lang.String[]'

      for (String string : stringsField) {
         System.out.println(string);
      }
   }
}
Compiled from "Main.java"
public class Main {
  static java.lang.String[] stringsField;

  public Main();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: iconst_2
       1: anewarray     #2                  // class java/lang/String
       4: dup
       5: iconst_0
       6: ldc           #3                  // String Hello
       8: aastore
       9: dup
      10: iconst_1
      11: ldc           #4                  // String World
      13: aastore
      14: astore_1
      15: new           #5                  // class java/util/LinkedList
      18: dup
      19: invokespecial #6                  // Method java/util/LinkedList."<init>":()V
      22: astore_2
      23: aload_1
      24: astore_3
      25: aload_3
      26: arraylength
      27: istore        4
      29: iconst_0
      30: istore        5
      32: iload         5
      34: iload         4
      36: if_icmpge     58
      39: aload_3
      40: iload         5
      42: aaload
      43: astore        6
      45: aload_2
      46: aload         6
      48: invokevirtual #7                  // Method java/util/LinkedList.add:(Ljava/lang/Object;)Z
      51: pop
      52: iinc          5, 1
      55: goto          32
      58: aload_2
      59: iconst_0
      60: anewarray     #2                  // class java/lang/String
      63: invokevirtual #8                  // Method java/util/LinkedList.toArray:([Ljava/lang/Object;)[Ljava/lang/Object;
      66: checkcast     #9                  // class "[Ljava/lang/String;"
      69: checkcast     #9                  // class "[Ljava/lang/String;"
      72: putstatic     #10                 // Field stringsField:[Ljava/lang/String;
      75: getstatic     #10                 // Field stringsField:[Ljava/lang/String;
      78: astore_3
      79: aload_3
      80: arraylength
      81: istore        4
      83: iconst_0
      84: istore        5
      86: iload         5
      88: iload         4
      90: if_icmpge     113
      93: aload_3
      94: iload         5
      96: aaload
      97: astore        6
      99: getstatic     #11                 // Field java/lang/System.out:Ljava/io/PrintStream;
     102: aload         6
     104: invokevirtual #12                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
     107: iinc          5, 1
     110: goto          86
     113: return
}
@jorgsaa jorgsaa added the Type: Bug Something isn't working label Jul 21, 2024
@jaskarth jaskarth added Priority: Medium Medium priority Subsystem: Variables Anything concerning variables, types, assignments, and casting labels Jul 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority: Medium Medium priority Subsystem: Variables Anything concerning variables, types, assignments, and casting Type: Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants