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:
Simon Ogorodnik
2022-08-26 16:24:11 +02:00
committed by teamcity
parent db0d8d9f57
commit 105358dcf6
19 changed files with 415 additions and 13 deletions
@@ -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> {