K2: allow/disallow coercion-to-unit for callable references more precisely

Before this commit, K2 always applied coercion-to-unit for
callable references if expected type was Unit, and actual non-Unit.
However, this may not work in case when actual return type is
a type parameter and it must be inferred into Unit.
In this commit we started to disallow coercion-to-unit
for references with synthetic outer call (~ top-level in K1)
AND a type parameter as a return type (both should be true to disallow).
This provides better K1 consistency,
while still keeping some broken K1 cases working in K2.

See also added comment in CallableReferenceResolution.kt.

#KT-62565 Fixed
This commit is contained in:
Mikhail Glukhikh
2023-12-05 15:12:31 +01:00
committed by Space Team
parent 25c0bd278e
commit 2680c8effd
16 changed files with 213 additions and 16 deletions
@@ -4833,6 +4833,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia
runTest("compiler/testData/diagnostics/tests/callableReference/function/ambiguityTopLevelVsTopLevel.kt");
}
@Test
@TestMetadata("ambiguityTopLevelVsTopLevelGeneric.kt")
public void testAmbiguityTopLevelVsTopLevelGeneric() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/function/ambiguityTopLevelVsTopLevelGeneric.kt");
}
@Test
@TestMetadata("annotationClassConstructor.kt")
public void testAnnotationClassConstructor() throws Exception {
@@ -17463,6 +17469,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia
runTest("compiler/testData/diagnostics/tests/inference/callableReferences/conversionLastStatementInLambda.kt");
}
@Test
@TestMetadata("conversionLastStatementInLambdaGeneric.kt")
public void testConversionLastStatementInLambdaGeneric() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/callableReferences/conversionLastStatementInLambdaGeneric.kt");
}
@Test
@TestMetadata("kt55931.kt")
public void testKt55931() throws Exception {
@@ -17825,6 +17837,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/coersionWithAnonymousFunctionsAndUnresolved.kt");
}
@Test
@TestMetadata("genericInApply.kt")
public void testGenericInApply() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/genericInApply.kt");
}
@Test
@TestMetadata("indirectCoercionWithExpectedType.kt")
public void testIndirectCoercionWithExpectedType() throws Exception {
@@ -4833,6 +4833,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated
runTest("compiler/testData/diagnostics/tests/callableReference/function/ambiguityTopLevelVsTopLevel.kt");
}
@Test
@TestMetadata("ambiguityTopLevelVsTopLevelGeneric.kt")
public void testAmbiguityTopLevelVsTopLevelGeneric() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/function/ambiguityTopLevelVsTopLevelGeneric.kt");
}
@Test
@TestMetadata("annotationClassConstructor.kt")
public void testAnnotationClassConstructor() throws Exception {
@@ -17463,6 +17469,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated
runTest("compiler/testData/diagnostics/tests/inference/callableReferences/conversionLastStatementInLambda.kt");
}
@Test
@TestMetadata("conversionLastStatementInLambdaGeneric.kt")
public void testConversionLastStatementInLambdaGeneric() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/callableReferences/conversionLastStatementInLambdaGeneric.kt");
}
@Test
@TestMetadata("kt55931.kt")
public void testKt55931() throws Exception {
@@ -17825,6 +17837,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/coersionWithAnonymousFunctionsAndUnresolved.kt");
}
@Test
@TestMetadata("genericInApply.kt")
public void testGenericInApply() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/genericInApply.kt");
}
@Test
@TestMetadata("indirectCoercionWithExpectedType.kt")
public void testIndirectCoercionWithExpectedType() throws Exception {
@@ -4827,6 +4827,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir
runTest("compiler/testData/diagnostics/tests/callableReference/function/ambiguityTopLevelVsTopLevel.kt");
}
@Test
@TestMetadata("ambiguityTopLevelVsTopLevelGeneric.kt")
public void testAmbiguityTopLevelVsTopLevelGeneric() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/function/ambiguityTopLevelVsTopLevelGeneric.kt");
}
@Test
@TestMetadata("annotationClassConstructor.kt")
public void testAnnotationClassConstructor() throws Exception {
@@ -17457,6 +17463,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir
runTest("compiler/testData/diagnostics/tests/inference/callableReferences/conversionLastStatementInLambda.kt");
}
@Test
@TestMetadata("conversionLastStatementInLambdaGeneric.kt")
public void testConversionLastStatementInLambdaGeneric() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/callableReferences/conversionLastStatementInLambdaGeneric.kt");
}
@Test
@TestMetadata("kt55931.kt")
public void testKt55931() throws Exception {
@@ -17819,6 +17831,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/coersionWithAnonymousFunctionsAndUnresolved.kt");
}
@Test
@TestMetadata("genericInApply.kt")
public void testGenericInApply() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/genericInApply.kt");
}
@Test
@TestMetadata("indirectCoercionWithExpectedType.kt")
public void testIndirectCoercionWithExpectedType() throws Exception {
@@ -4833,6 +4833,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia
runTest("compiler/testData/diagnostics/tests/callableReference/function/ambiguityTopLevelVsTopLevel.kt");
}
@Test
@TestMetadata("ambiguityTopLevelVsTopLevelGeneric.kt")
public void testAmbiguityTopLevelVsTopLevelGeneric() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/function/ambiguityTopLevelVsTopLevelGeneric.kt");
}
@Test
@TestMetadata("annotationClassConstructor.kt")
public void testAnnotationClassConstructor() throws Exception {
@@ -17463,6 +17469,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia
runTest("compiler/testData/diagnostics/tests/inference/callableReferences/conversionLastStatementInLambda.kt");
}
@Test
@TestMetadata("conversionLastStatementInLambdaGeneric.kt")
public void testConversionLastStatementInLambdaGeneric() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/callableReferences/conversionLastStatementInLambdaGeneric.kt");
}
@Test
@TestMetadata("kt55931.kt")
public void testKt55931() throws Exception {
@@ -17825,6 +17837,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/coersionWithAnonymousFunctionsAndUnresolved.kt");
}
@Test
@TestMetadata("genericInApply.kt")
public void testGenericInApply() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/genericInApply.kt");
}
@Test
@TestMetadata("indirectCoercionWithExpectedType.kt")
public void testIndirectCoercionWithExpectedType() throws Exception {
@@ -375,6 +375,7 @@ class FirCallResolver(
fun resolveCallableReference(
constraintSystemBuilder: ConstraintSystemBuilder,
resolvedCallableReferenceAtom: ResolvedCallableReferenceAtom,
hasSyntheticOuterCall: Boolean,
): Pair<CandidateApplicability, Boolean> {
val callableReferenceAccess = resolvedCallableReferenceAtom.reference
val calleeReference = callableReferenceAccess.calleeReference
@@ -383,8 +384,7 @@ class FirCallResolver(
val expectedType = resolvedCallableReferenceAtom.expectedType?.let(coneSubstitutor::substituteOrSelf)
val info = createCallableReferencesInfoForLHS(
callableReferenceAccess, lhs,
expectedType, constraintSystemBuilder,
callableReferenceAccess, lhs, expectedType, constraintSystemBuilder, hasSyntheticOuterCall
)
// No reset here!
val localCollector = CandidateCollector(components, components.resolutionStageRunner)
@@ -685,6 +685,7 @@ class FirCallResolver(
lhs: DoubleColonLHS?,
expectedType: ConeKotlinType?,
outerConstraintSystemBuilder: ConstraintSystemBuilder?,
hasSyntheticOuterCall: Boolean,
): CallInfo {
return CallInfo(
callableReferenceAccess,
@@ -703,6 +704,7 @@ class FirCallResolver(
expectedType,
outerConstraintSystemBuilder,
lhs,
hasSyntheticOuterCall,
)
}
@@ -37,10 +37,11 @@ data class CallInfo(
val candidateForCommonInvokeReceiver: Candidate? = null,
// Four properties for callable references only
// Five properties for callable references only
val expectedType: ConeKotlinType? = null,
val outerCSBuilder: ConstraintSystemBuilder? = null,
val lhs: DoubleColonLHS? = null,
val hasSyntheticOuterCall: Boolean = false,
val origin: FirFunctionCallOrigin = FirFunctionCallOrigin.Regular,
) : AbstractCallInfo() {
val arguments: List<FirExpression> get() = argumentList.arguments
@@ -136,18 +136,33 @@ private fun buildResultingTypeAndAdaptation(
parameters += receiverType
}
val returnType = callableReferenceAdaptation?.let {
parameters += it.argumentTypes
if (it.coercionStrategy == CoercionStrategy.COERCION_TO_UNIT) {
context.session.builtinTypes.unitType.type
} else {
returnTypeRef.coneType
val returnTypeWithoutCoercion = returnTypeRef.coneType
val returnType = if (callableReferenceAdaptation == null) {
returnTypeWithoutCoercion.also {
fir.valueParameters.mapTo(parameters) { it.returnTypeRef.coneType }
}
} else {
parameters += callableReferenceAdaptation.argumentTypes
// K1 simply doesn't perform any conversions for so-called "top level callable references",
// only for "callable reference arguments"
// (see CallableReferencesCandidateFactory.buildReflectionType, val buildTypeWithConversions)
// Here hasSyntheticOuterCall is ~ an equivalent of "top level callable references" in K1
// K2 behavior differs at least in two aspects:
// - it still allows to drop some default parameters / convert some varargs
// - see testData/diagnostics/tests/callableReference/adapted/simpleAdaptationOutsideOfCall.kt
// - see testData/diagnostics/tests/callableReference/resolve/withVararg.kt
// - coercion to unit is still allowed if a candidate return type is not type parameter based
// - see testData/diagnostics/tests/inference/callableReferences/conversionLastStatementInLambda.kt
val hasSyntheticOuterCall = candidate.callInfo.hasSyntheticOuterCall
if (callableReferenceAdaptation.coercionStrategy != CoercionStrategy.COERCION_TO_UNIT ||
hasSyntheticOuterCall && returnTypeWithoutCoercion.unwrapFlexibleAndDefinitelyNotNull() is ConeTypeParameterType
) {
returnTypeWithoutCoercion
} else {
context.session.builtinTypes.unitType.type
}
} ?: returnTypeRef.coneType.also {
fir.valueParameters.mapTo(parameters) { it.returnTypeRef.coneType }
}
val baseFunctionTypeKind = callableReferenceAdaptation?.suspendConversionStrategy?.kind
?: fir.specialFunctionTypeKind(context.session)
?: FunctionTypeKind.Function
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.fir.scopes.impl.typeAliasForConstructor
import org.jetbrains.kotlin.fir.scopes.processOverriddenFunctions
import org.jetbrains.kotlin.fir.symbols.ConeTypeParameterLookupTag
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.SyntheticCallableId.ACCEPT_SPECIFIC_TYPE
import org.jetbrains.kotlin.fir.symbols.SyntheticSymbol
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.fir.symbols.lazyResolveToPhase
@@ -562,7 +563,9 @@ internal object EagerResolveOfCallableReferences : CheckerStage() {
for (atom in candidate.postponedAtoms) {
if (atom is ResolvedCallableReferenceAtom) {
val (applicability, success) =
context.bodyResolveComponents.callResolver.resolveCallableReference(candidate.csBuilder, atom)
context.bodyResolveComponents.callResolver.resolveCallableReference(
candidate.csBuilder, atom, hasSyntheticOuterCall = candidate.callInfo.name == ACCEPT_SPECIFIC_TYPE.callableName
)
if (!success) {
// If the resolution was unsuccessful, we ensure that an error will be reported for the callable reference
// during completion by using the `resultingReference` of the postponed atom.
@@ -66,7 +66,7 @@ class PostponedArgumentsAnalyzer(
private fun processCallableReference(atom: ResolvedCallableReferenceAtom, candidate: Candidate) {
if (atom.mightNeedAdditionalResolution) {
callResolver.resolveCallableReference(candidate.csBuilder, atom)
callResolver.resolveCallableReference(candidate.csBuilder, atom, hasSyntheticOuterCall = false)
}
val callableReferenceAccess = atom.reference
@@ -0,0 +1,8 @@
fun foo(x: Int, y: Any): Int = x
fun <T> bar(x: T, y: Any): T = x
fun main() {
val fooRef: (Int, Any) -> Unit = ::foo
val barRef: (Int, Any) -> Unit = ::<!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>bar<!>
}
@@ -0,0 +1,8 @@
fun foo(x: Int, y: Any): Int = x
fun <T> bar(x: T, y: Any): T = x
fun main() {
val fooRef: (Int, Any) -> Unit = <!TYPE_MISMATCH!>::<!TYPE_MISMATCH!>foo<!><!>
val barRef: (Int, Any) -> Unit = <!TYPE_MISMATCH!>::<!TYPE_MISMATCH!>bar<!><!>
}
@@ -11,7 +11,7 @@ class A1 {
class A2 {
fun <K, V> a2(key: K): V = TODO()
fun test1(): (String) -> Unit = A2()::<!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>a2<!>
fun test1(): (String) -> Unit = A2()::a2
fun <T3> test2(): (T3) -> T3 = A2()::a2
}
@@ -19,7 +19,7 @@ class A3<T> {
fun <V> a3(key: T): V = TODO()
fun test1(): (T) -> Int = this::a3
fun test2(): (T) -> Unit = A3<T>()::<!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>a3<!>
fun test2(): (T) -> Unit = A3<T>()::a3
fun test3(): (Int) -> String = A3<Int>()::a3
fun <R> test4(): (R) -> Unit = <!RETURN_TYPE_MISMATCH!>this::<!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>a3<!><!>
@@ -0,0 +1,30 @@
// DIAGNOSTICS: -UNCHECKED_CAST
// This test is a copy of conversionLastStatementInLambda, but with generic functions
// However works diffirently in K1
fun main(b: Boolean) {
callWithLambda {
::test1
}
callWithLambda {
if (b) ::test1 else ::test2
}
callWithLambda {
if (b) {
::test1
} else {
::test2
}
}
callWithLambda {
(::test1)
}
}
fun <T> test1(): T = "" as T
fun <T> test2(): T = "" as T
fun callWithLambda(action: () -> () -> Unit) {}
@@ -0,0 +1,30 @@
// DIAGNOSTICS: -UNCHECKED_CAST
// This test is a copy of conversionLastStatementInLambda, but with generic functions
// However works diffirently in K1
fun main(b: Boolean) {
callWithLambda {
<!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>::test1<!>
}
callWithLambda {
if (b) <!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>::test1<!> else <!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>::test2<!>
}
callWithLambda {
if (b) {
::test1
} else {
::test2
}
}
callWithLambda {
(<!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>::test1<!>)
}
}
fun <T> test1(): T = "" as T
fun <T> test2(): T = "" as T
fun callWithLambda(action: () -> () -> Unit) {}
@@ -0,0 +1,10 @@
// FIR_IDENTICAL
// WITH_STDLIB
fun <N : Number> addNumber(n: N) = n
fun foo() = 42
fun test() {
foo().apply(::addNumber)
}
@@ -4833,6 +4833,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/callableReference/function/ambiguityTopLevelVsTopLevel.kt");
}
@Test
@TestMetadata("ambiguityTopLevelVsTopLevelGeneric.kt")
public void testAmbiguityTopLevelVsTopLevelGeneric() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/function/ambiguityTopLevelVsTopLevelGeneric.kt");
}
@Test
@TestMetadata("annotationClassConstructor.kt")
public void testAnnotationClassConstructor() throws Exception {
@@ -17463,6 +17469,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/inference/callableReferences/conversionLastStatementInLambda.kt");
}
@Test
@TestMetadata("conversionLastStatementInLambdaGeneric.kt")
public void testConversionLastStatementInLambdaGeneric() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/callableReferences/conversionLastStatementInLambdaGeneric.kt");
}
@Test
@TestMetadata("kt55931.kt")
public void testKt55931() throws Exception {
@@ -17825,6 +17837,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/coersionWithAnonymousFunctionsAndUnresolved.kt");
}
@Test
@TestMetadata("genericInApply.kt")
public void testGenericInApply() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/genericInApply.kt");
}
@Test
@TestMetadata("indirectCoercionWithExpectedType.kt")
public void testIndirectCoercionWithExpectedType() throws Exception {