FIR resolve: record & check implicit extension receiver type properly

This commit is contained in:
Mikhail Glukhikh
2019-05-23 12:25:14 +03:00
parent d9d582b226
commit d2bdbd8978
11 changed files with 111 additions and 49 deletions
@@ -36,7 +36,6 @@ 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
import org.jetbrains.kotlin.utils.addToStdlib.cast
import java.util.*
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.createCoroutineUnintercepted
@@ -92,6 +91,7 @@ class CheckerSinkImpl(override val components: InferenceComponents, var continua
class Candidate(
val symbol: ConeSymbol,
val dispatchReceiverValue: ClassDispatchReceiverValue?,
val implicitExtensionReceiverValue: ImplicitReceiverValue?,
val explicitReceiverKind: ExplicitReceiverKind,
private val inferenceComponents: InferenceComponents,
private val baseSystem: ConstraintStorage
@@ -146,7 +146,11 @@ interface TowerScopeLevel {
): ProcessorAction
interface TowerScopeLevelProcessor<T : ConeSymbol> {
fun consumeCandidate(symbol: T, dispatchReceiverValue: ClassDispatchReceiverValue?): ProcessorAction
fun consumeCandidate(
symbol: T,
dispatchReceiverValue: ClassDispatchReceiverValue?,
implicitExtensionReceiverValue: ImplicitReceiverValue?
): ProcessorAction
}
object Empty : TowerScopeLevel {
@@ -183,7 +187,7 @@ abstract class SessionBasedTowerLevel(val session: FirSession) : TowerScopeLevel
class MemberScopeTowerLevel(
session: FirSession,
val dispatchReceiver: ReceiverValue,
val implicitExtensionReceiver: ReceiverValue? = null
val implicitExtensionReceiver: ImplicitReceiverValue? = null
) : SessionBasedTowerLevel(session) {
private fun <T : ConeSymbol> processMembers(
@@ -198,9 +202,9 @@ class MemberScopeTowerLevel(
if (candidate is ConeCallableSymbol && candidate.hasConsistentExtensionReceiver(extensionReceiver)) {
// NB: we do not check dispatchReceiverValue != null here,
// because of objects & constructors (see comments in dispatchReceiverValue() implementation)
output.consumeCandidate(candidate, candidate.dispatchReceiverValue())
output.consumeCandidate(candidate, candidate.dispatchReceiverValue(), implicitExtensionReceiver)
} else if (candidate is ConeClassLikeSymbol) {
output.consumeCandidate(candidate, null)
output.consumeCandidate(candidate, null, implicitExtensionReceiver)
} else {
ProcessorAction.NEXT
}
@@ -208,7 +212,7 @@ class MemberScopeTowerLevel(
) return ProcessorAction.STOP
val withSynthetic = FirSyntheticPropertiesScope(session, scope, ReturnTypeCalculatorWithJump(session))
return withSynthetic.processScopeMembers { symbol ->
output.consumeCandidate(symbol, symbol.dispatchReceiverValue())
output.consumeCandidate(symbol, symbol.dispatchReceiverValue(), implicitExtensionReceiver)
}
}
@@ -246,7 +250,7 @@ private fun ConeCallableSymbol.hasExtensionReceiver(): Boolean = (this as? FirCa
class ScopeTowerLevel(
session: FirSession,
val scope: FirScope,
val implicitExtensionReceiver: ReceiverValue? = null
val implicitExtensionReceiver: ImplicitReceiverValue? = null
) : SessionBasedTowerLevel(session) {
override fun <T : ConeSymbol> processElementsByName(
token: TowerScopeLevel.Token<T>,
@@ -262,22 +266,27 @@ class ScopeTowerLevel(
TowerScopeLevel.Token.Properties -> scope.processPropertiesByName(name) { candidate ->
if (candidate.hasConsistentExtensionReceiver(extensionReceiver) && candidate.dispatchReceiverValue() == null) {
processor.consumeCandidate(candidate as T, dispatchReceiverValue = null)
processor.consumeCandidate(
candidate as T, dispatchReceiverValue = null,
implicitExtensionReceiverValue = implicitExtensionReceiver
)
} else {
ProcessorAction.NEXT
}
}
TowerScopeLevel.Token.Functions -> scope.processFunctionsByName(name) { candidate ->
if (candidate.hasConsistentExtensionReceiver(extensionReceiver) && candidate.dispatchReceiverValue() == null) {
processor.consumeCandidate(candidate as T, dispatchReceiverValue = null)
processor.consumeCandidate(
candidate as T, dispatchReceiverValue = null,
implicitExtensionReceiverValue = implicitExtensionReceiver)
} else {
ProcessorAction.NEXT
}
}
TowerScopeLevel.Token.Objects -> scope.processClassifiersByNameWithAction(name, FirPosition.OTHER) {
processor.consumeCandidate(
it as T,
dispatchReceiverValue = null
it as T, dispatchReceiverValue = null,
implicitExtensionReceiverValue = null
)
}
}
@@ -309,13 +318,13 @@ class QualifiedReceiverTowerLevel(session: FirSession) : SessionBasedTowerLevel(
return if (token == TowerScopeLevel.Token.Objects) {
scope.processClassifiersByNameWithAction(name, FirPosition.OTHER) {
processor.consumeCandidate(it as T, null)
processor.consumeCandidate(it as T, null, null)
}
} else {
scope.processCallables(name, token.cast()) {
val fir = it.firUnsafe<FirCallableMemberDeclaration>()
if (fir.isStatic || it.callableId.classId == null) {
processor.consumeCandidate(it as T, null)
processor.consumeCandidate(it as T, null, null)
} else {
ProcessorAction.NEXT
}
@@ -346,14 +355,19 @@ class QualifiedReceiverTowerDataConsumer<T : ConeSymbol>(
name,
explicitReceiver,
processor = object : TowerScopeLevel.TowerScopeLevelProcessor<T> {
override fun consumeCandidate(symbol: T, dispatchReceiverValue: ClassDispatchReceiverValue?): ProcessorAction {
override fun consumeCandidate(
symbol: T,
dispatchReceiverValue: ClassDispatchReceiverValue?,
implicitExtensionReceiverValue: ImplicitReceiverValue?
): ProcessorAction {
assert(dispatchReceiverValue == null)
resultCollector.consumeCandidate(
group,
candidateFactory.createCandidate(
symbol,
null,
ExplicitReceiverKind.NO_EXPLICIT_RECEIVER
dispatchReceiverValue = null,
implicitExtensionReceiverValue = null,
explicitReceiverKind = ExplicitReceiverKind.NO_EXPLICIT_RECEIVER
)
)
return ProcessorAction.NEXT
@@ -500,12 +514,17 @@ class ExplicitReceiverTowerDataConsumer<T : ConeSymbol>(
name,
explicitReceiver = null,
processor = object : TowerScopeLevel.TowerScopeLevelProcessor<T> {
override fun consumeCandidate(symbol: T, dispatchReceiverValue: ClassDispatchReceiverValue?): ProcessorAction {
override fun consumeCandidate(
symbol: T,
dispatchReceiverValue: ClassDispatchReceiverValue?,
implicitExtensionReceiverValue: ImplicitReceiverValue?
): ProcessorAction {
resultCollector.consumeCandidate(
group,
candidateFactory.createCandidate(
symbol,
dispatchReceiverValue,
implicitExtensionReceiverValue,
ExplicitReceiverKind.DISPATCH_RECEIVER
)
)
@@ -520,12 +539,17 @@ class ExplicitReceiverTowerDataConsumer<T : ConeSymbol>(
name,
explicitReceiver = explicitReceiver,
processor = object : TowerScopeLevel.TowerScopeLevelProcessor<T> {
override fun consumeCandidate(symbol: T, dispatchReceiverValue: ClassDispatchReceiverValue?): ProcessorAction {
override fun consumeCandidate(
symbol: T,
dispatchReceiverValue: ClassDispatchReceiverValue?,
implicitExtensionReceiverValue: ImplicitReceiverValue?
): ProcessorAction {
resultCollector.consumeCandidate(
group,
candidateFactory.createCandidate(
symbol,
dispatchReceiverValue,
implicitExtensionReceiverValue,
ExplicitReceiverKind.EXTENSION_RECEIVER
)
)
@@ -562,12 +586,17 @@ class NoExplicitReceiverTowerDataConsumer<T : ConeSymbol>(
name,
explicitReceiver = null,
processor = object : TowerScopeLevel.TowerScopeLevelProcessor<T> {
override fun consumeCandidate(symbol: T, dispatchReceiverValue: ClassDispatchReceiverValue?): ProcessorAction {
override fun consumeCandidate(
symbol: T,
dispatchReceiverValue: ClassDispatchReceiverValue?,
implicitExtensionReceiverValue: ImplicitReceiverValue?
): ProcessorAction {
resultCollector.consumeCandidate(
group,
candidateFactory.createCandidate(
symbol,
dispatchReceiverValue,
implicitExtensionReceiverValue,
ExplicitReceiverKind.NO_EXPLICIT_RECEIVER
)
)
@@ -32,9 +32,13 @@ class CandidateFactory(
fun createCandidate(
symbol: ConeSymbol,
dispatchReceiverValue: ClassDispatchReceiverValue?,
implicitExtensionReceiverValue: ImplicitReceiverValue?,
explicitReceiverKind: ExplicitReceiverKind
): Candidate {
return Candidate(symbol, dispatchReceiverValue, explicitReceiverKind, inferenceComponents, baseSystem)
return Candidate(
symbol, dispatchReceiverValue, implicitExtensionReceiverValue,
explicitReceiverKind, inferenceComponents, baseSystem
)
}
}
@@ -98,23 +98,35 @@ internal sealed class CheckReceivers : ResolutionStage() {
abstract fun ExplicitReceiverKind.shouldBeResolvedAsImplicit(): Boolean
override suspend fun check(candidate: Candidate, sink: CheckerSink, callInfo: CallInfo) {
val receiverParameterValue = candidate.getReceiverValue()
val expectedReceiverParameterValue = candidate.getReceiverValue()
val explicitReceiverExpression = callInfo.explicitReceiver
val explicitReceiverKind = candidate.explicitReceiverKind
if (receiverParameterValue != null) {
if (expectedReceiverParameterValue != null) {
if (explicitReceiverExpression != null && explicitReceiverKind.shouldBeResolvedAsExplicit()) {
resolveArgumentExpression(
candidate.csBuilder,
explicitReceiverExpression,
candidate.substitutor.substituteOrSelf(receiverParameterValue.type),
explicitReceiverExpression.typeRef,
sink,
argument = explicitReceiverExpression,
expectedType = candidate.substitutor.substituteOrSelf(expectedReceiverParameterValue.type),
expectedTypeRef = explicitReceiverExpression.typeRef,
sink = sink,
isReceiver = true,
isSafeCall = callInfo.isSafeCall,
typeProvider = callInfo.typeProvider,
acceptLambdaAtoms = { candidate.postponedAtoms += it }
)
} else {
val argumentExtensionReceiverValue = candidate.implicitExtensionReceiverValue
if (argumentExtensionReceiverValue != null && explicitReceiverKind.shouldBeResolvedAsImplicit()) {
resolvePlainArgumentType(
candidate.csBuilder,
argumentType = argumentExtensionReceiverValue.type,
expectedType = candidate.substitutor.substituteOrSelf(expectedReceiverParameterValue.type),
sink = sink,
isReceiver = true,
isSafeCall = callInfo.isSafeCall
)
}
}
}
}
+9 -1
View File
@@ -2,4 +2,12 @@ fun String.foo() {}
fun String.bar() {
foo()
}
}
class My {
fun bar() {
foo()
}
}
fun My.foo() {}
+12
View File
@@ -4,3 +4,15 @@ FILE: extension.kt
public final fun R|kotlin/String|.bar(): R|kotlin/Unit| {
R|/foo|()
}
public final class My : R|kotlin/Any| {
public constructor(): R|My| {
super<R|kotlin/Any|>()
}
public final fun bar(): R|kotlin/Unit| {
R|/foo|()
}
}
public final fun R|My|.foo(): R|kotlin/Unit| {
}
@@ -9,7 +9,7 @@ FILE: arrayFirstOrNull.kt
}
public final fun <T> R|kotlin/Array<out T>|.firstOrNullX(): R|T|? {
^firstOrNullX when () {
<Ambiguity: isEmpty, [kotlin/collections/isEmpty, kotlin/collections/isEmpty, kotlin/collections/isEmpty, kotlin/collections/isEmpty, kotlin/collections/isEmpty, kotlin/collections/isEmpty, kotlin/collections/isEmpty, kotlin/collections/isEmpty, kotlin/collections/isEmpty, kotlin/text/isEmpty]>#() -> {
R|kotlin/collections/isEmpty|<R|T|>() -> {
Null(null)
}
else -> {
@@ -19,37 +19,37 @@ FILE: topLevelResolve.kt
^id R|<local>/arg|
}
public final fun testMap(): R|kotlin/Unit| {
lval first: R|kotlin/collections/List<kotlin/Int>| = R|kotlin/collections/listOf|<R|kotlin/Int|>(Int(1), Int(2), Int(3)).R|kotlin/collections/map|<R|kotlin/Int|, R|kotlin/Int|>(<L> = map@fun <anonymous>(it: R|kotlin/Int|): R|kotlin/Function1<kotlin/Int, kotlin/Int>| {
lval first: R|kotlin/collections/List<kotlin/Int>| = R|kotlin/collections/listOf|<R|kotlin/Int|>(Int(1), Int(2), Int(3)).R|kotlin/collections/map|<R|kotlin/Int|, R|kotlin/Int|>(<L> = map@fun <anonymous>(it: R|kotlin/Int|): R|kotlin/Int| {
R|<local>/it|.R|kotlin/Int.times|(Int(2))
}
)
lval second: R|kotlin/collections/List<kotlin/Int>| = R|kotlin/intArrayOf|(Int(4), Int(5), Int(6)).R|kotlin/collections/map|<R|kotlin/Int|>(<L> = map@fun <anonymous>(it: R|kotlin/Int|): R|kotlin/Function1<kotlin/Int, kotlin/Int>| {
lval second: R|kotlin/collections/List<kotlin/Int>| = R|kotlin/intArrayOf|(Int(4), Int(5), Int(6)).R|kotlin/collections/map|<R|kotlin/Int|>(<L> = map@fun <anonymous>(it: R|kotlin/Int|): R|kotlin/Int| {
R|<local>/it|.R|kotlin/Int.times|(Int(2))
}
)
lval withId: R|kotlin/collections/List<kotlin/Int>| = R|kotlin/collections/listOf|<R|kotlin/Int|>(Int(1), Int(2), Int(3)).R|kotlin/collections/map|<R|kotlin/Int|, R|kotlin/Int|>(<L> = map@fun <anonymous>(it: R|kotlin/Int|): R|kotlin/Function1<kotlin/Int, kotlin/Int>| {
lval withId: R|kotlin/collections/List<kotlin/Int>| = R|kotlin/collections/listOf|<R|kotlin/Int|>(Int(1), Int(2), Int(3)).R|kotlin/collections/map|<R|kotlin/Int|, R|kotlin/Int|>(<L> = map@fun <anonymous>(it: R|kotlin/Int|): R|kotlin/Int| {
R|/id|<R|kotlin/Int|>(R|<local>/it|)
}
)
lval stringToInt: R|kotlin/collections/List<kotlin/Int>| = R|kotlin/collections/listOf|<R|kotlin/String|>(String(alpha), String(omega)).R|kotlin/collections/map|<R|kotlin/String|, R|kotlin/Int|>(<L> = map@fun <anonymous>(it: R|kotlin/String|): R|kotlin/Function1<kotlin/String, kotlin/Int>| {
lval stringToInt: R|kotlin/collections/List<kotlin/Int>| = R|kotlin/collections/listOf|<R|kotlin/String|>(String(alpha), String(omega)).R|kotlin/collections/map|<R|kotlin/String|, R|kotlin/Int|>(<L> = map@fun <anonymous>(it: R|kotlin/String|): R|kotlin/Int| {
R|<local>/it|.R|kotlin/String.length|
}
)
lval viaWith: R|kotlin/Nothing| = R|kotlin/with|<R|kotlin/collections/List<kotlin/Int>|, R|kotlin/Nothing|>(R|kotlin/collections/listOf|<R|kotlin/Int|>(Int(42)), <L> = with@fun R|kotlin/collections/List<kotlin/Int>|.<anonymous>(it: R|kotlin/collections/List<kotlin/Int>|): R|kotlin/Function2<kotlin/collections/List<kotlin/Int>, kotlin/collections/List<kotlin/Int>, kotlin/Nothing>| {
<Ambiguity: map, [kotlin/map, kotlin/collections/map, kotlin/collections/map, kotlin/collections/map, kotlin/collections/map, kotlin/collections/map, kotlin/collections/map, kotlin/collections/map, kotlin/collections/map, kotlin/collections/map, kotlin/collections/map, kotlin/collections/map, kotlin/collections/map, kotlin/collections/map, kotlin/collections/map, kotlin/collections/map, kotlin/sequences/map, kotlin/text/map]>#(<L> = map@fun <implicit>.<anonymous>(): <implicit> {
it#.times#(it#)
lval viaWith: R|kotlin/collections/List<kotlin/Int>| = R|kotlin/with|<R|kotlin/collections/List<kotlin/Int>|, R|kotlin/collections/List<kotlin/Int>|>(R|kotlin/collections/listOf|<R|kotlin/Int|>(Int(42)), <L> = with@fun R|kotlin/collections/List<kotlin/Int>|.<anonymous>(it: R|kotlin/collections/List<kotlin/Int>|): R|kotlin/collections/List<kotlin/Int>| {
R|kotlin/collections/map|<R|kotlin/Int|, R|kotlin/Int|>(<L> = map@fun <anonymous>(it: R|kotlin/Int|): R|kotlin/Int| {
R|<local>/it|.R|kotlin/Int.times|(R|<local>/it|)
}
)
}
)
}
public final fun testWith(): R|kotlin/Unit| {
lval length: R|kotlin/Int| = R|kotlin/with|<R|kotlin/String|, R|kotlin/Int|>(String(), <L> = with@fun R|kotlin/String|.<anonymous>(it: R|kotlin/String|): R|kotlin/Function2<kotlin/String, kotlin/String, kotlin/Int>| {
lval length: R|kotlin/Int| = R|kotlin/with|<R|kotlin/String|, R|kotlin/Int|>(String(), <L> = with@fun R|kotlin/String|.<anonymous>(it: R|kotlin/String|): R|kotlin/Int| {
R|kotlin/String.length|
}
)
lval indices: R|kotlin/Nothing| = R|kotlin/with|<R|kotlin/String|, R|kotlin/Nothing|>(String(), <L> = with@fun R|kotlin/String|.<anonymous>(it: R|kotlin/String|): R|kotlin/Function2<kotlin/String, kotlin/String, kotlin/Nothing>| {
<Ambiguity: indices, [kotlin/collections/indices, kotlin/collections/indices, kotlin/collections/indices, kotlin/collections/indices, kotlin/collections/indices, kotlin/collections/indices, kotlin/collections/indices, kotlin/collections/indices, kotlin/collections/indices, kotlin/collections/indices, kotlin/collections/indices, kotlin/collections/indices, kotlin/collections/indices, kotlin/collections/indices, kotlin/text/indices]>#
lval indices: R|kotlin/ranges/IntRange| = R|kotlin/with|<R|kotlin/String|, R|kotlin/ranges/IntRange|>(String(), <L> = with@fun R|kotlin/String|.<anonymous>(it: R|kotlin/String|): R|kotlin/ranges/IntRange| {
R|kotlin/text/indices|
}
)
lval indicesNoWith: R|kotlin/ranges/IntRange| = String().R|kotlin/text/indices|
+1 -1
View File
@@ -37,6 +37,6 @@ FILE fqName:<root> fileName:/calls.kt
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun ext3 (x: kotlin.Int): kotlin.Int declared in <root>'
CALL 'public final fun foo (x: kotlin.Int, y: kotlin.Int): kotlin.Int declared in <root>' type=kotlin.Int origin=null
x: ERROR_CALL 'Unresolved reference: <Unresolved name: ext1>#' type=IrErrorType
x: CALL 'public final fun ext1 (): kotlin.Int declared in <root>' type=kotlin.Int origin=null
y: GET_VAR 'x: kotlin.Int declared in <root>.ext3' type=kotlin.Int origin=null
@@ -5,8 +5,7 @@ FILE fqName:<root> fileName:/extensionPropertyGetterCall.kt
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun <get-okext> (): kotlin.String declared in <root>'
CONST String type=kotlin.String value="OK"
FUN name:test5 visibility:public modality:FINAL <> () returnType:IrErrorType
FUN name:test5 visibility:public modality:FINAL <> () returnType:kotlin.String
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test5 (): IrErrorType declared in <root>'
ERROR_CALL 'Unresolved reference: <Unresolved name: okext>#' type=IrErrorType
RETURN type=kotlin.Nothing from='public final fun test5 (): kotlin.String declared in <root>'
CALL 'public final fun <get-okext> (): kotlin.String declared in <root>' type=kotlin.String origin=null
+3 -4
View File
@@ -48,8 +48,7 @@ FILE fqName:<root> fileName:/references.kt
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun <get-okext> (): kotlin.String declared in <root>'
CONST String type=kotlin.String value="OK"
FUN name:test5 visibility:public modality:FINAL <> () returnType:IrErrorType
FUN name:test5 visibility:public modality:FINAL <> () returnType:kotlin.String
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test5 (): IrErrorType declared in <root>'
ERROR_CALL 'Unresolved reference: <Unresolved name: okext>#' type=IrErrorType
RETURN type=kotlin.Nothing from='public final fun test5 (): kotlin.String declared in <root>'
CALL 'public final fun <get-okext> (): kotlin.String declared in <root>' type=kotlin.String origin=null
+1 -2
View File
@@ -9,5 +9,4 @@ FILE fqName:<root> fileName:/samAdapter.kt
ERROR_CALL 'Unresolved reference: println#' type=IrErrorType
CONST String type=IrErrorType value="Hello, world!"
FUNCTION_REFERENCE 'local final fun <anonymous> (): IrErrorType declared in <root>.test1' type=IrErrorType origin=LAMBDA
ERROR_CALL 'Unresolved reference: <Inapplicable(WRONG_RECEIVER): [kotlin/run]>#' type=IrErrorType
ERROR_CALL 'Unresolved reference: <Inapplicable(PARAMETER_MAPPING_ERROR): [kotlin/run]>#' type=IrErrorType