Skip to content

Commit

Permalink
[FIR] Correctly detect super type in delegated constructor call
Browse files Browse the repository at this point in the history
  • Loading branch information
demiurg906 committed Apr 17, 2020
1 parent adfd478 commit 7f02d57
Show file tree
Hide file tree
Showing 56 changed files with 199 additions and 230 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// FILE: AJava.java

public class AJava {
public AJava(String s) {}
}

// FILE: main.kt

class BJava : AJava, C {
constructor(s: String) : super(s)
}

open class AKt(val s: String)

class BKt : AKt, C {
constructor(s: String) : super(s)
}

interface C

typealias QQQ = AKt

typealias DDD = C

class CKt : QQQ, DDD {
constructor(s: String) : super(s)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
FILE: main.kt
public final class BJava : R|AJava|, R|C| {
public constructor(s: R|kotlin/String|): R|BJava| {
super<R|AJava|>(R|<local>/s|)
}

}
public open class AKt : R|kotlin/Any| {
public constructor(s: R|kotlin/String|): R|AKt| {
super<R|kotlin/Any|>()
}

public final val s: R|kotlin/String| = R|<local>/s|
public get(): R|kotlin/String|

}
public final class BKt : R|AKt|, R|C| {
public constructor(s: R|kotlin/String|): R|BKt| {
super<R|AKt|>(R|<local>/s|)
}

}
public abstract interface C : R|kotlin/Any| {
}
public final typealias QQQ = R|AKt|
public final typealias DDD = R|C|
public final class CKt : R|QQQ|, R|DDD| {
public constructor(s: R|kotlin/String|): R|CKt| {
super<R|QQQ|>(R|<local>/s|)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ FILE: LinkedList.kt
FILE: HashSet.kt
public final class HashSet<T> : R|java/util/HashSet<T>| {
public constructor<T>(): R|util/HashSet<T>| {
super<R|kotlin/Any|>()
super<R|java/util/HashSet<T>|>()
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ FILE: notASupertype.kt
}
public final class B : R|A| {
public constructor(): R|B| {
super<R|kotlin/Any|>()
super<R|A|>()
}

public final fun g(): R|kotlin/Unit| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ FILE: checkArguments.kt
}
public final class C : R|B| {
public constructor(): R|C| {
super<R|kotlin/Any|>()
super<R|B|>()
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FILE: test.kt
public final class MyDecorator : R|Decorator<LookupElement>| {
public constructor(): R|MyDecorator| {
super<R|kotlin/Any|>()
super<R|Decorator<LookupElement>|>()
}

public final override fun getLookupString(): R|kotlin/String| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ FILE: test.kt

public final class Extra : R|test/Test| {
public constructor(x: R|kotlin/Int|): R|test/Test.Extra| {
super<R|kotlin/Any|>()
super<R|test/Test|>()
}

public final val x: R|kotlin/Int| = R|<local>/x|
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
package org.jetbrains.kotlin.fir.resolve.transformers.body.resolve

import com.intellij.openapi.progress.ProcessCanceledException
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.fir.*
import org.jetbrains.kotlin.fir.declarations.FirTypeParameter
import org.jetbrains.kotlin.fir.declarations.FirTypeParameterRefsOwner
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.diagnostics.DiagnosticKind
import org.jetbrains.kotlin.fir.diagnostics.ConeSimpleDiagnostic
import org.jetbrains.kotlin.fir.diagnostics.ConeStubDiagnostic
Expand All @@ -18,13 +18,15 @@ import org.jetbrains.kotlin.fir.expressions.builder.buildFunctionCall
import org.jetbrains.kotlin.fir.expressions.builder.buildVariableAssignment
import org.jetbrains.kotlin.fir.references.*
import org.jetbrains.kotlin.fir.references.builder.buildErrorNamedReference
import org.jetbrains.kotlin.fir.references.builder.buildExplicitSuperReference
import org.jetbrains.kotlin.fir.references.builder.buildSimpleNamedReference
import org.jetbrains.kotlin.fir.references.impl.FirSimpleNamedReference
import org.jetbrains.kotlin.fir.resolve.*
import org.jetbrains.kotlin.fir.resolve.calls.candidate
import org.jetbrains.kotlin.fir.resolve.diagnostics.*
import org.jetbrains.kotlin.fir.resolve.transformers.InvocationKindTransformer
import org.jetbrains.kotlin.fir.resolve.transformers.StoreReceiver
import org.jetbrains.kotlin.fir.resolve.transformers.firClassLike
import org.jetbrains.kotlin.fir.scopes.impl.withReplacedConeType
import org.jetbrains.kotlin.fir.symbols.StandardClassIds
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
Expand Down Expand Up @@ -604,6 +606,21 @@ class FirExpressionsResolveTransformer(transformer: FirBodyResolveTransformer) :
when (delegatedConstructorCall.calleeReference) {
is FirResolvedNamedReference, is FirErrorNamedReference -> return delegatedConstructorCall.compose()
}
if (delegatedConstructorCall.isSuper && delegatedConstructorCall.constructedTypeRef is FirImplicitTypeRef) {
val containers = components.context.containers
val containingClass = containers[containers.lastIndex - 1] as FirClass<*>
val superClass = containingClass.superTypeRefs.firstOrNull {
if (it !is FirResolvedTypeRef) return@firstOrNull false
val declaration = extractSuperTypeDeclaration(it) ?: return@firstOrNull false
declaration.classKind == ClassKind.CLASS
} as FirResolvedTypeRef? ?: session.builtinTypes.anyType
delegatedConstructorCall.replaceConstructedTypeRef(superClass)
delegatedConstructorCall.replaceCalleeReference(buildExplicitSuperReference {
source = delegatedConstructorCall.calleeReference.source
superTypeRef = superClass
})
}

dataFlowAnalyzer.enterCall(delegatedConstructorCall)
var callCompleted = true
var result = delegatedConstructorCall
Expand Down Expand Up @@ -661,6 +678,15 @@ class FirExpressionsResolveTransformer(transformer: FirBodyResolveTransformer) :
}
}

private fun extractSuperTypeDeclaration(typeRef: FirTypeRef): FirRegularClass? {
if (typeRef !is FirResolvedTypeRef) return null
return when (val declaration = typeRef.firClassLike(session)) {
is FirRegularClass -> declaration
is FirTypeAlias -> extractSuperTypeDeclaration(declaration.expandedTypeRef)
else -> null
}
}

@OptIn(ExperimentalStdlibApi::class)
override fun transformAugmentedArraySetCall(augmentedArraySetCall: FirAugmentedArraySetCall, data: ResolutionMode): CompositeTransformResult<FirStatement> {
assert(augmentedArraySetCall.operation in FirOperation.ASSIGNMENTS)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
open class A(val x: String) {
constructor(`in`: String, y: String) : this(`in` + y)

Expand Down
1 change: 0 additions & 1 deletion compiler/testData/codegen/box/classes/sealedInSameFile.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
class B : A()

sealed class A() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
open class Base(val callback: () -> String)

class Outer {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND_FIR: JVM_IR

inline class S(val string: String)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
open class A(val x: String = "abc", val y: String = "efg") {
constructor(x: String, y: String, z: Int): this(x, y + "#" + z.toString())

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
var log = ""

open class Base(val s: String)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
internal open class B<T>(val x: T, val y: T) {
constructor(x: T): this(x, x)
override fun toString() = "$x#$y"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
open class C(val grandParentProp: String)
fun box(): String {
var sideEffects: String = ""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
var sideEffects: String = ""

internal abstract class B {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
class CustomException : Throwable {
constructor(message: String?, cause: Throwable?) : super(message, cause)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
class MyThrowable : Throwable {
val x: String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class B1(x: List<String>) : A<CharSequence>("", x)
class B2(x: List<Int>) : <!INAPPLICABLE_CANDIDATE!>A<CharSequence><!>("", x)

class C : A<CharSequence> {
constructor(x: List<String>) : <!INAPPLICABLE_CANDIDATE!>super<!>("", x)
constructor(x: List<String>) : super("", x)
constructor(x: List<Int>, y: Int) : <!INAPPLICABLE_CANDIDATE!>super<!>("", x)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class B1(x: List<String>) : A<CharSequence>("", x)
class B2(x: List<Int>) : <!INAPPLICABLE_CANDIDATE!>A<CharSequence><!>("", x)

class C : A<CharSequence> {
constructor(x: List<String>) : <!INAPPLICABLE_CANDIDATE!>super<!>("", x)
constructor(x: List<String>) : super("", x)
constructor(x: List<Int>, y: Int) : <!INAPPLICABLE_CANDIDATE!>super<!>("", x)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ expect open class A {
}

expect class B : A {
constructor(i: Int)
<!INAPPLICABLE_CANDIDATE!>constructor(i: Int)<!>

constructor() : <!INAPPLICABLE_CANDIDATE!>super<!>("B")
constructor() : super("B")
}
4 changes: 2 additions & 2 deletions compiler/testData/diagnostics/tests/objects/Objects.fir.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ package toplevelObjectDeclarations
open fun foo() : Int = 1
}

class T : Foo {}
<!INAPPLICABLE_CANDIDATE!>class T : Foo {}<!>

object A : Foo {
<!INAPPLICABLE_CANDIDATE!>object A<!> : Foo {
val x : Int = 2

fun test() : Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ class B : A("") {
}

class C : A {
constructor() : <!INAPPLICABLE_CANDIDATE!>super<!>("")
constructor() : super("")
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ open class S(

class A : S {

constructor() : <!INAPPLICABLE_CANDIDATE!>super<!>(
constructor() : super(
foo(),
Nested(),
Inner(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class B2 : A() {}
class B3 : A("") {}

class B4 : A {
constructor() : <!INAPPLICABLE_CANDIDATE!>super<!>(1)
constructor() : super(1)
constructor(x: Int) : super()
constructor(x: Int, y: Int) : <!INAPPLICABLE_CANDIDATE!>super<!>("")
constructor(x: Int, y: Int) : super("")
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_PARAMETER
open class Outer {
inner open class A protected constructor(x: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class A : B {
val myProp: Int = 1
override val parentProp = 1

constructor(x: Int, y: Int = global): <!INAPPLICABLE_CANDIDATE!>super<!>(x + y + global) {
constructor(x: Int, y: Int = global): super(x + y + global) {
foo(x, y, myProp)
x + y + myProp + parentProp + super.parentProp
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ class A : B {
// no cycle, just call to super constuctor
constructor(x1: Double, x2: Double): this(x1, x2, 1.0)
constructor(x1: Double, x2: Double, x3: Double): this(x1, x2, x3, 1.0)
constructor(x1: Double, x2: Double, x3: Double, x4: Double): <!INAPPLICABLE_CANDIDATE!>super<!>(1.toByte())
constructor(x1: Double, x2: Double, x3: Double, x4: Double): super(1.toByte())
}

0 comments on commit 7f02d57

Please sign in to comment.