Deprecate simplification of complex boolean constant expressions in whens and loops
^KT-39883 In Progress
This commit is contained in:
committed by
teamcityserver
parent
8a2e0cedf9
commit
05883afc0a
@@ -1,4 +1,5 @@
|
||||
// FIR_IDENTICAL
|
||||
// LANGUAGE: +ProhibitSimplificationOfNonTrivialConstBooleanExpressions
|
||||
fun test() {
|
||||
@ann
|
||||
while (2 < 1) {}
|
||||
@@ -10,4 +11,4 @@ fun test() {
|
||||
for (i in 1..2) {}
|
||||
}
|
||||
|
||||
annotation class ann
|
||||
annotation class ann
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// LANGUAGE: -ProhibitSimplificationOfNonTrivialConstBooleanExpressions
|
||||
fun test() {
|
||||
@ann
|
||||
while (2 > 1) {}
|
||||
@@ -9,4 +10,4 @@ fun test() {
|
||||
for (i in 1..2) {}
|
||||
}
|
||||
|
||||
annotation class ann
|
||||
annotation class ann
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
// LANGUAGE: -ProhibitSimplificationOfNonTrivialConstBooleanExpressions
|
||||
fun test() {
|
||||
@ann
|
||||
while (2 > 1) {}
|
||||
while (<!NON_TRIVIAL_BOOLEAN_CONSTANT!>2 > 1<!>) {}
|
||||
|
||||
@ann
|
||||
<!UNREACHABLE_CODE!>do {} while (2 > 1)<!>
|
||||
<!UNREACHABLE_CODE!>do {} while (<!NON_TRIVIAL_BOOLEAN_CONSTANT!>2 > 1<!>)<!>
|
||||
|
||||
@ann
|
||||
<!UNREACHABLE_CODE!>for (i in 1..2) {}<!>
|
||||
}
|
||||
|
||||
annotation class ann
|
||||
annotation class ann
|
||||
|
||||
Vendored
+1
@@ -1,3 +1,4 @@
|
||||
// LANGUAGE: -ProhibitSimplificationOfNonTrivialConstBooleanExpressions
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_EXPRESSION
|
||||
|
||||
fun t1() : Int{
|
||||
|
||||
Vendored
+4
-3
@@ -1,3 +1,4 @@
|
||||
// LANGUAGE: -ProhibitSimplificationOfNonTrivialConstBooleanExpressions
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_EXPRESSION
|
||||
|
||||
fun t1() : Int{
|
||||
@@ -68,12 +69,12 @@ fun t5() : Int {
|
||||
return 1
|
||||
<!UNREACHABLE_CODE!>2<!>
|
||||
}
|
||||
while (<!UNREACHABLE_CODE!>1 > 2<!>)
|
||||
while (<!NON_TRIVIAL_BOOLEAN_CONSTANT, UNREACHABLE_CODE!>1 > 2<!>)
|
||||
<!UNREACHABLE_CODE!>return 1<!>
|
||||
}
|
||||
|
||||
fun t6() : Int {
|
||||
while (1 > 2) {
|
||||
while (<!NON_TRIVIAL_BOOLEAN_CONSTANT!>1 > 2<!>) {
|
||||
return 1
|
||||
<!UNREACHABLE_CODE!>2<!>
|
||||
}
|
||||
@@ -81,7 +82,7 @@ fun t6() : Int {
|
||||
}
|
||||
|
||||
fun t6break() : Int {
|
||||
while (1 > 2) {
|
||||
while (<!NON_TRIVIAL_BOOLEAN_CONSTANT!>1 > 2<!>) {
|
||||
break
|
||||
<!UNREACHABLE_CODE!>2<!>
|
||||
}
|
||||
|
||||
+2
-1
@@ -1,3 +1,4 @@
|
||||
// LANGUAGE: -ProhibitSimplificationOfNonTrivialConstBooleanExpressions
|
||||
fun unreachable() {}
|
||||
|
||||
fun a() {
|
||||
@@ -52,4 +53,4 @@ fun h(): Int {
|
||||
while (true) {
|
||||
if (true) return 12
|
||||
}
|
||||
} // should work
|
||||
} // should work
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// LANGUAGE: -ProhibitSimplificationOfNonTrivialConstBooleanExpressions
|
||||
fun unreachable() {}
|
||||
|
||||
fun a() {
|
||||
@@ -13,11 +14,11 @@ fun b() {
|
||||
}
|
||||
|
||||
fun c() {
|
||||
do {} while (1 == 1)
|
||||
do {} while (<!NON_TRIVIAL_BOOLEAN_CONSTANT!>1 == 1<!>)
|
||||
}
|
||||
|
||||
fun d() {
|
||||
while (2 == 2) {}
|
||||
while (<!NON_TRIVIAL_BOOLEAN_CONSTANT!>2 == 2<!>) {}
|
||||
}
|
||||
|
||||
fun use(arg: Any) = arg
|
||||
@@ -52,4 +53,4 @@ fun h(): Int {
|
||||
while (true) {
|
||||
if (true) return 12
|
||||
}
|
||||
} // should work
|
||||
} // should work
|
||||
|
||||
Vendored
+72
@@ -0,0 +1,72 @@
|
||||
// LANGUAGE: +ProhibitSimplificationOfNonTrivialConstBooleanExpressions
|
||||
// DIAGNOSTICS: -UNUSED_VARIABLE
|
||||
|
||||
fun test_1() {
|
||||
while (true) {
|
||||
|
||||
}
|
||||
val x = 1
|
||||
}
|
||||
|
||||
fun test_2() {
|
||||
while (true || false) {
|
||||
|
||||
}
|
||||
val x = 1
|
||||
}
|
||||
|
||||
fun test_3() {
|
||||
while (1 == 1) {
|
||||
|
||||
}
|
||||
val x = 1
|
||||
}
|
||||
|
||||
fun test_4() {
|
||||
while (false) {
|
||||
val x = 1
|
||||
}
|
||||
val y = 2
|
||||
}
|
||||
|
||||
fun test_5() {
|
||||
while (false && true) {
|
||||
val x = 1
|
||||
}
|
||||
val y = 2
|
||||
}
|
||||
|
||||
fun test_6() {
|
||||
do {
|
||||
|
||||
} while (true)
|
||||
val x = 1
|
||||
}
|
||||
|
||||
fun test_7() {
|
||||
do {
|
||||
|
||||
} while (true || false)
|
||||
val x = 1
|
||||
}
|
||||
|
||||
fun test_8() {
|
||||
do {
|
||||
|
||||
} while (1 == 1)
|
||||
val x = 1
|
||||
}
|
||||
|
||||
fun test_9() {
|
||||
do {
|
||||
val x = 1
|
||||
} while (false)
|
||||
val y = 2
|
||||
}
|
||||
|
||||
fun test_10() {
|
||||
do {
|
||||
val x = 1
|
||||
} while (false && true)
|
||||
val y = 2
|
||||
}
|
||||
Vendored
+73
@@ -0,0 +1,73 @@
|
||||
// LANGUAGE: +ProhibitSimplificationOfNonTrivialConstBooleanExpressions
|
||||
// DIAGNOSTICS: -UNUSED_VARIABLE
|
||||
|
||||
fun test_1() {
|
||||
while (true) {
|
||||
|
||||
}
|
||||
<!UNREACHABLE_CODE!>val x = 1<!>
|
||||
}
|
||||
|
||||
fun test_2() {
|
||||
while (true || false) {
|
||||
|
||||
}
|
||||
val x = 1
|
||||
}
|
||||
|
||||
fun test_3() {
|
||||
while (1 == 1) {
|
||||
|
||||
}
|
||||
val x = 1
|
||||
}
|
||||
|
||||
fun test_4() {
|
||||
while (false) {
|
||||
val x = 1
|
||||
}
|
||||
val y = 2
|
||||
}
|
||||
|
||||
fun test_5() {
|
||||
while (false && true) {
|
||||
val x = 1
|
||||
}
|
||||
val y = 2
|
||||
}
|
||||
|
||||
fun test_6() {
|
||||
do {
|
||||
|
||||
} while (true)
|
||||
<!UNREACHABLE_CODE!>val x = 1<!>
|
||||
}
|
||||
|
||||
fun test_7() {
|
||||
do {
|
||||
|
||||
} while (true || false)
|
||||
val x = 1
|
||||
}
|
||||
|
||||
fun test_8() {
|
||||
do {
|
||||
|
||||
} while (1 == 1)
|
||||
val x = 1
|
||||
}
|
||||
|
||||
fun test_9() {
|
||||
do {
|
||||
val x = 1
|
||||
} while (false)
|
||||
val y = 2
|
||||
}
|
||||
|
||||
fun test_10() {
|
||||
do {
|
||||
val x = 1
|
||||
} while (false && true)
|
||||
val y = 2
|
||||
}
|
||||
|
||||
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
package
|
||||
|
||||
public fun test_1(): kotlin.Unit
|
||||
public fun test_10(): kotlin.Unit
|
||||
public fun test_2(): kotlin.Unit
|
||||
public fun test_3(): kotlin.Unit
|
||||
public fun test_4(): kotlin.Unit
|
||||
public fun test_5(): kotlin.Unit
|
||||
public fun test_6(): kotlin.Unit
|
||||
public fun test_7(): kotlin.Unit
|
||||
public fun test_8(): kotlin.Unit
|
||||
public fun test_9(): kotlin.Unit
|
||||
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
// LANGUAGE: -ProhibitSimplificationOfNonTrivialConstBooleanExpressions
|
||||
// DIAGNOSTICS: -UNUSED_VARIABLE
|
||||
|
||||
fun test_1() {
|
||||
while (true) {
|
||||
|
||||
}
|
||||
val x = 1
|
||||
}
|
||||
|
||||
fun test_2() {
|
||||
while (true || false) {
|
||||
|
||||
}
|
||||
val x = 1
|
||||
}
|
||||
|
||||
fun test_3() {
|
||||
while (1 == 1) {
|
||||
|
||||
}
|
||||
val x = 1
|
||||
}
|
||||
|
||||
fun test_4() {
|
||||
while (false) {
|
||||
val x = 1
|
||||
}
|
||||
val y = 2
|
||||
}
|
||||
|
||||
fun test_5() {
|
||||
while (false && true) {
|
||||
val x = 1
|
||||
}
|
||||
val y = 2
|
||||
}
|
||||
|
||||
fun test_6() {
|
||||
do {
|
||||
|
||||
} while (true)
|
||||
val x = 1
|
||||
}
|
||||
|
||||
fun test_7() {
|
||||
do {
|
||||
|
||||
} while (true || false)
|
||||
val x = 1
|
||||
}
|
||||
|
||||
fun test_8() {
|
||||
do {
|
||||
|
||||
} while (1 == 1)
|
||||
val x = 1
|
||||
}
|
||||
|
||||
fun test_9() {
|
||||
do {
|
||||
val x = 1
|
||||
} while (false)
|
||||
val y = 2
|
||||
}
|
||||
|
||||
fun test_10() {
|
||||
do {
|
||||
val x = 1
|
||||
} while (false && true)
|
||||
val y = 2
|
||||
}
|
||||
Vendored
+72
@@ -0,0 +1,72 @@
|
||||
// LANGUAGE: -ProhibitSimplificationOfNonTrivialConstBooleanExpressions
|
||||
// DIAGNOSTICS: -UNUSED_VARIABLE
|
||||
|
||||
fun test_1() {
|
||||
while (true) {
|
||||
|
||||
}
|
||||
<!UNREACHABLE_CODE!>val x = 1<!>
|
||||
}
|
||||
|
||||
fun test_2() {
|
||||
while (<!NON_TRIVIAL_BOOLEAN_CONSTANT!>true || false<!>) {
|
||||
|
||||
}
|
||||
<!UNREACHABLE_CODE!>val x = 1<!>
|
||||
}
|
||||
|
||||
fun test_3() {
|
||||
while (<!NON_TRIVIAL_BOOLEAN_CONSTANT!>1 == 1<!>) {
|
||||
|
||||
}
|
||||
<!UNREACHABLE_CODE!>val x = 1<!>
|
||||
}
|
||||
|
||||
fun test_4() {
|
||||
while (false) {
|
||||
val x = 1
|
||||
}
|
||||
val y = 2
|
||||
}
|
||||
|
||||
fun test_5() {
|
||||
while (<!NON_TRIVIAL_BOOLEAN_CONSTANT!>false && true<!>) {
|
||||
val x = 1
|
||||
}
|
||||
val y = 2
|
||||
}
|
||||
|
||||
fun test_6() {
|
||||
do {
|
||||
|
||||
} while (true)
|
||||
<!UNREACHABLE_CODE!>val x = 1<!>
|
||||
}
|
||||
|
||||
fun test_7() {
|
||||
do {
|
||||
|
||||
} while (<!NON_TRIVIAL_BOOLEAN_CONSTANT!>true || false<!>)
|
||||
<!UNREACHABLE_CODE!>val x = 1<!>
|
||||
}
|
||||
|
||||
fun test_8() {
|
||||
do {
|
||||
|
||||
} while (<!NON_TRIVIAL_BOOLEAN_CONSTANT!>1 == 1<!>)
|
||||
<!UNREACHABLE_CODE!>val x = 1<!>
|
||||
}
|
||||
|
||||
fun test_9() {
|
||||
do {
|
||||
val x = 1
|
||||
} while (false)
|
||||
val y = 2
|
||||
}
|
||||
|
||||
fun test_10() {
|
||||
do {
|
||||
val x = 1
|
||||
} while (<!NON_TRIVIAL_BOOLEAN_CONSTANT!>false && true<!>)
|
||||
val y = 2
|
||||
}
|
||||
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
package
|
||||
|
||||
public fun test_1(): kotlin.Unit
|
||||
public fun test_10(): kotlin.Unit
|
||||
public fun test_2(): kotlin.Unit
|
||||
public fun test_3(): kotlin.Unit
|
||||
public fun test_4(): kotlin.Unit
|
||||
public fun test_5(): kotlin.Unit
|
||||
public fun test_6(): kotlin.Unit
|
||||
public fun test_7(): kotlin.Unit
|
||||
public fun test_8(): kotlin.Unit
|
||||
public fun test_9(): kotlin.Unit
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// LANGUAGE: -ProhibitSimplificationOfNonTrivialConstBooleanExpressions
|
||||
fun x(): Boolean { return true }
|
||||
|
||||
public fun foo(p: String?): Int {
|
||||
// Like whileTrue but 2 == 2 is in use
|
||||
while(2 == 2) {
|
||||
p!!.length
|
||||
if (x()) break
|
||||
}
|
||||
// Smart cast should not work in this case, see KT-6284
|
||||
return p<!UNSAFE_CALL!>.<!>length
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
// FIR_IDENTICAL
|
||||
// LANGUAGE: -ProhibitSimplificationOfNonTrivialConstBooleanExpressions
|
||||
fun x(): Boolean { return true }
|
||||
|
||||
public fun foo(p: String?): Int {
|
||||
// Like whileTrue but 2 == 2 is in use
|
||||
while(2 == 2) {
|
||||
while(<!NON_TRIVIAL_BOOLEAN_CONSTANT!>2 == 2<!>) {
|
||||
p!!.length
|
||||
if (x()) break
|
||||
}
|
||||
// Smart cast should not work in this case, see KT-6284
|
||||
return p<!UNSAFE_CALL!>.<!>length
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
/*
|
||||
* KOTLIN DIAGNOSTICS SPEC TEST (NEGATIVE)
|
||||
*
|
||||
* SPEC VERSION: 0.1-313
|
||||
* PRIMARY LINKS: expressions, when-expression -> paragraph 5 -> sentence 1
|
||||
* expressions, when-expression, exhaustive-when-expressions -> paragraph 2 -> sentence 3
|
||||
* expressions, when-expression, exhaustive-when-expressions -> paragraph 2 -> sentence 4
|
||||
* expressions, when-expression, exhaustive-when-expressions -> paragraph 2 -> sentence 5
|
||||
*/
|
||||
|
||||
// See also: KT-3743
|
||||
fun foo(arg: Boolean): String {
|
||||
// Must be exhaustive
|
||||
return <!NO_ELSE_IN_WHEN!>when<!>(arg) {
|
||||
2 == 2 -> "truth"
|
||||
2 == 1 -> "falsehood"
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
// FIR_IDENTICAL
|
||||
// !LANGUAGE: +ProhibitSimplificationOfNonTrivialConstBooleanExpressions
|
||||
/*
|
||||
* KOTLIN DIAGNOSTICS SPEC TEST (NEGATIVE)
|
||||
*
|
||||
@@ -11,7 +13,7 @@
|
||||
// See also: KT-3743
|
||||
fun foo(arg: Boolean): String {
|
||||
// Must be exhaustive
|
||||
return when(arg) {
|
||||
return <!NO_ELSE_IN_WHEN!>when<!>(arg) {
|
||||
2 == 2 -> "truth"
|
||||
2 == 1 -> "falsehood"
|
||||
}
|
||||
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
// FIR_IDENTICAL
|
||||
// !LANGUAGE: +ProhibitSimplificationOfNonTrivialConstBooleanExpressions
|
||||
// ISSUE: KT-39883
|
||||
|
||||
// Should always work
|
||||
fun test_0(b: Boolean): String = when (b) {
|
||||
true -> "true"
|
||||
false -> "false"
|
||||
}
|
||||
|
||||
// Deprecated
|
||||
fun test_1(b: Boolean): String = <!NO_ELSE_IN_WHEN!>when<!> (b) {
|
||||
1 == 1 -> "true"
|
||||
"" != "" -> "false"
|
||||
}
|
||||
|
||||
const val TRUE = true
|
||||
|
||||
// Already not working
|
||||
fun test_2(b: Boolean): String = <!NO_ELSE_IN_WHEN!>when<!>(b) {
|
||||
TRUE -> "true"
|
||||
false -> "false"
|
||||
}
|
||||
|
||||
const val s1 = "s1"
|
||||
const val s2 = "s2"
|
||||
|
||||
// Already not working
|
||||
fun test_3(b: Boolean): String = <!NO_ELSE_IN_WHEN!>when<!>(b) {
|
||||
true -> "true"
|
||||
s1 == s2 -> "false"
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package
|
||||
|
||||
public const val TRUE: kotlin.Boolean = true
|
||||
public const val s1: kotlin.String = "s1"
|
||||
public const val s2: kotlin.String = "s2"
|
||||
public fun test_0(/*0*/ b: kotlin.Boolean): kotlin.String
|
||||
public fun test_1(/*0*/ b: kotlin.Boolean): kotlin.String
|
||||
public fun test_2(/*0*/ b: kotlin.Boolean): kotlin.String
|
||||
public fun test_3(/*0*/ b: kotlin.Boolean): kotlin.String
|
||||
Vendored
+31
@@ -0,0 +1,31 @@
|
||||
// !LANGUAGE: -ProhibitSimplificationOfNonTrivialConstBooleanExpressions
|
||||
// ISSUE: KT-39883
|
||||
|
||||
// Should always work
|
||||
fun test_0(b: Boolean): String = when (b) {
|
||||
true -> "true"
|
||||
false -> "false"
|
||||
}
|
||||
|
||||
// Deprecated
|
||||
fun test_1(b: Boolean): String = <!NO_ELSE_IN_WHEN!>when<!> (b) {
|
||||
1 == 1 -> "true"
|
||||
"" != "" -> "false"
|
||||
}
|
||||
|
||||
const val TRUE = true
|
||||
|
||||
// Already not working
|
||||
fun test_2(b: Boolean): String = <!NO_ELSE_IN_WHEN!>when<!>(b) {
|
||||
TRUE -> "true"
|
||||
false -> "false"
|
||||
}
|
||||
|
||||
const val s1 = "s1"
|
||||
const val s2 = "s2"
|
||||
|
||||
// Already not working
|
||||
fun test_3(b: Boolean): String = <!NO_ELSE_IN_WHEN!>when<!>(b) {
|
||||
true -> "true"
|
||||
s1 == s2 -> "false"
|
||||
}
|
||||
Vendored
+31
@@ -0,0 +1,31 @@
|
||||
// !LANGUAGE: -ProhibitSimplificationOfNonTrivialConstBooleanExpressions
|
||||
// ISSUE: KT-39883
|
||||
|
||||
// Should always work
|
||||
fun test_0(b: Boolean): String = when (b) {
|
||||
true -> "true"
|
||||
false -> "false"
|
||||
}
|
||||
|
||||
// Deprecated
|
||||
fun test_1(b: Boolean): String = when (b) {
|
||||
<!NON_TRIVIAL_BOOLEAN_CONSTANT!>1 == 1<!> -> "true"
|
||||
<!NON_TRIVIAL_BOOLEAN_CONSTANT!>"" != ""<!> -> "false"
|
||||
}
|
||||
|
||||
const val TRUE = true
|
||||
|
||||
// Already not working
|
||||
fun test_2(b: Boolean): String = <!NO_ELSE_IN_WHEN!>when<!>(b) {
|
||||
TRUE -> "true"
|
||||
false -> "false"
|
||||
}
|
||||
|
||||
const val s1 = "s1"
|
||||
const val s2 = "s2"
|
||||
|
||||
// Already not working
|
||||
fun test_3(b: Boolean): String = <!NO_ELSE_IN_WHEN!>when<!>(b) {
|
||||
true -> "true"
|
||||
s1 == s2 -> "false"
|
||||
}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
package
|
||||
|
||||
public const val TRUE: kotlin.Boolean = true
|
||||
public const val s1: kotlin.String = "s1"
|
||||
public const val s2: kotlin.String = "s2"
|
||||
public fun test_0(/*0*/ b: kotlin.Boolean): kotlin.String
|
||||
public fun test_1(/*0*/ b: kotlin.Boolean): kotlin.String
|
||||
public fun test_2(/*0*/ b: kotlin.Boolean): kotlin.String
|
||||
public fun test_3(/*0*/ b: kotlin.Boolean): kotlin.String
|
||||
Reference in New Issue
Block a user