From 9fa0f51a6189892958c43f6843ae9d07fc76ac08 Mon Sep 17 00:00:00 2001 From: "Denis.Zharkov" Date: Tue, 29 Nov 2022 12:54:13 +0100 Subject: [PATCH] K2: Fix false-negative RETURN_TYPE_MISMATCH ^KT-53987 Fixed ^KT-55932 Fixed --- .../innerDeclarationsResolve/class.fir.txt | 2 +- ...CompilerTestFE10TestdataTestGenerated.java | 6 +++++ .../returnTypeMismatchOnOverride.fir.txt | 2 +- .../mutableList.fir.txt | 2 +- .../testData/resolve/kt54775.kt | 3 +-- .../basic.fir.txt | 2 +- .../basic.kt | 2 +- ...irOldFrontendDiagnosticsTestGenerated.java | 6 +++++ ...DiagnosticsWithLightTreeTestGenerated.java | 6 +++++ .../coneDiagnosticToFirDiagnostic.kt | 4 ---- .../fir/resolve/inference/FirCallCompleter.kt | 19 +++++++++------ .../model/FirConstraintPositionAndErrors.kt | 4 +--- .../tests/FunctionReturnTypes.fir.kt | 2 +- .../ifElseIntersection.fir.kt | 4 ++-- .../controlStructures/incorrectElvis.fir.kt | 4 ++++ .../tests/controlStructures/incorrectElvis.kt | 4 ++++ .../ObjectExpression.fir.kt | 2 +- .../genericMethodInGenericClass.fir.kt | 2 +- ...noOperatorModifierOnProvideDelegate.fir.kt | 16 +++++++++++++ .../noOperatorModifierOnProvideDelegate.kt | 1 - .../typeMismatchForGetReturnType.fir.kt | 11 +++++++++ .../typeMismatchForGetReturnType.kt | 1 - .../nullabilityAndSmartCasts/kt2146.fir.kt | 24 +++++++++---------- .../tests/resolve/newLineLambda.fir.kt | 6 ++--- .../smartCasts/publicVals/delegate.fir.kt | 2 +- .../implicitNothingInReturnPosition.fir.kt | 2 +- .../testsWithStdLib/elvisOnJavaList.fir.kt | 2 +- .../test/runners/DiagnosticTestGenerated.java | 6 +++++ 28 files changed, 101 insertions(+), 46 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/controlStructures/incorrectElvis.fir.kt create mode 100644 compiler/testData/diagnostics/tests/controlStructures/incorrectElvis.kt create mode 100644 compiler/testData/diagnostics/tests/delegatedProperty/provideDelegate/noOperatorModifierOnProvideDelegate.fir.kt create mode 100644 compiler/testData/diagnostics/tests/delegatedProperty/typeMismatchForGetReturnType.fir.kt diff --git a/analysis/low-level-api-fir/testdata/innerDeclarationsResolve/class.fir.txt b/analysis/low-level-api-fir/testdata/innerDeclarationsResolve/class.fir.txt index 5760debef36..cf18a38621a 100644 --- a/analysis/low-level-api-fir/testdata/innerDeclarationsResolve/class.fir.txt +++ b/analysis/low-level-api-fir/testdata/innerDeclarationsResolve/class.fir.txt @@ -11,7 +11,7 @@ FILE: class.kt private [BODY_RESOLVE] get(): public final [BODY_RESOLVE] fun foo([BODY_RESOLVE] a: ): { - ^foo R|kotlin/with#|<, >(R|/a|, = [BODY_RESOLVE] with@fun .(): { + ^foo R|kotlin/with#|<, >(R|/a|, = [BODY_RESOLVE] with@fun .(): { ^ #(String(a), this@R|/B|.R|/B.y|) } ) diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java index e3df9dd7a7e..56ce73cfc78 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java @@ -6560,6 +6560,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/controlStructures/improperElseInExpression.kt"); } + @Test + @TestMetadata("incorrectElvis.kt") + public void testIncorrectElvis() throws Exception { + runTest("compiler/testData/diagnostics/tests/controlStructures/incorrectElvis.kt"); + } + @Test @TestMetadata("jumpAcrossFunctionBoundary.kt") public void testJumpAcrossFunctionBoundary() throws Exception { diff --git a/compiler/fir/analysis-tests/testData/resolve/diagnostics/returnTypeMismatchOnOverride.fir.txt b/compiler/fir/analysis-tests/testData/resolve/diagnostics/returnTypeMismatchOnOverride.fir.txt index 13af537c076..8365de27f8d 100644 --- a/compiler/fir/analysis-tests/testData/resolve/diagnostics/returnTypeMismatchOnOverride.fir.txt +++ b/compiler/fir/analysis-tests/testData/resolve/diagnostics/returnTypeMismatchOnOverride.fir.txt @@ -180,7 +180,7 @@ FILE: returnTypeMismatchOnOverride.kt } public open fun kek(): R|Z| { - ^kek R|/Z.Z#|() + ^kek R|/Z.Z|() } } diff --git a/compiler/fir/analysis-tests/testData/resolve/extendedCheckers/canBeReplacedWithOperatorAssignment/mutableList.fir.txt b/compiler/fir/analysis-tests/testData/resolve/extendedCheckers/canBeReplacedWithOperatorAssignment/mutableList.fir.txt index ddbd58be026..5c250b98f39 100644 --- a/compiler/fir/analysis-tests/testData/resolve/extendedCheckers/canBeReplacedWithOperatorAssignment/mutableList.fir.txt +++ b/compiler/fir/analysis-tests/testData/resolve/extendedCheckers/canBeReplacedWithOperatorAssignment/mutableList.fir.txt @@ -1,5 +1,5 @@ FILE: mutableList.kt public final fun foo(): R|kotlin/Unit| { lvar listVar: R|kotlin/collections/MutableList| = R|kotlin/collections/mutableListOf|(vararg(Int(1), Int(2), Int(3))) - R|/listVar| = R|/listVar|.R|kotlin/collections/plus#|(Int(4)) + R|/listVar| = R|/listVar|.R|kotlin/collections/plus|(Int(4)) } diff --git a/compiler/fir/analysis-tests/testData/resolve/kt54775.kt b/compiler/fir/analysis-tests/testData/resolve/kt54775.kt index a41b10bab24..356c830b363 100644 --- a/compiler/fir/analysis-tests/testData/resolve/kt54775.kt +++ b/compiler/fir/analysis-tests/testData/resolve/kt54775.kt @@ -2,7 +2,6 @@ fun bar(): T { return null as T } -class X() : B by get() = bar() { +class X() : B by get() = bar() { val prop = bar() = 2 } - diff --git a/compiler/fir/analysis-tests/testData/resolveWithStdlib/diagnostics/functionReturnTypeMismatchChecker/basic.fir.txt b/compiler/fir/analysis-tests/testData/resolveWithStdlib/diagnostics/functionReturnTypeMismatchChecker/basic.fir.txt index a3b5ff1ffe0..834670893b4 100644 --- a/compiler/fir/analysis-tests/testData/resolveWithStdlib/diagnostics/functionReturnTypeMismatchChecker/basic.fir.txt +++ b/compiler/fir/analysis-tests/testData/resolveWithStdlib/diagnostics/functionReturnTypeMismatchChecker/basic.fir.txt @@ -20,7 +20,7 @@ FILE: basic.kt ^okOneLineFunction Int(10).R|kotlin/Int.plus|(Int(1)) } public final fun errorOneLineFunction(): R|kotlin/String| { - ^errorOneLineFunction Int(10).R|kotlin/Int.plus#|(Int(1)) + ^errorOneLineFunction Int(10).R|kotlin/Int.plus|(Int(1)) } public final class A : R|kotlin/Any| { public constructor(): R|A| { 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 06f8aea7865..9a37dcdcb6a 100644 --- a/compiler/fir/analysis-tests/testData/resolveWithStdlib/diagnostics/functionReturnTypeMismatchChecker/basic.kt +++ b/compiler/fir/analysis-tests/testData/resolveWithStdlib/diagnostics/functionReturnTypeMismatchChecker/basic.kt @@ -1,6 +1,6 @@ // bug: type of the expression in return statement is Char fun illegalReturnIf(): Char { - return if (1 < 2) 'a' else { 1 } + return if (1 < 2) 'a' else { 1 } } fun foo(): String { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index 4b153670533..1c5924433c3 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -6566,6 +6566,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/controlStructures/improperElseInExpression.kt"); } + @Test + @TestMetadata("incorrectElvis.kt") + public void testIncorrectElvis() throws Exception { + runTest("compiler/testData/diagnostics/tests/controlStructures/incorrectElvis.kt"); + } + @Test @TestMetadata("jumpAcrossFunctionBoundary.kt") public void testJumpAcrossFunctionBoundary() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java index 9bfa04eec36..9459c86a185 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java @@ -6560,6 +6560,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/controlStructures/improperElseInExpression.kt"); } + @Test + @TestMetadata("incorrectElvis.kt") + public void testIncorrectElvis() throws Exception { + runTest("compiler/testData/diagnostics/tests/controlStructures/incorrectElvis.kt"); + } + @Test @TestMetadata("jumpAcrossFunctionBoundary.kt") public void testJumpAcrossFunctionBoundary() throws Exception { diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/coneDiagnosticToFirDiagnostic.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/coneDiagnosticToFirDiagnostic.kt index 4d25bc7ea1a..abeba3a7c54 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/coneDiagnosticToFirDiagnostic.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/coneDiagnosticToFirDiagnostic.kt @@ -379,10 +379,6 @@ private fun ConstraintSystemError.toDiagnostic( when (position) { is ConeExpectedTypeConstraintPosition -> { - if (position.expectedTypeMismatchIsReportedInChecker) { - errorsToIgnore.add(this) - return null - } val inferredType = if (!lowerConeType.isNullableNothing) lowerConeType 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 ca42ce6fed6..f07472dd6cf 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 @@ -166,30 +166,35 @@ class FirCallCompleter( mayBeCoercionToUnitApplied: Boolean ) { val expectedType = expectedTypeRef?.coneTypeSafe() ?: return - val expectedTypeConstraintPosition = ConeExpectedTypeConstraintPosition(expectedTypeMismatchIsReportedInChecker) val system = candidate.system when { - !shouldEnforceExpectedType -> { - system.addSubtypeConstraintIfCompatible(initialType, expectedType, expectedTypeConstraintPosition) + // If type mismatch is assumed to be reported in the checker, we should not add a subtyping constraint that leads to error. + // Because it might make resulting type correct while, it's hopefully would be more clear if we let the call be inferred without + // the expected type, and then would report diagnostic in the checker. + // It's assumed to be safe & sound, because if constraint system has contradictions when expected type is added, + // the resulting expression type cannot be inferred to something that is a subtype of `expectedType`, + // thus the diagnostic should be reported. + !shouldEnforceExpectedType || expectedTypeMismatchIsReportedInChecker -> { + system.addSubtypeConstraintIfCompatible(initialType, expectedType, ConeExpectedTypeConstraintPosition) } isFromCast -> { if (candidate.isFunctionForExpectTypeFromCastFeature()) { system.addSubtypeConstraint( initialType, expectedType, - ConeExpectedTypeConstraintPosition(expectedTypeMismatchIsReportedInChecker = false), + ConeExpectedTypeConstraintPosition, ) } } !expectedType.isUnitOrFlexibleUnit || (!mayBeCoercionToUnitApplied && !expectedTypeMismatchIsReportedInChecker) -> { - system.addSubtypeConstraint(initialType, expectedType, expectedTypeConstraintPosition) + system.addSubtypeConstraint(initialType, expectedType, ConeExpectedTypeConstraintPosition) } system.notFixedTypeVariables.isEmpty() -> return expectedType.isUnit -> { - system.addEqualityConstraintIfCompatible(initialType, expectedType, expectedTypeConstraintPosition) + system.addEqualityConstraintIfCompatible(initialType, expectedType, ConeExpectedTypeConstraintPosition) } else -> { - system.addSubtypeConstraintIfCompatible(initialType, expectedType, expectedTypeConstraintPosition) + system.addSubtypeConstraintIfCompatible(initialType, expectedType, ConeExpectedTypeConstraintPosition) } } } diff --git a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/inference/model/FirConstraintPositionAndErrors.kt b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/inference/model/FirConstraintPositionAndErrors.kt index 0a4f323213d..f1f68e0831a 100644 --- a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/inference/model/FirConstraintPositionAndErrors.kt +++ b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/inference/model/FirConstraintPositionAndErrors.kt @@ -18,9 +18,7 @@ class ConeFixVariableConstraintPosition(variable: TypeVariableMarker) : FixVaria class ConeArgumentConstraintPosition(argument: FirElement) : ArgumentConstraintPosition(argument) -class ConeExpectedTypeConstraintPosition( - val expectedTypeMismatchIsReportedInChecker: Boolean -) : ExpectedTypeConstraintPosition(null) +object ConeExpectedTypeConstraintPosition : ExpectedTypeConstraintPosition(null) class ConeExplicitTypeParameterConstraintPosition( typeArgument: FirTypeProjection, diff --git a/compiler/testData/diagnostics/tests/FunctionReturnTypes.fir.kt b/compiler/testData/diagnostics/tests/FunctionReturnTypes.fir.kt index 675c1c8a286..b02fe89bd5b 100644 --- a/compiler/testData/diagnostics/tests/FunctionReturnTypes.fir.kt +++ b/compiler/testData/diagnostics/tests/FunctionReturnTypes.fir.kt @@ -152,7 +152,7 @@ fun illegalIfBlock(): Boolean { else { return 1 } } fun illegalReturnIf(): Char { - return if (1 < 2) 'a' else { 1 } + return if (1 < 2) 'a' else { 1 } } fun returnNothing(): Nothing { diff --git a/compiler/testData/diagnostics/tests/controlStructures/ifElseIntersection.fir.kt b/compiler/testData/diagnostics/tests/controlStructures/ifElseIntersection.fir.kt index a8c6a1734e6..ba8d25dc29a 100644 --- a/compiler/testData/diagnostics/tests/controlStructures/ifElseIntersection.fir.kt +++ b/compiler/testData/diagnostics/tests/controlStructures/ifElseIntersection.fir.kt @@ -24,11 +24,11 @@ fun bind2(r: Option): Option { } fun bind3(r: Option): Option { - return if (r is Some) { + return if (r is Some) { // Diagnoses an error correctly if (true) None() else r } - else r + else r } fun bindWhen(r: Option): Option { diff --git a/compiler/testData/diagnostics/tests/controlStructures/incorrectElvis.fir.kt b/compiler/testData/diagnostics/tests/controlStructures/incorrectElvis.fir.kt new file mode 100644 index 00000000000..45d0dd85702 --- /dev/null +++ b/compiler/testData/diagnostics/tests/controlStructures/incorrectElvis.fir.kt @@ -0,0 +1,4 @@ +// SKIP_TXT +// ISSUE: KT-55932 + +fun test(x: String?): Int = x?.length ?: "smth" diff --git a/compiler/testData/diagnostics/tests/controlStructures/incorrectElvis.kt b/compiler/testData/diagnostics/tests/controlStructures/incorrectElvis.kt new file mode 100644 index 00000000000..fc00a452ca1 --- /dev/null +++ b/compiler/testData/diagnostics/tests/controlStructures/incorrectElvis.kt @@ -0,0 +1,4 @@ +// SKIP_TXT +// ISSUE: KT-55932 + +fun test(x: String?): Int = x?.length ?: "smth" diff --git a/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/ObjectExpression.fir.kt b/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/ObjectExpression.fir.kt index 1497b189a2a..91db2803e42 100644 --- a/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/ObjectExpression.fir.kt +++ b/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/ObjectExpression.fir.kt @@ -6,6 +6,6 @@ fun foo() { val a = object { fun baz() = bar(if (x == null) 0 else x) - fun quux(): Int = if (x == null) x else x + fun quux(): Int = if (x == null) x else x } } diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/inference/genericMethodInGenericClass.fir.kt b/compiler/testData/diagnostics/tests/delegatedProperty/inference/genericMethodInGenericClass.fir.kt index b42b011a027..6c2b0d91eff 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/inference/genericMethodInGenericClass.fir.kt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/inference/genericMethodInGenericClass.fir.kt @@ -24,4 +24,4 @@ class C() { } var c1: Int by C() -var c2: Int by C() +var c2: Int by C() diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/provideDelegate/noOperatorModifierOnProvideDelegate.fir.kt b/compiler/testData/diagnostics/tests/delegatedProperty/provideDelegate/noOperatorModifierOnProvideDelegate.fir.kt new file mode 100644 index 00000000000..648295eb2c4 --- /dev/null +++ b/compiler/testData/diagnostics/tests/delegatedProperty/provideDelegate/noOperatorModifierOnProvideDelegate.fir.kt @@ -0,0 +1,16 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER + +import kotlin.reflect.KProperty + +class StringDelegate(val s: String) { + operator fun getValue(a: Any?, p: KProperty<*>): Int = 42 +} + +// NB no operator +fun String.provideDelegate(a: Any?, p: KProperty<*>) = StringDelegate(this) + +operator fun String.getValue(a: Any?, p: KProperty<*>) = this + +val test1: String by "OK" +val test2: Int by "OK" +val test3 by "OK" diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/provideDelegate/noOperatorModifierOnProvideDelegate.kt b/compiler/testData/diagnostics/tests/delegatedProperty/provideDelegate/noOperatorModifierOnProvideDelegate.kt index 73b980ddbad..d92e6ab8bf5 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/provideDelegate/noOperatorModifierOnProvideDelegate.kt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/provideDelegate/noOperatorModifierOnProvideDelegate.kt @@ -1,4 +1,3 @@ -// FIR_IDENTICAL // !DIAGNOSTICS: -UNUSED_PARAMETER import kotlin.reflect.KProperty diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/typeMismatchForGetReturnType.fir.kt b/compiler/testData/diagnostics/tests/delegatedProperty/typeMismatchForGetReturnType.fir.kt new file mode 100644 index 00000000000..faf7937226b --- /dev/null +++ b/compiler/testData/diagnostics/tests/delegatedProperty/typeMismatchForGetReturnType.fir.kt @@ -0,0 +1,11 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER + +import kotlin.reflect.KProperty + +val c: Int by Delegate() + +class Delegate { + operator fun getValue(t: Any?, p: KProperty<*>): String { + return "" + } +} diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/typeMismatchForGetReturnType.kt b/compiler/testData/diagnostics/tests/delegatedProperty/typeMismatchForGetReturnType.kt index 9f9edad8154..babcd067eb8 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/typeMismatchForGetReturnType.kt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/typeMismatchForGetReturnType.kt @@ -1,4 +1,3 @@ -// FIR_IDENTICAL // !DIAGNOSTICS: -UNUSED_PARAMETER import kotlin.reflect.KProperty diff --git a/compiler/testData/diagnostics/tests/nullabilityAndSmartCasts/kt2146.fir.kt b/compiler/testData/diagnostics/tests/nullabilityAndSmartCasts/kt2146.fir.kt index 760026300e9..13a30eef0bb 100644 --- a/compiler/testData/diagnostics/tests/nullabilityAndSmartCasts/kt2146.fir.kt +++ b/compiler/testData/diagnostics/tests/nullabilityAndSmartCasts/kt2146.fir.kt @@ -9,46 +9,46 @@ fun f1(s: Int?): Int { } fun f2(s: Int?): Int { - return when (s) { + return when (s) { !is Int -> s else -> s - } + } } fun f3(s: Int?): Int { - return when (s) { + return when (s) { is Int -> s else -> s - } + } } fun f4(s: Int?): Int { - return when { + return when { s == 4 -> s s == null -> s else -> s - } + } } fun f5(s: Int?): Int { - return when (s) { + return when (s) { s -> s s!! -> s s -> s else -> 0 - } + } } fun f6(s: Int?): Int { - return when { + return when { s is Int -> s else -> s - } + } } fun f7(s: Int?): Int { - return when { + return when { s !is Int -> s else -> s - } + } } diff --git a/compiler/testData/diagnostics/tests/resolve/newLineLambda.fir.kt b/compiler/testData/diagnostics/tests/resolve/newLineLambda.fir.kt index 3d1c12b15b4..3bf1d920507 100644 --- a/compiler/testData/diagnostics/tests/resolve/newLineLambda.fir.kt +++ b/compiler/testData/diagnostics/tests/resolve/newLineLambda.fir.kt @@ -99,13 +99,13 @@ fun testTwoLambdas() { {} {} - return if (true) { + return if (true) { twoLambdaArgs({}) {} {} } else { - {} - } + {} + } } } diff --git a/compiler/testData/diagnostics/tests/smartCasts/publicVals/delegate.fir.kt b/compiler/testData/diagnostics/tests/smartCasts/publicVals/delegate.fir.kt index ee146be949d..955a51c05b7 100644 --- a/compiler/testData/diagnostics/tests/smartCasts/publicVals/delegate.fir.kt +++ b/compiler/testData/diagnostics/tests/smartCasts/publicVals/delegate.fir.kt @@ -13,7 +13,7 @@ class Example { public fun foo(): String { // Smart cast is not possible if property is delegated - return if (p != null) p else "" + return if (p != null) p else "" } public fun bar(): String { diff --git a/compiler/testData/diagnostics/tests/typeParameters/implicitNothingInReturnPosition.fir.kt b/compiler/testData/diagnostics/tests/typeParameters/implicitNothingInReturnPosition.fir.kt index ebe07df208b..d7962a6e56e 100644 --- a/compiler/testData/diagnostics/tests/typeParameters/implicitNothingInReturnPosition.fir.kt +++ b/compiler/testData/diagnostics/tests/typeParameters/implicitNothingInReturnPosition.fir.kt @@ -39,7 +39,7 @@ class Context fun Any.decodeIn(typeFrom: Context): T = something() fun Any?.decodeOut1(typeFrom: Context): T { - return this?.decodeIn(typeFrom) ?: kotlin.Unit + return this?.decodeIn(typeFrom) ?: kotlin.Unit } fun Any.decodeOut2(typeFrom: Context): T { diff --git a/compiler/testData/diagnostics/testsWithStdLib/elvisOnJavaList.fir.kt b/compiler/testData/diagnostics/testsWithStdLib/elvisOnJavaList.fir.kt index 82516302fb1..bb08afc84d9 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/elvisOnJavaList.fir.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/elvisOnJavaList.fir.kt @@ -14,5 +14,5 @@ public class P { fun foo(c: P): MutableList { // Error should be here: see KT-8168 Typechecker fails for platform collection type - return c.getList() ?: listOf() + return c.getList() ?: listOf() } diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index fef52dba6c6..f5c28deb708 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -6566,6 +6566,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/controlStructures/improperElseInExpression.kt"); } + @Test + @TestMetadata("incorrectElvis.kt") + public void testIncorrectElvis() throws Exception { + runTest("compiler/testData/diagnostics/tests/controlStructures/incorrectElvis.kt"); + } + @Test @TestMetadata("jumpAcrossFunctionBoundary.kt") public void testJumpAcrossFunctionBoundary() throws Exception {