[FIR] Don't provide receiver as value in delegated constructor call

This commit is contained in:
Mikhail Glukhikh
2020-03-31 13:10:37 +03:00
parent a377a6fccb
commit 630adb34db
35 changed files with 78 additions and 69 deletions
@@ -68,8 +68,14 @@ class ImplicitReceiverStackImpl private constructor(
}
override operator fun get(name: String?): ImplicitReceiverValue<*>? {
if (name == null) return stack.lastOrNull()
return indexesPerLabel[Name.identifier(name)].lastOrNull()?.let { stack[it] }
if (name == null) {
return stack.lastOrNull {
it !is ImplicitDispatchReceiverValue || !it.inDelegated
}
}
return indexesPerLabel[Name.identifier(name)].lastOrNull()?.let { stack[it] }?.takeIf {
it !is ImplicitDispatchReceiverValue || !it.inDelegated
}
}
override fun lastDispatchReceiver(): ImplicitDispatchReceiverValue? {
@@ -84,8 +84,8 @@ internal class ExpressionReceiverValue(
sealed class ImplicitReceiverValue<S : AbstractFirBasedSymbol<*>>(
val boundSymbol: S,
type: ConeKotlinType,
private val useSiteSession: FirSession,
private val scopeSession: ScopeSession
protected val useSiteSession: FirSession,
protected val scopeSession: ScopeSession
) : ReceiverValue {
final override var type: ConeKotlinType = type
private set
@@ -120,6 +120,7 @@ sealed class ImplicitReceiverValue<S : AbstractFirBasedSymbol<*>>(
internal enum class ImplicitDispatchReceiverKind {
REGULAR,
REGULAR_IN_DELEGATED,
COMPANION,
COMPANION_FROM_SUPERTYPE
}
@@ -138,6 +139,11 @@ class ImplicitDispatchReceiverValue internal constructor(
useSiteSession, scopeSession, kind
)
fun copyForDelegated(): ImplicitDispatchReceiverValue =
ImplicitDispatchReceiverValue(boundSymbol, type, useSiteSession, scopeSession, ImplicitDispatchReceiverKind.REGULAR_IN_DELEGATED)
val inDelegated: Boolean get() = kind == ImplicitDispatchReceiverKind.REGULAR_IN_DELEGATED
val implicitCompanion: Boolean get() = kind != ImplicitDispatchReceiverKind.REGULAR
val companionFromSupertype: Boolean get() = kind == ImplicitDispatchReceiverKind.COMPANION_FROM_SUPERTYPE
@@ -60,7 +60,7 @@ class FirTowerResolver(
if (!it.implicitCompanion && klass?.isCompanion == true) {
explicitCompanions += klass.symbol
}
if (firstDispatchValue) {
if (firstDispatchValue && !it.inDelegated) {
if (!it.implicitCompanion &&
klass?.isInner == false &&
!symbol.classId.isLocal
@@ -69,7 +69,7 @@ class FirTowerResolver(
}
true
} else {
symbol.fir.classKind == ClassKind.OBJECT
symbol.fir.classKind == ClassKind.OBJECT && !it.inDelegated
}
}
}
@@ -594,13 +594,23 @@ class FirExpressionsResolveTransformer(transformer: FirBodyResolveTransformer) :
var callCompleted = true
var result = delegatedConstructorCall
try {
val lastDispatchReceiver = implicitReceiverStack.lastDispatchReceiver()
val name = lastDispatchReceiver?.boundSymbol?.classId?.shortClassName
if (lastDispatchReceiver != null) {
context.implicitReceiverStack.pop(name)
context.implicitReceiverStack.add(name, lastDispatchReceiver.copyForDelegated())
}
delegatedConstructorCall.transformChildren(transformer, ResolutionMode.ContextDependent)
if (lastDispatchReceiver != null) {
context.implicitReceiverStack.pop(name)
context.implicitReceiverStack.add(name, lastDispatchReceiver)
}
val typeArguments: List<FirTypeProjection>
val symbol: FirClassSymbol<*> = when (val reference = delegatedConstructorCall.calleeReference) {
is FirThisReference -> {
typeArguments = emptyList()
if (reference.boundSymbol == null) {
implicitReceiverStack.lastDispatchReceiver()?.boundSymbol?.also {
lastDispatchReceiver?.boundSymbol?.also {
reference.replaceBoundSymbol(it)
} ?: return delegatedConstructorCall.compose()
} else {
@@ -611,15 +621,15 @@ class FirExpressionsResolveTransformer(transformer: FirBodyResolveTransformer) :
// TODO: unresolved supertype
val supertype = reference.superTypeRef.coneTypeSafe<ConeClassLikeType>() ?: return delegatedConstructorCall.compose()
val expandedSupertype = supertype.fullyExpandedType(session)
val symbol =
expandedSupertype.lookupTag.toSymbol(session) as? FirClassSymbol<*> ?: return delegatedConstructorCall.compose()
val symbol =
expandedSupertype.lookupTag.toSymbol(session) as? FirClassSymbol<*> ?: return delegatedConstructorCall.compose()
val classTypeParametersCount = (symbol.fir as? FirTypeParametersOwner)?.typeParameters?.size ?: 0
typeArguments = expandedSupertype.typeArguments
.takeLast(classTypeParametersCount) // Hack for KT-37525
.takeIf { it.isNotEmpty() }
?.map { it.toFirTypeProjection() }
?: emptyList()
symbol
typeArguments = expandedSupertype.typeArguments
.takeLast(classTypeParametersCount) // Hack for KT-37525
.takeIf { it.isNotEmpty() }
?.map { it.toFirTypeProjection() }
?: emptyList()
symbol
}
else -> return delegatedConstructorCall.compose()
}
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
class B () {}
open class A(val b : B) {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
open class Base(val callback: () -> String)
class Outer {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
open class Base(val callback: () -> String)
class Outer {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
open class Outer(val fn: (() -> String)?) {
companion object {
val ok = "OK"
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun WithCompanion.test(): String {
object : WithCompanion(this) {}
return "OK"
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
abstract class Base(val fn: () -> String)
object Test : Base(run { { Test.ok() } }) {
@@ -1,5 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
open class Base(val s: String)
object Host {
@@ -6,5 +6,5 @@ class B : A {
override fun f() {}
class C : A by this {}
class D(val x : B = this)
class E : <!INAPPLICABLE_CANDIDATE!>P<!>(this)
class E : P(this)
}
@@ -37,8 +37,8 @@ class A(
Companion.CONST,
Nested.CONST,
Interface.CONST,
a,
b()
<!UNRESOLVED_REFERENCE!>a<!>,
<!UNRESOLVED_REFERENCE!>b<!>()
)
class Nested {
@@ -1,8 +0,0 @@
open class S(val a: Any, val b: Any, val c: Any) {}
object A : S(prop1, prop2, func()) {
val prop1 = 1
val prop2: Int
get() = 1
fun func() {}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
open class S(val a: Any, val b: Any, val c: Any) {}
object A : S(<!UNRESOLVED_REFERENCE!>prop1<!>, <!UNRESOLVED_REFERENCE!>prop2<!>, <!UNRESOLVED_REFERENCE!>func<!>()) {
@@ -20,8 +20,8 @@ class A : S (
Companion.CONST,
Nested.CONST,
Interface.CONST,
a,
b()
<!UNRESOLVED_REFERENCE!>a<!>,
<!UNRESOLVED_REFERENCE!>b<!>()
) {
class Nested {
@@ -22,8 +22,8 @@ class A : S {
Companion.CONST,
Nested.CONST,
Interface.CONST,
a,
b()
<!UNRESOLVED_REFERENCE!>a<!>,
<!UNRESOLVED_REFERENCE!>b<!>()
)
class Nested {
@@ -5,7 +5,7 @@ import java.util.HashMap
//KT-250 Incorrect variable resolve in constructor arguments of superclass
open class A(val x: Int)
class B(y: Int) : A(x) //x is resolved as a property in a, so no error is generated
class B(y: Int) : A(<!UNRESOLVED_REFERENCE!>x<!>) //x is resolved as a property in a, so no error is generated
//KT-617 Prohibit dollars in call to superclass constructors
open class M(p: Int)
@@ -29,9 +29,9 @@ abstract class TagWithText(name : String) : Tag(name) {
open class BodyTag(name : String) : TagWithText(name) {
}
class Body() : BodyTag(name) { // Must be an error!
class Body() : BodyTag(<!UNRESOLVED_REFERENCE!>name<!>) { // Must be an error!
}
class Body1() : BodyTag(this.name) { // Must be an error!
class Body1() : BodyTag(this.<!UNRESOLVED_REFERENCE!>name<!>) { // Must be an error!
}
//more tests
@@ -40,10 +40,10 @@ open class X(p: Int, r: Int) {
val s = "s"
}
class Y(i: Int) : X(i, rrr) {
class Y(i: Int) : X(i, <!UNRESOLVED_REFERENCE!>rrr<!>) {
val rrr = 3
}
class Z(val i: Int) : <!INAPPLICABLE_CANDIDATE!>X<!>(s, x) {
class Z(val i: Int) : X(<!UNRESOLVED_REFERENCE!>s<!>, <!UNRESOLVED_REFERENCE!>x<!>) {
val x = 2
}
@@ -6,7 +6,7 @@ open class Base<T>(p: Any?) {
class D: Base<Int>("") {
inner class B : Base<String> {
constructor() : <!INAPPLICABLE_CANDIDATE!>super<!>(foo1(""))
constructor() : <!INAPPLICABLE_CANDIDATE!>super<!>(<!INAPPLICABLE_CANDIDATE!>foo1<!>(""))
constructor(x: Int) : <!INAPPLICABLE_CANDIDATE!>super<!>(foo1(1))
}
}
@@ -7,7 +7,7 @@ open class Base<T>(p: Any?) {
class D: Base<Int>(1) {
inner class B : Base<Int> {
constructor() : <!INAPPLICABLE_CANDIDATE!>super<!>(foo1(1))
constructor(x: Int) : <!INAPPLICABLE_CANDIDATE!>super<!>(this@B.foo1(1))
constructor(x: Int) : <!INAPPLICABLE_CANDIDATE!>super<!>(this@B.<!UNRESOLVED_REFERENCE!>foo1<!>(1))
constructor(x: Int, y: Int) : <!INAPPLICABLE_CANDIDATE!>super<!>(this@D.foo1(1))
}
}
@@ -7,6 +7,6 @@ fun Base.foo() {
class B : Base {
constructor() : <!INAPPLICABLE_CANDIDATE!>super<!>(foo1())
constructor(x: Int) : <!INAPPLICABLE_CANDIDATE!>super<!>(this@foo.foo1())
constructor(x: Int, y: Int) : <!INAPPLICABLE_CANDIDATE!>super<!>(this@B.foo1())
constructor(x: Int, y: Int) : <!INAPPLICABLE_CANDIDATE!>super<!>(this@B.<!UNRESOLVED_REFERENCE!>foo1<!>())
}
}
@@ -5,9 +5,9 @@ open class Base<T>(p: Any?) {
fun Base<Int>.foo() {
class B : Base<String> {
constructor() : <!INAPPLICABLE_CANDIDATE!>super<!>(foo1(""))
constructor() : <!INAPPLICABLE_CANDIDATE!>super<!>(<!INAPPLICABLE_CANDIDATE!>foo1<!>(""))
constructor(x: Int) : <!INAPPLICABLE_CANDIDATE!>super<!>(foo1(1))
constructor(x: Int, y: Int) : <!INAPPLICABLE_CANDIDATE!>super<!>(this@foo.foo1(12))
constructor(x: Int, y: Int, z: Int) : <!INAPPLICABLE_CANDIDATE!>super<!>(this@B.foo1(""))
constructor(x: Int, y: Int, z: Int) : <!INAPPLICABLE_CANDIDATE!>super<!>(this@B.<!UNRESOLVED_REFERENCE!>foo1<!>(""))
}
}
@@ -7,5 +7,5 @@ class Outer {
constructor(x: Int)
constructor(x: Int, y: Int, z: Int = x + Inner().prop + this.Inner().prop) :
this(x + Inner().prop + this.Inner().prop)
this(x + Inner().prop <!AMBIGUITY!>+<!> this.<!UNRESOLVED_REFERENCE!>Inner<!>().<!UNRESOLVED_REFERENCE!>prop<!>)
}
@@ -7,9 +7,9 @@ class A {
constructor(x: () -> Int)
constructor() : this(
{
foo() +
this.foo() +
this@A.foo() +
foobar()
<!UNRESOLVED_REFERENCE!>foo<!>() +
this.<!UNRESOLVED_REFERENCE!>foo<!>() +
this@A.<!UNRESOLVED_REFERENCE!>foo<!>() +
<!UNRESOLVED_REFERENCE!>foobar<!>()
})
}
@@ -3,5 +3,5 @@ class A {
fun foo() = 1
constructor(x: Int)
constructor(x: Int, y: Int, z: Int = x + foo() + this.foo()) :
this(x + foo() + this.foo())
<!INAPPLICABLE_CANDIDATE!>this<!>(x <!AMBIGUITY!>+<!> <!UNRESOLVED_REFERENCE!>foo<!>() + this.<!UNRESOLVED_REFERENCE!>foo<!>())
}
@@ -6,7 +6,7 @@ class A {
fun foo() = 1
constructor(x: Any?)
constructor() : this(object {
fun bar() = foo() + this@A.foo() +
foobar() + super@A.hashCode()
fun bar() = <!UNRESOLVED_REFERENCE!>foo<!>() + this@A.<!UNRESOLVED_REFERENCE!>foo<!>() +
<!INAPPLICABLE_CANDIDATE!>foobar<!>() + super@A.hashCode()
})
}
@@ -4,8 +4,8 @@ class D : C {
constructor() : <!INAPPLICABLE_CANDIDATE!>super<!>(
{
val s = ""
s()
""()
<!UNRESOLVED_REFERENCE!>s<!>()
<!UNRESOLVED_REFERENCE!>""()<!>
42
}())
@@ -3,5 +3,5 @@ class A {
val prop = 1
constructor(x: Int)
constructor(x: Int, y: Int, z: Int = x + prop + this.prop) :
this(x + prop + this.prop)
<!INAPPLICABLE_CANDIDATE!>this<!>(x <!AMBIGUITY!>+<!> <!UNRESOLVED_REFERENCE!>prop<!> + this.<!UNRESOLVED_REFERENCE!>prop<!>)
}
@@ -3,5 +3,5 @@ open class B(x: Int)
class A : B {
val prop = 1
constructor(x: Int, y: Int = x + prop + this.prop) :
<!INAPPLICABLE_CANDIDATE!>super<!>(x + prop + this.prop)
<!INAPPLICABLE_CANDIDATE!>super<!>(x <!AMBIGUITY!>+<!> <!UNRESOLVED_REFERENCE!>prop<!> + this.<!UNRESOLVED_REFERENCE!>prop<!>)
}
@@ -4,5 +4,5 @@ open class B(x: Int) {
}
class A : B {
constructor(x: Int, y: Int = x + foo() + this.foo() + super.foo()) :
<!INAPPLICABLE_CANDIDATE!>super<!>(x + foo() + this.foo() + super.foo())
<!INAPPLICABLE_CANDIDATE!>super<!>(x <!AMBIGUITY!>+<!> <!UNRESOLVED_REFERENCE!>foo<!>() + this.<!UNRESOLVED_REFERENCE!>foo<!>() + super.foo())
}
@@ -5,5 +5,5 @@ open class B(x: Int) {
class A : B {
override fun foo() = 2
constructor(x: Int, y: Int = x + foo() + this.foo() + super.foo()) :
<!INAPPLICABLE_CANDIDATE!>super<!>(x + foo() + this.foo() + super.foo())
<!INAPPLICABLE_CANDIDATE!>super<!>(x <!AMBIGUITY!>+<!> <!UNRESOLVED_REFERENCE!>foo<!>() + this.<!UNRESOLVED_REFERENCE!>foo<!>() + super.foo())
}
@@ -2,5 +2,5 @@
open class B(val prop: Int)
class A : B {
constructor(x: Int, y: Int = x + prop + this.prop + super.prop) :
<!INAPPLICABLE_CANDIDATE!>super<!>(x + prop + this.prop + super.prop)
<!INAPPLICABLE_CANDIDATE!>super<!>(x <!AMBIGUITY!>+<!> <!UNRESOLVED_REFERENCE!>prop<!> + this.<!UNRESOLVED_REFERENCE!>prop<!> + super.prop)
}
@@ -5,10 +5,10 @@ val A.prop: Int get() = 2
class A {
constructor(x: Int)
constructor() : this(
foobar() +
constructor() : <!INAPPLICABLE_CANDIDATE!>this<!>(
<!UNRESOLVED_REFERENCE!>foobar<!>() +
this.foobar() +
prop +
<!UNRESOLVED_REFERENCE!>prop<!> +
this.prop +
this@A.prop
)
@@ -41,7 +41,7 @@ FILE fqName:<root> fileName:/thisRefToObjectInNestedClassConstructorCall.kt
CONSTRUCTOR visibility:public <> () returnType:<root>.Host.Derived1 [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> (x: kotlin.Any) [primary] declared in <root>.Base'
x: GET_VAR '<this>: <root>.Host.Derived1 declared in <root>.Host.Derived1' type=<root>.Host.Derived1 origin=null
x: GET_OBJECT 'CLASS OBJECT name:Host modality:FINAL visibility:public superTypes:[kotlin.Any]' type=<root>.Host
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Derived1 modality:FINAL visibility:public superTypes:[<root>.Base]'
PROPERTY FAKE_OVERRIDE name:x visibility:public modality:FINAL [fake_override,val]
FUN FAKE_OVERRIDE name:<get-x> visibility:public modality:FINAL <> ($this:<root>.Host.Derived1) returnType:kotlin.Any [fake_override]
@@ -8,7 +8,8 @@ FILE fqName:<root> fileName:/thisReferenceBeforeClassDeclared.kt
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.test.<no name provided>
CONSTRUCTOR visibility:private <> () returnType:<root>.test.<no name provided> [primary]
BLOCK_BODY
ERROR_CALL 'Cannot find delegated constructor call' type=<root>.WithCompanion
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> (a: <root>.WithCompanion.Companion) [primary] declared in <root>.WithCompanion'
a: GET_OBJECT 'CLASS OBJECT name:Companion modality:FINAL visibility:public [companion] superTypes:[kotlin.Any]' type=<root>.WithCompanion.Companion
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:<no name provided> modality:FINAL visibility:local superTypes:[<root>.WithCompanion]'
CONSTRUCTOR_CALL 'private constructor <init> () [primary] declared in <root>.test.<no name provided>' type=<root>.test.<no name provided> origin=OBJECT_LITERAL
VAR name:test2 type:<root>.test.<no name provided> [val]
@@ -18,7 +19,8 @@ FILE fqName:<root> fileName:/thisReferenceBeforeClassDeclared.kt
CONSTRUCTOR visibility:private <> () returnType:<root>.test.<no name provided> [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> (a: <root>.WithCompanion.Companion) [primary] declared in <root>.WithCompanion'
a: ERROR_CALL 'Unresolved reference: <Unresolved name: foo>#' type=IrErrorType
a: CALL 'public final fun foo (): <root>.WithCompanion.Companion declared in <root>.WithCompanion.Companion' type=<root>.WithCompanion.Companion origin=null
$this: GET_OBJECT 'CLASS OBJECT name:Companion modality:FINAL visibility:public [companion] superTypes:[kotlin.Any]' type=<root>.WithCompanion.Companion
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:<no name provided> modality:FINAL visibility:local superTypes:[<root>.WithCompanion]'
CONSTRUCTOR_CALL 'private constructor <init> () [primary] declared in <root>.test.<no name provided>' type=<root>.test.<no name provided> origin=OBJECT_LITERAL
CLASS CLASS name:WithCompanion modality:OPEN visibility:public superTypes:[kotlin.Any]