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,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
@@ -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<!>)
@@ -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("") }
)
}
@@ -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("") }<!>
)
}
@@ -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("") }
)
}