[FIR] Report error about ambiguous anonymous type approximation in type arguments of implicit return type of function

^KT-33917
^KT-56491 Fixed
This commit is contained in:
Dmitriy Novozhilov
2023-02-08 13:18:59 +02:00
committed by Space Team
parent 3c42521ce7
commit 393f880322
2 changed files with 20 additions and 4 deletions
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.fir.analysis.checkers.declaration
import org.jetbrains.kotlin.KtSourceElement
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors
@@ -45,18 +46,33 @@ object FirAmbiguousAnonymousTypeChecker : FirBasicDeclarationChecker() {
else -> error("Should not be there")
} ?: return
val classSymbol = typeRef.coneType.toSymbol(context.session) ?: return
checkTypeAndArguments(typeRef.coneType, context, reporter, declaration.source)
}
private fun checkTypeAndArguments(
type: ConeKotlinType,
context: CheckerContext,
reporter: DiagnosticReporter,
reportOn: KtSourceElement?
) {
val classSymbol = type.toSymbol(context.session)
if (classSymbol is FirAnonymousObjectSymbol && classSymbol.resolvedSuperTypeRefs.size > 1) {
// Any anonymous object that has only one super type is already approximated to the super type by
// org.jetbrains.kotlin.fir.types.TypeUtilsKt#hideLocalTypeIfNeeded. Hence, any remaining anonymous object must have more than
// one super types and hence are ambiguous.
reporter.reportOn(
declaration.source,
reportOn,
FirErrors.AMBIGUOUS_ANONYMOUS_TYPE_INFERRED,
classSymbol.resolvedSuperTypeRefs.map { it.coneType },
context
)
}
for (typeArgument in type.typeArguments) {
checkTypeAndArguments(
typeArgument.type ?: continue,
context, reporter, reportOn
)
}
}
private val FirBlock.singleExpressionType
@@ -34,11 +34,11 @@ private inline fun foo22(crossinline f: () -> Int) = Inv(Inv(object : I1 {
fun bar(): Int = f()
}
private inline fun foo31(crossinline f: () -> Int) = Inv(object : I1, I2 {
<!AMBIGUOUS_ANONYMOUS_TYPE_INFERRED!>private inline fun foo31(crossinline f: () -> Int)<!> = Inv(object : I1, I2 {
fun bar(): Int = f()
})
private inline fun foo32(crossinline f: () -> Int) = Inv(Inv(object : I1, I2 {
<!AMBIGUOUS_ANONYMOUS_TYPE_INFERRED!>private inline fun foo32(crossinline f: () -> Int)<!> = Inv(Inv(object : I1, I2 {
fun bar(): Int = f()
}))