diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java index ffc4eeafee9..62035795f91 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java @@ -8017,6 +8017,11 @@ public class FirDiagnosticsSmokeTestGenerated extends AbstractFirDiagnosticsSmok runTest("compiler/testData/diagnostics/tests/functionLiterals/LabeledFunctionLiterals.kt"); } + @TestMetadata("prematurelyAnalyzingLambdaWhileFixingTypeVariableForAnotherArgument.kt") + public void testPrematurelyAnalyzingLambdaWhileFixingTypeVariableForAnotherArgument() throws Exception { + runTest("compiler/testData/diagnostics/tests/functionLiterals/prematurelyAnalyzingLambdaWhileFixingTypeVariableForAnotherArgument.kt"); + } + @TestMetadata("returnNull.kt") public void testReturnNull() throws Exception { runTest("compiler/testData/diagnostics/tests/functionLiterals/returnNull.kt"); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceSession.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceSession.kt index 0602c5faf68..49e1860469f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceSession.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceSession.kt @@ -206,7 +206,7 @@ class CoroutineInferenceSession( val atomCompleter = createResolvedAtomCompleter(resultingSubstitutor, completedCall.context) val resultCallAtom = completedCall.callResolutionResult.resultCallAtom - for (subResolvedAtom in resultCallAtom.subResolvedAtoms) { + resultCallAtom.subResolvedAtoms?.forEach { subResolvedAtom -> atomCompleter.completeAll(subResolvedAtom) } atomCompleter.completeResolvedCall(resultCallAtom, completedCall.callResolutionResult.diagnostics) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt index cb93c7805ff..8205c13664a 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt @@ -139,7 +139,7 @@ class KotlinToResolvedCallTransformer( ) if (!ErrorUtils.isError(candidate.candidateDescriptor)) { - for (subKtPrimitive in candidate.subResolvedAtoms) { + candidate.subResolvedAtoms?.forEach { subKtPrimitive -> ktPrimitiveCompleter.completeAll(subKtPrimitive) } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt index 9a61285badb..6b5be5b10ed 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt @@ -73,7 +73,7 @@ class ResolvedAtomCompleter( } fun completeAll(resolvedAtom: ResolvedAtom) { - for (subKtPrimitive in resolvedAtom.subResolvedAtoms) { + resolvedAtom.subResolvedAtoms?.forEach { subKtPrimitive -> completeAll(subKtPrimitive) } complete(resolvedAtom) diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt index 0417ce56d6d..64ced81aa25 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt @@ -145,7 +145,7 @@ class KotlinConstraintSystemCompleter( to.addIfNotNull(this.safeAs()?.takeUnless { it.analyzed }) if (analyzed) { - subResolvedAtoms.forEach { it.process(to) } + subResolvedAtoms?.forEach { it.process(to) } } } @@ -177,7 +177,7 @@ class KotlinConstraintSystemCompleter( } if (analyzed) { - subResolvedAtoms.forEach { it.process(to) } + subResolvedAtoms?.forEach { it.process(to) } } } @@ -257,7 +257,7 @@ class KotlinConstraintSystemCompleter( return this } - subResolvedAtoms.forEach { subResolvedAtom -> + subResolvedAtoms?.forEach { subResolvedAtom -> subResolvedAtom.check()?.let { result -> return@check result } } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolutionAtoms.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolutionAtoms.kt index 6c9301a76a1..c737c7ef7a0 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolutionAtoms.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolutionAtoms.kt @@ -37,7 +37,7 @@ sealed class ResolvedAtom { var analyzed: Boolean = false private set - lateinit var subResolvedAtoms: List + var subResolvedAtoms: List? = null private set protected open fun setAnalyzedResults(subResolvedAtoms: List) { diff --git a/compiler/testData/diagnostics/tests/functionLiterals/prematurelyAnalyzingLambdaWhileFixingTypeVariableForAnotherArgument.kt b/compiler/testData/diagnostics/tests/functionLiterals/prematurelyAnalyzingLambdaWhileFixingTypeVariableForAnotherArgument.kt new file mode 100644 index 00000000000..092ff35c70c --- /dev/null +++ b/compiler/testData/diagnostics/tests/functionLiterals/prematurelyAnalyzingLambdaWhileFixingTypeVariableForAnotherArgument.kt @@ -0,0 +1,14 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER +// !LANGUAGE: +NewInference +// SKIP_TXT +// Issue: KT-35168 + +class Inv + +// Before the fix, here we had an exception while fixing a type variable for Inv due to prematurely analyzing a lambda +// The exception was: "UninitializedPropertyAccessException: lateinit property subResolvedAtoms has not been initialized" +class Foo(x: (T) -> T, y: Inv) + +fun bar() { + Foo({}, Inv()) +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index d1368033271..08f8ad2612f 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -8024,6 +8024,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { runTest("compiler/testData/diagnostics/tests/functionLiterals/LabeledFunctionLiterals.kt"); } + @TestMetadata("prematurelyAnalyzingLambdaWhileFixingTypeVariableForAnotherArgument.kt") + public void testPrematurelyAnalyzingLambdaWhileFixingTypeVariableForAnotherArgument() throws Exception { + runTest("compiler/testData/diagnostics/tests/functionLiterals/prematurelyAnalyzingLambdaWhileFixingTypeVariableForAnotherArgument.kt"); + } + @TestMetadata("returnNull.kt") public void testReturnNull() throws Exception { runTest("compiler/testData/diagnostics/tests/functionLiterals/returnNull.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index 24b26c3dcbd..956e0e6a37e 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -8019,6 +8019,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/functionLiterals/LabeledFunctionLiterals.kt"); } + @TestMetadata("prematurelyAnalyzingLambdaWhileFixingTypeVariableForAnotherArgument.kt") + public void testPrematurelyAnalyzingLambdaWhileFixingTypeVariableForAnotherArgument() throws Exception { + runTest("compiler/testData/diagnostics/tests/functionLiterals/prematurelyAnalyzingLambdaWhileFixingTypeVariableForAnotherArgument.kt"); + } + @TestMetadata("returnNull.kt") public void testReturnNull() throws Exception { runTest("compiler/testData/diagnostics/tests/functionLiterals/returnNull.kt"); diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/collections/FunctionUtils.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/collections/FunctionUtils.kt index 1ca976a1569..366f9483224 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/collections/FunctionUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/collections/FunctionUtils.kt @@ -72,7 +72,7 @@ fun ResolvedCall<*>.hasLastFunctionalParameterWithResult(context: BindingContext val lastArgument = valueArguments[lastParameter]?.arguments?.singleOrNull() ?: return false if (this is NewResolvedCallImpl<*>) { // TODO: looks like hack - resolvedCallAtom.subResolvedAtoms.firstOrNull { it is ResolvedLambdaAtom }.safeAs()?.let { lambdaAtom -> + resolvedCallAtom.subResolvedAtoms?.firstOrNull { it is ResolvedLambdaAtom }.safeAs()?.let { lambdaAtom -> return lambdaAtom.resultArguments.filterIsInstance().all { val type = it.receiverValue?.type ?: return@all false predicate(type)