[FIR] Support imported callable from object properly #KT-35730 Fixed

This commit is contained in:
Mikhail Glukhikh
2020-07-08 12:21:51 +03:00
parent 6b964cb61d
commit fbbf4e06ba
15 changed files with 218 additions and 28 deletions

View File

@@ -30,6 +30,6 @@ FILE: importedReceiver.kt
(Q|My|, String()).R|/My.bar|<R|kotlin/String|>()
Q|My|.R|/My.baz|()
(Q|My|, Boolean(true)).R|/My.gau|()
R|/Your.wat|()
Q|My|.R|/Your.wat|()
(Q|My|, Boolean(false)).R|FakeOverride</My.watwat: R|kotlin/Unit|>|<R|kotlin/Boolean|>()
}

View File

@@ -11,5 +11,5 @@ FILE: objectOverrideCallViaImport.kt
}
public final fun test(): R|kotlin/Unit| {
R|/Base.foo|()
Q|Derived|.R|/Base.foo|()
}

View File

@@ -1082,6 +1082,11 @@ public class Fir2IrTextTestGenerated extends AbstractFir2IrTextTest {
runTest("compiler/testData/ir/irText/expressions/kt30796.kt");
}
@TestMetadata("kt35730.kt")
public void testKt35730() throws Exception {
runTest("compiler/testData/ir/irText/expressions/kt35730.kt");
}
@TestMetadata("kt36956.kt")
public void testKt36956() throws Exception {
runTest("compiler/testData/ir/irText/expressions/kt36956.kt");

View File

@@ -5,9 +5,9 @@
package org.jetbrains.kotlin.fir.resolve.calls.tower
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.FirConstructor
import org.jetbrains.kotlin.fir.declarations.FirDeclarationOrigin
import org.jetbrains.kotlin.fir.declarations.isInner
import org.jetbrains.kotlin.fir.expressions.FirExpression
import org.jetbrains.kotlin.fir.expressions.builder.buildResolvedQualifier
@@ -20,7 +20,6 @@ import org.jetbrains.kotlin.fir.resolve.typeForQualifier
import org.jetbrains.kotlin.fir.scopes.FirScope
import org.jetbrains.kotlin.fir.scopes.ProcessorAction
import org.jetbrains.kotlin.fir.scopes.impl.FirAbstractImportingScope
import org.jetbrains.kotlin.fir.scopes.impl.FirExplicitSimpleImportingScope
import org.jetbrains.kotlin.fir.scopes.processClassifiersByName
import org.jetbrains.kotlin.fir.symbols.AbstractFirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.impl.*
@@ -204,13 +203,11 @@ class ScopeTowerLevel(
else -> true
}
private fun dispatchReceiverValue(scope: FirScope, candidate: FirCallableSymbol<*>): ReceiverValue? {
private fun dispatchReceiverValue(candidate: FirCallableSymbol<*>): ReceiverValue? {
val holderId = candidate.callableId.classId
if (holderId != null && scope is FirExplicitSimpleImportingScope) {
if (holderId != null && candidate.fir.origin == FirDeclarationOrigin.ImportedFromObject) {
val symbol = session.firSymbolProvider.getClassLikeSymbolByFqName(holderId)
if (symbol is FirRegularClassSymbol &&
symbol.fir.classKind.let { it == ClassKind.OBJECT || it == ClassKind.ENUM_ENTRY }
) {
if (symbol is FirRegularClassSymbol) {
val resolvedQualifier = buildResolvedQualifier {
packageFqName = holderId.packageFqName
relativeClassFqName = holderId.relativeClassName
@@ -234,6 +231,23 @@ class ScopeTowerLevel(
}
}
private fun <T : AbstractFirBasedSymbol<*>> consumeCallableCandidate(
candidate: FirCallableSymbol<*>,
processor: TowerScopeLevel.TowerScopeLevelProcessor<T>
) {
if (candidate.hasConsistentReceivers(extensionReceiver)) {
val dispatchReceiverValue = dispatchReceiverValue(candidate)
val unwrappedCandidate = if (candidate.fir.origin == FirDeclarationOrigin.ImportedFromObject) {
candidate.overriddenSymbol!!
} else candidate
@Suppress("UNCHECKED_CAST")
processor.consumeCandidate(
unwrappedCandidate as T, dispatchReceiverValue,
implicitExtensionReceiverValue = extensionReceiver as? ImplicitReceiverValue<*>
)
}
}
override fun <T : AbstractFirBasedSymbol<*>> processElementsByName(
token: TowerScopeLevel.Token<T>,
name: Name,
@@ -244,13 +258,7 @@ class ScopeTowerLevel(
when (token) {
TowerScopeLevel.Token.Properties -> scope.processPropertiesByName(name) { candidate ->
empty = false
if (candidate.hasConsistentReceivers(extensionReceiver)) {
val dispatchReceiverValue = dispatchReceiverValue(scope, candidate)
processor.consumeCandidate(
candidate as T, dispatchReceiverValue,
implicitExtensionReceiverValue = extensionReceiver as? ImplicitReceiverValue<*>
)
}
consumeCallableCandidate(candidate, processor)
}
TowerScopeLevel.Token.Functions -> scope.processFunctionsAndConstructorsByName(
name,
@@ -259,13 +267,7 @@ class ScopeTowerLevel(
includeInnerConstructors = includeInnerConstructors
) { candidate ->
empty = false
if (candidate.hasConsistentReceivers(extensionReceiver)) {
val dispatchReceiverValue = dispatchReceiverValue(scope, candidate)
processor.consumeCandidate(
candidate as T, dispatchReceiverValue,
implicitExtensionReceiverValue = extensionReceiver as? ImplicitReceiverValue<*>
)
}
consumeCallableCandidate(candidate, processor)
}
TowerScopeLevel.Token.Objects -> scope.processClassifiersByName(name) {
empty = false

View File

@@ -37,7 +37,7 @@ abstract class FirAbstractImportingScope(
val firClass = (symbol as FirClassSymbol<*>).fir
return if (firClass.classKind == ClassKind.OBJECT)
firClass.unsubstitutedScope(session, scopeSession)
FirObjectImportedCallableScope(classId, firClass.unsubstitutedScope(session, scopeSession))
else
firClass.scopeProvider.getStaticScope(firClass, session, scopeSession)
}

View File

@@ -0,0 +1,74 @@
/*
* 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.scopes.impl
import org.jetbrains.kotlin.fir.declarations.FirDeclarationOrigin
import org.jetbrains.kotlin.fir.declarations.builder.buildProperty
import org.jetbrains.kotlin.fir.declarations.builder.buildSimpleFunction
import org.jetbrains.kotlin.fir.scopes.FirScope
import org.jetbrains.kotlin.fir.symbols.CallableId
import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirVariableSymbol
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.Name
class FirObjectImportedCallableScope(
private val importedClassId: ClassId,
private val objectUseSiteScope: FirScope
) : FirScope() {
override fun processFunctionsByName(name: Name, processor: (FirFunctionSymbol<*>) -> Unit) {
objectUseSiteScope.processFunctionsByName(name) wrapper@{ symbol ->
if (symbol !is FirNamedFunctionSymbol) {
processor(symbol)
return@wrapper
}
val function = symbol.fir
val syntheticFunction = buildSimpleFunction {
source = function.source
session = function.session
origin = FirDeclarationOrigin.ImportedFromObject
returnTypeRef = function.returnTypeRef
receiverTypeRef = function.receiverTypeRef
this.name = function.name
status = function.status
this.symbol = FirNamedFunctionSymbol(CallableId(importedClassId, name), overriddenSymbol = symbol)
resolvePhase = function.resolvePhase
typeParameters.addAll(function.typeParameters)
valueParameters.addAll(function.valueParameters)
annotations.addAll(function.annotations)
}
processor(syntheticFunction.symbol)
}
}
override fun processPropertiesByName(name: Name, processor: (FirVariableSymbol<*>) -> Unit) {
objectUseSiteScope.processPropertiesByName(name) wrapper@{ symbol ->
if (symbol !is FirPropertySymbol) {
processor(symbol)
return@wrapper
}
val property = symbol.fir
val syntheticFunction = buildProperty {
source = property.source
session = property.session
origin = FirDeclarationOrigin.ImportedFromObject
returnTypeRef = property.returnTypeRef
receiverTypeRef = property.receiverTypeRef
this.name = property.name
status = property.status
isVar = property.isVar
isLocal = property.isLocal
this.symbol = FirPropertySymbol(CallableId(importedClassId, name), overriddenSymbol = symbol)
resolvePhase = property.resolvePhase
typeParameters.addAll(property.typeParameters)
annotations.addAll(property.annotations)
}
processor(syntheticFunction.symbol)
}
}
}

View File

@@ -13,6 +13,7 @@ sealed class FirDeclarationOrigin {
object SamConstructor : FirDeclarationOrigin()
object FakeOverride : FirDeclarationOrigin()
object Enhancement : FirDeclarationOrigin()
object ImportedFromObject : FirDeclarationOrigin()
class Plugin(val key: FirPluginKey) : FirDeclarationOrigin()
}

View File

@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// MODULE: lib
// FILE: lib.kt
package test

View File

@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
import C.f
import C.p
import C.ext

View File

@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
import Class.C.f
import Class.C.p
import Class.C.ext

View File

@@ -0,0 +1,48 @@
FILE fqName:<root> fileName:/kt35730.kt
CLASS INTERFACE name:Base modality:ABSTRACT visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Base
FUN name:foo visibility:public modality:OPEN <> ($this:<root>.Base) returnType:kotlin.Unit
$this: VALUE_PARAMETER name:<this> type:<root>.Base
BLOCK_BODY
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
overridden:
public open fun hashCode (): kotlin.Int declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
overridden:
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
CLASS OBJECT name:Derived modality:FINAL visibility:public superTypes:[<root>.Base]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Derived
CONSTRUCTOR visibility:private <> () returnType:<root>.Derived [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS OBJECT name:Derived modality:FINAL visibility:public superTypes:[<root>.Base]'
FUN FAKE_OVERRIDE name:foo visibility:public modality:OPEN <> ($this:<root>.Base) returnType:kotlin.Unit [fake_override]
overridden:
public open fun foo (): kotlin.Unit declared in <root>.Base
$this: VALUE_PARAMETER name:<this> type:<root>.Base
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
overridden:
public open fun hashCode (): kotlin.Int declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
overridden:
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN name:test visibility:public modality:FINAL <> () returnType:kotlin.Unit
BLOCK_BODY
CALL 'public open fun foo (): kotlin.Unit declared in <root>.Base' type=kotlin.Unit origin=null
$this: GET_OBJECT 'CLASS OBJECT name:Derived modality:FINAL visibility:public superTypes:[<root>.Base]' type=<root>.Derived
CALL 'public open fun foo (): kotlin.Unit declared in <root>.Base' type=kotlin.Unit origin=null
$this: GET_OBJECT 'CLASS OBJECT name:Derived modality:FINAL visibility:public superTypes:[<root>.Base]' type=<root>.Derived

View File

@@ -0,0 +1,10 @@
import Derived.foo
interface Base {
fun foo() {}
}
object Derived : Base
fun test() {
// Both calls resolved to Base.foo()
foo()
Derived.foo()
}

View File

@@ -0,0 +1,48 @@
FILE fqName:<root> fileName:/kt35730.kt
CLASS INTERFACE name:Base modality:ABSTRACT visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Base
FUN name:foo visibility:public modality:OPEN <> ($this:<root>.Base) returnType:kotlin.Unit
$this: VALUE_PARAMETER name:<this> type:<root>.Base
BLOCK_BODY
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
overridden:
public open fun hashCode (): kotlin.Int declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
overridden:
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
CLASS OBJECT name:Derived modality:FINAL visibility:public superTypes:[<root>.Base]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Derived
CONSTRUCTOR visibility:private <> () returnType:<root>.Derived [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS OBJECT name:Derived modality:FINAL visibility:public superTypes:[<root>.Base]'
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean [fake_override,operator] declared in <root>.Base
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:foo visibility:public modality:OPEN <> ($this:<root>.Base) returnType:kotlin.Unit [fake_override]
overridden:
public open fun foo (): kotlin.Unit declared in <root>.Base
$this: VALUE_PARAMETER name:<this> type:<root>.Base
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
overridden:
public open fun hashCode (): kotlin.Int [fake_override] declared in <root>.Base
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
overridden:
public open fun toString (): kotlin.String [fake_override] declared in <root>.Base
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN name:test visibility:public modality:FINAL <> () returnType:kotlin.Unit
BLOCK_BODY
CALL 'public open fun foo (): kotlin.Unit [fake_override] declared in <root>.Derived' type=kotlin.Unit origin=null
$this: GET_OBJECT 'CLASS OBJECT name:Derived modality:FINAL visibility:public superTypes:[<root>.Base]' type=<root>.Derived
CALL 'public open fun foo (): kotlin.Unit [fake_override] declared in <root>.Derived' type=kotlin.Unit origin=null
$this: GET_OBJECT 'CLASS OBJECT name:Derived modality:FINAL visibility:public superTypes:[<root>.Base]' type=<root>.Derived

View File

@@ -255,7 +255,7 @@ FILE fqName:<root> fileName:/useImportedMember.kt
$this: CALL 'public final fun EQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=EXCLEQ
arg0: CALL 'public final fun <get-fromClass> <T> (): T of <root>.BaseClass.<get-fromClass> declared in <root>.BaseClass' type=kotlin.String origin=GET_PROPERTY
<T>: kotlin.String
$this: CONST String type=kotlin.String value="10"
$this: GET_OBJECT 'CLASS OBJECT name:C modality:FINAL visibility:public superTypes:[<root>.BaseClass; <root>.I<kotlin.String>]' type=<root>.C
$receiver: CONST String type=kotlin.String value="10"
arg1: CONST String type=kotlin.String value="10"
then: RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in <root>'

View File

@@ -1081,6 +1081,11 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase {
runTest("compiler/testData/ir/irText/expressions/kt30796.kt");
}
@TestMetadata("kt35730.kt")
public void testKt35730() throws Exception {
runTest("compiler/testData/ir/irText/expressions/kt35730.kt");
}
@TestMetadata("kt36956.kt")
public void testKt36956() throws Exception {
runTest("compiler/testData/ir/irText/expressions/kt36956.kt");