K2: Fix false-negative RETURN_TYPE_MISMATCH

^KT-53987 Fixed
^KT-55932 Fixed
This commit is contained in:
Denis.Zharkov
2022-11-29 12:54:13 +01:00
committed by Space Team
parent d7399ed1cf
commit 9fa0f51a61
28 changed files with 101 additions and 46 deletions
@@ -11,7 +11,7 @@ FILE: class.kt
private [BODY_RESOLVE] get(): <ERROR TYPE REF: Symbol not found for C>
public final [BODY_RESOLVE] fun foo([BODY_RESOLVE] a: <ERROR TYPE REF: Symbol not found for A>): <ERROR TYPE REF: Cannot infer argument for type parameter R> {
^foo R|kotlin/with<CS errors: kotlin/with>#|<<ERROR TYPE REF: Cannot infer argument for type parameter T>, <ERROR TYPE REF: Cannot infer argument for type parameter R>>(R|<local>/a|, <L> = [BODY_RESOLVE] with@fun <ERROR TYPE REF: Cannot infer argument for type parameter T>.<anonymous>(): <ERROR TYPE REF: Cannot infer argument for type parameter R> <inline=Inline, kind=EXACTLY_ONCE> {
^foo R|kotlin/with<Inapplicable(INAPPLICABLE): kotlin/with>#|<<ERROR TYPE REF: Cannot infer argument for type parameter T>, <ERROR TYPE REF: Cannot infer argument for type parameter R>>(R|<local>/a|, <L> = [BODY_RESOLVE] with@fun <ERROR TYPE REF: Cannot infer argument for type parameter T>.<anonymous>(): <ERROR TYPE REF: Cannot infer argument for type parameter R> <inline=Unknown, kind=EXACTLY_ONCE> {
^ <Unresolved name: bar>#(String(a), this@R|/B|.R|/B.y|)
}
)
@@ -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 {
@@ -180,7 +180,7 @@ FILE: returnTypeMismatchOnOverride.kt
}
public open fun kek(): R|Z| {
^kek R|/Z.Z<CS errors: /Z.Z>#|()
^kek R|/Z.Z|()
}
}
@@ -1,5 +1,5 @@
FILE: mutableList.kt
public final fun foo(): R|kotlin/Unit| {
lvar listVar: R|kotlin/collections/MutableList<kotlin/Int>| = R|kotlin/collections/mutableListOf|<R|kotlin/Int|>(vararg(Int(1), Int(2), Int(3)))
R|<local>/listVar| = R|<local>/listVar|.R|kotlin/collections/plus<CS errors: kotlin/collections/plus>#|<R|kotlin/Int|>(Int(4))
R|<local>/listVar| = R|<local>/listVar|.R|kotlin/collections/plus|<R|kotlin/Int|>(Int(4))
}
+1 -2
View File
@@ -2,7 +2,6 @@ fun <T> bar(): T {
return null <!UNCHECKED_CAST!>as T<!>
}
class X() : <!UNRESOLVED_REFERENCE, UNRESOLVED_REFERENCE!>B<!> by <!ASSIGNMENT_IN_EXPRESSION_CONTEXT!><!VARIABLE_EXPECTED!><!UNRESOLVED_REFERENCE!>get<!>()<!> = bar()<!> {
class X() : <!UNRESOLVED_REFERENCE, UNRESOLVED_REFERENCE!>B<!> by <!ASSIGNMENT_IN_EXPRESSION_CONTEXT!><!VARIABLE_EXPECTED!><!UNRESOLVED_REFERENCE!>get<!>()<!> = <!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>bar<!>()<!> {
val prop = <!ASSIGNMENT_IN_EXPRESSION_CONTEXT!><!VARIABLE_EXPECTED!><!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>bar<!>()<!> = 2<!>
}
@@ -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<CS errors: 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| {
@@ -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 <!RETURN_TYPE_MISMATCH!>if (1 < 2) 'a' else { 1 }<!>
}
fun foo(): String {
@@ -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 {
@@ -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 {
@@ -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
@@ -166,30 +166,35 @@ class FirCallCompleter(
mayBeCoercionToUnitApplied: Boolean
) {
val expectedType = expectedTypeRef?.coneTypeSafe<ConeKotlinType>() ?: 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)
}
}
}
@@ -18,9 +18,7 @@ class ConeFixVariableConstraintPosition(variable: TypeVariableMarker) : FixVaria
class ConeArgumentConstraintPosition(argument: FirElement) : ArgumentConstraintPosition<FirElement>(argument)
class ConeExpectedTypeConstraintPosition(
val expectedTypeMismatchIsReportedInChecker: Boolean
) : ExpectedTypeConstraintPosition<Nothing?>(null)
object ConeExpectedTypeConstraintPosition : ExpectedTypeConstraintPosition<Nothing?>(null)
class ConeExplicitTypeParameterConstraintPosition(
typeArgument: FirTypeProjection,
@@ -152,7 +152,7 @@ fun illegalIfBlock(): Boolean {
else { return <!RETURN_TYPE_MISMATCH!>1<!> }
}
fun illegalReturnIf(): Char {
return if (1 < 2) 'a' else { 1 }
return <!RETURN_TYPE_MISMATCH!>if (1 < 2) 'a' else { 1 }<!>
}
fun returnNothing(): Nothing {
@@ -24,11 +24,11 @@ fun <T> bind2(r: Option<T>): Option<T> {
}
fun <T, R> bind3(r: Option<T>): Option<T> {
return if (r is Some) {
return <!RETURN_TYPE_MISMATCH!>if (r is Some) {
// Diagnoses an error correctly
if (true) None<R>() else r
}
else r
else r<!>
}
fun <T> bindWhen(r: Option<T>): Option<T> {
@@ -0,0 +1,4 @@
// SKIP_TXT
// ISSUE: KT-55932
fun test(x: String?): Int = <!RETURN_TYPE_MISMATCH!>x?.length ?: "smth"<!>
@@ -0,0 +1,4 @@
// SKIP_TXT
// ISSUE: KT-55932
fun test(x: String?): Int = <!TYPE_MISMATCH!>x?.length ?: "smth"<!>
@@ -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 = <!RETURN_TYPE_MISMATCH!>if (x == null) x else x<!>
}
}
@@ -24,4 +24,4 @@ class C<R>() {
}
var c1: Int by C()
var c2: Int by <!DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE!>C<Number>()<!>
var c2: Int by <!DELEGATE_SPECIAL_FUNCTION_RETURN_TYPE_MISMATCH!>C<Number>()<!>
@@ -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 <!DELEGATE_SPECIAL_FUNCTION_RETURN_TYPE_MISMATCH!>"OK"<!>
val test3 by "OK"
@@ -1,4 +1,3 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_PARAMETER
import kotlin.reflect.KProperty
@@ -0,0 +1,11 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
import kotlin.reflect.KProperty
val c: Int by <!DELEGATE_SPECIAL_FUNCTION_RETURN_TYPE_MISMATCH!>Delegate()<!>
class Delegate {
operator fun getValue(t: Any?, p: KProperty<*>): String {
return ""
}
}
@@ -1,4 +1,3 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_PARAMETER
import kotlin.reflect.KProperty
@@ -9,46 +9,46 @@ fun f1(s: Int?): Int {
}
fun f2(s: Int?): Int {
return when (s) {
return <!RETURN_TYPE_MISMATCH!>when (s) {
!is Int -> s
else -> s
}
}<!>
}
fun f3(s: Int?): Int {
return when (s) {
return <!RETURN_TYPE_MISMATCH!>when (s) {
is Int -> s
else -> s
}
}<!>
}
fun f4(s: Int?): Int {
return when {
return <!RETURN_TYPE_MISMATCH!>when {
s == 4 -> s
s == null -> s
else -> s
}
}<!>
}
fun f5(s: Int?): Int {
return when (s) {
return <!RETURN_TYPE_MISMATCH!>when (s) {
s -> s
s!! -> s
s -> s
else -> 0
}
}<!>
}
fun f6(s: Int?): Int {
return when {
return <!RETURN_TYPE_MISMATCH!>when {
s is Int -> s
else -> s
}
}<!>
}
fun f7(s: Int?): Int {
return when {
return <!RETURN_TYPE_MISMATCH!>when {
s !is Int -> s
else -> s
}
}<!>
}
@@ -99,13 +99,13 @@ fun testTwoLambdas() {
{}
<!MANY_LAMBDA_EXPRESSION_ARGUMENTS!>{}<!>
return if (true) {
return <!RETURN_TYPE_MISMATCH!>if (true) {
twoLambdaArgs({})
{}
<!MANY_LAMBDA_EXPRESSION_ARGUMENTS!>{}<!>
} else {
<!ARGUMENT_TYPE_MISMATCH!>{}<!>
}
{}
}<!>
}
}
@@ -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 <!RETURN_TYPE_MISMATCH!>if (p != null) p else ""<!>
}
public fun bar(): String {
@@ -39,7 +39,7 @@ class Context<T>
fun <T> Any.decodeIn(typeFrom: Context<in T>): T = something()
fun <T> Any?.decodeOut1(typeFrom: Context<out T>): T {
return this?.decodeIn(typeFrom) ?: kotlin.Unit
return <!RETURN_TYPE_MISMATCH!>this?.decodeIn(typeFrom) ?: kotlin.Unit<!>
}
fun <T> Any.decodeOut2(typeFrom: Context<out T>): T {
@@ -14,5 +14,5 @@ public class P {
fun foo(c: P): MutableList<Int> {
// Error should be here: see KT-8168 Typechecker fails for platform collection type
return c.getList() ?: <!TYPE_MISMATCH!>listOf()<!>
return <!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>c.getList() ?: <!TYPE_MISMATCH!>listOf()<!><!>
}
@@ -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 {