From 33f9576dd1785bbac5225b05fd3fef212a3ca780 Mon Sep 17 00:00:00 2001 From: Dmitry Savvinov Date: Thu, 7 Dec 2017 13:18:24 +0300 Subject: [PATCH] [NI] Turn off KnownTypeParameterSubstitutor for NI The main consequence of it is that TYPE_MISMATCH range for control structures became wider. Also, for extra safety, don't change behaviour of OI. --- .../ControlStructureTypingUtils.java | 4 +- .../diagnostics/tests/FunctionReturnTypes.kt | 10 ++--- .../controlStructures/ifElseIntersection.kt | 6 +-- .../tests/controlStructures/kt4310.kt | 4 +- .../specialConstructsAndPlatformTypes.kt | 4 +- .../dataFlowInfoTraversal/ObjectExpression.kt | 4 +- .../tests/dataFlowInfoTraversal/Return.kt | 2 +- .../tests/exposed/inaccessibleType.kt | 7 ++-- .../diagnostics/tests/implicitIntersection.kt | 4 +- .../diagnostics/tests/infos/SmartCasts.kt | 6 +-- .../tests/nullabilityAndSmartCasts/kt2146.kt | 37 ++++++++++--------- .../tests/smartCasts/publicVals/delegate.kt | 2 +- .../smartCasts/variables/ifNullAssignment.kt | 2 +- .../testData/diagnostics/tests/when/kt9929.kt | 6 +-- .../testData/diagnostics/tests/when/kt9972.kt | 4 +- 15 files changed, 53 insertions(+), 49 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ControlStructureTypingUtils.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ControlStructureTypingUtils.java index 4fee2d77e31..c271287a364 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ControlStructureTypingUtils.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ControlStructureTypingUtils.java @@ -133,8 +133,8 @@ public class ControlStructureTypingUtils { @NotNull KotlinType expectedType, @NotNull LanguageVersionSettings languageVersionSettings ) { - if (construct == ResolveConstruct.ELVIS - || construct == ResolveConstruct.EXCL_EXCL + if (languageVersionSettings.supportsFeature(LanguageFeature.NewInference) + || construct == ResolveConstruct.ELVIS || TypeUtils.noExpectedType(expectedType) || TypeUtils.isDontCarePlaceholder(expectedType) || KotlinBuiltIns.isUnitOrNullableUnit(expectedType) diff --git a/compiler/testData/diagnostics/tests/FunctionReturnTypes.kt b/compiler/testData/diagnostics/tests/FunctionReturnTypes.kt index 553ac467aff..ba1eae2ef4a 100644 --- a/compiler/testData/diagnostics/tests/FunctionReturnTypes.kt +++ b/compiler/testData/diagnostics/tests/FunctionReturnTypes.kt @@ -67,7 +67,7 @@ fun blockAndAndMismatch5() : Int { (return true) || (return false) } fun blockReturnValueTypeMatch1() : Int { - return if (1 > 2) 1.0 else 2.0 + return if (1 > 2) 1.0 else 2.0 } fun blockReturnValueTypeMatch2() : Int { return if (1 > 2) 1 @@ -134,8 +134,8 @@ fun blockNoReturnIfUnitInOneBranch(): Int { } } } -fun nonBlockReturnIfEmptyIf(): Int = if (1 < 2) {} else {} -fun nonBlockNoReturnIfUnitInOneBranch(): Int = if (1 < 2) {} else 2 +fun nonBlockReturnIfEmptyIf(): Int = if (1 < 2) {} else {} +fun nonBlockNoReturnIfUnitInOneBranch(): Int = if (1 < 2) {} else 2 val a = return 1 @@ -146,14 +146,14 @@ fun illegalConstantBlock(): String { return 1 } fun illegalIfBody(): Int = - if (1 < 2) 'a' else { 1.0 } + if (1 < 2) 'a' else { 1.0 } fun illegalIfBlock(): Boolean { if (1 < 2) return false 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.kt b/compiler/testData/diagnostics/tests/controlStructures/ifElseIntersection.kt index 0426df66121..c9565393ff3 100644 --- a/compiler/testData/diagnostics/tests/controlStructures/ifElseIntersection.kt +++ b/compiler/testData/diagnostics/tests/controlStructures/ifElseIntersection.kt @@ -25,11 +25,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/kt4310.kt b/compiler/testData/diagnostics/tests/controlStructures/kt4310.kt index 90d1e3ba2f7..761fd6a0092 100644 --- a/compiler/testData/diagnostics/tests/controlStructures/kt4310.kt +++ b/compiler/testData/diagnostics/tests/controlStructures/kt4310.kt @@ -2,9 +2,9 @@ package f fun test(a: Boolean, b: Boolean): Int { - return if(a) { + return if(a) { 1 - } else { + } else { if (b) { 3 } diff --git a/compiler/testData/diagnostics/tests/controlStructures/specialConstructsAndPlatformTypes.kt b/compiler/testData/diagnostics/tests/controlStructures/specialConstructsAndPlatformTypes.kt index a4015e3306e..5f6ffc58b5a 100644 --- a/compiler/testData/diagnostics/tests/controlStructures/specialConstructsAndPlatformTypes.kt +++ b/compiler/testData/diagnostics/tests/controlStructures/specialConstructsAndPlatformTypes.kt @@ -30,11 +30,11 @@ val testSafeCall4: String? = J.m[""]?.let { it.toString() } val testIf1: String = if (true) J.s else J.s val testIf2: String? = if (true) J.s else J.s -val testIf3: String = if (true) J.m[""] else J.m[""] +val testIf3: String = if (true) J.m[""] else J.m[""] val testIf4: String? = if (true) J.m[""] else J.m[""] val testWhen1: String = when { else -> J.s } val testWhen2: String? = when { else -> J.s } -val testWhen3: String = when { else -> J.m[""] } +val testWhen3: String = when { else -> J.m[""] } val testWhen4: String? = when { else -> J.m[""] } diff --git a/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/ObjectExpression.kt b/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/ObjectExpression.kt index d73a059aa74..6b334509680 100644 --- a/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/ObjectExpression.kt +++ b/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/ObjectExpression.kt @@ -1,3 +1,5 @@ +// !WITH_NEW_INFERENCE + fun bar(x: Int): Int = x + 1 fun foo() { @@ -5,6 +7,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/dataFlowInfoTraversal/Return.kt b/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/Return.kt index 526dc02d163..4f40d73a77c 100644 --- a/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/Return.kt +++ b/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/Return.kt @@ -9,7 +9,7 @@ fun foo(): Int { if (x != null) return x val y: Int? = null - if (y == null) return if (y != null) y else y + if (y == null) return if (y != null) y else y val z: Int? = null if (z != null) return if (z == null) z else z diff --git a/compiler/testData/diagnostics/tests/exposed/inaccessibleType.kt b/compiler/testData/diagnostics/tests/exposed/inaccessibleType.kt index c74cc2020e7..842fef83759 100644 --- a/compiler/testData/diagnostics/tests/exposed/inaccessibleType.kt +++ b/compiler/testData/diagnostics/tests/exposed/inaccessibleType.kt @@ -1,4 +1,5 @@ // !DIAGNOSTICS: -USELESS_CAST -UNUSED_PARAMETER -UNUSED_VARIABLE +// !WITH_NEW_INFERENCE // FILE: j/Base.java package j; @@ -38,7 +39,7 @@ fun listOf2(x1: T, x2: T): List = null!! fun arrayOf2(x1: T, x2: T): Array = null!! fun test() { - val test1: Base = if (true) d1 else d2 + val test1: Base = if (true) d1 else d2 val test2 = if (true) d1 else d2 @@ -47,10 +48,10 @@ fun test() { else -> d2 } - val test4: Base = when { + val test4: Base = when { true -> d1 else -> d2 - } + } val test5 = select(d1, d2) diff --git a/compiler/testData/diagnostics/tests/implicitIntersection.kt b/compiler/testData/diagnostics/tests/implicitIntersection.kt index 590eef5ab5b..123cf12a936 100644 --- a/compiler/testData/diagnostics/tests/implicitIntersection.kt +++ b/compiler/testData/diagnostics/tests/implicitIntersection.kt @@ -10,13 +10,13 @@ interface C fun foo(b: B) = if (b is A && b is C) b else null // Ok: given explicitly -fun gav(b: B): A? = if (b is A && b is C) b else null +fun gav(b: B): A? = if (b is A && b is C) b else null class My(b: B) { // Error! val x = if (b is A && b is C) b else null // Ok: given explicitly - val y: C? = if (b is A && b is C) b else null + val y: C? = if (b is A && b is C) b else null // Error! fun foo(b: B) = if (b is A && b is C) b else null } diff --git a/compiler/testData/diagnostics/tests/infos/SmartCasts.kt b/compiler/testData/diagnostics/tests/infos/SmartCasts.kt index 44c4bb3c84a..806ad2c3d81 100644 --- a/compiler/testData/diagnostics/tests/infos/SmartCasts.kt +++ b/compiler/testData/diagnostics/tests/infos/SmartCasts.kt @@ -142,10 +142,10 @@ fun getStringLength(obj : Any) : Char? { } fun toInt(i: Int?): Int = if (i != null) i else 0 -fun illegalWhenBody(a: Any): Int = when(a) { +fun illegalWhenBody(a: Any): Int = when(a) { is Int -> a - is String -> a -} + is String -> a +} fun illegalWhenBlock(a: Any): Int { when(a) { is Int -> return a diff --git a/compiler/testData/diagnostics/tests/nullabilityAndSmartCasts/kt2146.kt b/compiler/testData/diagnostics/tests/nullabilityAndSmartCasts/kt2146.kt index 47964ee6e2c..aa12cbaf575 100644 --- a/compiler/testData/diagnostics/tests/nullabilityAndSmartCasts/kt2146.kt +++ b/compiler/testData/diagnostics/tests/nullabilityAndSmartCasts/kt2146.kt @@ -1,3 +1,4 @@ +// !WITH_NEW_INFERENCE //KT-2146 Nullability casts in when. package kt2146 @@ -9,46 +10,46 @@ fun f1(s: Int?): Int { } fun f2(s: Int?): Int { - return when (s) { - !is Int -> 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 - } + else -> s + } } fun f4(s: Int?): Int { - return when { + return when { s == 4 -> s - s == null -> s + s == null -> s else -> s - } + } } fun f5(s: Int?): Int { - return when (s) { - s -> 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 - } + else -> s + } } fun f7(s: Int?): Int { - return when { - s !is Int -> s + return when { + s !is Int -> s else -> s - } + } } diff --git a/compiler/testData/diagnostics/tests/smartCasts/publicVals/delegate.kt b/compiler/testData/diagnostics/tests/smartCasts/publicVals/delegate.kt index 0a46463872b..508bd95f235 100644 --- a/compiler/testData/diagnostics/tests/smartCasts/publicVals/delegate.kt +++ b/compiler/testData/diagnostics/tests/smartCasts/publicVals/delegate.kt @@ -14,7 +14,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/smartCasts/variables/ifNullAssignment.kt b/compiler/testData/diagnostics/tests/smartCasts/variables/ifNullAssignment.kt index b4c09df476a..50ec836f0c6 100644 --- a/compiler/testData/diagnostics/tests/smartCasts/variables/ifNullAssignment.kt +++ b/compiler/testData/diagnostics/tests/smartCasts/variables/ifNullAssignment.kt @@ -3,7 +3,7 @@ fun basic(): String { var current: String? = null - current = if (current == null) "bar" else current + current = if (current == null) "bar" else current return current } diff --git a/compiler/testData/diagnostics/tests/when/kt9929.kt b/compiler/testData/diagnostics/tests/when/kt9929.kt index 9d6bbf47c28..c94ac61f194 100644 --- a/compiler/testData/diagnostics/tests/when/kt9929.kt +++ b/compiler/testData/diagnostics/tests/when/kt9929.kt @@ -1,10 +1,10 @@ // !WITH_NEW_INFERENCE -val test: Int = if (true) { +val test: Int = if (true) { when (2) { 1 -> 1 else -> null } -} +} else { 2 -} \ No newline at end of file +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/when/kt9972.kt b/compiler/testData/diagnostics/tests/when/kt9972.kt index 59c73aad218..c2a485d038c 100644 --- a/compiler/testData/diagnostics/tests/when/kt9972.kt +++ b/compiler/testData/diagnostics/tests/when/kt9972.kt @@ -1,11 +1,11 @@ // !WITH_NEW_INFERENCE fun test1(): Int { - val x: String = if (true) { + val x: String = if (true) { when { true -> Any() else -> null } - } else "" + } else "" return x.hashCode() }