From bcf6202863f3b13426de0ab14a54534527d2a121 Mon Sep 17 00:00:00 2001 From: Tianyu Geng Date: Fri, 25 Jun 2021 13:16:58 -0700 Subject: [PATCH] FIR checker: fix position strategy for UNRESOLVED_LABEL --- .../expresssions/invoke/farInvokeExtension.kt | 2 +- .../generator/diagnostics/FirDiagnosticsList.kt | 2 +- .../diagnostics/model/RegularDiagnosticData.kt | 2 +- .../kotlin/fir/analysis/diagnostics/FirErrors.kt | 2 +- .../diagnostics/LightTreePositioningStrategies.kt | 12 ++++++++++++ .../SourceElementPositioningStrategies.kt | 5 +++++ .../kotlin/diagnostics/PositioningStrategies.kt | 6 ++++++ .../diagnostics/tests/inner/innerThisSuper.fir.kt | 4 ++-- .../inner/nestedVsInnerAccessOuterMember.fir.kt | 4 ++-- .../tests/labels/labeledFunctionLiteral.fir.kt | 8 ++++---- .../regressions/UnavaliableQualifiedThis.fir.kt | 6 +++--- ...cessBaseGenericFromInnerExtendingSameBase2.fir.kt | 2 +- .../accessBaseWithSameExtension.fir.kt | 2 +- .../accessGenericBaseWithSameExtension.fir.kt | 2 +- .../headerCallChecker/lambdaAsArgument.fir.kt | 2 +- .../headerCallChecker/objectLiteralAsArgument.fir.kt | 2 +- .../objectLiteralAsDefaultValueParameter.fir.kt | 2 +- .../headerCallChecker/passingInstance.fir.kt | 4 ++-- .../headerCallChecker/thisAsExtensionReceiver.fir.kt | 2 +- .../tests/thisAndSuper/QualifiedThis.fir.kt | 4 ++-- .../tests/thisAndSuper/thisInFunctionLiterals.fir.kt | 4 ++-- .../thisAndSuper/thisInPropertyInitializer.fir.kt | 4 ++-- .../tests/thisAndSuper/thisInToplevelFunction.fir.kt | 4 ++-- .../contracts/dsl/errors/accessToOuterThis.fir.kt | 6 +++--- 24 files changed, 58 insertions(+), 35 deletions(-) diff --git a/compiler/fir/analysis-tests/testData/resolve/expresssions/invoke/farInvokeExtension.kt b/compiler/fir/analysis-tests/testData/resolve/expresssions/invoke/farInvokeExtension.kt index 928cbaf7235..ef21b0b6b13 100644 --- a/compiler/fir/analysis-tests/testData/resolve/expresssions/invoke/farInvokeExtension.kt +++ b/compiler/fir/analysis-tests/testData/resolve/expresssions/invoke/farInvokeExtension.kt @@ -2,7 +2,7 @@ fun x() {} -operator fun Int.invoke(): Foo = this@Foo +operator fun Int.invoke(): Foo = this@Foo class Foo { diff --git a/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/FirDiagnosticsList.kt b/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/FirDiagnosticsList.kt index 0d17759b2b1..09d4c9904f6 100644 --- a/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/FirDiagnosticsList.kt +++ b/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/FirDiagnosticsList.kt @@ -96,7 +96,7 @@ object DIAGNOSTICS_LIST : DiagnosticList("FirErrors") { val UNRESOLVED_REFERENCE by error(PositioningStrategy.REFERENCED_NAME_BY_QUALIFIED) { parameter("reference") } - val UNRESOLVED_LABEL by error() + val UNRESOLVED_LABEL by error(PositioningStrategy.LABEL) val DESERIALIZATION_ERROR by error() val ERROR_FROM_JAVA_RESOLUTION by error() val MISSING_STDLIB_CLASS by error() diff --git a/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/model/RegularDiagnosticData.kt b/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/model/RegularDiagnosticData.kt index 4bf3c0bc4fd..645f64e252d 100644 --- a/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/model/RegularDiagnosticData.kt +++ b/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/model/RegularDiagnosticData.kt @@ -101,7 +101,7 @@ enum class PositioningStrategy(private val strategy: String? = null) { UNREACHABLE_CODE, INLINE_PARAMETER_MODIFIER, ABSTRACT_MODIFIER, - + LABEL, ; val expressionToCreate get() = "SourceElementPositioningStrategies.${strategy ?: name}" diff --git a/compiler/fir/checkers/gen/org/jetbrains/kotlin/fir/analysis/diagnostics/FirErrors.kt b/compiler/fir/checkers/gen/org/jetbrains/kotlin/fir/analysis/diagnostics/FirErrors.kt index 08e0790e39a..573fc0eb599 100644 --- a/compiler/fir/checkers/gen/org/jetbrains/kotlin/fir/analysis/diagnostics/FirErrors.kt +++ b/compiler/fir/checkers/gen/org/jetbrains/kotlin/fir/analysis/diagnostics/FirErrors.kt @@ -127,7 +127,7 @@ object FirErrors { // Unresolved val INVISIBLE_REFERENCE by error1>(SourceElementPositioningStrategies.REFERENCE_BY_QUALIFIED) val UNRESOLVED_REFERENCE by error1(SourceElementPositioningStrategies.REFERENCED_NAME_BY_QUALIFIED) - val UNRESOLVED_LABEL by error0() + val UNRESOLVED_LABEL by error0(SourceElementPositioningStrategies.LABEL) val DESERIALIZATION_ERROR by error0() val ERROR_FROM_JAVA_RESOLUTION by error0() val MISSING_STDLIB_CLASS by error0() diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/LightTreePositioningStrategies.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/LightTreePositioningStrategies.kt index 88970b4b6db..15b7a3ccbac 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/LightTreePositioningStrategies.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/LightTreePositioningStrategies.kt @@ -898,6 +898,18 @@ object LightTreePositioningStrategies { return markElement(nodeToMark ?: node, startOffset, endOffset, tree, node) } } + + val LABEL: LightTreePositioningStrategy = object : LightTreePositioningStrategy() { + override fun mark( + node: LighterASTNode, + startOffset: Int, + endOffset: Int, + tree: FlyweightCapableTreeStructure + ): List { + val nodeToMark = tree.findChildByType(node, KtNodeTypes.LABEL_QUALIFIER) ?: node + return markElement(nodeToMark, startOffset, endOffset, tree, node) + } + } } fun FirSourceElement.hasValOrVar(): Boolean = diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/SourceElementPositioningStrategies.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/SourceElementPositioningStrategies.kt index 3a25bc1b7cc..53118620a67 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/SourceElementPositioningStrategies.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/SourceElementPositioningStrategies.kt @@ -283,6 +283,11 @@ object SourceElementPositioningStrategies { PositioningStrategies.ACTUAL_DECLARATION_NAME ) + val LABEL = SourceElementPositioningStrategy( + LightTreePositioningStrategies.LABEL, + PositioningStrategies.LABEL + ) + // TODO val INCOMPATIBLE_DECLARATION = DEFAULT diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/PositioningStrategies.kt b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/PositioningStrategies.kt index ef0ca0fe38a..20f36e102e3 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/PositioningStrategies.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/PositioningStrategies.kt @@ -940,6 +940,12 @@ object PositioningStrategies { } } + val LABEL: PositioningStrategy = object : PositioningStrategy() { + override fun mark(element: KtElement): List { + return super.mark((element as? KtExpressionWithLabel)?.labelQualifier ?: element) + } + } + /** * @param locateReferencedName whether to remove any nested parentheses while locating the reference element. This is useful for * diagnostics on super and unresolved references. For example, with the following, only the part inside the parentheses should be diff --git a/compiler/testData/diagnostics/tests/inner/innerThisSuper.fir.kt b/compiler/testData/diagnostics/tests/inner/innerThisSuper.fir.kt index b68718a42e9..b9b5193177f 100644 --- a/compiler/testData/diagnostics/tests/inner/innerThisSuper.fir.kt +++ b/compiler/testData/diagnostics/tests/inner/innerThisSuper.fir.kt @@ -6,11 +6,11 @@ interface Trait { class Outer : Trait { class Nested { - val t = this@Outer.bar() + val t = this@Outer.bar() val s = super@Outer.bar() inner class NestedInner { - val t = this@Outer.bar() + val t = this@Outer.bar() val s = super@Outer.bar() } } diff --git a/compiler/testData/diagnostics/tests/inner/nestedVsInnerAccessOuterMember.fir.kt b/compiler/testData/diagnostics/tests/inner/nestedVsInnerAccessOuterMember.fir.kt index 32b4d11d13f..63afb185698 100644 --- a/compiler/testData/diagnostics/tests/inner/nestedVsInnerAccessOuterMember.fir.kt +++ b/compiler/testData/diagnostics/tests/inner/nestedVsInnerAccessOuterMember.fir.kt @@ -7,8 +7,8 @@ class Outer { class Nested { fun f() = function() fun g() = property - fun h() = this@Outer.function() - fun i() = this@Outer.property + fun h() = this@Outer.function() + fun i() = this@Outer.property } inner class Inner { diff --git a/compiler/testData/diagnostics/tests/labels/labeledFunctionLiteral.fir.kt b/compiler/testData/diagnostics/tests/labels/labeledFunctionLiteral.fir.kt index f28f11062b0..8f3c8be28da 100644 --- a/compiler/testData/diagnostics/tests/labels/labeledFunctionLiteral.fir.kt +++ b/compiler/testData/diagnostics/tests/labels/labeledFunctionLiteral.fir.kt @@ -1,17 +1,17 @@ // !DIAGNOSTICS: -UNUSED_VARIABLE val funLit = lambda@ fun String.() { - val d1 = this@lambda + val d1 = this@lambda } fun test() { val funLit = lambda@ fun String.(): String { - return this@lambda + return this@lambda } } fun lambda() { val funLit = lambda@ fun String.(): String { - return this@lambda + return this@lambda } -} \ No newline at end of file +} diff --git a/compiler/testData/diagnostics/tests/regressions/UnavaliableQualifiedThis.fir.kt b/compiler/testData/diagnostics/tests/regressions/UnavaliableQualifiedThis.fir.kt index f63947415dc..b9d2dfba40b 100644 --- a/compiler/testData/diagnostics/tests/regressions/UnavaliableQualifiedThis.fir.kt +++ b/compiler/testData/diagnostics/tests/regressions/UnavaliableQualifiedThis.fir.kt @@ -4,10 +4,10 @@ interface Iterator { fun map(transform: (element: T) -> R) : Iterator = object : Iterator { - override fun next() : R = transform(this@map.next()) + override fun next() : R = transform(this@map.next()) override val hasNext : Boolean // There's no 'this' associated with the map() function, only this of the Iterator class - get() = this@map.hasNext + get() = this@map.hasNext } -} \ No newline at end of file +} diff --git a/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/accessBaseGenericFromInnerExtendingSameBase2.fir.kt b/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/accessBaseGenericFromInnerExtendingSameBase2.fir.kt index 6420ae5e373..d8a5107d63a 100644 --- a/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/accessBaseGenericFromInnerExtendingSameBase2.fir.kt +++ b/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/accessBaseGenericFromInnerExtendingSameBase2.fir.kt @@ -7,7 +7,7 @@ open class Base(p: Any?) { class D: Base(1) { inner class B : Base { constructor() : super(foo1(1)) - constructor(x: Int) : super(this@B.foo1(1)) + constructor(x: Int) : super(this@B.foo1(1)) constructor(x: Int, y: Int) : super(this@D.foo1(1)) } } diff --git a/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/accessBaseWithSameExtension.fir.kt b/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/accessBaseWithSameExtension.fir.kt index 30f118d048b..f9f5e038a0f 100644 --- a/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/accessBaseWithSameExtension.fir.kt +++ b/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/accessBaseWithSameExtension.fir.kt @@ -7,6 +7,6 @@ fun Base.foo() { class B : Base { constructor() : super(foo1()) constructor(x: Int) : super(this@foo.foo1()) - constructor(x: Int, y: Int) : super(this@B.foo1()) + constructor(x: Int, y: Int) : super(this@B.foo1()) } } diff --git a/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/accessGenericBaseWithSameExtension.fir.kt b/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/accessGenericBaseWithSameExtension.fir.kt index 672d0b95b96..b9089da2834 100644 --- a/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/accessGenericBaseWithSameExtension.fir.kt +++ b/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/accessGenericBaseWithSameExtension.fir.kt @@ -8,6 +8,6 @@ fun Base.foo() { constructor() : super(foo1("")) constructor(x: Int) : super(foo1(1)) constructor(x: Int, y: Int) : super(this@foo.foo1(12)) - constructor(x: Int, y: Int, z: Int) : super(this@B.foo1("")) + constructor(x: Int, y: Int, z: Int) : super(this@B.foo1("")) } } diff --git a/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/lambdaAsArgument.fir.kt b/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/lambdaAsArgument.fir.kt index 861f86c6b97..71491bcdd96 100644 --- a/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/lambdaAsArgument.fir.kt +++ b/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/lambdaAsArgument.fir.kt @@ -9,7 +9,7 @@ class A { { foo() + this.foo() + - this@A.foo() + + this@A.foo() + foobar() }) } diff --git a/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/objectLiteralAsArgument.fir.kt b/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/objectLiteralAsArgument.fir.kt index 1f459e5612a..f7a300636db 100644 --- a/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/objectLiteralAsArgument.fir.kt +++ b/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/objectLiteralAsArgument.fir.kt @@ -6,7 +6,7 @@ class A { fun foo() = 1 constructor(x: Any?) constructor() : this(object { - fun bar() = foo() + this@A.foo() + + fun bar() = foo() + this@A.foo() + foobar() + super@A.hashCode() }) } diff --git a/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/objectLiteralAsDefaultValueParameter.fir.kt b/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/objectLiteralAsDefaultValueParameter.fir.kt index c490b9d3b52..268fe821043 100644 --- a/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/objectLiteralAsDefaultValueParameter.fir.kt +++ b/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/objectLiteralAsDefaultValueParameter.fir.kt @@ -5,7 +5,7 @@ fun A.foobar() = 3 class A { fun foo() = 1 constructor( x: Any = object { - fun bar() = foo() + this@A.foo() + + fun bar() = foo() + this@A.foo() + foobar() }) } diff --git a/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/passingInstance.fir.kt b/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/passingInstance.fir.kt index 293fc00a6c4..fd1cdb16f88 100644 --- a/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/passingInstance.fir.kt +++ b/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/passingInstance.fir.kt @@ -3,6 +3,6 @@ fun foo(x: A) = 1 class A { constructor(x: Int) - constructor(x: Int, y: Int, z: Int = x + foo(this) + foo(this@A)) : - this(x + foo(this) + foo(this@A)) + constructor(x: Int, y: Int, z: Int = x + foo(this) + foo(this@A)) : + this(x + foo(this) + foo(this@A)) } diff --git a/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/thisAsExtensionReceiver.fir.kt b/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/thisAsExtensionReceiver.fir.kt index f2723b50411..bbbf93d5f81 100644 --- a/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/thisAsExtensionReceiver.fir.kt +++ b/compiler/testData/diagnostics/tests/secondaryConstructors/headerCallChecker/thisAsExtensionReceiver.fir.kt @@ -10,6 +10,6 @@ class A { this.foobar() + prop + this.prop + - this@A.prop + this@A.prop ) } diff --git a/compiler/testData/diagnostics/tests/thisAndSuper/QualifiedThis.fir.kt b/compiler/testData/diagnostics/tests/thisAndSuper/QualifiedThis.fir.kt index b9acf2d4078..8ff407bea83 100644 --- a/compiler/testData/diagnostics/tests/thisAndSuper/QualifiedThis.fir.kt +++ b/compiler/testData/diagnostics/tests/thisAndSuper/QualifiedThis.fir.kt @@ -2,11 +2,11 @@ class A() { fun foo() : Unit { this@A - this@a + this@a this } val x = this@A.foo() val y = this.foo() val z = foo() -} \ No newline at end of file +} diff --git a/compiler/testData/diagnostics/tests/thisAndSuper/thisInFunctionLiterals.fir.kt b/compiler/testData/diagnostics/tests/thisAndSuper/thisInFunctionLiterals.fir.kt index 801ab918912..6e6b0b98f09 100644 --- a/compiler/testData/diagnostics/tests/thisAndSuper/thisInFunctionLiterals.fir.kt +++ b/compiler/testData/diagnostics/tests/thisAndSuper/thisInFunctionLiterals.fir.kt @@ -11,8 +11,8 @@ class A(val a:Int) { checkSubtype(this@A) } val b: Double.() -> Unit = a@{ checkSubtype(this@a) + checkSubtype(this@xx) } - val c = a@{ -> this@a + checkSubtype(this@xx) } + val c = a@{ -> this@a + checkSubtype(this@xx) } return (a@{checkSubtype(this@a) + checkSubtype(this@xx)}) } } -} \ No newline at end of file +} diff --git a/compiler/testData/diagnostics/tests/thisAndSuper/thisInPropertyInitializer.fir.kt b/compiler/testData/diagnostics/tests/thisAndSuper/thisInPropertyInitializer.fir.kt index e85a7755d10..03fb1ff92c5 100644 --- a/compiler/testData/diagnostics/tests/thisAndSuper/thisInPropertyInitializer.fir.kt +++ b/compiler/testData/diagnostics/tests/thisAndSuper/thisInPropertyInitializer.fir.kt @@ -3,6 +3,6 @@ interface Base { } val String.test: Base = object: Base { override fun foo() { - this@test + this@test } -} \ No newline at end of file +} diff --git a/compiler/testData/diagnostics/tests/thisAndSuper/thisInToplevelFunction.fir.kt b/compiler/testData/diagnostics/tests/thisAndSuper/thisInToplevelFunction.fir.kt index c8dc08dba0b..185162e8eb3 100644 --- a/compiler/testData/diagnostics/tests/thisAndSuper/thisInToplevelFunction.fir.kt +++ b/compiler/testData/diagnostics/tests/thisAndSuper/thisInToplevelFunction.fir.kt @@ -1,4 +1,4 @@ fun foo1() : Unit { this - this@a -} \ No newline at end of file + this@a +} diff --git a/compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/accessToOuterThis.fir.kt b/compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/accessToOuterThis.fir.kt index 4220f63f9f1..39287222f77 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/accessToOuterThis.fir.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/accessToOuterThis.fir.kt @@ -10,13 +10,13 @@ class Foo { inner class Bar { fun good() { contract { - returns() implies (this@Bar != null) + returns() implies (this@Bar != null) } } fun badOuter() { contract { - returns() implies (this@Foo != null) + returns() implies (this@Foo != null) } } @@ -34,7 +34,7 @@ class Foo { fun A?.badWithReceiver() { contract { - returns() implies (this@Bar != null) + returns() implies (this@Bar != null) } } }