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

CompileStatic does not throw expected exception

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 2.5.5
    • Fix Version/s: None
    • Component/s: Compiler
    • Labels:
      None

      Description

      While reading about Java Generics and wildcards (see https://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html), I came across the following code in the referenced oracle document

      Collection<?> c = new ArrayList<String>();
       c.add(new Object()); // Compile time error

      Now, I decided to wrap this in a class and try it out in Java and then in Groovy.

      Here is the Java code

       

      import java.util.*;
      public class TestCollection {
       static Collection<?> c = new ArrayList<String>();
       public static void main(String[] args) {
         c.add(new Object()); // Compile time error
       }
      }
      

       

        

      and here is the groovy code

       

      import java.util.*;
      import groovy.transform.CompileStatic;
      
      @CompileStatic
      public class TestCollection {
       static Collection<?> c = new ArrayList<String>();
       public static void main(String[] args) {
         c.add(new Object()); // Compile time error
         println c.first();
       }
      }
      

       

      When I attempt to compile the Java code I get the following expected error

      $ javac TestCollection.java
       TestCollection.java:7: error: incompatible types: Object cannot be converted to CAP#1
       c.add(new Object()); // Compile time error
       ^
       where CAP#1 is a fresh type-variable:
       CAP#1 extends Object from capture of ?

      But, when I run the code through Groovy, I do not get a compile-time error.  Instead, I get the following output

      java.lang.Object@41a0aa7d

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                restagner Robert Stagner
              • Votes:
                1 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 50m
                  50m