[
https://issues.apache.org/jira/browse/GROOVY-9985?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17302536#comment-17302536
]
Paul King edited comment on GROOVY-9985 at 3/16/21, 1:47 PM:
-------------------------------------------------------------
There is a PR for the array initialization case:
[https://github.com/apache/groovy/pull/1520]
The +{{as}}+ coercion is a little more difficult since it is currently treated
like an escape hatch for static checking. It isn't wired in even for non-array
cases, e.g.:
{code:java}
import groovy.transform.TypeChecked
import org.codehaus.groovy.runtime.typehandling.GroovyCastException
import static groovy.test.GroovyAssert.shouldFail
@TypeChecked
def method() {
def x = '42' as Integer // <1>
assert x instanceof Integer && x.toString() == '42'
shouldFail(GroovyCastException) {
def y = '42' as Date // <2>
}
}
method()
{code}
For case <1>, dynamic Groovy coerces the String to a number when the explicit
+{{as}}+ is present, so static checking lets this through to accommodate this
common Groovy idiom. For case <2>, there is no String to Date coercion, at
least not built in but the escape hatch just lets it through anyway. Obviously
we could make the static checking smarter and know about the allowable built-in
coercions at least. Once enabled, wiring into the array case if needed would be
relatively straight forward.
I am inclined to split this issue in two and cover both cases separately.
Thoughts?
was (Author: paulk):
There is a PR for the array initialization case:
[https://github.com/apache/groovy/pull/1520]
The +{{as}}+ coercion is a little more difficult since it is currently treated
like an escape hatch for static checking. It isn't wired in even for non-array
cases, e.g.:
{code:java}
import groovy.transform.TypeChecked
import org.codehaus.groovy.runtime.typehandling.GroovyCastException
import static groovy.test.GroovyAssert.shouldFail
@TypeChecked
def method() {
def x = '42' as Integer // <1>
assert x instanceof Integer && x.toString() == '42'
shouldFail(GroovyCastException) {
def y = '42' as Date // <2>
}
}
method()
{code}
For case <1>, dynamic Groovy coerces the String to a number when the explicit
+{{as}}+ is present, so static checking lets this through to accommodate this
common Groovy idiom. For case <2>, there is no String to Date coercion, at
least not built in but the escape hatch just lets it through anyway. Obviously
we could make the static checking smarter and know about the allowable built-in
coercions at least. Once enabled, wiring into the array case if needed would be
relatively straight forward.
> 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
> Assignee: Paul King
> Priority: Major
> Time Spent: 10m
> Remaining Estimate: 0h
>
> 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)