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
+12
@@ -14457,6 +14457,18 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
|
||||
runTest("compiler/testData/diagnostics/tests/inference/builderInference/labaledCall.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("multiLambdaRestriction.kt")
|
||||
public void testMultiLambdaRestriction() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/inference/builderInference/multiLambdaRestriction.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("multiLambdaRestrictionDisabled.kt")
|
||||
public void testMultiLambdaRestrictionDisabled() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/inference/builderInference/multiLambdaRestrictionDisabled.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("propertySubstitution.kt")
|
||||
public void testPropertySubstitution() throws Exception {
|
||||
|
||||
+12
@@ -14457,6 +14457,18 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti
|
||||
runTest("compiler/testData/diagnostics/tests/inference/builderInference/labaledCall.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("multiLambdaRestriction.kt")
|
||||
public void testMultiLambdaRestriction() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/inference/builderInference/multiLambdaRestriction.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("multiLambdaRestrictionDisabled.kt")
|
||||
public void testMultiLambdaRestrictionDisabled() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/inference/builderInference/multiLambdaRestrictionDisabled.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("propertySubstitution.kt")
|
||||
public void testPropertySubstitution() throws Exception {
|
||||
|
||||
+12
@@ -14457,6 +14457,18 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac
|
||||
runTest("compiler/testData/diagnostics/tests/inference/builderInference/labaledCall.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("multiLambdaRestriction.kt")
|
||||
public void testMultiLambdaRestriction() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/inference/builderInference/multiLambdaRestriction.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("multiLambdaRestrictionDisabled.kt")
|
||||
public void testMultiLambdaRestrictionDisabled() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/inference/builderInference/multiLambdaRestrictionDisabled.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("propertySubstitution.kt")
|
||||
public void testPropertySubstitution() throws Exception {
|
||||
|
||||
@@ -861,6 +861,8 @@ public interface Errors {
|
||||
DiagnosticFactory3<PsiElement, String, String, String> OVERLOAD_RESOLUTION_AMBIGUITY_BECAUSE_OF_STUB_TYPES = DiagnosticFactory3.create(ERROR);
|
||||
DiagnosticFactory3<PsiElement, KotlinType, String, String> STUB_TYPE_IN_ARGUMENT_CAUSES_AMBIGUITY = DiagnosticFactory3.create(ERROR);
|
||||
DiagnosticFactory4<PsiElement, KotlinType, String, String, BuilderLambdaLabelingInfo> STUB_TYPE_IN_RECEIVER_CAUSES_AMBIGUITY = DiagnosticFactory4.create(ERROR);
|
||||
DiagnosticFactory2<PsiElement, Name, Name> BUILDER_INFERENCE_MULTI_LAMBDA_RESTRICTION = DiagnosticFactory2.create(ERROR);
|
||||
|
||||
DiagnosticFactory1<PsiElement, Collection<? extends ResolvedCall<?>>> NONE_APPLICABLE = DiagnosticFactory1.create(ERROR);
|
||||
DiagnosticFactory1<PsiElement, Collection<? extends ResolvedCall<?>>> CANNOT_COMPLETE_RESOLVE = DiagnosticFactory1.create(ERROR);
|
||||
DiagnosticFactory1<PsiElement, Collection<? extends ResolvedCall<?>>> UNRESOLVED_REFERENCE_WRONG_RECEIVER =
|
||||
|
||||
+1
@@ -944,6 +944,7 @@ public class DefaultErrorMessages {
|
||||
STRING, STRING, STRING);
|
||||
MAP.put(STUB_TYPE_IN_ARGUMENT_CAUSES_AMBIGUITY, "The type of an argument 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);
|
||||
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);
|
||||
MAP.put(BUILDER_INFERENCE_MULTI_LAMBDA_RESTRICTION, "Unstable inference behaviour with multiple lambdas. Please either specify type argument for generic parameter `{0}` of `{1}` explicitly or add @BuilderInference to corresponding parameter declaration", TO_STRING, TO_STRING);
|
||||
MAP.put(NONE_APPLICABLE, "None of the following functions can be called with the arguments supplied: {0}", AMBIGUOUS_CALLS);
|
||||
MAP.put(CANNOT_COMPLETE_RESOLVE, "Cannot choose among the following candidates without completing type inference: {0}", AMBIGUOUS_CALLS);
|
||||
MAP.put(UNRESOLVED_REFERENCE_WRONG_RECEIVER, "Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: {0}", AMBIGUOUS_CALLS);
|
||||
|
||||
+15
@@ -10,9 +10,11 @@ import org.jetbrains.kotlin.builtins.functions.FunctionInvokeDescriptor
|
||||
import org.jetbrains.kotlin.builtins.isExtensionFunctionType
|
||||
import org.jetbrains.kotlin.config.LanguageFeature
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
|
||||
import org.jetbrains.kotlin.diagnostics.Errors.*
|
||||
import org.jetbrains.kotlin.diagnostics.Errors.BadNamedArgumentsTarget.*
|
||||
import org.jetbrains.kotlin.diagnostics.reportDiagnosticOnce
|
||||
import org.jetbrains.kotlin.name.SpecialNames
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.isNull
|
||||
import org.jetbrains.kotlin.psi.psiUtil.lastBlockStatementOrThis
|
||||
@@ -319,6 +321,19 @@ class DiagnosticReporterByTrackingStrategy(
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
MultiLambdaBuilderInferenceRestriction::class.java -> {
|
||||
diagnostic as MultiLambdaBuilderInferenceRestriction
|
||||
val typeParameter = diagnostic.typeParameter as? TypeParameterDescriptor
|
||||
|
||||
trace.reportDiagnosticOnce(
|
||||
BUILDER_INFERENCE_MULTI_LAMBDA_RESTRICTION.on(
|
||||
callArgument.psiCallArgument.valueArgument.asElement(),
|
||||
typeParameter?.name ?: SpecialNames.NO_NAME_PROVIDED,
|
||||
typeParameter?.containingDeclaration?.name ?: SpecialNames.NO_NAME_PROVIDED,
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+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) {
|
||||
|
||||
+3
-1
@@ -1,6 +1,7 @@
|
||||
// WITH_STDLIB
|
||||
// !DIAGNOSTICS: -OPT_IN_USAGE_ERROR
|
||||
// For FIR, see: KT-50704
|
||||
import kotlin.experimental.ExperimentalTypeInference
|
||||
|
||||
@JvmName("foo1")
|
||||
fun foo(x: Inv<String>) {}
|
||||
@@ -201,7 +202,8 @@ interface Foo2<K, V> {
|
||||
fun entries(): MutableSet<MutableMap.MutableEntry<K, V>>
|
||||
}
|
||||
|
||||
fun <L, K, V> twoBuilderLambdas(block: Foo<L>.() -> Unit, block2: Foo2<K, V>.() -> Unit) {}
|
||||
@OptIn(ExperimentalTypeInference::class)
|
||||
fun <L, K, V> twoBuilderLambdas(@BuilderInference block: Foo<L>.() -> Unit, @BuilderInference block2: Foo2<K, V>.() -> Unit) {}
|
||||
|
||||
fun test() {
|
||||
twoBuilderLambdas(
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// WITH_STDLIB
|
||||
// !DIAGNOSTICS: -OPT_IN_USAGE_ERROR
|
||||
// For FIR, see: KT-50704
|
||||
import kotlin.experimental.ExperimentalTypeInference
|
||||
|
||||
@JvmName("foo1")
|
||||
fun foo(x: Inv<String>) {}
|
||||
@@ -201,7 +202,8 @@ interface Foo2<K, V> {
|
||||
fun entries(): MutableSet<MutableMap.MutableEntry<K, V>>
|
||||
}
|
||||
|
||||
fun <L, K, V> twoBuilderLambdas(block: Foo<L>.() -> Unit, block2: Foo2<K, V>.() -> Unit) {}
|
||||
@OptIn(ExperimentalTypeInference::class)
|
||||
fun <L, K, V> twoBuilderLambdas(@BuilderInference block: Foo<L>.() -> Unit, @BuilderInference block2: Foo2<K, V>.() -> Unit) {}
|
||||
|
||||
fun test() {
|
||||
twoBuilderLambdas(
|
||||
|
||||
@@ -19,7 +19,7 @@ public fun foo11(/*0*/ x: kotlin.collections.MutableSet<kotlin.collections.Mutab
|
||||
@kotlin.jvm.JvmName(name = "foo111") public fun foo11(/*0*/ x: kotlin.collections.MutableSet<kotlin.collections.MutableMap.MutableEntry<kotlin.String, kotlin.Int>>): kotlin.Unit
|
||||
public fun main(): kotlin.Unit
|
||||
public fun test(): kotlin.Unit
|
||||
public fun </*0*/ L, /*1*/ K, /*2*/ V> twoBuilderLambdas(/*0*/ block: Foo<L>.() -> kotlin.Unit, /*1*/ block2: Foo2<K, V>.() -> kotlin.Unit): kotlin.Unit
|
||||
@kotlin.OptIn(markerClass = {kotlin.experimental.ExperimentalTypeInference::class}) public fun </*0*/ L, /*1*/ K, /*2*/ V> twoBuilderLambdas(/*0*/ @kotlin.BuilderInference block: Foo<L>.() -> kotlin.Unit, /*1*/ @kotlin.BuilderInference block2: Foo2<K, V>.() -> kotlin.Unit): kotlin.Unit
|
||||
public fun kotlin.Int.bar(): kotlin.Unit
|
||||
@kotlin.jvm.JvmName(name = "bar1") public fun kotlin.String.bar(): kotlin.Unit
|
||||
public fun kotlin.Int.foo0003(/*0*/ y: kotlin.Number, /*1*/ z: kotlin.String): kotlin.Unit
|
||||
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
// WITH_STDLIB
|
||||
fun test() {
|
||||
foo(
|
||||
flow { emit(0) }
|
||||
) { it.collect <!TOO_MANY_ARGUMENTS!>{}<!> }
|
||||
|
||||
// 0. Initial
|
||||
// W <: Any / declared upper bound
|
||||
// FlowCollector<W>.() -> Unit <: FlowCollector<W>.() -> Unit / from Argument { emit(0) }
|
||||
// F <: Any / declared upper bound
|
||||
// Flow<W> <: F / from Argument flow { emit(0) }
|
||||
// Scope<F>.(F) -> Unit -> Scope<F>.(F) -> Unit / from Argument { it.collect() }
|
||||
|
||||
// 1. after analyze for { emit(0 }
|
||||
// Unit <: Unit / from Lambda argument, probably { emit(0) }
|
||||
// Int <: W / from For builder inference call
|
||||
// Flow<Int> <: F / from For builder inference call
|
||||
|
||||
// 2. after analyze for { it.collect {} }
|
||||
// Unit <: Unit / from Lambda argument, probably { it.collect {} }
|
||||
// Flow<*> <: F / from For builder inference call
|
||||
// ERROR_TYPE <: W / from For builder inference call
|
||||
}
|
||||
|
||||
fun <F : Any> foo(
|
||||
bar: F,
|
||||
block: Scope<F>.(F) -> Unit
|
||||
) {}
|
||||
|
||||
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
|
||||
fun <W> flow(@BuilderInference block: FlowCollector<W>.()->Unit): Flow<W> {
|
||||
val collector = FlowCollectorImpl<W>()
|
||||
collector.block()
|
||||
return object : Flow<W> {
|
||||
override fun collect(collector: FlowCollector<W>) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Scope<S>
|
||||
|
||||
interface Flow<out O> {
|
||||
fun collect(collector: FlowCollector<O>)
|
||||
}
|
||||
|
||||
fun interface FlowCollector<in I> {
|
||||
|
||||
fun emit(value: I)
|
||||
}
|
||||
|
||||
class FlowCollectorImpl<C> : FlowCollector<C> {
|
||||
override fun emit(value: C) {}
|
||||
}
|
||||
|
||||
fun Flow<*>.collect() {}
|
||||
@@ -1,10 +1,26 @@
|
||||
// FIR_IDENTICAL
|
||||
// WITH_STDLIB
|
||||
// SKIP_TXT
|
||||
fun test() {
|
||||
foo(
|
||||
flow { emit(0) }
|
||||
) { it.collect <!TOO_MANY_ARGUMENTS!>{}<!> }
|
||||
) <!BUILDER_INFERENCE_MULTI_LAMBDA_RESTRICTION!>{ it.collect <!TOO_MANY_ARGUMENTS!>{}<!> }<!>
|
||||
|
||||
// 0. Initial
|
||||
// W <: Any / declared upper bound
|
||||
// FlowCollector<W>.() -> Unit <: FlowCollector<W>.() -> Unit / from Argument { emit(0) }
|
||||
// F <: Any / declared upper bound
|
||||
// Flow<W> <: F / from Argument flow { emit(0) }
|
||||
// Scope<F>.(F) -> Unit -> Scope<F>.(F) -> Unit / from Argument { it.collect() }
|
||||
|
||||
// 1. after analyze for { emit(0 }
|
||||
// Unit <: Unit / from Lambda argument, probably { emit(0) }
|
||||
// Int <: W / from For builder inference call
|
||||
// Flow<Int> <: F / from For builder inference call
|
||||
|
||||
// 2. after analyze for { it.collect {} }
|
||||
// Unit <: Unit / from Lambda argument, probably { it.collect {} }
|
||||
// Flow<*> <: F / from For builder inference call
|
||||
// ERROR_TYPE <: W / from For builder inference call
|
||||
}
|
||||
|
||||
fun <F : Any> foo(
|
||||
@@ -13,7 +29,7 @@ fun <F : Any> foo(
|
||||
) {}
|
||||
|
||||
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
|
||||
fun <W> flow(BuilderInference block: FlowCollector<W>.()->Unit): Flow<W> {
|
||||
fun <W> flow(@BuilderInference block: FlowCollector<W>.()->Unit): Flow<W> {
|
||||
val collector = FlowCollectorImpl<W>()
|
||||
collector.block()
|
||||
return object : Flow<W> {
|
||||
|
||||
@@ -12,7 +12,7 @@ data class Output(val source: InputWrapper<List<String>>)
|
||||
fun main2(input: InputWrapper<Unit>): Output {
|
||||
val output = input.<!INFERRED_INTO_DECLARED_UPPER_BOUNDS!>doMapping<!>(
|
||||
foo = { buildList { add("this is List<String>") } },
|
||||
bar = { it.isNotEmpty() },
|
||||
<!BUILDER_INFERENCE_MULTI_LAMBDA_RESTRICTION!>bar = { it.isNotEmpty() }<!>,
|
||||
)
|
||||
|
||||
return Output(source = <!TYPE_MISMATCH!>output<!>)
|
||||
|
||||
Vendored
+70
@@ -0,0 +1,70 @@
|
||||
// WITH_STDLIB
|
||||
// SKIP_TXT
|
||||
|
||||
fun List<Int>.myExt() {}
|
||||
fun <T> List<T>.myGenericExt() {}
|
||||
|
||||
fun <R> a(first: R, second: (List<R>) -> Unit) {}
|
||||
|
||||
fun test1() {
|
||||
a(
|
||||
buildList { add("") },
|
||||
second = {
|
||||
it.myGenericExt()
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
fun <R> b(first: () -> List<R>, second: (List<R>) -> Unit) {}
|
||||
|
||||
fun test2() {
|
||||
b(
|
||||
first = {
|
||||
buildList { add("") }
|
||||
},
|
||||
second = {
|
||||
it.myExt() // Note: must be extension to add constraints
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fun <Q> select(a: Q, b: Q): Q = a
|
||||
|
||||
// Note: no builder inference annotation
|
||||
fun <R> myBuildList(builder: MutableList<R>.() -> Unit): List<R> {
|
||||
val list = mutableListOf<R>()
|
||||
list.builder()
|
||||
return list
|
||||
}
|
||||
|
||||
fun test3() {
|
||||
select(
|
||||
buildList { add("") },
|
||||
buildList { add(1) }
|
||||
)
|
||||
|
||||
select (
|
||||
myBuildList { add("") },
|
||||
myBuildList { add(1) },
|
||||
)
|
||||
|
||||
select (
|
||||
run { myBuildList { add("") } },
|
||||
myBuildList { add(1) },
|
||||
)
|
||||
}
|
||||
|
||||
fun <D> buildPartList(left: MutableList<D>.() -> Unit, right: MutableList<D>.() -> Unit): List<D> {
|
||||
val list = mutableListOf<D>()
|
||||
list.left()
|
||||
list.right()
|
||||
return list
|
||||
}
|
||||
|
||||
fun test4() {
|
||||
buildPartList(
|
||||
left = { add(1) },
|
||||
right = { add("") }
|
||||
)
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
// WITH_STDLIB
|
||||
// SKIP_TXT
|
||||
|
||||
fun List<Int>.myExt() {}
|
||||
fun <T> List<T>.myGenericExt() {}
|
||||
|
||||
fun <R> a(first: R, second: (List<R>) -> Unit) {}
|
||||
|
||||
fun test1() {
|
||||
a(
|
||||
buildList { add("") },
|
||||
<!BUILDER_INFERENCE_MULTI_LAMBDA_RESTRICTION("R; a")!>second = {
|
||||
it.myGenericExt()
|
||||
}<!>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
fun <R> b(first: () -> List<R>, second: (List<R>) -> Unit) {}
|
||||
|
||||
fun test2() {
|
||||
b(
|
||||
first = {
|
||||
buildList { add("") }
|
||||
},
|
||||
<!BUILDER_INFERENCE_MULTI_LAMBDA_RESTRICTION("R; b")!>second = {
|
||||
it.myExt() // Note: must be extension to add constraints
|
||||
}<!>
|
||||
)
|
||||
}
|
||||
|
||||
fun <Q> select(a: Q, b: Q): Q = a
|
||||
|
||||
// Note: no builder inference annotation
|
||||
fun <R> myBuildList(builder: MutableList<R>.() -> Unit): List<R> {
|
||||
val list = mutableListOf<R>()
|
||||
list.builder()
|
||||
return list
|
||||
}
|
||||
|
||||
fun test3() {
|
||||
select(
|
||||
buildList { add("") },
|
||||
buildList { add(1) }
|
||||
)
|
||||
|
||||
select (
|
||||
myBuildList <!BUILDER_INFERENCE_MULTI_LAMBDA_RESTRICTION("R; myBuildList")!>{ add("") }<!>,
|
||||
myBuildList <!BUILDER_INFERENCE_MULTI_LAMBDA_RESTRICTION("R; myBuildList")!>{ add(1) }<!>,
|
||||
)
|
||||
|
||||
select (
|
||||
run { myBuildList <!BUILDER_INFERENCE_MULTI_LAMBDA_RESTRICTION("R; myBuildList")!>{ add("") }<!> },
|
||||
myBuildList <!BUILDER_INFERENCE_MULTI_LAMBDA_RESTRICTION("R; myBuildList")!>{ add(1) }<!>,
|
||||
)
|
||||
}
|
||||
|
||||
fun <D> buildPartList(left: MutableList<D>.() -> Unit, right: MutableList<D>.() -> Unit): List<D> {
|
||||
val list = mutableListOf<D>()
|
||||
list.left()
|
||||
list.right()
|
||||
return list
|
||||
}
|
||||
|
||||
fun test4() {
|
||||
buildPartList(
|
||||
<!BUILDER_INFERENCE_MULTI_LAMBDA_RESTRICTION("D; buildPartList")!>left = { add(1) }<!>,
|
||||
<!BUILDER_INFERENCE_MULTI_LAMBDA_RESTRICTION("D; buildPartList")!>right = { add("") }<!>
|
||||
)
|
||||
}
|
||||
Vendored
+72
@@ -0,0 +1,72 @@
|
||||
// FIR_IDENTICAL
|
||||
// WITH_STDLIB
|
||||
// LANGUAGE: +NoBuilderInferenceWithoutAnnotationRestriction
|
||||
// SKIP_TXT
|
||||
|
||||
fun List<Int>.myExt() {}
|
||||
fun <T> List<T>.myGenericExt() {}
|
||||
|
||||
fun <R> a(first: R, second: (List<R>) -> Unit) {}
|
||||
|
||||
fun test1() {
|
||||
a(
|
||||
buildList { add("") },
|
||||
second = {
|
||||
it.myGenericExt()
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
fun <R> b(first: () -> List<R>, second: (List<R>) -> Unit) {}
|
||||
|
||||
fun test2() {
|
||||
b(
|
||||
first = {
|
||||
buildList { add("") }
|
||||
},
|
||||
second = {
|
||||
it.myExt() // Note: must be extension to add constraints
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fun <Q> select(a: Q, b: Q): Q = a
|
||||
|
||||
// Note: no builder inference annotation
|
||||
fun <R> myBuildList(builder: MutableList<R>.() -> Unit): List<R> {
|
||||
val list = mutableListOf<R>()
|
||||
list.builder()
|
||||
return list
|
||||
}
|
||||
|
||||
fun test3() {
|
||||
select(
|
||||
buildList { add("") },
|
||||
buildList { add(1) }
|
||||
)
|
||||
|
||||
select (
|
||||
myBuildList { add("") },
|
||||
myBuildList { add(1) },
|
||||
)
|
||||
|
||||
select (
|
||||
run { myBuildList { add("") } },
|
||||
myBuildList { add(1) },
|
||||
)
|
||||
}
|
||||
|
||||
fun <D> buildPartList(left: MutableList<D>.() -> Unit, right: MutableList<D>.() -> Unit): List<D> {
|
||||
val list = mutableListOf<D>()
|
||||
list.left()
|
||||
list.right()
|
||||
return list
|
||||
}
|
||||
|
||||
fun test4() {
|
||||
buildPartList(
|
||||
left = { add(1) },
|
||||
right = { add("") }
|
||||
)
|
||||
}
|
||||
Generated
+12
@@ -14463,6 +14463,18 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
|
||||
runTest("compiler/testData/diagnostics/tests/inference/builderInference/labaledCall.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("multiLambdaRestriction.kt")
|
||||
public void testMultiLambdaRestriction() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/inference/builderInference/multiLambdaRestriction.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("multiLambdaRestrictionDisabled.kt")
|
||||
public void testMultiLambdaRestrictionDisabled() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/inference/builderInference/multiLambdaRestrictionDisabled.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("propertySubstitution.kt")
|
||||
public void testPropertySubstitution() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user