FIR: Add workaround for OverloadResolutionByLambdaReturnType

Mostly, that should help for existing flatMap usages

^KT-43129 Submitted
This commit is contained in:
Denis Zharkov
2020-11-02 20:45:39 +03:00
parent 07ed89b02b
commit 4612f26bfb
19 changed files with 87 additions and 209 deletions
@@ -9,6 +9,7 @@ import org.jetbrains.kotlin.fir.NoMutableState
import org.jetbrains.kotlin.fir.resolve.calls.ConeCallConflictResolverFactory
import org.jetbrains.kotlin.fir.resolve.calls.ConeCompositeConflictResolver
import org.jetbrains.kotlin.fir.resolve.calls.ConeOverloadConflictResolver
import org.jetbrains.kotlin.fir.resolve.calls.ConeOverloadResolutionByLambdaConflictResolve
import org.jetbrains.kotlin.fir.resolve.inference.InferenceComponents
import org.jetbrains.kotlin.resolve.calls.results.TypeSpecificityComparator
import org.jetbrains.kotlin.resolve.jvm.JvmTypeSpecificityComparator
@@ -22,7 +23,8 @@ object JvmCallConflictResolverFactory : ConeCallConflictResolverFactory() {
val specificityComparator = JvmTypeSpecificityComparator(components.ctx)
return ConeCompositeConflictResolver(
ConeOverloadConflictResolver(specificityComparator, components),
ConeEquivalentCallConflictResolver(specificityComparator, components)
ConeEquivalentCallConflictResolver(specificityComparator, components),
ConeOverloadResolutionByLambdaConflictResolve(components.session),
)
}
}
@@ -6,7 +6,7 @@
package org.jetbrains.kotlin.fir.resolve.calls
class ConeCompositeConflictResolver(
private vararg val conflictResolvers: AbstractConeCallConflictResolver
private vararg val conflictResolvers: ConeCallConflictResolver
) : ConeCallConflictResolver() {
override fun chooseMaximallySpecificCandidates(
candidates: Set<Candidate>,
@@ -22,4 +22,4 @@ class ConeCompositeConflictResolver(
}
return currentCandidates
}
}
}
@@ -0,0 +1,31 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.fir.resolve.calls
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.FirCallableDeclaration
import org.jetbrains.kotlin.fir.resolve.fqName
import org.jetbrains.kotlin.resolve.descriptorUtil.OVERLOAD_RESOLUTION_BY_LAMBDA_ANNOTATION
class ConeOverloadResolutionByLambdaConflictResolve(private val session: FirSession) : ConeCallConflictResolver() {
override fun chooseMaximallySpecificCandidates(
candidates: Set<Candidate>,
discriminateGenerics: Boolean,
discriminateAbstracts: Boolean
): Set<Candidate> {
if (candidates.size == 1) return candidates
candidates.singleOrNull { candidate ->
(candidate.symbol.fir as? FirCallableDeclaration<*>)?.annotations?.any {
it.fqName(session) == OVERLOAD_RESOLUTION_BY_LAMBDA_ANNOTATION
} == true
}?.let {
return setOf(it)
}
return candidates
}
}
@@ -1,10 +0,0 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER
// WITH_RUNTIME
fun <T> List<Option<T>>.flatten(): List<T> = <!AMBIGUITY!>flatMap<!> { <!UNRESOLVED_REFERENCE!>it<!>.<!NONE_APPLICABLE!>fold<!>(::emptyList, <!UNRESOLVED_REFERENCE!>::listOf<!>) }
class Option<out T> {
fun <R> fold(ifEmpty: () -> R, ifSome: (T) -> R): R = TODO()
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER
// WITH_RUNTIME
@@ -3,18 +3,18 @@
class A
fun test_1(list: List<Set<A>>) {
list.<!AMBIGUITY!>flatMapTo<!>(mutableSetOf()) { <!UNRESOLVED_REFERENCE!>it<!> }
list.flatMapTo(mutableSetOf()) { it }
}
fun test_2(list: List<Set<A>>) {
sequence<A> {
<!DEBUG_INFO_EXPRESSION_TYPE("ERROR CLASS: Ambiguity: flatMapTo, [kotlin/collections/flatMapTo, kotlin/collections/flatMapTo]")!>list.<!AMBIGUITY!>flatMapTo<!>(mutableSetOf()) { <!UNRESOLVED_REFERENCE!>it<!> }<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.collections.MutableSet<A>")!>list.flatMapTo(mutableSetOf()) { it }<!>
}
}
fun test_3(list: List<Set<A>>) {
sequence {
list.<!AMBIGUITY!>flatMapTo<!>(mutableSetOf()) { <!UNRESOLVED_REFERENCE!>it<!> }
list.flatMapTo(mutableSetOf()) { it }
yield(A())
}
}
@@ -22,6 +22,6 @@ fun test_3(list: List<Set<A>>) {
fun test_4(list: List<Set<A>>) {
sequence {
yield(A())
list.<!AMBIGUITY!>flatMapTo<!>(mutableSetOf()) { <!UNRESOLVED_REFERENCE!>it<!> }
list.flatMapTo(mutableSetOf()) { it }
}
}
@@ -1,41 +0,0 @@
// !LANGUAGE: +NewInference +OverloadResolutionByLambdaReturnType
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNUSED_EXPRESSION
// ISSUE: KT-11265
// FILE: OverloadResolutionByLambdaReturnType.kt
package kotlin
annotation class OverloadResolutionByLambdaReturnType
// FILE: main.kt
import kotlin.OverloadResolutionByLambdaReturnType
@kotlin.jvm.JvmName("myFlatMapIterable")
@OverloadResolutionByLambdaReturnType
fun <T, R> Sequence<T>.myFlatMap(transform: (T) -> Iterable<R>): Sequence<R> {
TODO()
}
fun <T, R> Sequence<T>.myFlatMap(transform: (T) -> Sequence<R>): Sequence<R> {
TODO()
}
interface A {
val supertypes: Collection<B>
}
interface B {
val descriptors: Sequence<C>?
}
interface C
fun <K : Any> elvis(x: K?, y: K): K = y
fun test(a: A) {
a.supertypes.asSequence().<!AMBIGUITY!>myFlatMap<!> {
<!INAPPLICABLE_CANDIDATE!>elvis<!>(<!UNRESOLVED_REFERENCE!>it<!>.<!UNRESOLVED_REFERENCE!>descriptors<!>, sequenceOf())
}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: +NewInference +OverloadResolutionByLambdaReturnType
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNUSED_EXPRESSION
// ISSUE: KT-11265
@@ -1,35 +0,0 @@
// !LANGUAGE: +NewInference +OverloadResolutionByLambdaReturnType
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNUSED_EXPRESSION -EXPERIMENTAL_API_USAGE -EXPERIMENTAL_UNSIGNED_LITERALS
// ISSUE: KT-11265
// FILE: OverloadResolutionByLambdaReturnType.kt
package kotlin
annotation class OverloadResolutionByLambdaReturnType
// FILE: main.kt
import kotlin.OverloadResolutionByLambdaReturnType
@OverloadResolutionByLambdaReturnType
fun <R> UByteArray.fooMap(t: (UByte) -> Iterable<R>): List<R> {
TODO("ub.fm")
}
@OverloadResolutionByLambdaReturnType
fun <T, R> Iterable<T>.fooMap(t: (T) -> Iterable<R>): List<R> {
TODO("i.fm(i)")
}
@JvmName("fooMapSeq")
fun <T, R> Iterable<T>.fooMap(t: (T) -> Sequence<R>): List<R> {
TODO("i.fm(s)")
}
fun test() {
val list = ubyteArrayOf(0u).<!AMBIGUITY!>fooMap<!> { <!NONE_APPLICABLE!>listOf<!>(<!UNRESOLVED_REFERENCE!>it<!>) }
takeUByteList(list)
}
fun takeUByteList(list: List<UByte>) {}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: +NewInference +OverloadResolutionByLambdaReturnType
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNUSED_EXPRESSION -EXPERIMENTAL_API_USAGE -EXPERIMENTAL_UNSIGNED_LITERALS
// ISSUE: KT-11265
@@ -1,54 +0,0 @@
// !LANGUAGE: +NewInference +OverloadResolutionByLambdaReturnType
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNUSED_EXPRESSION -EXPERIMENTAL_API_USAGE -EXPERIMENTAL_UNSIGNED_LITERALS
// ISSUE: KT-11265
// FILE: OverloadResolutionByLambdaReturnType.kt
package kotlin
annotation class OverloadResolutionByLambdaReturnType
// FILE: main.kt
import kotlin.OverloadResolutionByLambdaReturnType
public inline fun <T, R> Iterable<T>.myFlatMap(transform: (T) -> Iterable<R>): List<R> {
TODO()
}
@OverloadResolutionByLambdaReturnType
@kotlin.jvm.JvmName("myFlatMapSequence")
public inline fun <T, R> Iterable<T>.myFlatMap(transform: (T) -> Sequence<R>): List<R> {
TODO()
}
interface Name
interface DeclarationDescriptor {
val nextCandidates: List<DeclarationDescriptor>?
val nextCandidatesSeq: Sequence<DeclarationDescriptor>?
val name: Name
}
fun test_1(name: Name, toplevelDescriptors: List<DeclarationDescriptor>): List<DeclarationDescriptor> {
val candidates = toplevelDescriptors.<!AMBIGUITY!>myFlatMap<!> { container ->
val nextCandidates = container.<!UNRESOLVED_REFERENCE!>nextCandidates<!> ?: return@myFlatMap emptyList()
nextCandidates
}
return candidates
}
fun test_2(name: Name, toplevelDescriptors: List<DeclarationDescriptor>): List<DeclarationDescriptor> {
val candidates = toplevelDescriptors.<!AMBIGUITY!>myFlatMap<!> { container ->
val nextCandidates = container.<!UNRESOLVED_REFERENCE!>nextCandidatesSeq<!> ?: return@myFlatMap sequenceOf()
nextCandidates
}
return candidates
}
fun test_3(name: Name, toplevelDescriptors: List<DeclarationDescriptor>): List<DeclarationDescriptor> {
val candidates = toplevelDescriptors.<!AMBIGUITY!>myFlatMap<!> { container ->
val nextCandidates = container.<!UNRESOLVED_REFERENCE!>nextCandidatesSeq<!>!!
nextCandidates
}
return candidates
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: +NewInference +OverloadResolutionByLambdaReturnType
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNUSED_EXPRESSION -EXPERIMENTAL_API_USAGE -EXPERIMENTAL_UNSIGNED_LITERALS
// ISSUE: KT-11265
@@ -20,17 +20,17 @@ fun takeString(s: String) {}
fun takeInt(s: Int) {}
fun test_1() {
val x = <!AMBIGUITY!>create<!> { "" }
val x = create { "" }
takeString(x)
}
fun test_2() {
val x = <!AMBIGUITY!>create<!> { 1 }
takeInt(x)
val x = create { 1 }
<!INAPPLICABLE_CANDIDATE!>takeInt<!>(x)
}
fun test_3() {
val x = <!AMBIGUITY!>create<!> { 1.0 }
val x = create { 1.0 }
}
@OverloadResolutionByLambdaReturnType
@@ -38,11 +38,11 @@ fun <K> create(x: K, f: (K) -> Int): Int = 1
fun <T> create(x: T, f: (T) -> String): String = ""
fun test_4() {
val x = <!AMBIGUITY!>create<!>("") { "" }
val x = create("") { "" }
takeString(x)
}
fun test_5() {
val x = <!AMBIGUITY!>create<!>("") { 1 }
takeInt(x)
}
val x = create("") { 1 }
<!INAPPLICABLE_CANDIDATE!>takeInt<!>(x)
}
@@ -20,17 +20,17 @@ fun takeString(s: String) {}
fun takeInt(s: Int) {}
fun test_1() {
val x = <!AMBIGUITY!>create<!> { "" }
val x = create { "" }
takeString(x)
}
fun test_2() {
val x = <!AMBIGUITY!>create<!> { 1 }
takeInt(x)
val x = create { 1 }
<!INAPPLICABLE_CANDIDATE!>takeInt<!>(x)
}
fun test_3() {
val x = <!AMBIGUITY!>create<!> { 1.0 }
val x = create { 1.0 }
}
@OverloadResolutionByLambdaReturnType
@@ -38,13 +38,13 @@ fun <K> create(x: K, f: (K) -> Int): Int = 1
fun <T> create(x: T, f: (T) -> String): String = ""
fun test_4() {
val x = <!AMBIGUITY!>create<!>("") { "" }
val x = create("") { "" }
takeString(x)
}
fun test_5() {
val x = <!AMBIGUITY!>create<!>("") { 1 }
takeInt(x)
val x = create("") { 1 }
<!INAPPLICABLE_CANDIDATE!>takeInt<!>(x)
}
interface A
@@ -56,6 +56,6 @@ fun foo(f: () -> A): Int = 1
fun foo(f: () -> B): String = ""
fun test_6(c: C) {
val x = <!AMBIGUITY!>foo<!> { c }
val x = foo { c }
takeString(x)
}
@@ -1,24 +0,0 @@
// !LANGUAGE: -NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNUSED_EXPRESSION
// ISSUE: KT-11265
// FILE: OverloadResolutionByLambdaReturnType.kt
package kotlin
annotation class OverloadResolutionByLambdaReturnType
// FILE: main.kt
import kotlin.OverloadResolutionByLambdaReturnType
@OverloadResolutionByLambdaReturnType
fun create(f: (Int) -> Int): Int = 1
fun create(f: (Int) -> String): String = ""
fun takeString(s: String) {}
fun test_1() {
val x = <!AMBIGUITY!>create<!> { "" }
takeString("")
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: -NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNUSED_EXPRESSION
// ISSUE: KT-11265
@@ -25,10 +25,10 @@ inline fun <T, R> Array<out T>.myFlatMap(transform: (T) -> Sequence<R>): List<R>
fun String.toList(): List<String> = null!!
fun test_1(a: Array<String>, b: Boolean) {
a.<!AMBIGUITY!>myFlatMap<!> { <!UNRESOLVED_REFERENCE!>it<!>.toList().ifEmpty { return } }
a.<!AMBIGUITY!>myFlatMap<!> {
a.myFlatMap { it.toList().ifEmpty { return } }
a.myFlatMap {
if (b) return
<!UNRESOLVED_REFERENCE!>it<!>.toList()
it.toList()
}
}
@@ -43,9 +43,9 @@ fun <T, R> Array<out T>.noInlineFlatMap(transform: (T) -> Sequence<R>): List<R>
}
fun test_2(a: Array<String>, b: Boolean) {
a.<!AMBIGUITY!>noInlineFlatMap<!> { <!UNRESOLVED_REFERENCE!>it<!>.toList().ifEmpty { return } }
a.<!AMBIGUITY!>noInlineFlatMap<!> {
a.noInlineFlatMap { it.toList().ifEmpty { return } }
a.noInlineFlatMap {
if (b) return
<!UNRESOLVED_REFERENCE!>it<!>.toList()
it.toList()
}
}
@@ -23,21 +23,21 @@ val test3: MutableList<Int> =
}
val test4: Collection<Int> =
listOf(1, 2, 3).<!AMBIGUITY!>flatMapTo<!>(LinkedHashSet()) {
<!NONE_APPLICABLE!>listOf<!>(<!UNRESOLVED_REFERENCE!>it<!>)
listOf(1, 2, 3).flatMapTo(LinkedHashSet()) {
listOf(it)
}
val test5: Collection<Int> =
listOf(1, 2, 3).<!AMBIGUITY!>flatMapTo<!>(LinkedHashSet()) { // TODO
if (true) <!NONE_APPLICABLE!>listOf<!>(<!UNRESOLVED_REFERENCE!>it<!>) else <!NONE_APPLICABLE!>listOf<!>(<!UNRESOLVED_REFERENCE!>it<!>)
listOf(1, 2, 3).flatMapTo(LinkedHashSet()) { // TODO
if (true) listOf(it) else listOf(it)
}
val test6: Collection<Int> =
listOf(1, 2, 3).<!AMBIGUITY!>flatMapTo<!>(LinkedHashSet<Int>()) {
if (true) <!NONE_APPLICABLE!>listOf<!>(<!UNRESOLVED_REFERENCE!>it<!>) else <!NONE_APPLICABLE!>listOf<!>(<!UNRESOLVED_REFERENCE!>it<!>)
listOf(1, 2, 3).flatMapTo(LinkedHashSet<Int>()) {
if (true) listOf(it) else listOf(it)
}
val test7: Collection<Int> =
listOf(1, 2, 3).<!AMBIGUITY!>flatMapTo<!>(LinkedHashSet()) {
<!INAPPLICABLE_CANDIDATE!>select<!>(<!NONE_APPLICABLE!>listOf<!>(<!UNRESOLVED_REFERENCE!>it<!>), <!NONE_APPLICABLE!>listOf<!>(<!UNRESOLVED_REFERENCE!>it<!>))
listOf(1, 2, 3).flatMapTo(LinkedHashSet()) {
select(listOf(it), listOf(it))
}
@@ -4,12 +4,16 @@ FILE fqName:<root> fileName:/typeParametersInImplicitCast.kt
VALUE_PARAMETER name:lss index:0 type:kotlin.collections.List<kotlin.collections.List<T of <root>.problematic>>
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun problematic <T> (lss: kotlin.collections.List<kotlin.collections.List<T of <root>.problematic>>): kotlin.collections.List<T of <root>.problematic> declared in <root>'
ERROR_CALL 'Unresolved reference: <Ambiguity: flatMap, [kotlin/collections/flatMap, kotlin/collections/flatMap]>#' type=IrErrorType
FUN_EXPR type=kotlin.Function0<kotlin.collections.List<kotlin.Any?>> origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.collections.List<kotlin.Any?>
CALL 'public final fun flatMap <T, R> (transform: kotlin.Function1<T of kotlin.collections.flatMap, kotlin.collections.Iterable<R of kotlin.collections.flatMap>>): kotlin.collections.List<R of kotlin.collections.flatMap> [inline] declared in kotlin.collections' type=kotlin.collections.List<T of <root>.problematic> origin=null
<T>: kotlin.collections.List<T of <root>.problematic>
<R>: T of <root>.problematic
$receiver: GET_VAR 'lss: kotlin.collections.List<kotlin.collections.List<T of <root>.problematic>> declared in <root>.problematic' type=kotlin.collections.List<kotlin.collections.List<T of <root>.problematic>> origin=null
transform: FUN_EXPR type=kotlin.Function1<kotlin.collections.List<T of <root>.problematic>, kotlin.collections.Iterable<T of <root>.problematic>> origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> (it:kotlin.collections.List<T of <root>.problematic>) returnType:kotlin.collections.Iterable<T of <root>.problematic>
VALUE_PARAMETER name:it index:0 type:kotlin.collections.List<T of <root>.problematic>
BLOCK_BODY
RETURN type=kotlin.Nothing from='local final fun <anonymous> (): kotlin.collections.List<kotlin.Any?> declared in <root>.problematic'
TYPE_OP type=kotlin.collections.List<kotlin.Any?> origin=IMPLICIT_NOTNULL typeOperand=kotlin.collections.List<kotlin.Any?>
CALL 'public/*package*/ open fun id <T> (v: kotlin.collections.List<T of <root>.ListId.id?>?): kotlin.collections.List<T of <root>.ListId.id?> declared in <root>.ListId' type=kotlin.collections.List<kotlin.Any?> origin=null
<T>: kotlin.Any?
v: ERROR_CALL 'Unresolved reference: <Unresolved name: it>#' type=IrErrorType
RETURN type=kotlin.Nothing from='local final fun <anonymous> (it: kotlin.collections.List<T of <root>.problematic>): kotlin.collections.Iterable<T of <root>.problematic> declared in <root>.problematic'
TYPE_OP type=kotlin.collections.List<T of <root>.problematic?> origin=IMPLICIT_NOTNULL typeOperand=kotlin.collections.List<T of <root>.problematic?>
CALL 'public/*package*/ open fun id <T> (v: kotlin.collections.List<T of <root>.ListId.id?>?): kotlin.collections.List<T of <root>.ListId.id?> declared in <root>.ListId' type=kotlin.collections.List<T of <root>.problematic?> origin=null
<T>: T of <root>.problematic?
v: GET_VAR 'it: kotlin.collections.List<T of <root>.problematic> declared in <root>.problematic.<anonymous>' type=kotlin.collections.List<T of <root>.problematic> origin=null