Thodoris Sotiropoulos created GROOVY-11383:
----------------------------------------------

             Summary: incorrect comparison is missed by the compiler
                 Key: GROOVY-11383
                 URL: https://issues.apache.org/jira/browse/GROOVY-11383
             Project: Groovy
          Issue Type: Bug
          Components: Static Type Checker
            Reporter: Thodoris Sotiropoulos


I have the following program
{code:java}
class Test {
  public static void main(String[] args) {
    int x = 1;
    x > new Object();
  }
} {code}
h3. Actual behavior

The code compilers but I receive the following runtime exception
{code:java}
 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
{code}
h3. Expected behavior

The code should have been rejected with
{code:java}
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
 {code}
h3. Notes

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



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to