[FIR] Add diagnostic CONFLICTING_OVERLOADS & REDECLARATION
This commit is contained in:
Vendored
+4
-4
@@ -2,15 +2,15 @@
|
||||
@Target(AnnotationTarget.CLASS)
|
||||
annotation class Ann
|
||||
|
||||
var x: Int
|
||||
<!REDECLARATION!>var x: Int
|
||||
get() = 1
|
||||
set(@Ann private x) { }
|
||||
set(@Ann private x) { }<!>
|
||||
|
||||
|
||||
var x: String = ""
|
||||
<!REDECLARATION!>var x: String = ""
|
||||
set(param: <!REDUNDANT_SETTER_PARAMETER_TYPE!>String<!>) {
|
||||
field = "$param "
|
||||
}
|
||||
}<!>
|
||||
|
||||
class My {
|
||||
var y: Int = 1
|
||||
|
||||
Vendored
+4
-4
@@ -11,11 +11,11 @@ internal inline fun internal() {
|
||||
f()
|
||||
}
|
||||
|
||||
class C {
|
||||
<!REDECLARATION!>class C {
|
||||
internal val z = object {
|
||||
fun foo() = 13
|
||||
}
|
||||
}
|
||||
}<!>
|
||||
|
||||
class Foo2<
|
||||
T1,
|
||||
@@ -27,12 +27,12 @@ class Foo2<
|
||||
internal inner class B<T,T2,>
|
||||
}
|
||||
|
||||
<!REDUNDANT_VISIBILITY_MODIFIER!>public<!> class C {
|
||||
<!REDECLARATION!><!REDUNDANT_VISIBILITY_MODIFIER!>public<!> class C {
|
||||
<!REDUNDANT_VISIBILITY_MODIFIER!>public<!> val foo: Int = 0
|
||||
|
||||
<!REDUNDANT_VISIBILITY_MODIFIER!>public<!> fun bar() {}
|
||||
|
||||
}
|
||||
}<!>
|
||||
|
||||
open class D {
|
||||
protected open fun willRemainProtected() {
|
||||
|
||||
+3
-3
@@ -17,7 +17,7 @@ class LocalFreezableVar<T>(private var value: T) {
|
||||
}
|
||||
|
||||
|
||||
class C
|
||||
<!REDECLARATION!>class C<!>
|
||||
operator fun C.plus(a: Any): C = this
|
||||
operator fun C.plusAssign(a: Any) {}
|
||||
|
||||
@@ -172,14 +172,14 @@ fun notAssignedWhenNotUsed(p: Int) {
|
||||
|
||||
var global = 1
|
||||
|
||||
class C {
|
||||
<!REDECLARATION!>class C {
|
||||
var field = 2
|
||||
|
||||
fun foo() {
|
||||
print(field)
|
||||
print(global)
|
||||
}
|
||||
}
|
||||
}<!>
|
||||
|
||||
fun withDelegate() {
|
||||
var s: String by Delegates.notNull()
|
||||
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
<!CONFLICTING_OVERLOADS!>fun test(x: Int) {}<!>
|
||||
|
||||
<!CONFLICTING_OVERLOADS!>fun test(y: Int) {}<!>
|
||||
|
||||
fun test() {}
|
||||
|
||||
fun test(z: Int, c: Char) {}
|
||||
|
||||
<!REDECLARATION!>open class A {
|
||||
open fun rest(s: String) {}
|
||||
|
||||
open val u = 20
|
||||
}<!>
|
||||
|
||||
<!REDECLARATION!>class A {
|
||||
|
||||
}<!>
|
||||
|
||||
<!REDECLARATION!>class B : A {
|
||||
<!CONFLICTING_OVERLOADS!>override fun rest(s: String) {}<!>
|
||||
|
||||
<!CONFLICTING_OVERLOADS!>fun rest(s: String) {}<!>
|
||||
|
||||
fun rest(l: Long) {}
|
||||
|
||||
override val u = 310
|
||||
}<!>
|
||||
|
||||
<!REDECLARATION!>interface B<!>
|
||||
|
||||
<!REDECLARATION!>enum class B<!>
|
||||
|
||||
<!REDECLARATION!>val u = 10<!>
|
||||
<!REDECLARATION!>val u = 20<!>
|
||||
|
||||
<!REDECLARATION!>typealias TA = A<!>
|
||||
<!REDECLARATION!>typealias TA = B<!>
|
||||
|
||||
typealias BA = A
|
||||
|
||||
fun <T> kek(t: T) where T : (String) -> Any?, T : Char {}
|
||||
fun <T> kek(t: T) where T : () -> Boolean, T : String {}
|
||||
fun <T : Int> kek(t: T) {}
|
||||
|
||||
fun lol(a: Array<Int>) {}
|
||||
fun lol(a: Array<Boolean>) {}
|
||||
|
||||
<!CONFLICTING_OVERLOADS!>fun <T> mem(t: T) where T : () -> Boolean, T : String {}<!>
|
||||
<!CONFLICTING_OVERLOADS!>fun <T> mem(t: T) where T : String, T : () -> Boolean {}<!>
|
||||
|
||||
class M {
|
||||
companion <!REDECLARATION!>object<!> {}
|
||||
<!REDECLARATION!>val Companion = object : Any {}<!>
|
||||
}
|
||||
|
||||
fun B.foo() {}
|
||||
|
||||
class L {
|
||||
fun B.foo() {}
|
||||
}
|
||||
|
||||
fun mest()
|
||||
|
||||
class mest
|
||||
|
||||
fun() {}
|
||||
|
||||
private fun() {}
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
FILE: conflictingOverloads.kt
|
||||
public final fun test(x: R|kotlin/Int|): R|kotlin/Unit| {
|
||||
}
|
||||
public final fun test(y: R|kotlin/Int|): R|kotlin/Unit| {
|
||||
}
|
||||
public final fun test(): R|kotlin/Unit| {
|
||||
}
|
||||
public final fun test(z: R|kotlin/Int|, c: R|kotlin/Char|): R|kotlin/Unit| {
|
||||
}
|
||||
public open class A : R|kotlin/Any| {
|
||||
public constructor(): R|A| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
public open fun rest(s: R|kotlin/String|): R|kotlin/Unit| {
|
||||
}
|
||||
|
||||
public open val u: R|kotlin/Int| = Int(20)
|
||||
public get(): R|kotlin/Int|
|
||||
|
||||
}
|
||||
public final class A : R|kotlin/Any| {
|
||||
public constructor(): R|A| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
}
|
||||
public final class B : R|A| {
|
||||
public constructor(): R|B| {
|
||||
super<R|A|>()
|
||||
}
|
||||
|
||||
public final override fun rest(s: R|kotlin/String|): R|kotlin/Unit| {
|
||||
}
|
||||
|
||||
public final fun rest(s: R|kotlin/String|): R|kotlin/Unit| {
|
||||
}
|
||||
|
||||
public final fun rest(l: R|kotlin/Long|): R|kotlin/Unit| {
|
||||
}
|
||||
|
||||
public final override val u: R|kotlin/Int| = Int(310)
|
||||
public get(): R|kotlin/Int|
|
||||
|
||||
}
|
||||
public abstract interface B : R|kotlin/Any| {
|
||||
}
|
||||
public final enum class B : R|kotlin/Enum<B>| {
|
||||
private constructor(): R|B| {
|
||||
super<R|kotlin/Enum<B>|>()
|
||||
}
|
||||
|
||||
public final static fun values(): R|kotlin/Array<B>| {
|
||||
}
|
||||
|
||||
public final static fun valueOf(value: R|kotlin/String|): R|B| {
|
||||
}
|
||||
|
||||
}
|
||||
public final val u: R|kotlin/Int| = Int(10)
|
||||
public get(): R|kotlin/Int|
|
||||
public final val u: R|kotlin/Int| = Int(20)
|
||||
public get(): R|kotlin/Int|
|
||||
public final typealias TA = R|A|
|
||||
public final typealias TA = R|B|
|
||||
public final typealias BA = R|A|
|
||||
public final fun <T : R|(kotlin/String) -> kotlin/Any?|, R|kotlin/Char|> kek(t: R|T|): R|kotlin/Unit| {
|
||||
}
|
||||
public final fun <T : R|() -> kotlin/Boolean|, R|kotlin/String|> kek(t: R|T|): R|kotlin/Unit| {
|
||||
}
|
||||
public final fun <T : R|kotlin/Int|> kek(t: R|T|): R|kotlin/Unit| {
|
||||
}
|
||||
public final fun lol(a: R|kotlin/Array<kotlin/Int>|): R|kotlin/Unit| {
|
||||
}
|
||||
public final fun lol(a: R|kotlin/Array<kotlin/Boolean>|): R|kotlin/Unit| {
|
||||
}
|
||||
public final fun <T : R|() -> kotlin/Boolean|, R|kotlin/String|> mem(t: R|T|): R|kotlin/Unit| {
|
||||
}
|
||||
public final fun <T : R|kotlin/String|, R|() -> kotlin/Boolean|> mem(t: R|T|): R|kotlin/Unit| {
|
||||
}
|
||||
public final class M : R|kotlin/Any| {
|
||||
public constructor(): R|M| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
public final companion object Companion : R|kotlin/Any| {
|
||||
private constructor(): R|M.Companion| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final val Companion: R|<anonymous>| = object : R|kotlin/Any| {
|
||||
private constructor(): R|<anonymous>| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public get(): R|<anonymous>|
|
||||
|
||||
}
|
||||
public final fun R|B|.foo(): R|kotlin/Unit| {
|
||||
}
|
||||
public final class L : R|kotlin/Any| {
|
||||
public constructor(): R|L| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
public final fun R|B|.foo(): R|kotlin/Unit| {
|
||||
}
|
||||
|
||||
}
|
||||
public final fun mest(): R|kotlin/Unit|
|
||||
public final class mest : R|kotlin/Any| {
|
||||
public constructor(): R|mest| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
}
|
||||
public final fun <no name provided>(): R|kotlin/Unit| {
|
||||
}
|
||||
private final fun <no name provided>(): R|kotlin/Unit| {
|
||||
}
|
||||
+2
-2
@@ -1,9 +1,9 @@
|
||||
class A {
|
||||
companion object {
|
||||
companion <!REDECLARATION!>object<!> {
|
||||
|
||||
}
|
||||
|
||||
companion <!MANY_COMPANION_OBJECTS!>object<!> {
|
||||
companion <!MANY_COMPANION_OBJECTS, REDECLARATION!>object<!> {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
object A
|
||||
<!REDECLARATION!>object A<!>
|
||||
|
||||
val A = 10
|
||||
<!REDECLARATION!>val A = 10<!>
|
||||
|
||||
|
||||
fun foo() = A
|
||||
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
fun takeString(s: String) {}
|
||||
<!CONFLICTING_OVERLOADS!>fun takeString(s: String) {}<!>
|
||||
|
||||
class Wrapper(val s: String?) {
|
||||
fun withThis() {
|
||||
@@ -11,4 +11,4 @@ class Wrapper(val s: String?) {
|
||||
}
|
||||
}
|
||||
|
||||
fun takeString(s: String) {}
|
||||
<!CONFLICTING_OVERLOADS!>fun takeString(s: String) {}<!>
|
||||
+4
-4
@@ -23,11 +23,11 @@ fun test_2(a: A?) {
|
||||
}
|
||||
}
|
||||
|
||||
fun test_3(x: Any?) {
|
||||
<!CONFLICTING_OVERLOADS!>fun test_3(x: Any?) {
|
||||
val a = x as? A ?: return
|
||||
a.foo() // Should be OK
|
||||
x.foo() // Should be OK
|
||||
}
|
||||
}<!>
|
||||
|
||||
// ----------------- Unstable -----------------
|
||||
|
||||
@@ -53,8 +53,8 @@ fun test_2(a: B?) {
|
||||
}
|
||||
}
|
||||
|
||||
fun test_3(x: Any?) {
|
||||
<!CONFLICTING_OVERLOADS!>fun test_3(x: Any?) {
|
||||
val a = x as? B ?: return
|
||||
a.foo() // Should be OK
|
||||
x.foo() // Should be OK
|
||||
}
|
||||
}<!>
|
||||
@@ -13,7 +13,7 @@ class B()
|
||||
class C(val b : B)
|
||||
fun get(f: Boolean) = if (f) {A.A1} else {""}
|
||||
|
||||
fun case2() {
|
||||
<!CONFLICTING_OVERLOADS!>fun case2() {
|
||||
|
||||
val flag: Any = get(false) //string
|
||||
val l1 = when (flag!!) { // should be NO_ELSE_IN_WHEN
|
||||
@@ -25,9 +25,9 @@ fun case2() {
|
||||
A.A1 -> B()
|
||||
A.A2 -> B()
|
||||
}
|
||||
}
|
||||
}<!>
|
||||
|
||||
fun case2() {
|
||||
<!CONFLICTING_OVERLOADS!>fun case2() {
|
||||
|
||||
val flag: Any = get(true) //A
|
||||
val l1 = when (flag!!) {// should be NO_ELSE_IN_WHEN
|
||||
@@ -39,7 +39,7 @@ fun case2() {
|
||||
A.A1 -> B()
|
||||
A.A2 -> B()
|
||||
}
|
||||
}
|
||||
}<!>
|
||||
|
||||
fun case3() {
|
||||
|
||||
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
fun bar(x: String): Int = 1
|
||||
fun bar(x: String): Double = 1
|
||||
<!CONFLICTING_OVERLOADS!>fun bar(x: String): Int = 1<!>
|
||||
<!CONFLICTING_OVERLOADS!>fun bar(x: String): Double = 1<!>
|
||||
|
||||
fun baz(x: String): Int = 1
|
||||
fun <T, R> foobaz(x: T): R = TODO()
|
||||
|
||||
+3
-3
@@ -1,8 +1,8 @@
|
||||
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
|
||||
<!CONFLICTING_OVERLOADS!>@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
|
||||
<!HIDDEN!>@kotlin.internal.LowPriorityInOverloadResolution<!>
|
||||
fun foo(): Int = 1
|
||||
fun foo(): Int = 1<!>
|
||||
|
||||
fun foo(): String = ""
|
||||
<!CONFLICTING_OVERLOADS!>fun foo(): String = ""<!>
|
||||
|
||||
fun test() {
|
||||
val s = foo()
|
||||
|
||||
@@ -13,7 +13,7 @@ fun test() {
|
||||
val Any.bar get() = "456"
|
||||
val String.bar get() = "987"
|
||||
|
||||
val t = "".bar
|
||||
<!REDECLARATION!>val t = "".bar<!>
|
||||
|
||||
val p = Pair(0, "")
|
||||
|
||||
@@ -21,4 +21,4 @@ class Base<T>(val x: T)
|
||||
class Derived : Base<Int>(10)
|
||||
val xx = Derived().x + 1
|
||||
|
||||
val t = throw AssertionError("")
|
||||
<!REDECLARATION!>val t = throw AssertionError("")<!>
|
||||
Generated
+5
@@ -916,6 +916,11 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/diagnostics/anonymousObjectByDelegate.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("conflictingOverloads.kt")
|
||||
public void testConflictingOverloads() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/diagnostics/conflictingOverloads.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("cyclicConstructorDelegationCall.kt")
|
||||
public void testCyclicConstructorDelegationCall() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/diagnostics/cyclicConstructorDelegationCall.kt");
|
||||
|
||||
+5
@@ -916,6 +916,11 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/diagnostics/anonymousObjectByDelegate.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("conflictingOverloads.kt")
|
||||
public void testConflictingOverloads() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/diagnostics/conflictingOverloads.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("cyclicConstructorDelegationCall.kt")
|
||||
public void testCyclicConstructorDelegationCall() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/diagnostics/cyclicConstructorDelegationCall.kt");
|
||||
|
||||
@@ -53,25 +53,31 @@ fun FirSession.registerCheckersComponent() {
|
||||
}
|
||||
|
||||
private class ComposedDeclarationCheckers : DeclarationCheckers() {
|
||||
override val fileCheckers: List<FirFileChecker>
|
||||
get() = _fileCheckers
|
||||
override val declarationCheckers: List<FirBasicDeclarationChecker>
|
||||
get() = _declarationCheckers
|
||||
|
||||
override val memberDeclarationCheckers: List<FirMemberDeclarationChecker>
|
||||
get() = _memberDeclarationCheckers
|
||||
|
||||
override val regularClassCheckers: List<FirRegularClassChecker>
|
||||
get() = _regularClassCheckers
|
||||
override val constructorCheckers: List<FirConstructorChecker>
|
||||
get() = _constructorCheckers
|
||||
override val controlFlowAnalyserCheckers: List<FirControlFlowChecker>
|
||||
get() = _controlFlowAnalyserCheckers
|
||||
|
||||
private val _fileCheckers: MutableList<FirFileChecker> = mutableListOf()
|
||||
private val _declarationCheckers: MutableList<FirBasicDeclarationChecker> = mutableListOf()
|
||||
private val _memberDeclarationCheckers: MutableList<FirMemberDeclarationChecker> = mutableListOf()
|
||||
private val _regularClassCheckers: MutableList<FirRegularClassChecker> = mutableListOf()
|
||||
private val _constructorCheckers: MutableList<FirConstructorChecker> = mutableListOf()
|
||||
private val _controlFlowAnalyserCheckers: MutableList<FirControlFlowChecker> = mutableListOf()
|
||||
|
||||
fun register(checkers: DeclarationCheckers) {
|
||||
_fileCheckers += checkers.allFileCheckers
|
||||
_declarationCheckers += checkers.declarationCheckers
|
||||
_memberDeclarationCheckers += checkers.allMemberDeclarationCheckers
|
||||
_regularClassCheckers += checkers.allRegularClassCheckers
|
||||
_constructorCheckers += checkers.allConstructorCheckers
|
||||
_controlFlowAnalyserCheckers += checkers.controlFlowAnalyserCheckers
|
||||
}
|
||||
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
import org.jetbrains.kotlin.fir.FirFakeSourceElementKind
|
||||
import org.jetbrains.kotlin.fir.declarations.*
|
||||
import org.jetbrains.kotlin.fir.symbols.CallableId
|
||||
import org.jetbrains.kotlin.fir.types.FirErrorTypeRef
|
||||
import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef
|
||||
import org.jetbrains.kotlin.fir.types.FirTypeRef
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
|
||||
private open class RepresentationBuilder {
|
||||
var receiver = ""
|
||||
var name = ""
|
||||
|
||||
open fun build() = "[$receiver] $name"
|
||||
}
|
||||
|
||||
private fun buildRepresentation(init: RepresentationBuilder.() -> Unit): String {
|
||||
return RepresentationBuilder().apply(init).build()
|
||||
}
|
||||
|
||||
private class FunctionRepresentationBuilder : RepresentationBuilder() {
|
||||
var typeArguments = ""
|
||||
var parameters = ""
|
||||
|
||||
override fun build() = "<$typeArguments> [$receiver] $name ($parameters)"
|
||||
}
|
||||
|
||||
private fun buildFunctionRepresentation(init: FunctionRepresentationBuilder.() -> Unit): String {
|
||||
return FunctionRepresentationBuilder().apply(init).build()
|
||||
}
|
||||
|
||||
private fun ClassId.represent() = packageFqName.asString() + '/' + relativeClassName.asString()
|
||||
|
||||
private fun CallableId.represent() = if (className != null) {
|
||||
packageName.asString() + '/' + className + '.' + callableName
|
||||
} else {
|
||||
packageName.asString() + '/' + callableName
|
||||
}
|
||||
|
||||
private fun FirTypeRef.represent() = when (this) {
|
||||
is FirResolvedTypeRef -> type.toString()
|
||||
is FirErrorTypeRef -> "ERROR"
|
||||
else -> "?"
|
||||
}
|
||||
|
||||
private fun FirTypeParameter.represent() = name.asString() + " : " + bounds
|
||||
.map { it.represent() }
|
||||
.sorted()
|
||||
.joinToString()
|
||||
|
||||
private fun FirValueParameter.represent(): String {
|
||||
val prefix = if (this.isVararg) "vararg " else ""
|
||||
return prefix + " " + this.returnTypeRef.represent()
|
||||
}
|
||||
|
||||
private fun FirProperty.represent() = buildRepresentation {
|
||||
receiver = receiverTypeRef?.represent() ?: ""
|
||||
name = symbol.callableId.represent()
|
||||
}
|
||||
|
||||
private fun FirSimpleFunction.represent() = buildFunctionRepresentation {
|
||||
typeArguments = typeParameters.joinToString { it.represent() }
|
||||
receiver = receiverTypeRef?.represent() ?: ""
|
||||
name = symbol.callableId.represent()
|
||||
parameters = valueParameters.joinToString { it.represent() }
|
||||
}
|
||||
|
||||
private fun FirTypeAlias.represent() = buildRepresentation {
|
||||
name = symbol.classId.represent()
|
||||
}
|
||||
|
||||
private fun FirRegularClass.represent() = buildRepresentation {
|
||||
name = symbol.classId.represent()
|
||||
}
|
||||
|
||||
private val NO_NAME_PROVIDED = Name.special("<no name provided>")
|
||||
|
||||
// - see testEnumValuesValueOf.
|
||||
// it generates a static function that has
|
||||
// the same signature as the function defined
|
||||
// explicitly.
|
||||
// - see tests with `fun () {}`.
|
||||
// you can't redeclare something that has no name.
|
||||
private fun FirDeclaration.isCollectable() = when (this) {
|
||||
is FirSimpleFunction -> source?.kind !is FirFakeSourceElementKind && name != NO_NAME_PROVIDED
|
||||
is FirRegularClass -> name != NO_NAME_PROVIDED
|
||||
else -> true
|
||||
}
|
||||
|
||||
class FirDeclarationInspector {
|
||||
val otherDeclarations = mutableMapOf<String, MutableList<FirDeclaration>>()
|
||||
val functionDeclarations = mutableMapOf<String, MutableList<FirSimpleFunction>>()
|
||||
|
||||
fun collect(declaration: FirDeclaration) {
|
||||
if (!declaration.isCollectable()) {
|
||||
return
|
||||
}
|
||||
|
||||
if (declaration is FirSimpleFunction) {
|
||||
return collectFunction(declaration)
|
||||
}
|
||||
|
||||
val key = when (declaration) {
|
||||
is FirRegularClass -> declaration.represent()
|
||||
is FirTypeAlias -> declaration.represent()
|
||||
is FirProperty -> declaration.represent()
|
||||
else -> return
|
||||
}
|
||||
|
||||
var value = otherDeclarations[key]
|
||||
|
||||
if (value == null) {
|
||||
value = mutableListOf()
|
||||
otherDeclarations[key] = value
|
||||
}
|
||||
|
||||
value.add(declaration)
|
||||
}
|
||||
|
||||
private fun collectFunction(declaration: FirSimpleFunction) {
|
||||
val key = declaration.represent()
|
||||
var value = functionDeclarations[key]
|
||||
|
||||
if (value == null) {
|
||||
value = mutableListOf()
|
||||
functionDeclarations[key] = value
|
||||
}
|
||||
|
||||
value.add(declaration)
|
||||
}
|
||||
|
||||
private fun contains(declaration: FirDeclaration) = when (declaration) {
|
||||
is FirSimpleFunction -> declaration.represent() in functionDeclarations
|
||||
is FirRegularClass -> declaration.represent() in otherDeclarations
|
||||
is FirTypeAlias -> declaration.represent() in otherDeclarations
|
||||
is FirProperty -> declaration.represent() in otherDeclarations
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
+4
@@ -12,11 +12,15 @@ abstract class DeclarationCheckers {
|
||||
val EMPTY: DeclarationCheckers = object : DeclarationCheckers() {}
|
||||
}
|
||||
|
||||
open val fileCheckers: List<FirFileChecker> = emptyList()
|
||||
open val declarationCheckers: List<FirBasicDeclarationChecker> = emptyList()
|
||||
open val memberDeclarationCheckers: List<FirMemberDeclarationChecker> = emptyList()
|
||||
open val regularClassCheckers: List<FirRegularClassChecker> = emptyList()
|
||||
open val constructorCheckers: List<FirConstructorChecker> = emptyList()
|
||||
open val controlFlowAnalyserCheckers: List<FirControlFlowChecker> = emptyList()
|
||||
|
||||
internal val allFileCheckers: List<FirFileChecker> get() = fileCheckers + declarationCheckers
|
||||
internal val allMemberDeclarationCheckers: List<FirMemberDeclarationChecker> get() = memberDeclarationCheckers + declarationCheckers
|
||||
internal val allRegularClassCheckers: List<FirRegularClassChecker> get() = regularClassCheckers + allMemberDeclarationCheckers
|
||||
internal val allConstructorCheckers: List<FirConstructorChecker> get() = constructorCheckers + allMemberDeclarationCheckers
|
||||
}
|
||||
|
||||
+9
@@ -9,12 +9,17 @@ import org.jetbrains.kotlin.fir.analysis.cfa.FirPropertyInitializationAnalyzer
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.cfa.FirControlFlowChecker
|
||||
|
||||
object CommonDeclarationCheckers : DeclarationCheckers() {
|
||||
override val fileCheckers: List<FirFileChecker> = listOf(
|
||||
|
||||
)
|
||||
|
||||
override val declarationCheckers: List<FirBasicDeclarationChecker> = listOf(
|
||||
FirAnnotationClassDeclarationChecker,
|
||||
FirModifierChecker,
|
||||
FirManyCompanionObjectsChecker,
|
||||
FirLocalEntityNotAllowedChecker,
|
||||
FirTypeParametersInObjectChecker,
|
||||
FirConflictsChecker,
|
||||
)
|
||||
|
||||
override val memberDeclarationCheckers: List<FirMemberDeclarationChecker> = listOf(
|
||||
@@ -26,6 +31,10 @@ object CommonDeclarationCheckers : DeclarationCheckers() {
|
||||
FirPrimaryConstructorRequiredForDataClassChecker,
|
||||
)
|
||||
|
||||
override val regularClassCheckers: List<FirRegularClassChecker> = listOf(
|
||||
|
||||
)
|
||||
|
||||
override val constructorCheckers: List<FirConstructorChecker> = listOf(
|
||||
FirConstructorAllowedChecker,
|
||||
)
|
||||
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* 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.FirDeclarationInspector
|
||||
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.FirDeclaration
|
||||
import org.jetbrains.kotlin.fir.declarations.FirFile
|
||||
import org.jetbrains.kotlin.fir.declarations.FirRegularClass
|
||||
|
||||
object FirConflictsChecker : FirBasicDeclarationChecker() {
|
||||
override fun check(declaration: FirDeclaration, context: CheckerContext, reporter: DiagnosticReporter) {
|
||||
val inspector = FirDeclarationInspector()
|
||||
|
||||
when (declaration) {
|
||||
is FirFile -> checkFile(declaration, inspector)
|
||||
is FirRegularClass -> checkRegularClass(declaration, inspector)
|
||||
else -> return
|
||||
}
|
||||
|
||||
inspector.functionDeclarations.forEachNonSingle { it, hint ->
|
||||
reporter.reportConflictingOverloads(it.source, hint)
|
||||
}
|
||||
|
||||
inspector.otherDeclarations.forEachNonSingle { it, hint ->
|
||||
reporter.reportConflictingDeclarations(it.source, hint)
|
||||
}
|
||||
}
|
||||
|
||||
private fun Map<String, List<FirDeclaration>>.forEachNonSingle(action: (FirDeclaration, String) -> Unit) {
|
||||
for (value in values) {
|
||||
if (value.size > 1) {
|
||||
val hint = value.joinToString { that -> that.toString() }
|
||||
|
||||
value.forEach {
|
||||
action(it, hint)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkFile(declaration: FirFile, inspector: FirDeclarationInspector) {
|
||||
for (it in declaration.declarations) {
|
||||
inspector.collect(it)
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkRegularClass(declaration: FirRegularClass, inspector: FirDeclarationInspector) {
|
||||
for (it in declaration.declarations) {
|
||||
inspector.collect(it)
|
||||
}
|
||||
}
|
||||
|
||||
private fun DiagnosticReporter.reportConflictingOverloads(source: FirSourceElement?, declarations: String) {
|
||||
source?.let { report(FirErrors.CONFLICTING_OVERLOADS.on(it, declarations)) }
|
||||
}
|
||||
|
||||
private fun DiagnosticReporter.reportConflictingDeclarations(source: FirSourceElement?, declarations: String) {
|
||||
source?.let { report(FirErrors.REDECLARATION.on(it, declarations)) }
|
||||
}
|
||||
}
|
||||
+3
-4
@@ -7,15 +7,14 @@ package org.jetbrains.kotlin.fir.analysis.checkers.declaration
|
||||
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.DiagnosticReporter
|
||||
import org.jetbrains.kotlin.fir.declarations.FirConstructor
|
||||
import org.jetbrains.kotlin.fir.declarations.FirDeclaration
|
||||
import org.jetbrains.kotlin.fir.declarations.FirMemberDeclaration
|
||||
import org.jetbrains.kotlin.fir.declarations.*
|
||||
|
||||
abstract class FirDeclarationChecker<in D : FirDeclaration> {
|
||||
abstract fun check(declaration: D, context: CheckerContext, reporter: DiagnosticReporter)
|
||||
|
||||
}
|
||||
|
||||
typealias FirFileChecker = FirDeclarationChecker<FirFile>
|
||||
typealias FirBasicDeclarationChecker = FirDeclarationChecker<FirDeclaration>
|
||||
typealias FirMemberDeclarationChecker = FirDeclarationChecker<FirMemberDeclaration>
|
||||
typealias FirRegularClassChecker = FirDeclarationChecker<FirRegularClass>
|
||||
typealias FirConstructorChecker = FirDeclarationChecker<FirConstructor>
|
||||
|
||||
+3
-4
@@ -14,10 +14,7 @@ import org.jetbrains.kotlin.fir.analysis.checkers.getModifierList
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.source
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.DiagnosticReporter
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors
|
||||
import org.jetbrains.kotlin.fir.declarations.FirClass
|
||||
import org.jetbrains.kotlin.fir.declarations.FirDeclaration
|
||||
import org.jetbrains.kotlin.fir.declarations.FirProperty
|
||||
import org.jetbrains.kotlin.fir.declarations.FirValueParameter
|
||||
import org.jetbrains.kotlin.fir.declarations.*
|
||||
import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertyAccessor
|
||||
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
|
||||
import org.jetbrains.kotlin.lexer.KtTokens.*
|
||||
@@ -167,6 +164,8 @@ object FirModifierChecker : FirBasicDeclarationChecker() {
|
||||
}
|
||||
|
||||
override fun check(declaration: FirDeclaration, context: CheckerContext, reporter: DiagnosticReporter) {
|
||||
if (declaration is FirFile) return
|
||||
|
||||
val source = declaration.source ?: return
|
||||
if (!isDeclarationMappedToSourceCorrectly(declaration, source)) return
|
||||
if (context.containingDeclarations.last() is FirDefaultPropertyAccessor) return
|
||||
|
||||
+5
-1
@@ -17,12 +17,16 @@ class DeclarationCheckersDiagnosticComponent(
|
||||
) : AbstractDiagnosticCollectorComponent(collector) {
|
||||
private val checkers = session.checkersComponent.declarationCheckers
|
||||
|
||||
override fun visitFile(file: FirFile, data: CheckerContext) {
|
||||
runCheck { checkers.fileCheckers.check(file, data, it) }
|
||||
}
|
||||
|
||||
override fun visitProperty(property: FirProperty, data: CheckerContext) {
|
||||
runCheck { checkers.memberDeclarationCheckers.check(property, data, it) }
|
||||
}
|
||||
|
||||
override fun visitRegularClass(regularClass: FirRegularClass, data: CheckerContext) {
|
||||
runCheck { checkers.memberDeclarationCheckers.check(regularClass, data, it) }
|
||||
runCheck { checkers.regularClassCheckers.check(regularClass, data, it) }
|
||||
}
|
||||
|
||||
override fun visitSimpleFunction(simpleFunction: FirSimpleFunction, data: CheckerContext) {
|
||||
|
||||
+4
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.ANNOTATION_CLASS_
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.ANNOTATION_PARAMETER_DEFAULT_VALUE_MUST_BE_CONSTANT
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.ASSIGN_OPERATOR_AMBIGUITY
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.BREAK_OR_CONTINUE_OUTSIDE_A_LOOP
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.CONFLICTING_OVERLOADS
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.CONSTRUCTOR_IN_INTERFACE
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.CONSTRUCTOR_IN_OBJECT
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.CYCLIC_CONSTRUCTOR_DELEGATION_CALL
|
||||
@@ -65,6 +66,7 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.PROJECTION_ON_NON
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.QUALIFIED_SUPERTYPE_EXTENDED_BY_OTHER_SUPERTYPE
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.RECURSION_IN_IMPLICIT_TYPES
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.RECURSION_IN_SUPERTYPES
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.REDECLARATION
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.REDUNDANT_MODIFIER
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.REPEATED_MODIFIER
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.RETURN_NOT_ALLOWED
|
||||
@@ -193,6 +195,8 @@ class FirDefaultErrorMessages : DefaultErrorMessages.Extension {
|
||||
|
||||
// Redeclarations
|
||||
map.put(MANY_COMPANION_OBJECTS, "Only one companion object is allowed per class")
|
||||
map.put(CONFLICTING_OVERLOADS, "Conflicting overloads: {0}", TO_STRING) // *
|
||||
map.put(REDECLARATION, "Conflicting declarations: {0}", TO_STRING) // *
|
||||
|
||||
// Invalid local declarations
|
||||
map.put(LOCAL_OBJECT_NOT_ALLOWED, "Named object ''{0}'' is a singleton and cannot be local. Try to use anonymous object instead", TO_STRING) // +
|
||||
|
||||
@@ -118,6 +118,8 @@ object FirErrors {
|
||||
|
||||
// Redeclarations
|
||||
val MANY_COMPANION_OBJECTS by error0<FirSourceElement, PsiElement>()
|
||||
val CONFLICTING_OVERLOADS by error1<FirSourceElement, PsiElement, String>()
|
||||
val REDECLARATION by error1<FirSourceElement, PsiElement, String>()
|
||||
|
||||
// Invalid local declarations
|
||||
val LOCAL_OBJECT_NOT_ALLOWED by error1<FirSourceElement, PsiElement, Name>()
|
||||
|
||||
@@ -10,7 +10,7 @@ import org.jetbrains.kotlin.fir.symbols.StandardClassIds
|
||||
fun ConeTypeProjection.createArrayOf(nullable: Boolean = false): ConeKotlinType {
|
||||
if (this is ConeKotlinTypeProjection) {
|
||||
val type = type.lowerBoundIfFlexible()
|
||||
if (type is ConeClassLikeType) {
|
||||
if (type is ConeClassLikeType && type.nullability != ConeNullability.NULLABLE) {
|
||||
val classId = type.lookupTag.classId
|
||||
val primitiveArrayId =
|
||||
StandardClassIds.primitiveArrayTypeByElementType[classId] ?: StandardClassIds.unsignedArrayTypeByElementType[classId]
|
||||
|
||||
Reference in New Issue
Block a user