[
https://issues.apache.org/jira/browse/GROOVY-9985?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17301650#comment-17301650
]
Paul King edited comment on GROOVY-9985 at 3/16/21, 5:08 AM:
-------------------------------------------------------------
Just adding a currently working case for reference:
{code}
@groovy.transform.CompileStatic
class Main {
static void main(args) {
Integer[] arr3 = [1, "fda"] // compile error as expected:
// [Static type checking] - Cannot assign value of type java.lang.String
into array of type java.lang.Integer[]
}
}
{code}
Also, these are correctly detected as errors:
{code}
Integer[] arr = new String[]{1, "str"}
Integer[] arr2 = [1, "fda"] as String[]
{code}
So, the assignment is correctly checked, just not inside the cast expression or
array initializer expression.
was (Author: paulk):
Just adding a currently working case for reference:
{code}
@groovy.transform.CompileStatic
class Main {
static void main(args) {
Integer[] arr3 = [1, "fda"] // compile error as expected:
// [Static type checking] - Cannot assign value of type java.lang.String
into array of type java.lang.Integer[]
}
}
{code}
> STC does not report type mismatches in array initializers
> ---------------------------------------------------------
>
> Key: GROOVY-9985
> URL: https://issues.apache.org/jira/browse/GROOVY-9985
> Project: Groovy
> Issue Type: Bug
> Components: Static Type Checker
> Reporter: Thodoris Sotiropoulos
> Priority: Major
>
> The following program compiles (with @CompileStatic), although it shouldn't.
> {code:java}
> public class Main {
> public static void main(String[] args) {
> Integer[] arr = new Integer[]{1, "str"}; // does not report error
> Integer[] arr2 = [1, "fda"] as Integer[] // does not report error
> }
> }
> {code}
> As a result, an exception is thrown at runtime
> {code:java}
> Exception in thread "main"
> org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast
> object 'str' with class 'java.lang.String' to class 'java.lang.Integer'
> at
> org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToNumber(DefaultTypeTransformation.java:172)
> at
> org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:282)
> at
> org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:243)
> at
> org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:318)
> at Main.main(test.groovy:4)
> {code}
> Tested on
> [https://github.com/apache/groovy/commit/f0eea862549529ef4e93fafe337f86dd4ac98751]
>
--
This message was sent by Atlassian Jira
(v8.3.4#803005)