You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2021/03/24 14:21:01 UTC
[jira] [Comment Edited] (GROOVY-9994) Type checking of array
initializer expressions
[ https://issues.apache.org/jira/browse/GROOVY-9994?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17307842#comment-17307842 ]
Eric Milles edited comment on GROOVY-9994 at 3/24/21, 2:20 PM:
---------------------------------------------------------------
Also the size expressions for "new Object[x][y][z]" are not type-checked and may result in GroovyCastException at runtime.
Here is a quick change that will check size exprs:
{code:java}
@Override
public void visitArrayExpression(final ArrayExpression expression) {
super.visitArrayExpression(expression);
// GROOVY-9985, GROOVY-9994
ClassNode elementType;
List<Expression> expressions;
if (expression.hasInitializer()) {
elementType = expression.getElementType();
expressions = expression.getExpressions();
} else {
elementType = int_TYPE;
expressions = expression.getSizeExpression();
}
for (Expression elementExpr : expressions) {
if (!checkCast(elementType, elementExpr)) {
addStaticTypeError("Cannot convert from " + prettyPrintType(getType(elementExpr)) + " to " + prettyPrintType(elementType), elementExpr);
}
}
}
{code}
I could split this out to a separate issue and add pull request or it could be incorporated into the fix for this ticket.
was (Author: emilles):
Also the size expressions for "new Object[x][y][z]" are not type-checked and may result in GroovyCastException at runtime.
> Type checking of array initializer expressions
> ----------------------------------------------
>
> Key: GROOVY-9994
> URL: https://issues.apache.org/jira/browse/GROOVY-9994
> Project: Groovy
> Issue Type: Bug
> Reporter: Thodoris Sotiropoulos
> Priority: Major
>
> This is a follow up of https://issues.apache.org/jira/browse/GROOVY-9985
> After the bug fix of the above GROOVY-9985, STC indeed catches the type error in the following program
> {code:java}
> public class Main {
> public static void main(String[] args) {
> Integer[] arr = new Integer[] {"fd" 1}; // compile-time error here
> Integer y = arr[0];
> }
> }
> {code}
> However, STC still is not able to catch type errors like the following
> {code:java}
> public class Main {
> public static void main(String[] args) {
> Integer[] arr = new Integer[] {new Object(), 1}; // STC misses the error here
> Integer y = arr[0]; // ClassCastException at runtime
> }
>
> }
> {code}
>
--
This message was sent by Atlassian Jira
(v8.3.4#803005)