Skip to content

Commit 678816f

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
1 parent 6f6e37f commit 678816f

File tree

44 files changed

+554
-15
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+554
-15
lines changed

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

+18
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/LLFirBlackBoxCodegenBasedTestGenerated.java

+12
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

+18
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/LLFirReversedBlackBoxCodegenBasedTestGenerated.java

+12
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

+18
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

+18
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

+12
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/FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated.java

+12
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

+12
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
@@ -949,6 +949,7 @@ enum BadNamedArgumentsTarget {
949949
DiagnosticFactory4<PsiElement, KotlinType, String, String, BuilderLambdaLabelingInfo> STUB_TYPE_IN_RECEIVER_CAUSES_AMBIGUITY = DiagnosticFactory4.create(ERROR);
950950
DiagnosticFactory2<PsiElement, Name, Name> BUILDER_INFERENCE_MULTI_LAMBDA_RESTRICTION = DiagnosticFactory2.create(ERROR);
951951
DiagnosticFactory2<PsiElement, Name, Name> BUILDER_INFERENCE_STUB_RECEIVER = DiagnosticFactory2.create(ERROR);
952+
DiagnosticFactory1<KtDeclaration, Name> BUILDER_INFERENCE_STUB_PARAMETER_TYPE = DiagnosticFactory1.create(ERROR);
952953
DiagnosticFactory4<KtElement, Name, Name, KotlinType, KotlinType> UPPER_BOUND_VIOLATION_IN_CONSTRAINT = DiagnosticFactory4.create(ERROR);
953954

954955
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
@@ -1106,6 +1106,7 @@ public static DiagnosticRenderer getRendererForDiagnostic(@NotNull UnboundDiagno
11061106
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);
11071107
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);
11081108
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);
1109+
MAP.put(BUILDER_INFERENCE_STUB_PARAMETER_TYPE, "The type of parameter `{0}` cannot be inferred by builder inference", TO_STRING);
11091110
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);
11101111
MAP.put(NONE_APPLICABLE, "None of the following functions can be called with the arguments supplied: {0}", AMBIGUOUS_CALLS);
11111112
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+
}

compiler/testData/codegen/box/builderInference/issues/kt52757.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
// IGNORE_LIGHT_ANALYSIS
44
// IGNORE_BACKEND_K1: ANY
5-
// REASON: compile-time failure in K1/JVM (java.lang.AssertionError: Could not load module <Error module> @ org.jetbrains.kotlin.backend.common.serialization.KotlinIrLinker.resolveModuleDeserializer)
6-
// REASON: compile-time failure in K1/Native, K1/WASM, K1/JS (java.lang.NullPointerException @ org.jetbrains.kotlin.backend.common.serialization.mangle.descriptor.DescriptorExportCheckerVisitor.isExported)
5+
// Reason: red code
76

87
fun box(): String {
98
build {

compiler/testData/codegen/box/builderInference/issues/kt53109.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
// IGNORE_LIGHT_ANALYSIS
44
// IGNORE_BACKEND_K1: ANY
5-
// REASON: compile-time failure in K1/JVM (java.lang.AssertionError: Could not load module <Error module> @ org.jetbrains.kotlin.backend.common.serialization.KotlinIrLinker.resolveModuleDeserializer)
6-
// REASON: compile-time failure in K1/Native, K1/WASM, K1/JS (java.lang.NullPointerException @ org.jetbrains.kotlin.backend.common.serialization.mangle.descriptor.DescriptorExportCheckerVisitor.isExported)
5+
// REASON: red code
76

87
fun box(): String {
98
build {

0 commit comments

Comments
 (0)