From 82cadba80b3334c0f81b97805dd8e2374fc63b11 Mon Sep 17 00:00:00 2001 From: pyos Date: Tue, 30 Mar 2021 16:28:00 +0200 Subject: [PATCH] FIR: report errors on FirResolvedTypeRef with ConeClassErrorType Not sure what the difference from FirErrorTypeRef is. --- .../ErrorNodeDiagnosticCollectorComponent.kt | 8 ++++++++ .../kotlin/fir/resolve/inference/FirCallCompleter.kt | 8 +++++++- .../body/resolve/FirDeclarationsResolveTransformer.kt | 11 +++++------ .../testData/diagnostics/tests/AutoCreatedIt.fir.kt | 2 +- .../testData/diagnostics/tests/EnumEntryAsType.fir.kt | 2 +- .../tests/FunctionCalleeExpressions.fir.kt | 4 ++-- .../functionAsExpression/WithGenericParameters.fir.kt | 6 +++--- .../ExpectedParametersTypesMismatch.fir.kt | 4 ++-- .../postponedArgumentsAnalysis/basic.fir.kt | 2 +- .../tests/inference/extensionLambdasAndArrow.fir.kt | 6 +++--- .../cannotInferParameterTypeWithInference.fir.kt | 2 +- .../tests/labels/labeledFunctionLiteral.fir.kt | 4 ++-- .../diagnostics/tests/regressions/kt30245.fir.kt | 10 +++++----- .../tests/typealias/typeAliasAsSuperQualifier.fir.kt | 6 +++--- .../tests/when/whenAndLambdaWithExpectedType.fir.kt | 2 +- 15 files changed, 45 insertions(+), 32 deletions(-) diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/collectors/components/ErrorNodeDiagnosticCollectorComponent.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/collectors/components/ErrorNodeDiagnosticCollectorComponent.kt index d6029bc8bf8..935d57711c8 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/collectors/components/ErrorNodeDiagnosticCollectorComponent.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/collectors/components/ErrorNodeDiagnosticCollectorComponent.kt @@ -19,7 +19,9 @@ import org.jetbrains.kotlin.fir.references.FirErrorNamedReference import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeAmbiguityError import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeInapplicableCandidateError import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeUnresolvedNameError +import org.jetbrains.kotlin.fir.types.ConeClassErrorType import org.jetbrains.kotlin.fir.types.FirErrorTypeRef +import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef class ErrorNodeDiagnosticCollectorComponent(collector: AbstractDiagnosticCollector) : AbstractDiagnosticCollectorComponent(collector) { override fun visitErrorLoop(errorLoop: FirErrorLoop, data: CheckerContext) { @@ -32,6 +34,12 @@ class ErrorNodeDiagnosticCollectorComponent(collector: AbstractDiagnosticCollect reportFirDiagnostic(errorTypeRef.diagnostic, source, reporter, data) } + override fun visitResolvedTypeRef(resolvedTypeRef: FirResolvedTypeRef, data: CheckerContext) { + val errorType = resolvedTypeRef.type as? ConeClassErrorType ?: return + val source = resolvedTypeRef.source ?: return + reportFirDiagnostic(errorType.diagnostic, source, reporter, data) + } + override fun visitErrorNamedReference(errorNamedReference: FirErrorNamedReference, data: CheckerContext) { val source = errorNamedReference.source ?: return val qualifiedAccess = data.qualifiedAccesses.lastOrNull()?.takeIf { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt index c7004f7ec6c..4eb72d29d6b 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt @@ -210,7 +210,13 @@ class FirCallCompleter( val lookupTracker = session.lookupTracker lambdaArgument.valueParameters.forEachIndexed { index, parameter -> - val newReturnTypeRef = parameter.returnTypeRef.resolvedTypeFromPrototype(parameters[index].approximateLambdaInputType()) + val newReturnType = parameters[index].approximateLambdaInputType() + val newReturnTypeRef = if (parameter.returnTypeRef is FirImplicitTypeRef) { + buildResolvedTypeRef { + source = parameter.source + type = newReturnType + } + } else parameter.returnTypeRef.resolvedTypeFromPrototype(newReturnType) parameter.replaceReturnTypeRef(newReturnTypeRef) lookupTracker?.recordTypeResolveAsLookup(newReturnTypeRef, parameter.source, null) } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt index 1a76ab4a44b..685db954644 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt @@ -669,12 +669,11 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor if (param.returnTypeRef is FirResolvedTypeRef) { param } else { - param.transformReturnTypeRef( - StoreType, - param.returnTypeRef.resolvedTypeFromPrototype( - resolvedLambdaAtom.parameters[index] - ) - ) + val resolvedType = buildResolvedTypeRef { + source = param.source + type = resolvedLambdaAtom.parameters[index] + } + param.replaceReturnTypeRef(resolvedType) param } } diff --git a/compiler/testData/diagnostics/tests/AutoCreatedIt.fir.kt b/compiler/testData/diagnostics/tests/AutoCreatedIt.fir.kt index b171ef0b6ea..09a0d4d3f8d 100644 --- a/compiler/testData/diagnostics/tests/AutoCreatedIt.fir.kt +++ b/compiler/testData/diagnostics/tests/AutoCreatedIt.fir.kt @@ -13,7 +13,7 @@ fun text() { bar2 {} bar2 {1} bar2 {it} - bar2 {it -> it} + bar2 {it -> it} } fun bar(f : (Int, Int) -> Int) {} diff --git a/compiler/testData/diagnostics/tests/EnumEntryAsType.fir.kt b/compiler/testData/diagnostics/tests/EnumEntryAsType.fir.kt index 40af8425223..a904de616fe 100644 --- a/compiler/testData/diagnostics/tests/EnumEntryAsType.fir.kt +++ b/compiler/testData/diagnostics/tests/EnumEntryAsType.fir.kt @@ -18,7 +18,7 @@ class MyColor(val x: Color.RED, y: Color.RED fun local(arg: Color.RED): Color.RED = arg val temp: Color.RED = Color.RED - temp as? Color.RED + temp as? Color.RED if (temp is Color.RED) { return temp as Color.RED } diff --git a/compiler/testData/diagnostics/tests/FunctionCalleeExpressions.fir.kt b/compiler/testData/diagnostics/tests/FunctionCalleeExpressions.fir.kt index 0447a576c88..966b9ec8bf6 100644 --- a/compiler/testData/diagnostics/tests/FunctionCalleeExpressions.fir.kt +++ b/compiler/testData/diagnostics/tests/FunctionCalleeExpressions.fir.kt @@ -40,8 +40,8 @@ fun main(args : Array) { foo2()({}) foo2(){} (foo2()){} - (foo2()){x -> } - foo2()({x -> }) + (foo2()){x -> } + foo2()({x -> }) val a = fooT1(1)() checkSubtype(a) diff --git a/compiler/testData/diagnostics/tests/functionAsExpression/WithGenericParameters.fir.kt b/compiler/testData/diagnostics/tests/functionAsExpression/WithGenericParameters.fir.kt index 022fae99769..a83ae2a8e6b 100644 --- a/compiler/testData/diagnostics/tests/functionAsExpression/WithGenericParameters.fir.kt +++ b/compiler/testData/diagnostics/tests/functionAsExpression/WithGenericParameters.fir.kt @@ -12,8 +12,8 @@ fun fun_with_where() = fun () {}) - devNull(fun T.() {}) - devNull(fun (): T = null!!) - devNull(fun (t: T) {}) + devNull(fun T.() {}) + devNull(fun (): T = null!!) + devNull(fun (t: T) {}) devNull(fun () where T:A {}) } diff --git a/compiler/testData/diagnostics/tests/functionLiterals/ExpectedParametersTypesMismatch.fir.kt b/compiler/testData/diagnostics/tests/functionLiterals/ExpectedParametersTypesMismatch.fir.kt index c9ef4f81daf..5d58d8c3227 100644 --- a/compiler/testData/diagnostics/tests/functionLiterals/ExpectedParametersTypesMismatch.fir.kt +++ b/compiler/testData/diagnostics/tests/functionLiterals/ExpectedParametersTypesMismatch.fir.kt @@ -13,7 +13,7 @@ fun test1() { s: String-> "" } foo0 { - x, y -> "" + x, y -> "" } foo1 { @@ -23,7 +23,7 @@ fun test1() { s: String -> "" } foo1 { - x, y -> "" + x, y -> "" } foo1 { -> 42 diff --git a/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/basic.fir.kt b/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/basic.fir.kt index 87bdcc67010..0c04617fb4b 100644 --- a/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/basic.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/basic.fir.kt @@ -151,7 +151,7 @@ fun main() { select(id(fun String.(x: String) {}), id(fun(x: String, y: String) { }), { x -> this }) select(id(fun String.(x: String) {}), id(fun(x: String, y: String) { }), { x: String, y: String -> x }) // Convert to extension lambda is impossible because the lambda parameter types aren't specified explicitly - select(id(fun String.(x: String) {}), id(fun(x: String, y: String) { }), { x, y -> x }) + select(id(fun String.(x: String) {}), id(fun(x: String, y: String) { }), { x, y -> x }) select(id(id(fun(x: String, y: String) { }), fun String.(x: String) {}), { x, y -> x }) val x26: Int.(String) -> Int = fun (x: String) = 10 // it must be error, see KT-38439 // Receiver must be specified in anonymous function declaration diff --git a/compiler/testData/diagnostics/tests/inference/extensionLambdasAndArrow.fir.kt b/compiler/testData/diagnostics/tests/inference/extensionLambdasAndArrow.fir.kt index 1e63cd4b0d3..390d99e0ca9 100644 --- a/compiler/testData/diagnostics/tests/inference/extensionLambdasAndArrow.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/extensionLambdasAndArrow.fir.kt @@ -8,9 +8,9 @@ fun main() { val x3: () -> String = if (true) {{ -> "this" }} else {{ -> "this" }} val x4: String.() -> String = if (true) {{ str: String -> "this" }} else {{ str: String -> "this" }} val x41: String.(String) -> String = if (true) {{ str: String, str2: String -> "this" }} else {{ str: String, str2: String -> "this" }} - val x42: String.(String) -> String = if (true) {{ str, str2 -> "this" }} else {{ str, str2 -> "this" }} - val x5: String.() -> String = if (true) {{ str -> "this" }} else {{ str -> "this" }} - val x6: String.() -> String = if (true) {{ str -> "this" }} else {{ "this" }} + val x42: String.(String) -> String = if (true) {{ str, str2 -> "this" }} else {{ str, str2 -> "this" }} + val x5: String.() -> String = if (true) {{ str -> "this" }} else {{ str -> "this" }} + val x6: String.() -> String = if (true) {{ str -> "this" }} else {{ "this" }} val x7: String.() -> String = select({ -> this }, { -> this }) val x8: String.() -> String = select({ this }, { this }) } diff --git a/compiler/testData/diagnostics/tests/inference/reportingImprovements/cannotInferParameterTypeWithInference.fir.kt b/compiler/testData/diagnostics/tests/inference/reportingImprovements/cannotInferParameterTypeWithInference.fir.kt index e70cdb0a116..04f0dd15e3b 100644 --- a/compiler/testData/diagnostics/tests/inference/reportingImprovements/cannotInferParameterTypeWithInference.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/reportingImprovements/cannotInferParameterTypeWithInference.fir.kt @@ -13,5 +13,5 @@ fun test1() { fun bar(f: (A)->Unit) {} fun test2() { - bar { a -> } // here we don't have 'cannot infer parameter type' error + bar { a -> } // here we don't have 'cannot infer parameter type' error } diff --git a/compiler/testData/diagnostics/tests/labels/labeledFunctionLiteral.fir.kt b/compiler/testData/diagnostics/tests/labels/labeledFunctionLiteral.fir.kt index 3bdc376abb4..f28f11062b0 100644 --- a/compiler/testData/diagnostics/tests/labels/labeledFunctionLiteral.fir.kt +++ b/compiler/testData/diagnostics/tests/labels/labeledFunctionLiteral.fir.kt @@ -5,13 +5,13 @@ val funLit = lambda@ fun String.() { } fun test() { - val funLit = lambda@ fun String.(): String { + val funLit = lambda@ fun String.(): String { return this@lambda } } fun lambda() { - val funLit = lambda@ fun String.(): String { + val funLit = lambda@ fun String.(): String { return this@lambda } } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/regressions/kt30245.fir.kt b/compiler/testData/diagnostics/tests/regressions/kt30245.fir.kt index 8b844f809cd..57fb7b3ddae 100644 --- a/compiler/testData/diagnostics/tests/regressions/kt30245.fir.kt +++ b/compiler/testData/diagnostics/tests/regressions/kt30245.fir.kt @@ -46,9 +46,9 @@ fun test1() { // to extension lambda 0 val w11 = W1 { i: Int -> i } // oi- ni- val i11: E0 = id { i: Int -> i } // o1+ ni+ - val w12 = W1 { i -> i } // oi- ni- - val i12: E0 = id { i -> i } // oi- ni- - val j12 = id { i -> i } // oi- ni- + val w12 = W1 { i -> i } // oi- ni- + val i12: E0 = id { i -> i } // oi- ni- + val j12 = id { i -> i } // oi- ni- // yet unsupported cases - considering lambdas as extension ones unconditionally // val w13 = W1 { it } // this or it: oi- ni- @@ -83,8 +83,8 @@ fun test2() { // to extension lambda 1 val i27: E1 = when (e) { E.VALUE -> { s: String -> this + s.length } } // oi+ ni+ val i27a: E1 = when (e) { E.VALUE -> { s -> this + s.length } } // oi+ ni+ - val w28 = W2 { i: Int, s -> i + s.length } // oi- ni- - val i28: E1 = id { i: Int, s -> i + s.length } // oi- ni- + val w28 = W2 { i: Int, s -> i + s.length } // oi- ni- + val i28: E1 = id { i: Int, s -> i + s.length } // oi- ni- val w29 = W2 { i: Int, s: String -> i + s.length } // oi- ni- val i29: E1 = id { i: Int, s: String -> i + s.length } // oi+ ni+ diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasAsSuperQualifier.fir.kt b/compiler/testData/diagnostics/tests/typealias/typeAliasAsSuperQualifier.fir.kt index e93c650a22e..639922c7ed6 100644 --- a/compiler/testData/diagnostics/tests/typealias/typeAliasAsSuperQualifier.fir.kt +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasAsSuperQualifier.fir.kt @@ -22,7 +22,7 @@ class TestSuperForBase : B() { override fun foo() { super.foo() super.foo() - super.foo() + super<MyBase>.foo() super.foo() } } @@ -34,8 +34,8 @@ class TestSuperForGenericBase : GB() { override fun foo() { super.foo() super.foo() - super.foo() - super.foo() // Type arguments don't matter here + super<MyBase>.foo() + super<MyBaseInt>.foo() // Type arguments don't matter here super.foo() } } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/when/whenAndLambdaWithExpectedType.fir.kt b/compiler/testData/diagnostics/tests/when/whenAndLambdaWithExpectedType.fir.kt index ce5df31b6d7..027c1ea2405 100644 --- a/compiler/testData/diagnostics/tests/when/whenAndLambdaWithExpectedType.fir.kt +++ b/compiler/testData/diagnostics/tests/when/whenAndLambdaWithExpectedType.fir.kt @@ -30,7 +30,7 @@ val test3: (String) -> Boolean = val test4: (String) -> Boolean = when { - true -> { s1, s2 -> true } + true -> { s1, s2 -> true } else -> null!! }