[ 
https://issues.apache.org/jira/browse/GROOVY-11437?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Eric Milles updated GROOVY-11437:
---------------------------------
    Language: groovy

> incorrect typing in nested ternary operator leads to type unsafety
> ------------------------------------------------------------------
>
>                 Key: GROOVY-11437
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11437
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>            Reporter: Thodoris Sotiropoulos
>            Priority: Minor
>
> I have the following program, which leads to an unexpected typing result 
> during an LUB operation.
> {code:java}
> import java.time.*;
> import java.time.chrono.*;
> class Test {
>   public static void main(String[] args) {
>     LocalDateTime now = LocalDateTime.now();
>     int x = 1;
>     ((false ? 1 : 
>         (false ? x : (ChronoLocalDateTime<LocalDate>) now)) >= x)
>   }
> }{code}
> h3. Actual behavior
> The code compiles, but I receive the following ClassCastException
> {code:java}
> Exception in thread "main" java.lang.IllegalArgumentException: Cannot compare 
> java.time.LocalDateTime with value '2024-07-08T17:00:53.307529' and 
> java.lang.Integer with value '1'
>     at 
> org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.compareToWithEqualityCheck(DefaultTypeTransformation.java:815)
>     at 
> org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.compareTo(DefaultTypeTransformation.java:750)
>     at 
> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.compareTo(ScriptBytecodeAdapter.java:802)
>     at 
> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.compareGreaterThanEqual(ScriptBytecodeAdapter.java:915)
>     at Test.main(test.groovy:9)
> Caused by: java.lang.ClassCastException: class java.lang.Integer cannot be 
> cast to class java.time.chrono.ChronoLocalDateTime (java.lang.Integer and 
> java.time.chrono.ChronoLocalDateTime are in module java.base of loader 
> 'bootstrap')
>     at java.base/java.time.LocalDateTime.compareTo(LocalDateTime.java:135)
>     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:1227)
>     at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1006)
>     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:801)
>     ... 4 more {code}
> h3. Expected behavior
> The code should have been rejected.
> Tested against master (commit: c773e04ce402816b8f92c0318d08bde32e723eed)



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

Reply via email to