[FIR] Handle fully qualified expressions separately in tower resolver

This commit is contained in:
Mikhail Glukhikh
2020-01-10 17:11:43 +03:00
parent 587430ff90
commit 5c6341b4e4
36 changed files with 204 additions and 47 deletions
@@ -25,7 +25,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage
import org.jetbrains.kotlin.resolve.calls.model.PostponedResolvedAtomMarker
import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind
class CallInfo(
data class CallInfo(
val callKind: CallKind,
val name: Name,
@@ -5,18 +5,32 @@
package org.jetbrains.kotlin.fir.resolve.calls
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.FirRegularClass
import org.jetbrains.kotlin.fir.declarations.impl.FirImportImpl
import org.jetbrains.kotlin.fir.declarations.impl.FirResolvedImportImpl
import org.jetbrains.kotlin.fir.declarations.isInner
import org.jetbrains.kotlin.fir.expressions.FirResolvedQualifier
import org.jetbrains.kotlin.fir.expressions.impl.FirQualifiedAccessExpressionImpl
import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents
import org.jetbrains.kotlin.fir.resolve.calls.TowerDataKind.*
import org.jetbrains.kotlin.fir.resolve.transformQualifiedAccessUsingSmartcastInfo
import org.jetbrains.kotlin.fir.resolve.transformers.ReturnTypeCalculator
import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.firUnsafe
import org.jetbrains.kotlin.fir.scopes.FirScope
import org.jetbrains.kotlin.fir.scopes.ProcessorAction
import org.jetbrains.kotlin.fir.scopes.ProcessorAction.NONE
import org.jetbrains.kotlin.fir.scopes.impl.FirExplicitSimpleImportingScope
import org.jetbrains.kotlin.fir.scopes.impl.FirLocalScope
import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
import org.jetbrains.kotlin.fir.types.ConeIntegerLiteralType
import org.jetbrains.kotlin.fir.types.coneTypeSafe
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind
import org.jetbrains.kotlin.resolve.descriptorUtil.HIDES_MEMBERS_NAME_LIST
import org.jetbrains.kotlin.util.OperatorNameConventions
enum class TowerDataKind {
EMPTY, // Corresponds to stub tower level which is replaced by receiver-related level
@@ -228,13 +242,100 @@ class FirTowerResolver(
}
}
private fun createExplicitReceiverForInvoke(candidate: Candidate): FirQualifiedAccessExpressionImpl {
val symbol = candidate.symbol as FirCallableSymbol<*>
return FirQualifiedAccessExpressionImpl(null).apply {
calleeReference = FirNamedReferenceWithCandidate(
null,
symbol.callableId.callableName,
candidate
)
dispatchReceiver = candidate.dispatchReceiverExpression()
typeRef = typeCalculator.tryCalculateReturnType(symbol.firUnsafe())
}
}
// Only case when qualifiedReceiver is a package (no classId) is handled here
private fun runResolverForFullyQualifiedReceiver(
implicitReceiverValues: List<ImplicitReceiverValue<*>>,
info: CallInfo,
collector: CandidateCollector,
qualifiedReceiver: FirResolvedQualifier
): CandidateCollector {
val qualifierScope = FirExplicitSimpleImportingScope(
listOf(
FirResolvedImportImpl(
FirImportImpl(null, FqName.topLevel(info.name), false, null),
qualifiedReceiver.packageFqName,
qualifiedReceiver.relativeClassFqName
)
), session, components.scopeSession
)
val candidateFactory = CandidateFactory(components, info)
when (info.callKind) {
CallKind.VariableAccess -> {
qualifierScope.processPropertiesByName(info.name) {
collector.consumeCandidate(0, candidateFactory.createCandidate(it, ExplicitReceiverKind.NO_EXPLICIT_RECEIVER))
ProcessorAction.NEXT
}
qualifierScope.processClassifiersByName(info.name) {
if (it is FirClassSymbol<*> && it.fir.classKind == ClassKind.OBJECT) {
collector.consumeCandidate(0, candidateFactory.createCandidate(it, ExplicitReceiverKind.NO_EXPLICIT_RECEIVER))
}
ProcessorAction.NEXT
}
}
CallKind.Function -> {
qualifierScope.processFunctionsAndConstructorsByName(info.name, session, components) {
collector.consumeCandidate(0, candidateFactory.createCandidate(it, ExplicitReceiverKind.NO_EXPLICIT_RECEIVER))
ProcessorAction.NEXT
}
val invokeReceiverCollector = CandidateCollector(components, components.resolutionStageRunner)
val invokeReceiverCandidateFactory = CandidateFactory(
components,
info.replaceWithVariableAccess()
)
qualifierScope.processPropertiesByName(info.name) {
invokeReceiverCollector.consumeCandidate(
0, invokeReceiverCandidateFactory.createCandidate(it, ExplicitReceiverKind.NO_EXPLICIT_RECEIVER)
)
ProcessorAction.NEXT
}
if (invokeReceiverCollector.isSuccess()) {
for (invokeReceiverCandidate in invokeReceiverCollector.bestCandidates()) {
val invokeReceiverExpression = createExplicitReceiverForInvoke(invokeReceiverCandidate).let {
components.transformQualifiedAccessUsingSmartcastInfo(it)
}
runResolver(
implicitReceiverValues,
info.copy(explicitReceiver = invokeReceiverExpression, name = OperatorNameConventions.INVOKE),
collector
)
}
}
}
CallKind.CallableReference -> return collector
else -> throw AssertionError("Unsupported call kind in tower resolver: ${info.callKind}")
}
return collector
}
fun runResolver(
implicitReceiverValues: List<ImplicitReceiverValue<*>>, info: CallInfo,
implicitReceiverValues: List<ImplicitReceiverValue<*>>,
info: CallInfo,
collector: CandidateCollector = this.collector
): CandidateCollector {
this.implicitReceiverValues = implicitReceiverValues
towerDataConsumer = towerDataConsumer(info, collector)
val receiver = info.explicitReceiver
if (receiver is FirResolvedQualifier && receiver.classId == null) {
return runResolverForFullyQualifiedReceiver(implicitReceiverValues, info, collector, receiver)
}
towerDataConsumer = towerDataConsumer(info, collector)
val shouldProcessExtensionsBeforeMembers =
info.callKind == CallKind.Function && info.name in HIDES_MEMBERS_NAME_LIST
val shouldProcessExplicitReceiverScopeOnly =
@@ -0,0 +1,19 @@
package my
class A {
companion object X {
fun foo() {}
}
}
val xx = A()
fun test() {
val x = A
A.foo()
A.X.foo()
fun A.invoke() {}
my.xx()
}
@@ -0,0 +1,28 @@
FILE: qualifierWithCompanion.kt
public final class A : R|kotlin/Any| {
public constructor(): R|my/A| {
super<R|kotlin/Any|>()
}
public final companion object X : R|kotlin/Any| {
private constructor(): R|my/A.X| {
super<R|kotlin/Any|>()
}
public final fun foo(): R|kotlin/Unit| {
}
}
}
public final val xx: R|my/A| = R|my/A.A|()
public get(): R|my/A|
public final fun test(): R|kotlin/Unit| {
lval x: R|my/A.X| = Q|my/A|
Q|my/A|.R|my/A.X.foo|()
Q|my/A.X|.R|my/A.X.foo|()
local final fun R|my/A|.invoke(): R|kotlin/Unit| {
}
R|my/xx|.R|<local>/invoke|()
}
@@ -178,6 +178,11 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest {
runTest("compiler/fir/resolve/testData/resolve/problems2.kt");
}
@TestMetadata("qualifierWithCompanion.kt")
public void testQualifierWithCompanion() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/qualifierWithCompanion.kt");
}
@TestMetadata("recursiveCallOnWhenWithSealedClass.kt")
public void testRecursiveCallOnWhenWithSealedClass() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/recursiveCallOnWhenWithSealedClass.kt");
@@ -178,6 +178,11 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos
runTest("compiler/fir/resolve/testData/resolve/problems2.kt");
}
@TestMetadata("qualifierWithCompanion.kt")
public void testQualifierWithCompanion() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/qualifierWithCompanion.kt");
}
@TestMetadata("recursiveCallOnWhenWithSealedClass.kt")
public void testRecursiveCallOnWhenWithSealedClass() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/recursiveCallOnWhenWithSealedClass.kt");
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// TARGET_BACKEND: JVM
fun box() : String {
+1 -1
View File
@@ -1,6 +1,6 @@
// !DIAGNOSTICS: -UNREACHABLE_CODE
fun TODO(): Nothing = throw java.lang.<!UNRESOLVED_REFERENCE!>IllegalStateException<!>()
fun TODO(): Nothing = throw java.lang.IllegalStateException()
open class OpenClass
class FinalClass : OpenClass()
@@ -151,7 +151,7 @@ fun foo(a : Nothing) : Unit {
}
fun fail() : Nothing {
throw java.lang.<!UNRESOLVED_REFERENCE!>RuntimeException<!>()
throw java.lang.RuntimeException()
}
fun nullIsNotNothing() : Unit {
@@ -140,7 +140,7 @@ fun testCoercionToAny() {
fun fooWithAnuNullableResult(s: String?, name: String, optional: Boolean): Any? {
return if (s == null) {
if (!optional) {
throw java.lang.<!UNRESOLVED_REFERENCE!>IllegalArgumentException<!>("Parameter '$name' was not found in the request")
throw java.lang.IllegalArgumentException("Parameter '$name' was not found in the request")
}
null
} else {
@@ -23,7 +23,7 @@ operator fun <T : Any> T?.iterator() = object : MyIterator<T> {
hasNext = false
return this@iterator!!
}
throw java.util.<!UNRESOLVED_REFERENCE!>NoSuchElementException<!>()
throw java.util.NoSuchElementException()
}
}
@@ -11,6 +11,6 @@ public class Clazz<Psi> {
public fun <T, C : MutableCollection<in T>> Iterable<T>.filterTo(destination: C, predicate: (T) -> Boolean) {}
fun test(clazz: Clazz<out Any>) {
val result = java.util.<!UNRESOLVED_REFERENCE!>ArrayList<!><Any>()
clazz.foo().<!INAPPLICABLE_CANDIDATE!>filterTo<!>(result) { x -> true }
val result = java.util.ArrayList<Any>()
clazz.foo().filterTo(result) { x -> true }
}
@@ -10,7 +10,7 @@ fun test1() {
}
fun test2() {
val m0 = java.util.<!UNRESOLVED_REFERENCE!>HashMap<!>()
val m1 = java.util.<!UNRESOLVED_REFERENCE!>HashMap<!><String, String, String>()
val m2 = java.util.<!UNRESOLVED_REFERENCE!>HashMap<!><String>()
val m0 = java.util.HashMap()
val m1 = java.util.HashMap<String, String, String>()
val m2 = java.util.HashMap<String>()
}
@@ -28,7 +28,7 @@ fun test() {
fun <T> arrayList(vararg values: T) : ArrayList<T> = values.toCollection(ArrayList<T>(values.size))
fun <T, R> Collection<T>.map(transform : (T) -> R) : List<R> {
return <!INAPPLICABLE_CANDIDATE!>mapTo<!>(java.util.<!UNRESOLVED_REFERENCE!>ArrayList<!><R>(this.size), transform)
return mapTo(java.util.ArrayList<R>(this.size), transform)
}
fun <T, R, C: MutableCollection<in R>> Collection<T>.mapTo(result: C, transform : (T) -> R) : C {
@@ -9,5 +9,5 @@ package a
fun foo() {
// If this fails, it means that we have broken the rule that Java returns are always nullable
a.<!UNRESOLVED_REFERENCE!>Test<!><Int>().<!UNRESOLVED_REFERENCE!>t<!>() + 1
a.Test<Int>().t() + 1
}
@@ -4,8 +4,8 @@
package kt1270
fun foo() {
val sc = <!UNRESOLVED_REFERENCE!>java.util.<!UNRESOLVED_REFERENCE!>HashMap<!><String, SomeClass>()[""]<!>
val value = sc.<!UNRESOLVED_REFERENCE!>value<!>
val sc = java.util.HashMap<String, SomeClass>()[""]
val value = sc.<!INAPPLICABLE_CANDIDATE!>value<!>
}
private class SomeClass() {
@@ -1,7 +1,7 @@
// KT-459 Type argument inference fails when class names are fully qualified
fun test() {
val attributes : java.util.HashMap<String, String> = java.util.<!UNRESOLVED_REFERENCE!>HashMap<!>() // failure!
val attributes : java.util.HashMap<String, String> = java.util.HashMap() // failure!
attributes["href"] = "1" // inference fails, but it shouldn't
}
@@ -3,9 +3,9 @@
package demo
fun <T> filter(list : Array<T>, filter : (T) -> Boolean) : List<T> {
val answer = java.util.<!UNRESOLVED_REFERENCE!>ArrayList<!><T>();
val answer = java.util.ArrayList<T>();
for (l in list) {
if (filter(l)) answer.<!UNRESOLVED_REFERENCE!>add<!>(l)
if (filter(l)) answer.add(l)
}
return answer;
}
@@ -12,6 +12,6 @@ inline fun myRun(block: () -> Unit): Unit {
}
fun test() {
myRun { throw java.lang.<!UNRESOLVED_REFERENCE!>IllegalArgumentException<!>() }
myRun { throw java.lang.IllegalArgumentException() }
val x: Int = 42
}
@@ -22,7 +22,7 @@ fun throwIfNotCalled() {
return@outer
}
}
throw java.lang.<!UNRESOLVED_REFERENCE!>IllegalArgumentException<!>()
throw java.lang.IllegalArgumentException()
}
// x *is* initialized here, because if myRun was never called -> exception
// were thrown and control flow wouldn't be here
@@ -39,7 +39,7 @@ fun catchThrowIfNotCalled() {
return@outer
}
}
throw java.lang.<!UNRESOLVED_REFERENCE!>IllegalArgumentException<!>()
throw java.lang.IllegalArgumentException()
}
} catch (ignored: java.lang.IllegalArgumentException) { }
@@ -12,6 +12,6 @@ inline fun <T> myRun(block: () -> T): T {
}
fun throwInLambda(): Int {
val x = myRun { throw java.lang.<!UNRESOLVED_REFERENCE!>IllegalArgumentException<!>(); 42 }
val x = myRun { throw java.lang.IllegalArgumentException(); 42 }
return x
}
@@ -18,6 +18,6 @@ fun bar(b: Boolean?): Boolean {
// not pointless, but not supported yet
returns(true) implies (b == true)
}
if (b == null) throw java.lang.<!UNRESOLVED_REFERENCE!>IllegalArgumentException<!>("")
if (b == null) throw java.lang.IllegalArgumentException("")
return b
}
@@ -18,7 +18,7 @@ fun testCheckWithMessage(x: Any?) {
}
fun testCheckWithFailingMessage(x: Any?) {
check(x is String) { throw kotlin.<!UNRESOLVED_REFERENCE!>IllegalStateException<!>("What a strange idea") }
check(x is String) { throw kotlin.IllegalStateException("What a strange idea") }
x.length
}
@@ -17,7 +17,7 @@ fun testRequireWithMessage(x: Any?) {
}
fun testRequireWithFailingMessage(x: Any?) {
require(x is String) { throw kotlin.<!UNRESOLVED_REFERENCE!>IllegalStateException<!>("What a strange idea") }
require(x is String) { throw kotlin.IllegalStateException("What a strange idea") }
x.length
}
@@ -8,7 +8,7 @@ fun myAssert(condition: Boolean, message: String = "") {
contract {
returns() implies (condition)
}
if (!condition) throw kotlin.<!UNRESOLVED_REFERENCE!>IllegalArgumentException<!>(message)
if (!condition) throw kotlin.IllegalArgumentException(message)
}
fun test(x: Any?) {
@@ -8,7 +8,7 @@ fun myAssert(condition: Boolean) {
contract {
returns() implies (condition)
}
if (!condition) throw kotlin.<!UNRESOLVED_REFERENCE!>IllegalArgumentException<!>("Assertion failed")
if (!condition) throw kotlin.IllegalArgumentException("Assertion failed")
}
fun testWithCatch(x: Any?) {
@@ -9,7 +9,7 @@ fun myAssert(condition: Boolean) {
contract {
returns() implies (condition)
}
if (!condition) throw kotlin.<!UNRESOLVED_REFERENCE!>IllegalArgumentException<!>("Assertion failed")
if (!condition) throw kotlin.IllegalArgumentException("Assertion failed")
}
fun isString(x: Any?): Boolean {
@@ -8,7 +8,7 @@ fun myAssert(condition: Boolean) {
contract {
returns() implies (condition)
}
if (!condition) throw kotlin.<!UNRESOLVED_REFERENCE!>IllegalArgumentException<!>("Assertion failed")
if (!condition) throw kotlin.IllegalArgumentException("Assertion failed")
}
fun testAssertSmartcast(x: Any?) {
@@ -18,14 +18,14 @@ class C : java.util.concurrent.ConcurrentHashMap<String, Int>() {
}
fun main() {
val hm = java.util.concurrent.<!UNRESOLVED_REFERENCE!>ConcurrentHashMap<!><String, Int>()
val hm = java.util.concurrent.ConcurrentHashMap<String, Int>()
"" in hm
"" !in hm
1 <!AMBIGUITY, UNRESOLVED_REFERENCE!>!in<!> hm
2 <!AMBIGUITY!>in<!> hm
1 !in hm
2 in hm
hm.contains("")
hm.<!AMBIGUITY!>contains<!>(1)
hm.contains(1)
"" in (hm as Map<String, Int>)
"" !in (hm as Map<String, Int>)
@@ -18,14 +18,14 @@ class C : java.util.concurrent.ConcurrentHashMap<String, Int>() {
}
fun main() {
val hm = java.util.concurrent.<!UNRESOLVED_REFERENCE!>ConcurrentHashMap<!><String, Int>()
val hm = java.util.concurrent.ConcurrentHashMap<String, Int>()
"" in hm
"" !in hm
1 <!AMBIGUITY, UNRESOLVED_REFERENCE!>!in<!> hm
2 <!AMBIGUITY!>in<!> hm
1 !in hm
2 in hm
hm.contains("")
hm.<!AMBIGUITY!>contains<!>(1)
hm.contains(1)
"" in (hm as Map<String, Int>)
"" !in (hm as Map<String, Int>)
@@ -70,7 +70,7 @@ class My(val x: Int) {
when {
y > 0 -> MyBase.derivedWrapper()
x < 0 -> MyBase.exoticWrapper(x)
else -> throw java.lang.<!UNRESOLVED_REFERENCE!>NullPointerException<!>("")
else -> throw java.lang.NullPointerException("")
}
}
}
@@ -1,9 +1,9 @@
fun fn(): Nothing = throw java.lang.<!UNRESOLVED_REFERENCE!>RuntimeException<!>("oops")
fun fn(): Nothing = throw java.lang.RuntimeException("oops")
val x: Nothing = throw java.lang.<!UNRESOLVED_REFERENCE!>RuntimeException<!>("oops")
val x: Nothing = throw java.lang.RuntimeException("oops")
class SomeClass {
fun method() {
throw java.lang.<!UNRESOLVED_REFERENCE!>AssertionError<!>("!!!")
throw java.lang.AssertionError("!!!")
}
}
@@ -42,7 +42,7 @@ fun Throwable.fillInStackTrace() = 1
fun foo(x: List<String>, y: Throwable, z: A3) {
x.stream()
java.util.<!UNRESOLVED_REFERENCE!>ArrayList<!><String>().<!UNRESOLVED_REFERENCE!>stream<!>()
java.util.ArrayList<String>().stream()
y.fillInStackTrace() checkType { <!UNRESOLVED_REFERENCE!>_<!><Int>() }
@@ -1,3 +1,3 @@
// FILE: test.kt
val fooException = Exception("foo")
val barException = kotlin.<!UNRESOLVED_REFERENCE!>Exception<!>("bar")
val barException = kotlin.Exception("bar")
@@ -2,4 +2,4 @@
// !API_VERSION: 1.0
// FILE: test.kt
val fooException = Exception("foo")
val barException = kotlin.<!UNRESOLVED_REFERENCE!>Exception<!>("bar")
val barException = kotlin.Exception("bar")
@@ -1,4 +1,4 @@
// !LANGUAGE: -TypeAliases
// FILE: test.kt
val fooException = Exception("foo")
val barException = kotlin.<!UNRESOLVED_REFERENCE!>Exception<!>("bar")
val barException = kotlin.Exception("bar")