[FIR] Add diagnostic for constructor delegation cycles
This commit is contained in:
+1
-1
@@ -5,7 +5,7 @@ FILE: annotationClassMember.kt
|
||||
}
|
||||
|
||||
public constructor(s: R|kotlin/Nothing?|): R|A| {
|
||||
this<R|A|>()
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
init {
|
||||
|
||||
Vendored
+67
@@ -0,0 +1,67 @@
|
||||
class B
|
||||
class C
|
||||
|
||||
class A() {
|
||||
constructor(a: Int) : <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this<!>("test") {}
|
||||
constructor(a: String) : <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this<!>(10) {}
|
||||
|
||||
constructor(a: Boolean) : <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this<!>('\n') {}
|
||||
constructor(a: Char) : <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this<!>(0.0) {}
|
||||
constructor(a: Double) : <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this<!>(false) {}
|
||||
|
||||
constructor(b: B) : <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this<!>(3.14159265) {}
|
||||
|
||||
constructor(c: C) : this() {}
|
||||
constructor(a: List<Int>) : this(C()) {}
|
||||
}
|
||||
|
||||
class D {
|
||||
constructor(i: Boolean) {}
|
||||
constructor(i: Int) : <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this<!>(3) {}
|
||||
}
|
||||
|
||||
class E<T> {
|
||||
// this is not an error about the
|
||||
// selection of the proper constructor
|
||||
// but a type mismatch for the first
|
||||
// argument
|
||||
constructor(e: T, i: Int) : <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this<!>(i, 10) {}
|
||||
}
|
||||
|
||||
class I<T> {
|
||||
// this is not an error about the
|
||||
// selection of the proper constructor
|
||||
// but a type mismatch for the first
|
||||
// argument
|
||||
constructor(e: T, i: Int) : <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this<!>(i, 10)
|
||||
}
|
||||
|
||||
class J<T> {
|
||||
constructor(e: T, i: Int) : this(i, 10)
|
||||
constructor(e: Int, i: Int)
|
||||
}
|
||||
|
||||
class F(s: String) {
|
||||
constructor(i: Boolean) {}
|
||||
constructor(i: Int) : <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this<!>(3) {}
|
||||
}
|
||||
|
||||
class G(x: Int) {
|
||||
constructor() {}
|
||||
}
|
||||
|
||||
class H(x: Int) {
|
||||
constructor()
|
||||
}
|
||||
|
||||
class K(x: Int) {
|
||||
constructor() : <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this<!>() {}
|
||||
}
|
||||
|
||||
class M {
|
||||
constructor(m: Int)
|
||||
}
|
||||
|
||||
class U : M {
|
||||
<!INAPPLICABLE_CANDIDATE!>constructor()<!>
|
||||
}
|
||||
Vendored
+139
@@ -0,0 +1,139 @@
|
||||
FILE: cyclicConstructorDelegationCall.kt
|
||||
public final class B : R|kotlin/Any| {
|
||||
public constructor(): R|B| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
}
|
||||
public final class C : R|kotlin/Any| {
|
||||
public constructor(): R|C| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
}
|
||||
public final class A : R|kotlin/Any| {
|
||||
public constructor(): R|A| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
public constructor(a: R|kotlin/Int|): R|A| {
|
||||
this<R|A|>(String(test))
|
||||
}
|
||||
|
||||
public constructor(a: R|kotlin/String|): R|A| {
|
||||
this<R|A|>(Int(10))
|
||||
}
|
||||
|
||||
public constructor(a: R|kotlin/Boolean|): R|A| {
|
||||
this<R|A|>(Char(10))
|
||||
}
|
||||
|
||||
public constructor(a: R|kotlin/Char|): R|A| {
|
||||
this<R|A|>(Double(0.0))
|
||||
}
|
||||
|
||||
public constructor(a: R|kotlin/Double|): R|A| {
|
||||
this<R|A|>(Boolean(false))
|
||||
}
|
||||
|
||||
public constructor(b: R|B|): R|A| {
|
||||
this<R|A|>(Double(3.14159265))
|
||||
}
|
||||
|
||||
public constructor(c: R|C|): R|A| {
|
||||
this<R|A|>()
|
||||
}
|
||||
|
||||
public constructor(a: R|kotlin/collections/List<kotlin/Int>|): R|A| {
|
||||
this<R|A|>(R|/C.C|())
|
||||
}
|
||||
|
||||
}
|
||||
public final class D : R|kotlin/Any| {
|
||||
public constructor(i: R|kotlin/Boolean|): R|D| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
public constructor(i: R|kotlin/Int|): R|D| {
|
||||
this<R|D|>(Int(3))
|
||||
}
|
||||
|
||||
}
|
||||
public final class E<T> : R|kotlin/Any| {
|
||||
public constructor<T>(e: R|T|, i: R|kotlin/Int|): R|E<T>| {
|
||||
this<R|E<T>|>(R|<local>/i|, Int(10))
|
||||
}
|
||||
|
||||
}
|
||||
public final class I<T> : R|kotlin/Any| {
|
||||
public constructor<T>(e: R|T|, i: R|kotlin/Int|): R|I<T>| {
|
||||
this<R|I<T>|>(R|<local>/i|, Int(10))
|
||||
}
|
||||
|
||||
}
|
||||
public final class J<T> : R|kotlin/Any| {
|
||||
public constructor<T>(e: R|T|, i: R|kotlin/Int|): R|J<T>| {
|
||||
this<R|J<T>|>(R|<local>/i|, Int(10))
|
||||
}
|
||||
|
||||
public constructor<T>(e: R|kotlin/Int|, i: R|kotlin/Int|): R|J<T>| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
}
|
||||
public final class F : R|kotlin/Any| {
|
||||
public constructor(s: R|kotlin/String|): R|F| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
public constructor(i: R|kotlin/Boolean|): R|F| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
public constructor(i: R|kotlin/Int|): R|F| {
|
||||
this<R|F|>(Int(3))
|
||||
}
|
||||
|
||||
}
|
||||
public final class G : R|kotlin/Any| {
|
||||
public constructor(x: R|kotlin/Int|): R|G| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
public constructor(): R|G| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
}
|
||||
public final class H : R|kotlin/Any| {
|
||||
public constructor(x: R|kotlin/Int|): R|H| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
public constructor(): R|H| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
}
|
||||
public final class K : R|kotlin/Any| {
|
||||
public constructor(x: R|kotlin/Int|): R|K| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
public constructor(): R|K| {
|
||||
this<R|K|>()
|
||||
}
|
||||
|
||||
}
|
||||
public final class M : R|kotlin/Any| {
|
||||
public constructor(m: R|kotlin/Int|): R|M| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
}
|
||||
public final class U : R|M| {
|
||||
public constructor(): R|U| {
|
||||
super<R|M|>()
|
||||
}
|
||||
|
||||
}
|
||||
Generated
+5
@@ -916,6 +916,11 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/diagnostics/anonymousObjectByDelegate.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("cyclicConstructorDelegationCall.kt")
|
||||
public void testCyclicConstructorDelegationCall() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/diagnostics/cyclicConstructorDelegationCall.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("incompatibleModifiers.kt")
|
||||
public void testIncompatibleModifiers() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/diagnostics/incompatibleModifiers.kt");
|
||||
|
||||
+5
@@ -916,6 +916,11 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/diagnostics/anonymousObjectByDelegate.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("cyclicConstructorDelegationCall.kt")
|
||||
public void testCyclicConstructorDelegationCall() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/diagnostics/cyclicConstructorDelegationCall.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("incompatibleModifiers.kt")
|
||||
public void testIncompatibleModifiers() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/diagnostics/incompatibleModifiers.kt");
|
||||
|
||||
+1
@@ -20,6 +20,7 @@ object CommonDeclarationCheckers : DeclarationCheckers() {
|
||||
override val memberDeclarationCheckers: List<FirMemberDeclarationChecker> = listOf(
|
||||
FirInfixFunctionDeclarationChecker,
|
||||
FirExposedVisibilityDeclarationChecker,
|
||||
FirCyclicConstructorDelegationCallChecker,
|
||||
)
|
||||
|
||||
override val constructorCheckers: List<FirConstructorChecker> = listOf(
|
||||
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.fir.analysis.checkers.declaration
|
||||
|
||||
import org.jetbrains.kotlin.fir.FirSourceElement
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.DiagnosticReporter
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors
|
||||
import org.jetbrains.kotlin.fir.declarations.FirConstructor
|
||||
import org.jetbrains.kotlin.fir.declarations.FirMemberDeclaration
|
||||
import org.jetbrains.kotlin.fir.declarations.FirRegularClass
|
||||
import org.jetbrains.kotlin.fir.references.FirResolvedNamedReference
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
|
||||
|
||||
object FirCyclicConstructorDelegationCallChecker : FirMemberDeclarationChecker() {
|
||||
override fun check(declaration: FirMemberDeclaration, context: CheckerContext, reporter: DiagnosticReporter) {
|
||||
if (declaration !is FirRegularClass) {
|
||||
return
|
||||
}
|
||||
|
||||
val cyclicConstructors = mutableSetOf<FirConstructor>()
|
||||
|
||||
for (it in declaration.declarations) {
|
||||
if (it is FirConstructor && !it.isPrimary) {
|
||||
it.findCycle(cyclicConstructors)?.let { visited ->
|
||||
cyclicConstructors += visited
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cyclicConstructors.forEach {
|
||||
reporter.reportCyclicConstructorDelegationCall(it.delegatedConstructor?.source)
|
||||
}
|
||||
}
|
||||
|
||||
private fun FirConstructor.findCycle(knownCyclicConstructors: Set<FirConstructor> = emptySet()): Set<FirConstructor>? {
|
||||
val visitedConstructors = mutableSetOf(this)
|
||||
|
||||
var it = this
|
||||
var delegated = this.getDelegated()
|
||||
|
||||
while (!it.isPrimary && delegated != null) {
|
||||
if (delegated in visitedConstructors || delegated in knownCyclicConstructors) {
|
||||
return visitedConstructors
|
||||
}
|
||||
|
||||
it = delegated
|
||||
delegated = delegated.getDelegated()
|
||||
visitedConstructors.add(it)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
private fun FirConstructor.getDelegated(): FirConstructor? = delegatedConstructor
|
||||
?.calleeReference.safeAs<FirResolvedNamedReference>()
|
||||
?.resolvedSymbol
|
||||
?.fir.safeAs()
|
||||
|
||||
private fun DiagnosticReporter.reportCyclicConstructorDelegationCall(source: FirSourceElement?) {
|
||||
source?.let { report(FirErrors.CYCLIC_CONSTRUCTOR_DELEGATION_CALL.on(it)) }
|
||||
}
|
||||
}
|
||||
@@ -64,6 +64,8 @@ object FirErrors {
|
||||
val VAR_ANNOTATION_PARAMETER by existing<FirSourceElement, KtParameter>(Errors.VAR_ANNOTATION_PARAMETER)
|
||||
val NOT_AN_ANNOTATION_CLASS by error1<FirSourceElement, PsiElement, String>()
|
||||
|
||||
val CYCLIC_CONSTRUCTOR_DELEGATION_CALL by warning0<FirSourceElement, PsiElement>()
|
||||
|
||||
// Exposed visibility group
|
||||
val EXPOSED_TYPEALIAS_EXPANDED_TYPE by error3<FirSourceElement, PsiElement, FirEffectiveVisibility, FirMemberDeclaration, FirEffectiveVisibility>()
|
||||
val EXPOSED_FUNCTION_RETURN_TYPE by error3<FirSourceElement, PsiElement, FirEffectiveVisibility, FirMemberDeclaration, FirEffectiveVisibility>()
|
||||
|
||||
+1
-1
@@ -834,7 +834,7 @@ class DeclarationsConverter(
|
||||
}
|
||||
|
||||
val isImplicit = constructorDelegationCall.asText.isEmpty()
|
||||
val isThis = (isImplicit && classWrapper.hasPrimaryConstructor) || thisKeywordPresent
|
||||
val isThis = thisKeywordPresent //|| (isImplicit && classWrapper.hasPrimaryConstructor)
|
||||
val delegatedType =
|
||||
when {
|
||||
isThis -> classWrapper.delegatedSelfTypeRef
|
||||
|
||||
@@ -1095,7 +1095,7 @@ class RawFirBuilder(
|
||||
delegatedSelfTypeRef: FirTypeRef,
|
||||
hasPrimaryConstructor: Boolean,
|
||||
): FirDelegatedConstructorCall {
|
||||
val isThis = isCallToThis || (isImplicit && hasPrimaryConstructor)
|
||||
val isThis = isCallToThis //|| (isImplicit && hasPrimaryConstructor)
|
||||
val source = this.toFirSourceElement()
|
||||
val delegatedType = when {
|
||||
isThis -> delegatedSelfTypeRef
|
||||
|
||||
Reference in New Issue
Block a user