K1: add diagnostic BUILDER_INFERENCE_MULTI_LAMBDA_RESTRICTION
Let's call builder lambdas (BL) a lambda that has non-fixed input type projection at the moment of lambda arguments analysis, such lambdas is a subject to be analyzed with builder inference Due to bug in constraint system joining algorithm, currently system of two or more such lambdas may lead to unsound type inference Diagnostic added here should be reported in case when there are two BL that shares a common constraint system, while not annotated with @BuilderInference, as a protection against aforementioned bug It's reported by ConstraintSystemCompleter when such situation has occurred during builder inference phase, it is the same place that decides wherever lambdas is subject to builder inference or not KT-53740
This commit is contained in:
committed by
teamcity
parent
db0d8d9f57
commit
105358dcf6
+8
@@ -204,6 +204,14 @@ class PostponedArgumentsAnalyzer(
|
||||
return
|
||||
}
|
||||
|
||||
// WARN: Following type constraint system unification algorithm is incorrect,
|
||||
// as in fact direction of constraint should depend on projection direction
|
||||
// To perform constraint unification properly, original constraints should be
|
||||
// unified instead of simple result type based constraint
|
||||
// Other possible solution is to add equality constraint, but it will be too strict
|
||||
// and will limit usability
|
||||
// Nevertheless, proper design should be done before fixing this
|
||||
// Causes KT-53740
|
||||
for ((constructor, resultType) in postponedVariables) {
|
||||
val variableWithConstraints = constraintSystemBuilder.currentStorage().notFixedTypeVariables[constructor] ?: continue
|
||||
val variable = variableWithConstraints.typeVariable
|
||||
|
||||
+39
-6
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.types.error.ErrorTypeKind
|
||||
import org.jetbrains.kotlin.types.error.ErrorUtils
|
||||
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
|
||||
import org.jetbrains.kotlin.types.model.TypeVariableMarker
|
||||
import org.jetbrains.kotlin.types.model.TypeVariableTypeConstructorMarker
|
||||
import org.jetbrains.kotlin.types.model.safeSubstitute
|
||||
import org.jetbrains.kotlin.types.typeUtil.asTypeProjection
|
||||
import org.jetbrains.kotlin.utils.addIfNotNull
|
||||
@@ -158,7 +159,7 @@ class KotlinConstraintSystemCompleter(
|
||||
|
||||
// Stage 7: try to complete call with the builder inference if there are uninferred type variables
|
||||
val areThereAppearedProperConstraintsForSomeVariable = tryToCompleteWithBuilderInference(
|
||||
completionMode, topLevelAtoms, topLevelType, postponedArguments, collectVariablesFromContext, analyze
|
||||
completionMode, topLevelAtoms, topLevelType, postponedArguments, collectVariablesFromContext, diagnosticsHolder, analyze
|
||||
)
|
||||
|
||||
if (areThereAppearedProperConstraintsForSomeVariable)
|
||||
@@ -208,6 +209,7 @@ class KotlinConstraintSystemCompleter(
|
||||
topLevelType: UnwrappedType,
|
||||
postponedArguments: List<PostponedResolvedAtom>,
|
||||
collectVariablesFromContext: Boolean,
|
||||
diagnosticsHolder: KotlinDiagnosticsHolder,
|
||||
analyze: (PostponedResolvedAtom) -> Unit
|
||||
): Boolean {
|
||||
if (completionMode == ConstraintSystemCompletionMode.PARTIAL) return false
|
||||
@@ -218,24 +220,55 @@ class KotlinConstraintSystemCompleter(
|
||||
if (!useBuilderInferenceOnlyIfNeeded) return false
|
||||
|
||||
val lambdaArguments = postponedArguments.filterIsInstance<ResolvedLambdaAtom>().takeIf { it.isNotEmpty() } ?: return false
|
||||
|
||||
fun ResolvedLambdaAtom.notFixedInputTypeVariables(): List<TypeVariableTypeConstructorMarker> =
|
||||
inputTypes.flatMap { it.extractTypeVariables() }.filter { it !in fixedTypeVariables }
|
||||
|
||||
val useBuilderInferenceWithoutAnnotation =
|
||||
languageVersionSettings.supportsFeature(LanguageFeature.UseBuilderInferenceWithoutAnnotation)
|
||||
|
||||
val checkForDangerousBuilderInference =
|
||||
!languageVersionSettings.supportsFeature(LanguageFeature.NoBuilderInferenceWithoutAnnotationRestriction)
|
||||
|
||||
// Let's call builder lambda (BL) a lambda that has non-zero not fixed input type variables in
|
||||
// type arguments of it's input types
|
||||
// ex: MutableList<T>.() -> Unit
|
||||
// During type inference of call-site such lambda will be considered BL, if
|
||||
// T not fixed yet
|
||||
// Given we have two or more builder lambdas among postponed arguments, it could result in incorrect type inference due to
|
||||
// incorrect constraint propagation into common system
|
||||
// See KT-53740
|
||||
// Constraint propagation into common system happens at
|
||||
// org.jetbrains.kotlin.resolve.calls.components.PostponedArgumentsAnalyzer.applyResultsOfAnalyzedLambdaToCandidateSystem
|
||||
val dangerousBuilderInferenceWithoutAnnotation =
|
||||
lambdaArguments.size >= 2 && lambdaArguments.count { it.notFixedInputTypeVariables().isNotEmpty() } >= 2
|
||||
|
||||
val builder = getBuilder()
|
||||
for (argument in lambdaArguments) {
|
||||
if (!argument.atom.hasBuilderInferenceAnnotation && !useBuilderInferenceWithoutAnnotation)
|
||||
continue
|
||||
val reallyHasBuilderInferenceAnnotation = argument.atom.hasBuilderInferenceAnnotation
|
||||
|
||||
// no annotation and builder inference without annotation is disabled
|
||||
if (!reallyHasBuilderInferenceAnnotation && !useBuilderInferenceWithoutAnnotation) continue
|
||||
|
||||
// Imitate having builder inference annotation. TODO: Remove after getting rid of @BuilderInference
|
||||
if (!argument.atom.hasBuilderInferenceAnnotation && useBuilderInferenceWithoutAnnotation) {
|
||||
if (!reallyHasBuilderInferenceAnnotation) {
|
||||
argument.atom.hasBuilderInferenceAnnotation = true
|
||||
}
|
||||
|
||||
val notFixedInputTypeVariables = argument.inputTypes
|
||||
.flatMap { it.extractTypeVariables() }.filter { it !in fixedTypeVariables }
|
||||
val notFixedInputTypeVariables = argument.notFixedInputTypeVariables()
|
||||
|
||||
// lambda is subject to builder inference past this point
|
||||
if (notFixedInputTypeVariables.isEmpty()) continue
|
||||
|
||||
// we have dangerous inference situation
|
||||
// if lambda annotated with BuilderInference it's probably safe, due to type shape
|
||||
// otherwise report multi-lambda builder inference restriction diagnostic
|
||||
if (checkForDangerousBuilderInference && dangerousBuilderInferenceWithoutAnnotation && !reallyHasBuilderInferenceAnnotation) {
|
||||
for (variable in notFixedInputTypeVariables) {
|
||||
diagnosticsHolder.addDiagnostic(MultiLambdaBuilderInferenceRestriction(argument.atom, variable.typeParameter))
|
||||
}
|
||||
}
|
||||
|
||||
for (variable in notFixedInputTypeVariables) {
|
||||
builder.markPostponedVariable(notFixedTypeVariables.getValue(variable).typeVariable)
|
||||
}
|
||||
|
||||
+8
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.resolve.calls.tower.CandidateApplicability
|
||||
import org.jetbrains.kotlin.resolve.calls.tower.CandidateApplicability.*
|
||||
import org.jetbrains.kotlin.types.TypeConstructor
|
||||
import org.jetbrains.kotlin.types.UnwrappedType
|
||||
import org.jetbrains.kotlin.types.model.TypeParameterMarker
|
||||
|
||||
interface TransformableToWarning<T : KotlinCallDiagnostic> {
|
||||
fun transformToWarning(): T?
|
||||
@@ -65,6 +66,13 @@ class NonVarargSpread(val argument: KotlinCallArgument) : KotlinCallDiagnostic(I
|
||||
override fun report(reporter: DiagnosticReporter) = reporter.onCallArgumentSpread(argument, this)
|
||||
}
|
||||
|
||||
class MultiLambdaBuilderInferenceRestriction(
|
||||
val argument: KotlinCallArgument,
|
||||
val typeParameter: TypeParameterMarker?
|
||||
) : KotlinCallDiagnostic(RESOLVED) {
|
||||
override fun report(reporter: DiagnosticReporter) = reporter.onCallArgument(argument, this)
|
||||
}
|
||||
|
||||
class MixingNamedAndPositionArguments(override val argument: KotlinCallArgument) : InapplicableArgumentDiagnostic()
|
||||
|
||||
class NamedArgumentNotAllowed(val argument: KotlinCallArgument, val descriptor: CallableDescriptor) : KotlinCallDiagnostic(INAPPLICABLE) {
|
||||
|
||||
Reference in New Issue
Block a user