new inference: do not accept varargs when array is passed there

This commit is contained in:
Anna Kozlova
2014-02-20 17:08:17 +01:00
parent 9fc4b68c25
commit de7f053c42
3 changed files with 33 additions and 3 deletions

View File

@@ -173,9 +173,25 @@ public class InferenceSession {
private static PsiType getParameterType(PsiParameter[] parameters, PsiExpression[] args, int i, PsiSubstitutor substitutor) {
PsiType parameterType = substitutor.substitute(parameters[i < parameters.length ? i : parameters.length - 1].getType());
if (parameterType instanceof PsiEllipsisType) {
if (args.length != parameters.length ||
PsiPolyExpressionUtil.isPolyExpression(args[i]) ||
args[i] != null && !(args[i].getType() instanceof PsiArrayType)) {
final PsiExpression arg = args[i];
if (arg instanceof PsiNewExpression) {
if (((PsiNewExpression)arg).getArrayDimensions().length == parameterType.getArrayDimensions() || ((PsiNewExpression)arg).getArrayInitializer() != null) {
return parameterType;
}
}
if (arg instanceof PsiMethodCallExpression) {
final PsiMethod method = ((PsiMethodCallExpression)arg).resolveMethod();
if (method != null) {
final PsiType returnType = method.getReturnType();
if (returnType != null && returnType.getArrayDimensions() == parameterType.getArrayDimensions()) {
return parameterType;
}
}
}
if (args.length != parameters.length ||
PsiPolyExpressionUtil.isPolyExpression(arg) ||
arg != null && !(arg.getType() instanceof PsiArrayType)) {
parameterType = ((PsiEllipsisType)parameterType).getComponentType();
}
}

View File

@@ -0,0 +1,10 @@
import java.util.Set;
class FooBar {
Set<String> strings = null;
{
bar(strings.toArray(new String[strings.size()]));
}
void bar(String... a){}
}

View File

@@ -136,6 +136,10 @@ public class GraphInferenceHighlightingTest extends LightDaemonAnalyzerTestCase
doTest();
}
public void testArrayPassedToVarargsMethod() throws Exception {
doTest();
}
private void doTest() throws Exception {
doTest(false);
}