Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7690

TypeChecked Groovy does not throw illegal assignment error at compile time when using raw generic type

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.5
    • 4.0.0-beta-2
    • None
    • None

    Description

      I want to provoke a compiler error. I am omitting the type parameter of a generic List and use it as a raw type. When getting an object from the raw list it should be of type Object. When casting this Object implicitly to an Integer the compiler should throw an illegal assignment error but it doesn't.

      The following code example should illustrate the problem. Case 2 and 3 should throw the same error but only case 3 is producing an error.

      import groovy.transform.TypeChecked
      
      @TypeChecked
      class Test {
      
          static method() {
              ArrayList<Integer> integerArrayList = new ArrayList<>();
              ArrayList rawArrayList = new ArrayList();
              ArrayList<Object> objectArrayList = new ArrayList<>();
      
              integerArrayList << 1;
              rawArrayList << new Object();
              objectArrayList << new Object();
      
              Integer x = integerArrayList.get(0);    // Case 1: works as expected
              Integer y = rawArrayList.get(0);        // Case 2: doesn't throw a compile error but should
              Integer z = objectArrayList.get(0);     // Case 3: throws a compile error as expected
          }
      }
      

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              eseidinger Emanuel Seidinger
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: