From 46cb6b3242cb42ec3f1fafb439215f31b64a2656 Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Thu, 1 Aug 2013 16:12:10 +0400 Subject: [PATCH] complete resolve for array access expression --- .../jet/lang/resolve/calls/CandidateResolver.java | 2 +- .../calls/context/ResolutionResultsCache.java | 15 +++++++++++++-- .../expressions/BasicExpressionTypingVisitor.java | 6 +++--- .../tests/dataFlowInfoTraversal/ArrayAccess.kt | 6 +++--- .../tests/inference/nestedCalls/arrayAccess.kt | 11 +++++++++++ .../jet/checkers/JetDiagnosticsTestGenerated.java | 5 +++++ 6 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/inference/nestedCalls/arrayAccess.kt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/CandidateResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/CandidateResolver.java index f7ad1c1e06b..7800e8e2152 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/CandidateResolver.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/CandidateResolver.java @@ -344,7 +344,7 @@ public class CandidateResolver { JetExpression keyExpression = getDeferredComputationKeyExpression(expression); CallCandidateResolutionContext storedContextForArgument = - context.resolutionResultsCache.getDeferredComputation(CallKey.create(Call.CallType.DEFAULT, keyExpression)); + context.resolutionResultsCache.getDeferredComputation(keyExpression); if (storedContextForArgument == null) { PsiElement parent = expression.getParent(); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/context/ResolutionResultsCache.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/context/ResolutionResultsCache.java index 244068bd6bb..2e99cb0a997 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/context/ResolutionResultsCache.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/context/ResolutionResultsCache.java @@ -16,12 +16,14 @@ package org.jetbrains.jet.lang.resolve.calls.context; +import com.google.common.collect.Lists; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.CallableDescriptor; import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; import org.jetbrains.jet.lang.descriptors.VariableDescriptor; import org.jetbrains.jet.lang.psi.CallKey; +import org.jetbrains.jet.lang.psi.JetExpression; import org.jetbrains.jet.lang.resolve.BindingTrace; import org.jetbrains.jet.lang.resolve.BindingTraceContext; import org.jetbrains.jet.lang.resolve.DelegatingBindingTrace; @@ -30,6 +32,8 @@ import org.jetbrains.jet.util.slicedmap.BasicWritableSlice; import org.jetbrains.jet.util.slicedmap.Slices; import org.jetbrains.jet.util.slicedmap.WritableSlice; +import static org.jetbrains.jet.lang.psi.Call.CallType; + public class ResolutionResultsCache { public static class MemberType {} @@ -81,8 +85,15 @@ public class ResolutionResultsCache { } @Nullable - public CallCandidateResolutionContext getDeferredComputation(@NotNull CallKey callKey) { - return trace.get(DEFERRED_COMPUTATION_FOR_CALL, callKey); + public CallCandidateResolutionContext getDeferredComputation(@NotNull JetExpression expression) { + for (CallType callType : Lists.newArrayList(CallType.DEFAULT, CallType.ARRAY_GET_METHOD, CallType.ARRAY_SET_METHOD)) { + CallKey callKey = CallKey.create(callType, expression); + CallCandidateResolutionContext context = trace.get(DEFERRED_COMPUTATION_FOR_CALL, callKey); + if (context != null) { + return context; + } + } + return null; } @NotNull diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java index 3172ee6ae31..325114c9272 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java @@ -1139,7 +1139,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor { @Override public JetTypeInfo visitArrayAccessExpression(JetArrayAccessExpression expression, ExpressionTypingContext context) { - JetTypeInfo typeInfo = resolveArrayAccessGetMethod(expression, context.replaceExpectedType(NO_EXPECTED_TYPE)); + JetTypeInfo typeInfo = resolveArrayAccessGetMethod(expression, context); return DataFlowUtils.checkType(typeInfo.getType(), expression, context, typeInfo.getDataFlowInfo()); } @@ -1289,7 +1289,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor { JetExpression arrayExpression = arrayAccessExpression.getArrayExpression(); if (arrayExpression == null) return JetTypeInfo.create(null, oldContext.dataFlowInfo); - JetTypeInfo arrayTypeInfo = facade.getTypeInfo(arrayExpression, oldContext); + JetTypeInfo arrayTypeInfo = facade.getTypeInfo(arrayExpression, oldContext.replaceExpectedType(NO_EXPECTED_TYPE)); JetType arrayType = arrayTypeInfo.getType(); if (arrayType == null) return arrayTypeInfo; @@ -1315,7 +1315,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor { dataFlowInfo = facade.getTypeInfo(rightHandSide, context.replaceDataFlowInfo(dataFlowInfo)).getDataFlowInfo(); } - if (!functionResults.isSuccess()) { + if (!functionResults.isSingleResult()) { traceForResolveResult.report(isGet ? NO_GET_METHOD.on(arrayAccessExpression) : NO_SET_METHOD.on(arrayAccessExpression)); return JetTypeInfo.create(null, dataFlowInfo); } diff --git a/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/ArrayAccess.kt b/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/ArrayAccess.kt index 3d178c3f5dd..c7cb7312883 100644 --- a/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/ArrayAccess.kt +++ b/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/ArrayAccess.kt @@ -4,10 +4,10 @@ fun foo() { val x: Int? = null val a = Array(3, {0}) - if (x != null) bar(a[x]) else bar(a[x]) + if (x != null) bar(a[x]) else bar(a[x]) bar(a[if (x == null) 0 else x]) - bar(a[x]) + bar(a[x]) - "123"[x]; + "123"[x]; if (x != null) "123"[x]; } diff --git a/compiler/testData/diagnostics/tests/inference/nestedCalls/arrayAccess.kt b/compiler/testData/diagnostics/tests/inference/nestedCalls/arrayAccess.kt new file mode 100644 index 00000000000..af64eb4f064 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/nestedCalls/arrayAccess.kt @@ -0,0 +1,11 @@ +package b + +class A { + fun get(i: Int): List = throw Exception("$i") +} + +fun bar(l: List) = l + +fun test(a: A) { + bar(a[12]) +} diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index d35e225b977..277a707ab2b 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -2846,6 +2846,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/diagnostics/tests/inference/nestedCalls"), Pattern.compile("^(.+)\\.kt$"), true); } + @TestMetadata("arrayAccess.kt") + public void testArrayAccess() throws Exception { + doTest("compiler/testData/diagnostics/tests/inference/nestedCalls/arrayAccess.kt"); + } + @TestMetadata("checkTypesForQualifiedProperties.kt") public void testCheckTypesForQualifiedProperties() throws Exception { doTest("compiler/testData/diagnostics/tests/inference/nestedCalls/checkTypesForQualifiedProperties.kt");