From 2ca2154a8a31e09a233f5933bb973f9738156bc9 Mon Sep 17 00:00:00 2001 From: Maxim Medvedev Date: Wed, 4 Aug 2010 11:21:09 +0400 Subject: [PATCH] IDEA-57101 Groovy array member access is untyped --- .../expressions/path/GrIndexPropertyImpl.java | 14 +++++++------- .../groovy/lang/resolve/TypeInferenceTest.java | 8 ++++++++ .../resolve/inference/arrayAccess/A.groovy | 7 +++++++ 3 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 plugins/groovy/testdata/resolve/inference/arrayAccess/A.groovy diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/path/GrIndexPropertyImpl.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/path/GrIndexPropertyImpl.java index 4e9f16abf393..9d9a610c2eb7 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/path/GrIndexPropertyImpl.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/path/GrIndexPropertyImpl.java @@ -75,7 +75,12 @@ public class GrIndexPropertyImpl extends GrExpressionImpl implements GrIndexProp argTypes[i] = argType; } - if (thisType instanceof GrTupleType && + if (thisType instanceof PsiArrayType && + argTypes.length == 1 && + TypesUtil.isAssignable(PsiType.INT, argTypes[0], getManager(), getResolveScope())) { + return TypesUtil.boxPrimitiveType(((PsiArrayType)thisType).getComponentType(), getManager(), getResolveScope()); + } + else if (thisType instanceof GrTupleType && argTypes.length == 1 && TypesUtil.isAssignable(PsiType.INT, argTypes[0], getManager(), getResolveScope())) { PsiType[] types = ((GrTupleType)thisType).getParameters(); @@ -96,12 +101,7 @@ public class GrIndexPropertyImpl extends GrExpressionImpl implements GrIndexProp } PsiType componentType = null; - if (thisType instanceof PsiArrayType && - argTypes.length == 1 && - TypesUtil.isAssignable(PsiType.INT, argTypes[0], getManager(), getResolveScope())) { - componentType = TypesUtil.boxPrimitiveType(((PsiArrayType)thisType).getComponentType(), getManager(), getResolveScope()); - } - else if (InheritanceUtil.isInheritor(thisType, CommonClassNames.JAVA_UTIL_MAP) && argTypes.length == 1) { + if (InheritanceUtil.isInheritor(thisType, CommonClassNames.JAVA_UTIL_MAP) && argTypes.length == 1) { componentType = TypesUtil .boxPrimitiveType(substituteTypeParameter(thisType, CommonClassNames.JAVA_UTIL_MAP, 1, true), getManager(), getResolveScope()); } diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/TypeInferenceTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/TypeInferenceTest.java index bfe4adfa098a..42afcc4f608c 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/TypeInferenceTest.java +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/TypeInferenceTest.java @@ -15,11 +15,14 @@ */ package org.jetbrains.plugins.groovy.lang.resolve; +import com.intellij.psi.CommonClassNames; import com.intellij.psi.PsiIntersectionType; +import com.intellij.psi.PsiReference; import com.intellij.psi.PsiType; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression; import org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType; import org.jetbrains.plugins.groovy.util.TestUtils; +import org.jmock.lib.CamelCaseNamingScheme; /** * @author ven @@ -122,4 +125,9 @@ public class TypeInferenceTest extends GroovyResolveTestCase { final GrReferenceExpression ref = (GrReferenceExpression)configureByFile("arrayLikeAccessWithIntSequence/A.groovy").getElement(); assertEquals("java.util.List", ref.getType().getCanonicalText()); } + + public void testArrayAccess() { + final GrReferenceExpression ref = (GrReferenceExpression)configureByFile("arrayAccess/A.groovy"); + assertEquals(CommonClassNames.JAVA_LANG_STRING, ref.getType().getCanonicalText()); + } } diff --git a/plugins/groovy/testdata/resolve/inference/arrayAccess/A.groovy b/plugins/groovy/testdata/resolve/inference/arrayAccess/A.groovy new file mode 100644 index 000000000000..4ae80232e1c1 --- /dev/null +++ b/plugins/groovy/testdata/resolve/inference/arrayAccess/A.groovy @@ -0,0 +1,7 @@ +def foo(String[] a) { + def var = a[0] + + print var +} + +def bar(String a) {} \ No newline at end of file