FIR: Fix resolution for invokes on class qualifiers

This commit is contained in:
Denis Zharkov
2020-03-30 13:04:00 +03:00
parent 10531d2874
commit 9abe669443
24 changed files with 173 additions and 104 deletions
@@ -0,0 +1,9 @@
internal class Method<out T : String?>private constructor(val name: String, val signature: T) {
companion object {
operator fun invoke(name: String): Method<String?> = TODO()
}
}
fun foo() {
Method("asd")
}
@@ -0,0 +1,27 @@
FILE: companionInvoke.kt
internal final class Method<out T : R|kotlin/String?|> : R|kotlin/Any| {
private constructor<out T : R|kotlin/String?|>(name: R|kotlin/String|, signature: R|T|): R|Method<T>| {
super<R|kotlin/Any|>()
}
public[internal] final val name: R|kotlin/String| = R|<local>/name|
public get(): R|kotlin/String|
public[internal] final val signature: R|T| = R|<local>/signature|
public get(): R|T|
public[internal] final companion object Companion : R|kotlin/Any| {
private constructor(): R|Method.Companion| {
super<R|kotlin/Any|>()
}
public[internal] final operator fun invoke(name: R|kotlin/String|): R|Method<kotlin/String?>| {
^invoke R|kotlin/TODO|()
}
}
}
public final fun foo(): R|kotlin/Unit| {
Q|Method|.R|/Method.Companion.invoke|(String(asd))
}
@@ -0,0 +1,7 @@
object Bar {
operator fun invoke(x: String) {}
}
fun foo() {
Bar("asd")
}
@@ -0,0 +1,13 @@
FILE: objectInvoke.kt
public final object Bar : R|kotlin/Any| {
private constructor(): R|Bar| {
super<R|kotlin/Any|>()
}
public final operator fun invoke(x: R|kotlin/String|): R|kotlin/Unit| {
}
}
public final fun foo(): R|kotlin/Unit| {
Q|Bar|.R|/Bar.invoke|(String(asd))
}
@@ -8,13 +8,13 @@ fun case1(a: A?) {
Case1.invoke(it) //resolved to private constructor
<!INAPPLICABLE_CANDIDATE!>Case1<!>(it) //resolved to private constructor
Case1(it) //resolved to private constructor
<!INAPPLICABLE_CANDIDATE!>Case1<!>(A()) //resolved to private constructor
Case1(A()) //resolved to private constructor
}
<!INAPPLICABLE_CANDIDATE!>Case1<!>(A()) //resolved to private constructor
<!INAPPLICABLE_CANDIDATE!>Case1<!>(a = A()) //resolved to private constructor
Case1(A()) //resolved to private constructor
Case1(a = A()) //resolved to private constructor
}
class Case1 private constructor(val a: String) {
@@ -25,8 +25,8 @@ class Case1 private constructor(val a: String) {
// TESTCASE NUMBER: 2
fun case2(a: A){
<!INAPPLICABLE_CANDIDATE!>Case2<!>(a)
<!INAPPLICABLE_CANDIDATE!>Case2<!>(a = a)
Case2(a)
Case2(a = a)
}
class Case2 {
@@ -44,4 +44,4 @@ class Case3 {
companion object {
operator fun invoke(parameterA: A) = "" //(2)
}
}
}
@@ -6,14 +6,14 @@ FILE: companionObjectCall.kt
}
public final fun case1(a: R|A?|): R|kotlin/Unit| {
lval test: R|kotlin/Nothing?| = R|<local>/a|?.R|kotlin/let|<R|A|, R|kotlin/Nothing|>(<L> = let@fun <anonymous>(it: R|A|): R|kotlin/Nothing| <kind=EXACTLY_ONCE> {
lval test: R|kotlin/String?| = R|<local>/a|?.R|kotlin/let|<R|A|, R|kotlin/String|>(<L> = let@fun <anonymous>(it: R|A|): R|kotlin/String| <kind=EXACTLY_ONCE> {
Q|Case1|.R|/Case1.Companion.invoke|(R|<local>/it|)
<Inapplicable(HIDDEN): [/Case1.Case1]>#(R|<local>/it|)
^ <Inapplicable(HIDDEN): [/Case1.Case1]>#(R|/A.A|())
Q|Case1|.R|/Case1.Companion.invoke|(R|<local>/it|)
^ Q|Case1|.R|/Case1.Companion.invoke|(R|/A.A|())
}
)
<Inapplicable(HIDDEN): [/Case1.Case1]>#(R|/A.A|())
<Inapplicable(HIDDEN): [/Case1.Case1]>#(a = R|/A.A|())
Q|Case1|.R|/Case1.Companion.invoke|(R|/A.A|())
Q|Case1|.R|/Case1.Companion.invoke|(a = R|/A.A|())
}
public final class Case1 : R|kotlin/Any| {
private constructor(a: R|kotlin/String|): R|Case1| {
@@ -36,8 +36,8 @@ FILE: companionObjectCall.kt
}
public final fun case2(a: R|A|): R|kotlin/Unit| {
<Inapplicable(PARAMETER_MAPPING_ERROR): [/Case2.Case2]>#(R|<local>/a|)
<Inapplicable(INAPPLICABLE): [/Case2.Case2]>#(a = R|<local>/a|)
Q|Case2|.R|/Case2.Companion.invoke|(R|<local>/a|)
Q|Case2|.R|/Case2.Companion.invoke|(a = R|<local>/a|)
}
public final class Case2 : R|kotlin/Any| {
public constructor(): R|Case2| {
@@ -57,8 +57,8 @@ FILE: companionObjectCall.kt
}
public final fun case3(a: R|A|): R|kotlin/Unit| {
Q|Case3|.R|/Case3.Companion|.R|/Case3.Companion.invoke|(R|<local>/a|)
Q|Case3|.R|/Case3.Companion|.R|/Case3.Companion.invoke|(parameterA = R|<local>/a|)
Q|Case3.Companion|.R|/Case3.Companion.invoke|(R|<local>/a|)
Q|Case3.Companion|.R|/Case3.Companion.invoke|(parameterA = R|<local>/a|)
}
public final class Case3 : R|kotlin/Any| {
public constructor(): R|Case3| {
@@ -567,6 +567,29 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest {
}
}
@TestMetadata("compiler/fir/analysis-tests/testData/resolve/callResolution")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class CallResolution extends AbstractFirDiagnosticsTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInCallResolution() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/analysis-tests/testData/resolve/callResolution"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
}
@TestMetadata("companionInvoke.kt")
public void testCompanionInvoke() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/callResolution/companionInvoke.kt");
}
@TestMetadata("objectInvoke.kt")
public void testObjectInvoke() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/callResolution/objectInvoke.kt");
}
}
@TestMetadata("compiler/fir/analysis-tests/testData/resolve/cfg")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -567,6 +567,29 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos
}
}
@TestMetadata("compiler/fir/analysis-tests/testData/resolve/callResolution")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class CallResolution extends AbstractFirDiagnosticsWithLightTreeTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInCallResolution() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/analysis-tests/testData/resolve/callResolution"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
}
@TestMetadata("companionInvoke.kt")
public void testCompanionInvoke() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/callResolution/companionInvoke.kt");
}
@TestMetadata("objectInvoke.kt")
public void testObjectInvoke() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/callResolution/objectInvoke.kt");
}
}
@TestMetadata("compiler/fir/analysis-tests/testData/resolve/cfg")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -8,7 +8,6 @@ package org.jetbrains.kotlin.fir
import org.jetbrains.kotlin.fir.declarations.FirMemberDeclaration
import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.expressions.builder.buildExpressionStub
import org.jetbrains.kotlin.fir.expressions.builder.buildResolvedQualifier
import org.jetbrains.kotlin.fir.expressions.builder.buildResolvedReifiedParameterReference
import org.jetbrains.kotlin.fir.references.FirNamedReference
import org.jetbrains.kotlin.fir.references.FirReference
@@ -210,22 +209,7 @@ class FirCallResolver(
when {
referencedSymbol is FirClassLikeSymbol<*> -> {
val classId = referencedSymbol.classId
return buildResolvedQualifier {
source = nameReference.source
packageFqName = classId.packageFqName
relativeClassFqName = classId.relativeClassName
safe = false
typeArguments.addAll(qualifiedAccess.typeArguments)
symbol = referencedSymbol
}.apply {
resultType = if (classId.isLocal) {
typeForQualifierByDeclaration(referencedSymbol.fir, resultType, session)
?: session.builtinTypes.unitType
} else {
typeForQualifier(this)
}
}
return buildResolvedQualifierForClass(referencedSymbol, nameReference.source, qualifiedAccess.typeArguments)
}
referencedSymbol is FirTypeParameterSymbol && referencedSymbol.fir.isReified -> {
return buildResolvedReifiedParameterReference {
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.fir.expressions.FirResolvable
import org.jetbrains.kotlin.fir.expressions.FirResolvedQualifier
import org.jetbrains.kotlin.fir.expressions.builder.FirResolvedReifiedParameterReferenceBuilder
import org.jetbrains.kotlin.fir.expressions.builder.buildExpressionWithSmartcast
import org.jetbrains.kotlin.fir.expressions.builder.buildResolvedQualifier
import org.jetbrains.kotlin.fir.references.FirErrorNamedReference
import org.jetbrains.kotlin.fir.references.FirResolvedNamedReference
import org.jetbrains.kotlin.fir.references.FirSuperReference
@@ -245,6 +246,30 @@ fun createKPropertyType(
return ConeClassLikeTypeImpl(ConeClassLikeLookupTagImpl(classId), arguments.toTypedArray(), isNullable = false)
}
fun BodyResolveComponents.buildResolvedQualifierForClass(
regularClass: FirClassLikeSymbol<*>,
sourceElement: FirSourceElement? = null,
// TODO: Clarify if we actually need type arguments for qualifier?
typeArgumentsForQualifier: List<FirTypeProjection> = emptyList()
): FirResolvedQualifier {
val classId = regularClass.classId
return buildResolvedQualifier {
source = sourceElement
packageFqName = classId.packageFqName
relativeClassFqName = classId.relativeClassName
safe = false
typeArguments.addAll(typeArgumentsForQualifier)
symbol = regularClass
}.apply {
resultType = if (classId.isLocal) {
typeForQualifierByDeclaration(regularClass.fir, resultType, session)
?: session.builtinTypes.unitType
} else {
typeForQualifier(this)
}
}
}
fun BodyResolveComponents.typeForQualifier(resolvedQualifier: FirResolvedQualifier): FirTypeRef {
val classSymbol = resolvedQualifier.symbol
val resultType = resolvedQualifier.resultType
@@ -16,7 +16,7 @@ import org.jetbrains.kotlin.fir.expressions.FirResolvedQualifier
import org.jetbrains.kotlin.fir.expressions.builder.FirQualifiedAccessExpressionBuilder
import org.jetbrains.kotlin.fir.references.FirSuperReference
import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents
import org.jetbrains.kotlin.fir.resolve.constructType
import org.jetbrains.kotlin.fir.resolve.buildResolvedQualifierForClass
import org.jetbrains.kotlin.fir.resolve.scope
import org.jetbrains.kotlin.fir.resolve.transformQualifiedAccessUsingSmartcastInfo
import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.firUnsafe
@@ -28,7 +28,6 @@ import org.jetbrains.kotlin.fir.scopes.impl.FirStaticScope
import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol
import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef
import org.jetbrains.kotlin.fir.types.impl.FirImplicitBuiltinTypeRef
import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind
import org.jetbrains.kotlin.resolve.descriptorUtil.HIDES_MEMBERS_NAME_LIST
@@ -688,25 +687,30 @@ private fun BodyResolveComponents.createExplicitReceiverForInvoke(
invokeBuiltinExtensionMode: Boolean,
extensionReceiverExpression: FirExpression
): FirExpression {
val (name, typeRef) = when (val symbol = candidate.symbol) {
is FirCallableSymbol<*> -> {
symbol.callableId.callableName to returnTypeCalculator.tryCalculateReturnType(symbol.firUnsafe())
}
is FirRegularClassSymbol -> {
symbol.classId.shortClassName to buildResolvedTypeRef {
type = symbol.constructType(emptyArray(), isNullable = false)
}
}
return when (val symbol = candidate.symbol) {
is FirCallableSymbol<*> -> createExplicitReceiverForInvokeByCallable(
candidate, info, invokeBuiltinExtensionMode, extensionReceiverExpression, symbol
)
is FirRegularClassSymbol -> buildResolvedQualifierForClass(symbol, sourceElement = null)
else -> throw AssertionError()
}
}
private fun BodyResolveComponents.createExplicitReceiverForInvokeByCallable(
candidate: Candidate,
info: CallInfo,
invokeBuiltinExtensionMode: Boolean,
extensionReceiverExpression: FirExpression,
symbol: FirCallableSymbol<*>
): FirExpression {
return FirQualifiedAccessExpressionBuilder().apply {
calleeReference = FirNamedReferenceWithCandidate(
null,
name,
symbol.callableId.callableName,
candidate
)
dispatchReceiver = candidate.dispatchReceiverExpression()
this.typeRef = typeRef
this.typeRef = returnTypeCalculator.tryCalculateReturnType(symbol.firUnsafe())
if (!invokeBuiltinExtensionMode) {
extensionReceiver = extensionReceiverExpression
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// KJS_WITH_FULL_RUNTIME
// !LANGUAGE: +NewInference
// WITH_RUNTIME
@@ -35,4 +34,4 @@ fun box(): String {
if (!test2()) return "fail2"
return "OK"
}
}
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
class A {
companion object {
operator fun invoke(i: Int) = i
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
interface B
operator fun B.invoke(i: Int) = i
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
class A {
class Nested {
companion object {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
import A.Nested
class A {
@@ -1,6 +1,5 @@
// IGNORE_BACKEND_FIR: JVM_IR
object A
operator fun A.invoke(i: Int) = i
fun box() = if (A(42) == 42) "OK" else "fail"
fun box() = if (A(42) == 42) "OK" else "fail"
@@ -1,6 +1,5 @@
// IGNORE_BACKEND_FIR: JVM_IR
object A {
operator fun invoke(i: Int) = i
}
fun box() = if (A(42) == 42) "OK" else "fail"
fun box() = if (A(42) == 42) "OK" else "fail"
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
class TestClass {
companion object {
inline operator fun <T> invoke(task: () -> T) = task()
@@ -12,4 +11,4 @@ fun box(): String {
val ok = "OK"
val x = TestClass { return ok }
}
}
@@ -1,39 +0,0 @@
// SKIP_TXT
// !DIAGNOSTICS: -UNUSED_PARAMETER
// !LANGUAGE: +NewInference
fun main() {
Configuration().commands {
<!INAPPLICABLE_CANDIDATE!>Command1<!> { <!UNRESOLVED_REFERENCE!><!UNRESOLVED_REFERENCE!>someService<!>::execute<!> } // Overload resolution ambiguity. All these functions match.
<!INAPPLICABLE_CANDIDATE!>Command2<!> { <!UNRESOLVED_REFERENCE!><!UNRESOLVED_REFERENCE!>someService<!>::execute<!> } // Overload resolution ambiguity. All these functions match.
<!INAPPLICABLE_CANDIDATE!>Command1<!> { { <!UNRESOLVED_REFERENCE!>someService<!>.<!UNRESOLVED_REFERENCE!>execute<!>(<!UNRESOLVED_REFERENCE!>it<!>) } } // fine
<!INAPPLICABLE_CANDIDATE!>Command2<!> { { <!UNRESOLVED_REFERENCE!>someService<!>.<!UNRESOLVED_REFERENCE!>execute<!>(<!UNRESOLVED_REFERENCE!>it<!>) } } // fine
}
}
interface Command
interface CommandFactory<TCommand : Command>
class Command1 : Command {
companion object : CommandFactory<Command1>
}
class Command2 : Command {
companion object : CommandFactory<Command2>
}
class Configuration {
val commands = Commands()
inline fun commands(configure: Commands.() -> Unit) {
commands.configure()
}
class Commands {
operator fun <TCommand : Command> CommandFactory<TCommand>.invoke(
handler: Transaction.() -> ((command: TCommand) -> Unit)
) {
}
}
}
interface Transaction {
val someService: SomeService
}
interface SomeService {
fun execute(command: Command1)
fun execute(command: Command2)
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// SKIP_TXT
// !DIAGNOSTICS: -UNUSED_PARAMETER
// !LANGUAGE: +NewInference
@@ -9,9 +9,9 @@ class TestClass {
}
fun test(s: String): String {
val a = <!INAPPLICABLE_CANDIDATE!>TestClass<!> { "K" }
a <!INAPPLICABLE_CANDIDATE!>checkType<!> { <!UNRESOLVED_REFERENCE!>_<!><String>() }
val a = TestClass { "K" }
a checkType { <!UNRESOLVED_REFERENCE!>_<!><String>() }
val b = <!INAPPLICABLE_CANDIDATE!>TestClass<!> { return s }
b <!INAPPLICABLE_CANDIDATE!>checkType<!> { <!UNRESOLVED_REFERENCE!>_<!><Nothing>() }
}
val b = TestClass { return s }
b checkType { <!UNRESOLVED_REFERENCE!>_<!><Nothing>() }
}
@@ -20,4 +20,4 @@ val a = A
val b = B
val c = C
val d = D
val e = <!INAPPLICABLE_CANDIDATE!>E<!>(42)
val e = E(42)
@@ -83,7 +83,7 @@ FILE fqName:<root> fileName:/objectAsCallable.kt
FIELD PROPERTY_BACKING_FIELD name:test1 type:kotlin.Int visibility:private [final,static]
EXPRESSION_BODY
CALL 'public final fun invoke (i: kotlin.Int): kotlin.Int [operator] declared in <root>' type=kotlin.Int origin=null
$receiver: ERROR_CALL 'Unresolved reference: R|/A|' type=IrErrorType
$receiver: GET_OBJECT 'CLASS OBJECT name:A modality:FINAL visibility:public superTypes:[kotlin.Any]' type=<root>.A
i: CONST Int type=kotlin.Int value=42
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-test1> visibility:public modality:FINAL <> () returnType:kotlin.Int
correspondingProperty: PROPERTY name:test1 visibility:public modality:FINAL [val]