Skip to content

Commit c262b97

Browse files
mglukhikhSpace Team
authored and
Space Team
committed
K1: introduce BUILDER_INFERENCE_STUB_PARAMETER_TYPE to prevent compiler crashes
This diagnostic is reported in rare situations when StubTypeForBuilderInference is kept as a parameter type of for loop or lambda. Before this commit, we had in K1 "Could not load module <error module>" from IrLinker instead. Related to: KT-52757, KT-53109, KT-63841, KT-64066 #KT-53478 Fixed (cherry picked from commit 678816f)
1 parent 1deb116 commit c262b97

File tree

33 files changed

+661
-7
lines changed

33 files changed

+661
-7
lines changed

analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java

+1
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,7 @@ enum BadNamedArgumentsTarget {
952952
DiagnosticFactory4<PsiElement, KotlinType, String, String, BuilderLambdaLabelingInfo> STUB_TYPE_IN_RECEIVER_CAUSES_AMBIGUITY = DiagnosticFactory4.create(ERROR);
953953
DiagnosticFactory2<PsiElement, Name, Name> BUILDER_INFERENCE_MULTI_LAMBDA_RESTRICTION = DiagnosticFactory2.create(ERROR);
954954
DiagnosticFactory2<PsiElement, Name, Name> BUILDER_INFERENCE_STUB_RECEIVER = DiagnosticFactory2.create(ERROR);
955+
DiagnosticFactory1<KtDeclaration, Name> BUILDER_INFERENCE_STUB_PARAMETER_TYPE = DiagnosticFactory1.create(ERROR);
955956
DiagnosticFactory4<KtElement, Name, Name, KotlinType, KotlinType> UPPER_BOUND_VIOLATION_IN_CONSTRAINT = DiagnosticFactory4.create(ERROR);
956957

957958
DiagnosticFactory1<PsiElement, Collection<? extends ResolvedCall<?>>> NONE_APPLICABLE = DiagnosticFactory1.create(ERROR);

compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java

+1
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,7 @@ public static DiagnosticRenderer getRendererForDiagnostic(@NotNull UnboundDiagno
11051105
MAP.put(STUB_TYPE_IN_RECEIVER_CAUSES_AMBIGUITY, "The type of a receiver hasn''t been inferred yet. To disambiguate this call, explicitly cast it to `{0}` if you want the builder''s type parameter(s) `{1}` to be inferred to `{2}`.", RENDER_TYPE, STRING, STRING, null);
11061106
MAP.put(BUILDER_INFERENCE_MULTI_LAMBDA_RESTRICTION, "Unstable inference behaviour with multiple lambdas. Please either specify the type argument for generic parameter `{0}` of `{1}` explicitly", TO_STRING, TO_STRING);
11071107
MAP.put(BUILDER_INFERENCE_STUB_RECEIVER, "The type of a receiver hasn''t been inferred yet. Please specify type argument for generic parameter `{0}` of `{1}` explicitly", TO_STRING, TO_STRING);
1108+
MAP.put(BUILDER_INFERENCE_STUB_PARAMETER_TYPE, "The type of parameter `{0}` cannot be inferred by builder inference", TO_STRING);
11081109
MAP.put(UPPER_BOUND_VIOLATION_IN_CONSTRAINT, "Upper bound violation for generic parameter `{0}` of `{1}`: {3} is not a subtype of {2}", TO_STRING, TO_STRING, RENDER_TYPE, RENDER_TYPE);
11091110
MAP.put(NONE_APPLICABLE, "None of the following functions can be called with the arguments supplied: {0}", AMBIGUOUS_CALLS);
11101111
MAP.put(CANNOT_COMPLETE_RESOLVE, "Cannot choose among the following candidates without completing type inference: {0}", AMBIGUOUS_CALLS);

compiler/frontend/src/org/jetbrains/kotlin/resolve/PlatformConfiguratorBase.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ private val DEFAULT_DECLARATION_CHECKERS = listOf(
6262
EnumEntriesRedeclarationChecker,
6363
VolatileAnnotationChecker,
6464
ActualTypealiasToSpecialAnnotationChecker,
65+
StubForBuilderInferenceParameterTypeChecker,
6566
)
6667

6768
private val DEFAULT_CALL_CHECKERS = listOf(
@@ -80,7 +81,7 @@ private val DEFAULT_CALL_CHECKERS = listOf(
8081
NewSchemeOfIntegerOperatorResolutionChecker, EnumEntryVsCompanionPriorityCallChecker, CompanionInParenthesesLHSCallChecker,
8182
ResolutionToPrivateConstructorOfSealedClassChecker, EqualityCallChecker, UnsupportedUntilOperatorChecker,
8283
BuilderInferenceAssignmentChecker, IncorrectCapturedApproximationCallChecker, CompanionIncorrectlyUnboundedWhenUsedAsLHSCallChecker,
83-
CustomEnumEntriesMigrationCallChecker, EnumEntriesUnsupportedChecker
84+
CustomEnumEntriesMigrationCallChecker, EnumEntriesUnsupportedChecker,
8485
)
8586
private val DEFAULT_TYPE_CHECKERS = emptyList<AdditionalTypeChecker>()
8687
private val DEFAULT_CLASSIFIER_USAGE_CHECKERS = listOf(

compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/BuilderInferenceAssignmentChecker.kt

+17-6
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import org.jetbrains.kotlin.descriptors.PropertyDescriptor
1111
import org.jetbrains.kotlin.diagnostics.Errors
1212
import org.jetbrains.kotlin.lexer.KtTokens
1313
import org.jetbrains.kotlin.psi.KtBinaryExpression
14+
import org.jetbrains.kotlin.psi.KtLambdaExpression
1415
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
1516
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
17+
import org.jetbrains.kotlin.resolve.BindingContext
1618
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
1719
import org.jetbrains.kotlin.resolve.calls.util.getType
1820
import org.jetbrains.kotlin.types.StubTypeForBuilderInference
@@ -24,18 +26,27 @@ object BuilderInferenceAssignmentChecker : CallChecker {
2426
val resultingDescriptor = resolvedCall.resultingDescriptor
2527
if (resultingDescriptor !is PropertyDescriptor) return
2628
if (context.languageVersionSettings.supportsFeature(LanguageFeature.NoBuilderInferenceWithoutAnnotationRestriction)) return
27-
if (resolvedCall.candidateDescriptor.returnType !is StubTypeForBuilderInference) return
2829
val callElement = resolvedCall.call.callElement
2930
if (callElement !is KtNameReferenceExpression) return
3031
val binaryExpression = callElement.getParentOfType<KtBinaryExpression>(strict = true) ?: return
3132
if (binaryExpression.operationToken != KtTokens.EQ) return
3233
if (!BasicExpressionTypingVisitor.isLValue(callElement, binaryExpression)) return
33-
34-
val leftType = resultingDescriptor.returnType?.takeIf { !it.isError } ?: return
3534
val right = binaryExpression.right ?: return
36-
val rightType = right.getType(context.trace.bindingContext) ?: return
3735

38-
if (isAssignmentCorrectWithDataFlowInfo(leftType, right, rightType, context)) return
39-
context.trace.report(Errors.TYPE_MISMATCH.on(right, leftType, rightType))
36+
if (resolvedCall.candidateDescriptor.returnType is StubTypeForBuilderInference) {
37+
val leftType = resultingDescriptor.returnType?.takeIf { !it.isError } ?: return
38+
val rightType = right.getType(context.trace.bindingContext) ?: return
39+
40+
if (isAssignmentCorrectWithDataFlowInfo(leftType, right, rightType, context)) return
41+
context.trace.report(Errors.TYPE_MISMATCH.on(right, leftType, rightType))
42+
} else if (right is KtLambdaExpression) {
43+
val functionLiteral = right.functionLiteral
44+
val functionDescriptor = context.trace.get(BindingContext.FUNCTION, right.functionLiteral) ?: return
45+
for ((index, valueParameterDescriptor) in functionDescriptor.valueParameters.withIndex()) {
46+
if (valueParameterDescriptor.type !is StubTypeForBuilderInference) continue
47+
val target = functionLiteral.valueParameters.getOrNull(index) ?: functionLiteral
48+
context.trace.report(Errors.BUILDER_INFERENCE_STUB_PARAMETER_TYPE.on(target, valueParameterDescriptor.name))
49+
}
50+
}
4051
}
4152
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.resolve.checkers
7+
8+
import org.jetbrains.kotlin.config.LanguageFeature
9+
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
10+
import org.jetbrains.kotlin.descriptors.ValueDescriptor
11+
import org.jetbrains.kotlin.diagnostics.Errors
12+
import org.jetbrains.kotlin.psi.KtDeclaration
13+
import org.jetbrains.kotlin.psi.KtParameter
14+
import org.jetbrains.kotlin.types.StubTypeForBuilderInference
15+
16+
object StubForBuilderInferenceParameterTypeChecker : DeclarationChecker {
17+
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
18+
if (declaration !is KtParameter ||
19+
descriptor !is ValueDescriptor ||
20+
descriptor.returnType !is StubTypeForBuilderInference
21+
) return
22+
if (context.languageVersionSettings.supportsFeature(LanguageFeature.NoBuilderInferenceWithoutAnnotationRestriction)) return
23+
24+
context.trace.report(Errors.BUILDER_INFERENCE_STUB_PARAMETER_TYPE.on(declaration, declaration.nameAsSafeName))
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// ISSUE: KT-53478
2+
// IGNORE_BACKEND_K1: ANY
3+
// Reason: red code
4+
5+
class UncompilingClass<T : Any>(
6+
val block: (UncompilingClass<T>.() -> Unit)? = null,
7+
) {
8+
9+
var uncompilingFun: ((T) -> Unit)? = null
10+
}
11+
12+
fun handleInt(arg: Int) = Unit
13+
14+
fun box(): String {
15+
val obj = UncompilingClass {
16+
uncompilingFun = { handleInt(it) }
17+
}
18+
return "OK"
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// WITH_STDLIB
2+
// ISSUE: KT-64066
3+
// IGNORE_BACKEND_K1: ANY
4+
// Reason: red code
5+
6+
fun box(): String {
7+
val map = buildMap {
8+
put(1, 1)
9+
for (v in values) {}
10+
}
11+
if (map[1] != 1) return "FAIL"
12+
return "OK"
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// ISSUE: KT-53478
2+
3+
class UncompilingClass<T : Any>(
4+
val block: (UncompilingClass<T>.() -> Unit)? = null,
5+
) {
6+
7+
var uncompilingFun: ((T) -> Unit)? = null
8+
}
9+
10+
fun handleInt(arg: Int) = Unit
11+
12+
fun box() {
13+
val obj = UncompilingClass {
14+
uncompilingFun = { handleInt(it) }
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// ISSUE: KT-53478
2+
3+
class UncompilingClass<T : Any>(
4+
val block: (UncompilingClass<T>.() -> Unit)? = null,
5+
) {
6+
7+
var uncompilingFun: ((T) -> Unit)? = null
8+
}
9+
10+
fun handleInt(arg: Int) = Unit
11+
12+
fun box() {
13+
val obj = UncompilingClass {
14+
uncompilingFun = <!BUILDER_INFERENCE_STUB_PARAMETER_TYPE!>{ handleInt(it) }<!>
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// WITH_STDLIB
2+
// ISSUE: KT-64066
3+
4+
fun box() {
5+
val map = buildMap {
6+
put(1, 1)
7+
for (v in values) {}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// WITH_STDLIB
2+
// ISSUE: KT-64066
3+
4+
fun box() {
5+
val map = buildMap {
6+
put(1, 1)
7+
for (<!BUILDER_INFERENCE_STUB_PARAMETER_TYPE!>v<!> in values) {}
8+
}
9+
}

0 commit comments

Comments
 (0)