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

incorrect comparison is missed by the compiler

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • None
    • 5.0.0-alpha-9
    • Static Type Checker
    • None

    Description

      I have the following program

      class Test {
        public static void main(String[] args) {
          int x = 1;
          x > new Object();
        }
      } 

      Actual behavior

      The code compilers but I receive the following runtime exception

       Exception in thread "main" java.lang.IllegalArgumentException: Cannot compare java.lang.Integer with value '1' and java.lang.Object with value 'java.lang.Object@609cd4d8'
              at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.compareToWithEqualityCheck(DefaultTypeTransformation.java:816)
              at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.compareTo(DefaultTypeTransformation.java:751)
              at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.compareTo(ScriptBytecodeAdapter.java:802)
              at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.compareGreaterThan(ScriptBytecodeAdapter.java:888)
              at Test.main(test.groovy:5)
      Caused by: java.lang.ClassCastException: class java.lang.Object cannot be cast to class java.lang.Integer (java.lang.Object and java.lang.Integer are in module java.base of loader 'bootstrap')
              at java.base/java.lang.Integer.compareTo(Integer.java:59)
              at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
              at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.base/java.lang.reflect.Method.invoke(Method.java:566)
              at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:338)
              at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:274)
              at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1225)
              at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1005)
              at org.codehaus.groovy.runtime.InvokerHelper.invokePojoMethod(InvokerHelper.java:623)
              at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:614)
              at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.compareToWithEqualityCheck(DefaultTypeTransformation.java:802)
              ... 4 more
      

      Expected behavior

      The code should have been rejected with

      test.groovy: 5: [Static type checking] - Cannot find matching method int#compareTo(java.lang.Object). Please check if the declared type is correct and if the method exists.
       @ line 5, column 5.
             x > new Object();
             ^1 error
       

      Notes

      • Tested against master (commit: ea4b1efad7dcb805b4f4fa6454fc95fa338147cb)
      • Groovy 4.0.10 rejects the program as expected.

      Attachments

        Activity

          People

            emilles Eric Milles
            theosot Thodoris Sotiropoulos
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: