From 81b1ed9ad4dd8fa448a591eefe4bdff5c7d39560 Mon Sep 17 00:00:00 2001 From: Ivan Kochurkin Date: Thu, 7 Apr 2022 16:07:11 +0300 Subject: [PATCH] [FIR] Fix false positive RETURN_TYPE_MISMATCH in intellij ultimate --- .../diagnostics/functionReturnTypeMismatchChecker/basic.kt | 2 +- .../expression/FirFunctionReturnTypeMismatchChecker.kt | 4 +--- .../return/IfWithoutElseWithExplicitType.fir.kt | 2 +- .../functionLiterals/return/LocalReturnSecondUnit.fir.kt | 6 ++++++ .../tests/functionLiterals/return/LocalReturnSecondUnit.kt | 3 +-- .../return/LocalReturnsWithExplicitReturnType.fir.kt | 2 +- .../generics/projectionsScope/typeMismatchInLambda.fir.kt | 2 +- .../coercionToUnit/coercionWithExpectedType.fir.kt | 2 +- .../completion/postponedArgumentsAnalysis/basic.fir.kt | 2 +- 9 files changed, 14 insertions(+), 11 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnSecondUnit.fir.kt diff --git a/compiler/fir/analysis-tests/testData/resolveWithStdlib/diagnostics/functionReturnTypeMismatchChecker/basic.kt b/compiler/fir/analysis-tests/testData/resolveWithStdlib/diagnostics/functionReturnTypeMismatchChecker/basic.kt index 9b997ca7c49..2ce0b1ab128 100644 --- a/compiler/fir/analysis-tests/testData/resolveWithStdlib/diagnostics/functionReturnTypeMismatchChecker/basic.kt +++ b/compiler/fir/analysis-tests/testData/resolveWithStdlib/diagnostics/functionReturnTypeMismatchChecker/basic.kt @@ -37,7 +37,7 @@ fun errorWithLambda(): String { return@foo } foo { bar() - return@foo 10 + return@foo 10 } return "" diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirFunctionReturnTypeMismatchChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirFunctionReturnTypeMismatchChecker.kt index 9f21ca24bd8..43205cd0d62 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirFunctionReturnTypeMismatchChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirFunctionReturnTypeMismatchChecker.kt @@ -25,9 +25,7 @@ object FirFunctionReturnTypeMismatchChecker : FirReturnExpressionChecker() { override fun check(expression: FirReturnExpression, context: CheckerContext, reporter: DiagnosticReporter) { if (expression.source == null) return val targetElement = expression.target.labeledElement - if (targetElement is FirErrorFunction || - targetElement is FirAnonymousFunction && targetElement.isLambda && expression.target.labelName == null - ) { + if (targetElement is FirErrorFunction || targetElement is FirAnonymousFunction && targetElement.isLambda) { return } val resultExpression = expression.result diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/IfWithoutElseWithExplicitType.fir.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/IfWithoutElseWithExplicitType.fir.kt index e39438c48f3..fecb9ea5bf4 100644 --- a/compiler/testData/diagnostics/tests/functionLiterals/return/IfWithoutElseWithExplicitType.fir.kt +++ b/compiler/testData/diagnostics/tests/functionLiterals/return/IfWithoutElseWithExplicitType.fir.kt @@ -5,7 +5,7 @@ val a: () -> Int = l@ { } val b: () -> Unit = l@ { - if (flag) return@l 4 + if (flag) return@l 4 } val c: () -> Any = l@ { diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnSecondUnit.fir.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnSecondUnit.fir.kt new file mode 100644 index 00000000000..d1fb85056b4 --- /dev/null +++ b/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnSecondUnit.fir.kt @@ -0,0 +1,6 @@ +val flag = true + +val a = b@ { + if (flag) return@b 4 + return@b +} diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnSecondUnit.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnSecondUnit.kt index 11d8ab569cd..859d60ede90 100644 --- a/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnSecondUnit.kt +++ b/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnSecondUnit.kt @@ -1,7 +1,6 @@ -// FIR_IDENTICAL val flag = true val a = b@ { if (flag) return@b 4 return@b -} \ No newline at end of file +} diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnsWithExplicitReturnType.fir.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnsWithExplicitReturnType.fir.kt index 959a6fdc54d..e5f864f2baa 100644 --- a/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnsWithExplicitReturnType.fir.kt +++ b/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnsWithExplicitReturnType.fir.kt @@ -1,6 +1,6 @@ fun test(a: Int) { runf@{ - if (a > 0) return@f "" + if (a > 0) return@f "" return@f 1 } diff --git a/compiler/testData/diagnostics/tests/generics/projectionsScope/typeMismatchInLambda.fir.kt b/compiler/testData/diagnostics/tests/generics/projectionsScope/typeMismatchInLambda.fir.kt index ba6cde6ace1..c71c7c06a0f 100644 --- a/compiler/testData/diagnostics/tests/generics/projectionsScope/typeMismatchInLambda.fir.kt +++ b/compiler/testData/diagnostics/tests/generics/projectionsScope/typeMismatchInLambda.fir.kt @@ -20,7 +20,7 @@ fun test(a: A, z: Out) { a.bar { z.id() } a.foo { - z.foobar(if (1 > 2) return@foo "" else "") + z.foobar(if (1 > 2) return@foo "" else "") "" } } diff --git a/compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionWithExpectedType.fir.kt b/compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionWithExpectedType.fir.kt index b346199bf14..5fa78bd038f 100644 --- a/compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionWithExpectedType.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionWithExpectedType.fir.kt @@ -12,7 +12,7 @@ val a: () -> Unit = l@{ val b: () -> Unit = l@{ // Error, coercion can't be applied at this position! - if (true) return@l "hello" + if (true) return@l "hello" // However, this is OK, because here coercion is applied "hello" 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 bbb3aa5a498..d2dfa2a385d 100644 --- a/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/basic.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/basic.fir.kt @@ -103,7 +103,7 @@ fun main() { select( id { x, y -> x.inv() + y.toByte() }, id<(Int, Number) -> Int> { x, y -> x.inv() }, - {} as (Number, Number) -> Int + {} as (Number, Number) -> Int ) // Inferring lambda parameter types by a few expected types (a few upper constraints)