FIR: Implement FE 1.0 semantics for super unqualified calls
See original logic at org.jetbrains.kotlin.types.expressions.unqualifiedSuper.UnqualifiedSuperKt#resolveUnqualifiedSuperFromExpressionContext ^KT-39070 Fixed ^KT-39599 Related
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
interface A
|
||||
|
||||
open class B {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
return super.equals(other)
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return super.hashCode()
|
||||
}
|
||||
}
|
||||
|
||||
class C : A, B() {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
return super.equals(other)
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return super.hashCode()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
FILE: superAny.kt
|
||||
public abstract interface A : R|kotlin/Any| {
|
||||
}
|
||||
public open class B : R|kotlin/Any| {
|
||||
public constructor(): R|B| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
public open override fun equals(other: R|kotlin/Any?|): R|kotlin/Boolean| {
|
||||
^equals this@R|/B|.super<R|kotlin/Any|>.R|kotlin/Any.equals|(R|<local>/other|)
|
||||
}
|
||||
|
||||
public open override fun hashCode(): R|kotlin/Int| {
|
||||
^hashCode this@R|/B|.super<R|kotlin/Any|>.R|kotlin/Any.hashCode|()
|
||||
}
|
||||
|
||||
}
|
||||
public final class C : R|A|, R|B| {
|
||||
public constructor(): R|C| {
|
||||
super<R|B|>()
|
||||
}
|
||||
|
||||
public final override fun equals(other: R|kotlin/Any?|): R|kotlin/Boolean| {
|
||||
^equals this@R|/C|.super<R|B|>.R|/B.equals|(R|<local>/other|)
|
||||
}
|
||||
|
||||
public final override fun hashCode(): R|kotlin/Int| {
|
||||
^hashCode this@R|/C|.super<R|B|>.R|/B.hashCode|()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,7 +15,7 @@ class C : A, B() {
|
||||
override fun foo() {
|
||||
super.foo()
|
||||
|
||||
super.<!AMBIGUITY!>bar<!>() // should be ambiguity (NB: really we should have overridden bar in C)
|
||||
super.<!UNRESOLVED_REFERENCE!>bar<!>() // should be ambiguity (NB: really we should have overridden bar in C)
|
||||
|
||||
super.baz() // Ok
|
||||
baz() // Ok
|
||||
|
||||
@@ -27,9 +27,9 @@ FILE: incorrectSuperCall.kt
|
||||
}
|
||||
|
||||
public final override fun foo(): R|kotlin/Unit| {
|
||||
this@R|/C|.super<R|A|R|B|>.R|/B.foo|()
|
||||
this@R|/C|.super<R|A|R|B|>.<Ambiguity: bar, [/A.bar, /B.bar]>#()
|
||||
this@R|/C|.super<R|A|R|B|>.R|/B.baz|()
|
||||
this@R|/C|.super<R|B|>.R|/B.foo|()
|
||||
this@R|/C|.super<<ERROR TYPE REF: Ambiguous supertype>>.<Unresolved name: bar>#()
|
||||
this@R|/C|.super<R|B|>.R|/B.baz|()
|
||||
this@R|/C|.R|/A.baz|()
|
||||
}
|
||||
|
||||
|
||||
Generated
+5
@@ -654,6 +654,11 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/callResolution/safeCallOnTypeAlias.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("superAny.kt")
|
||||
public void testSuperAny() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/callResolution/superAny.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("syntheticPropertiesWrongImplicitReceiver.kt")
|
||||
public void testSyntheticPropertiesWrongImplicitReceiver() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/callResolution/syntheticPropertiesWrongImplicitReceiver.kt");
|
||||
|
||||
+5
@@ -654,6 +654,11 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/callResolution/safeCallOnTypeAlias.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("superAny.kt")
|
||||
public void testSuperAny() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/callResolution/superAny.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("syntheticPropertiesWrongImplicitReceiver.kt")
|
||||
public void testSyntheticPropertiesWrongImplicitReceiver() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/callResolution/syntheticPropertiesWrongImplicitReceiver.kt");
|
||||
|
||||
+2
-2
@@ -20,7 +20,7 @@ import org.jetbrains.kotlin.utils.addToStdlib.safeAs
|
||||
|
||||
object FirNotASupertypeChecker : FirQualifiedAccessChecker() {
|
||||
override fun check(functionCall: FirQualifiedAccessExpression, context: CheckerContext, reporter: DiagnosticReporter) {
|
||||
val superReference = functionCall.calleeReference.safeAs<FirSuperReference>()
|
||||
val superReference = functionCall.calleeReference.safeAs<FirSuperReference>()?.takeIf { it.hadExplicitTypeInSource() }
|
||||
|
||||
val targetClass = superReference
|
||||
?.superTypeRef
|
||||
@@ -57,4 +57,4 @@ object FirNotASupertypeChecker : FirQualifiedAccessChecker() {
|
||||
report(FirErrors.NOT_A_SUPERTYPE.on(it))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -23,6 +23,7 @@ object FirQualifiedSupertypeExtendedByOtherSupertypeChecker : FirQualifiedAccess
|
||||
override fun check(functionCall: FirQualifiedAccessExpression, context: CheckerContext, reporter: DiagnosticReporter) {
|
||||
// require to be called over a super reference
|
||||
val superReference = functionCall.calleeReference.safeAs<FirSuperReference>()
|
||||
?.takeIf { it.hadExplicitTypeInSource() }
|
||||
?: return
|
||||
|
||||
val explicitType = superReference.superTypeRef.safeAs<FirResolvedTypeRef>()
|
||||
@@ -62,4 +63,4 @@ object FirQualifiedSupertypeExtendedByOtherSupertypeChecker : FirQualifiedAccess
|
||||
report(FirErrors.QUALIFIED_SUPERTYPE_EXTENDED_BY_OTHER_SUPERTYPE.on(it))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+12
-9
@@ -5,23 +5,26 @@
|
||||
|
||||
package org.jetbrains.kotlin.fir.analysis.checkers.expression
|
||||
|
||||
import org.jetbrains.kotlin.KtNodeTypes.*
|
||||
import org.jetbrains.kotlin.KtNodeTypes.CLASS
|
||||
import org.jetbrains.kotlin.KtNodeTypes.OBJECT_LITERAL
|
||||
import org.jetbrains.kotlin.fir.FirSourceElement
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.DiagnosticReporter
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors
|
||||
import org.jetbrains.kotlin.fir.expressions.FirQualifiedAccessExpression
|
||||
import org.jetbrains.kotlin.fir.references.FirSuperReference
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
|
||||
|
||||
object FirSuperNotAvailableChecker : FirQualifiedAccessChecker() {
|
||||
override fun check(functionCall: FirQualifiedAccessExpression, context: CheckerContext, reporter: DiagnosticReporter) {
|
||||
if (functionCall.source?.elementType == SUPER_EXPRESSION) {
|
||||
val isInsideClass = context.containingDeclarations.any {
|
||||
it.source?.elementType == CLASS || it.source?.elementType == OBJECT_LITERAL
|
||||
}
|
||||
if (functionCall.calleeReference.safeAs<FirSuperReference>()?.hadExplicitTypeInSource() != true) return
|
||||
|
||||
if (!isInsideClass) {
|
||||
reporter.report(functionCall.source)
|
||||
}
|
||||
val isInsideClass = context.containingDeclarations.any {
|
||||
it.source?.elementType == CLASS || it.source?.elementType == OBJECT_LITERAL
|
||||
}
|
||||
|
||||
if (!isInsideClass) {
|
||||
reporter.report(functionCall.source)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,4 +33,4 @@ object FirSuperNotAvailableChecker : FirQualifiedAccessChecker() {
|
||||
report(FirErrors.SUPER_NOT_AVAILABLE.on(it))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* 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.analysis.checkers.expression
|
||||
|
||||
import org.jetbrains.kotlin.fir.FirFakeSourceElementKind
|
||||
import org.jetbrains.kotlin.fir.references.FirSuperReference
|
||||
|
||||
fun FirSuperReference.hadExplicitTypeInSource(): Boolean = superTypeRef.source?.kind != FirFakeSourceElementKind.SuperCallImplicitType
|
||||
@@ -117,7 +117,7 @@ class FirCallResolver(
|
||||
explicitReceiver.calleeReference as? FirSuperReference
|
||||
?: return transformExplicitReceiver(transformer, ResolutionMode.ContextIndependent) as Q
|
||||
|
||||
transformer.transformSuperReceiver(callee, explicitReceiver)
|
||||
transformer.transformSuperReceiver(callee, explicitReceiver, this)
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
/*
|
||||
* 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 com.intellij.util.SmartList
|
||||
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||
import org.jetbrains.kotlin.descriptors.Modality
|
||||
import org.jetbrains.kotlin.fir.declarations.FirCallableMemberDeclaration
|
||||
import org.jetbrains.kotlin.fir.declarations.FirProperty
|
||||
import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction
|
||||
import org.jetbrains.kotlin.fir.declarations.modality
|
||||
import org.jetbrains.kotlin.fir.expressions.FirFunctionCall
|
||||
import org.jetbrains.kotlin.fir.expressions.FirQualifiedAccess
|
||||
import org.jetbrains.kotlin.fir.references.FirNamedReference
|
||||
import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents
|
||||
import org.jetbrains.kotlin.fir.resolve.scope
|
||||
import org.jetbrains.kotlin.fir.resolve.toSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.StandardClassIds
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.unwrapSubstitutionOverrides
|
||||
import org.jetbrains.kotlin.fir.typeContext
|
||||
import org.jetbrains.kotlin.fir.types.ConeClassLikeType
|
||||
import org.jetbrains.kotlin.fir.types.ConeKotlinType
|
||||
import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef
|
||||
import org.jetbrains.kotlin.fir.types.FirTypeRef
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.types.AbstractTypeChecker
|
||||
import org.jetbrains.kotlin.utils.addIfNotNull
|
||||
|
||||
fun BodyResolveComponents.findTypesForSuperCandidates(
|
||||
superTypeRefs: List<FirTypeRef>,
|
||||
containingCall: FirQualifiedAccess,
|
||||
): Collection<ConeKotlinType> {
|
||||
val supertypes = superTypeRefs.map { (it as FirResolvedTypeRef).type }
|
||||
if (supertypes.size <= 1) return supertypes
|
||||
|
||||
return when (containingCall) {
|
||||
is FirFunctionCall -> {
|
||||
val calleeName = containingCall.calleeReference.name
|
||||
if (isCallingMethodOfAny(containingCall)) {
|
||||
resolveSupertypesForMethodOfAny(supertypes, calleeName)
|
||||
} else {
|
||||
resolveSupertypesByCalleeName(supertypes, calleeName)
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
resolveSupertypesByPropertyName(
|
||||
supertypes,
|
||||
(containingCall.calleeReference as? FirNamedReference)?.name ?: return emptyList()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val ARITY_OF_METHODS_OF_ANY = hashMapOf("hashCode" to 0, "equals" to 1, "toString" to 0)
|
||||
|
||||
private fun isCallingMethodOfAny(callExpression: FirFunctionCall): Boolean =
|
||||
ARITY_OF_METHODS_OF_ANY.getOrElse(callExpression.calleeReference.name.asString(), { -1 }) == callExpression.argumentList.arguments.size
|
||||
|
||||
private fun BodyResolveComponents.resolveSupertypesForMethodOfAny(
|
||||
supertypes: Collection<ConeKotlinType>,
|
||||
calleeName: Name
|
||||
): Collection<ConeKotlinType> {
|
||||
val typesWithConcreteOverride = resolveSupertypesByMembers(supertypes, false) {
|
||||
getFunctionMembers(it, calleeName)
|
||||
}
|
||||
return if (typesWithConcreteOverride.isNotEmpty())
|
||||
typesWithConcreteOverride
|
||||
else
|
||||
listOf(session.builtinTypes.anyType.type)
|
||||
}
|
||||
|
||||
private fun BodyResolveComponents.resolveSupertypesByCalleeName(supertypes: Collection<ConeKotlinType>, calleeName: Name): Collection<ConeKotlinType> =
|
||||
resolveSupertypesByMembers(supertypes, true) {
|
||||
getFunctionMembers(it, calleeName) +
|
||||
getPropertyMembers(it, calleeName)
|
||||
}
|
||||
|
||||
private fun BodyResolveComponents.resolveSupertypesByPropertyName(supertypes: Collection<ConeKotlinType>, propertyName: Name): Collection<ConeKotlinType> =
|
||||
resolveSupertypesByMembers(supertypes, true) {
|
||||
getPropertyMembers(it, propertyName)
|
||||
}
|
||||
|
||||
private inline fun BodyResolveComponents.resolveSupertypesByMembers(
|
||||
supertypes: Collection<ConeKotlinType>,
|
||||
allowNonConcreteMembers: Boolean,
|
||||
getMembers: (ConeKotlinType) -> Collection<FirCallableMemberDeclaration<*>>
|
||||
): Collection<ConeKotlinType> {
|
||||
val typesWithConcreteMembers = SmartList<ConeKotlinType>()
|
||||
val typesWithNonConcreteMembers = SmartList<ConeKotlinType>()
|
||||
|
||||
for (supertype in supertypes) {
|
||||
val members = getMembers(supertype)
|
||||
if (members.isNotEmpty()) {
|
||||
if (members.any { isConcreteMember(supertype, it) })
|
||||
typesWithConcreteMembers.add(supertype)
|
||||
else
|
||||
typesWithNonConcreteMembers.add(supertype)
|
||||
}
|
||||
}
|
||||
|
||||
typesWithConcreteMembers.removeAll { typeWithConcreteMember ->
|
||||
typesWithNonConcreteMembers.any { typeWithNonConcreteMember ->
|
||||
AbstractTypeChecker.isSubtypeOf(session.typeContext, typeWithNonConcreteMember, typeWithConcreteMember)
|
||||
}
|
||||
}
|
||||
|
||||
return when {
|
||||
typesWithConcreteMembers.isNotEmpty() ->
|
||||
typesWithConcreteMembers
|
||||
allowNonConcreteMembers ->
|
||||
typesWithNonConcreteMembers
|
||||
else ->
|
||||
emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalStdlibApi::class)
|
||||
private fun BodyResolveComponents.getFunctionMembers(type: ConeKotlinType, name: Name): Collection<FirCallableMemberDeclaration<*>> = buildList {
|
||||
type.scope(session, scopeSession)?.processFunctionsByName(name) {
|
||||
addIfNotNull(it.fir as? FirSimpleFunction)
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalStdlibApi::class)
|
||||
private fun BodyResolveComponents.getPropertyMembers(type: ConeKotlinType, name: Name): Collection<FirCallableMemberDeclaration<*>> = buildList {
|
||||
type.scope(session, scopeSession)?.processPropertiesByName(name) {
|
||||
addIfNotNull(it.fir as? FirProperty)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun BodyResolveComponents.isConcreteMember(supertype: ConeKotlinType, member: FirCallableMemberDeclaration<*>): Boolean {
|
||||
// "Concrete member" is a function or a property that is not abstract,
|
||||
// and is not an implicit fake override for a method of Any on an interface.
|
||||
|
||||
if (member.modality == Modality.ABSTRACT)
|
||||
return false
|
||||
|
||||
val classSymbol =
|
||||
(supertype as? ConeClassLikeType)?.lookupTag?.toSymbol(session) as? FirRegularClassSymbol ?: return true
|
||||
if (classSymbol.fir.classKind != ClassKind.INTERFACE) return true
|
||||
return member.symbol.unwrapSubstitutionOverrides().callableId.classId != StandardClassIds.Any
|
||||
}
|
||||
+16
-12
@@ -15,7 +15,6 @@ import org.jetbrains.kotlin.fir.diagnostics.DiagnosticKind
|
||||
import org.jetbrains.kotlin.fir.expressions.*
|
||||
import org.jetbrains.kotlin.fir.expressions.builder.buildErrorExpression
|
||||
import org.jetbrains.kotlin.fir.expressions.builder.buildFunctionCall
|
||||
import org.jetbrains.kotlin.fir.expressions.builder.buildVarargArgumentsExpression
|
||||
import org.jetbrains.kotlin.fir.expressions.builder.buildVariableAssignment
|
||||
import org.jetbrains.kotlin.fir.references.*
|
||||
import org.jetbrains.kotlin.fir.references.builder.buildErrorNamedReference
|
||||
@@ -39,7 +38,6 @@ import org.jetbrains.kotlin.fir.visitors.*
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.types.Variance
|
||||
import kotlin.math.min
|
||||
|
||||
open class FirExpressionsResolveTransformer(transformer: FirBodyResolveTransformer) : FirPartialBodyResolveTransformer(transformer) {
|
||||
private inline val builtinTypes: BuiltinTypes get() = session.builtinTypes
|
||||
@@ -84,7 +82,7 @@ open class FirExpressionsResolveTransformer(transformer: FirBodyResolveTransform
|
||||
qualifiedAccessExpression
|
||||
}
|
||||
is FirSuperReference -> {
|
||||
transformSuperReceiver(callee, qualifiedAccessExpression)
|
||||
transformSuperReceiver(callee, qualifiedAccessExpression, null)
|
||||
}
|
||||
is FirDelegateFieldReference -> {
|
||||
val delegateFieldSymbol = callee.resolvedSymbol
|
||||
@@ -126,7 +124,8 @@ open class FirExpressionsResolveTransformer(transformer: FirBodyResolveTransform
|
||||
|
||||
fun transformSuperReceiver(
|
||||
superReference: FirSuperReference,
|
||||
superReferenceContainer: FirQualifiedAccessExpression
|
||||
superReferenceContainer: FirQualifiedAccessExpression,
|
||||
containingCall: FirQualifiedAccess?
|
||||
): FirQualifiedAccessExpression {
|
||||
val labelName = superReference.labelName
|
||||
val implicitReceiver =
|
||||
@@ -146,21 +145,26 @@ open class FirExpressionsResolveTransformer(transformer: FirBodyResolveTransform
|
||||
else -> {
|
||||
val superTypeRefs = implicitReceiver?.boundSymbol?.phasedFir?.superTypeRefs
|
||||
val resultType = when {
|
||||
superTypeRefs?.isNotEmpty() != true -> {
|
||||
superTypeRefs?.isNotEmpty() != true || containingCall == null -> {
|
||||
buildErrorTypeRef {
|
||||
source = superReferenceContainer.source
|
||||
// NB: NOT_A_SUPERTYPE is reported by a separate checker
|
||||
diagnostic = ConeStubDiagnostic(ConeSimpleDiagnostic("No super type", DiagnosticKind.Other))
|
||||
}
|
||||
}
|
||||
superTypeRefs.size == 1 -> {
|
||||
superTypeRefs.single()
|
||||
}
|
||||
else -> {
|
||||
buildComposedSuperTypeRef {
|
||||
source = superReferenceContainer.source
|
||||
superTypeRefs.mapTo(this.superTypeRefs) { it as FirResolvedTypeRef }
|
||||
}
|
||||
val types = components.findTypesForSuperCandidates(superTypeRefs, containingCall)
|
||||
if (types.size == 1)
|
||||
buildResolvedTypeRef {
|
||||
source = superReferenceContainer.source?.fakeElement(FirFakeSourceElementKind.SuperCallImplicitType)
|
||||
type = types.single()
|
||||
}
|
||||
else
|
||||
buildErrorTypeRef {
|
||||
source = superReferenceContainer.source
|
||||
// NB: NOT_A_SUPERTYPE is reported by a separate checker
|
||||
diagnostic = ConeStubDiagnostic(ConeSimpleDiagnostic("Ambiguous supertype", DiagnosticKind.Other))
|
||||
}
|
||||
}
|
||||
}
|
||||
superReferenceContainer.resultType = resultType
|
||||
|
||||
@@ -138,6 +138,10 @@ sealed class FirFakeSourceElementKind : FirSourceElementKind() {
|
||||
// a ?: b --> when(val $subj = a) { .... }
|
||||
// where `val $subj = a` has a fake source
|
||||
object WhenGeneratedSubject : FirFakeSourceElementKind()
|
||||
|
||||
// super.foo() --> super<Supertype>.foo()
|
||||
// where `Supertype` has a fake source
|
||||
object SuperCallImplicitType : FirFakeSourceElementKind()
|
||||
}
|
||||
|
||||
sealed class FirSourceElement {
|
||||
|
||||
Reference in New Issue
Block a user