[NI] Fix exception on incomplete try/catch block

There was an inconsistency on creating PSI call and corresponding
 descriptor. See variable `catchBlocks` from visitor, it's created
 only if PSI elements are not null, but for descriptor parameters
 there wasn't such check

 #KT-32134 Fixed
 #EA-139748 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2019-07-01 01:49:13 +03:00
parent 42a5c488c1
commit 515e666733
7 changed files with 48 additions and 8 deletions
@@ -9297,6 +9297,11 @@ public class FirDiagnosticsSmokeTestGenerated extends AbstractFirDiagnosticsSmok
runTest("compiler/testData/diagnostics/tests/incompleteCode/incompleteEquals.kt");
}
@TestMetadata("incompleteTryCatchBlock.kt")
public void testIncompleteTryCatchBlock() throws Exception {
runTest("compiler/testData/diagnostics/tests/incompleteCode/incompleteTryCatchBlock.kt");
}
@TestMetadata("kt1955.kt")
public void testKt1955() throws Exception {
runTest("compiler/testData/diagnostics/tests/incompleteCode/kt1955.kt");
@@ -123,24 +123,30 @@ public class ControlStructureTypingUtils {
/*package*/ ResolvedCall<FunctionDescriptor> resolveTryAsCall(
@NotNull Call call,
@NotNull KtTryExpression tryExpression,
@NotNull List<Pair<KtExpression, VariableDescriptor>> catchedExceptions,
@NotNull ExpressionTypingContext context,
@Nullable MutableDataFlowInfoForArguments dataFlowInfoForArguments
) {
List<String> argumentNames = Lists.newArrayList("tryBlock");
List<Boolean> argumentsNullability = Lists.newArrayList(false);
for (int i = 0; i < tryExpression.getCatchClauses().size(); i++) {
argumentNames.add("catchBlock" + i);
argumentsNullability.add(false);
}
SimpleFunctionDescriptorImpl function =
createFunctionDescriptorForSpecialConstruction(ResolveConstruct.TRY, argumentNames, argumentsNullability);
int counter = 0;
for (Pair<KtExpression, VariableDescriptor> descriptorPair : catchedExceptions) {
// catchedExceptions are corresponding to PSI arguments that were used to create a call
// therefore, it's important to use only them to have consistent parameters
argumentNames.add("catchBlock" + counter);
argumentsNullability.add(false);
KtExpression catchBlock = descriptorPair.getFirst();
VariableDescriptor catchedExceptionDescriptor = descriptorPair.getSecond();
context.trace.record(BindingContext.NEW_INFERENCE_CATCH_EXCEPTION_PARAMETER, catchBlock, Ref.create(catchedExceptionDescriptor));
counter++;
}
SimpleFunctionDescriptorImpl function =
createFunctionDescriptorForSpecialConstruction(ResolveConstruct.TRY, argumentNames, argumentsNullability);
return resolveSpecialConstructionAsCall(call, function, ResolveConstruct.TRY, context, dataFlowInfoForArguments);
}
@@ -611,7 +611,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
dataFlowInfoForArguments = createDataFlowInfoForArgumentsOfTryCall(callForTry, dataFlowInfoBeforeTry, dataFlowInfoBeforeTry);
}
ResolvedCall<FunctionDescriptor> resolvedCall = components.controlStructureTypingUtils
.resolveTryAsCall(callForTry, tryExpression, catchClausesBlocksAndParameters, tryInputContext, dataFlowInfoForArguments);
.resolveTryAsCall(callForTry, catchClausesBlocksAndParameters, tryInputContext, dataFlowInfoForArguments);
KotlinType resultType = resolvedCall.getResultingDescriptor().getReturnType();
BindingContext bindingContext = tryInputContext.trace.getBindingContext();
@@ -0,0 +1,14 @@
fun test1() {
try {
} catch (<!SYNTAX!><!>)<!SYNTAX!><!>
}
fun test2() {
try { }<!SYNTAX!><!>
}
fun test3() {
try {
} catch (<!SYNTAX!><!>{}<!SYNTAX!>)<!> <!UNUSED_LAMBDA_EXPRESSION!>{}<!>
}
@@ -0,0 +1,5 @@
package
public fun test1(): kotlin.Unit
public fun test2(): kotlin.Unit
public fun test3(): kotlin.Unit
@@ -9304,6 +9304,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
runTest("compiler/testData/diagnostics/tests/incompleteCode/incompleteEquals.kt");
}
@TestMetadata("incompleteTryCatchBlock.kt")
public void testIncompleteTryCatchBlock() throws Exception {
runTest("compiler/testData/diagnostics/tests/incompleteCode/incompleteTryCatchBlock.kt");
}
@TestMetadata("kt1955.kt")
public void testKt1955() throws Exception {
runTest("compiler/testData/diagnostics/tests/incompleteCode/kt1955.kt");
@@ -9299,6 +9299,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/incompleteCode/incompleteEquals.kt");
}
@TestMetadata("incompleteTryCatchBlock.kt")
public void testIncompleteTryCatchBlock() throws Exception {
runTest("compiler/testData/diagnostics/tests/incompleteCode/incompleteTryCatchBlock.kt");
}
@TestMetadata("kt1955.kt")
public void testKt1955() throws Exception {
runTest("compiler/testData/diagnostics/tests/incompleteCode/kt1955.kt");