-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PowerAssert] Capture safe cast expressions
Previously the only type operations captured by power-assert where type checks. This caused safe casts to not be included in the diagram, and more alarmingly, ignored if they were the root expression of a parameter. This resulted in incorrect behavior of power-assert transformed code in certain situations. To fix, capture safe cast expressions and include all other type operations as a constant expression. ^KT-69401 Fixed
- Loading branch information
1 parent
5b26d35
commit 4307ba4
Showing
8 changed files
with
151 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
plugins/power-assert/testData/codegen/cast/SafeCast.box.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
assert((a as? String)?.length == 5) | ||
| | | | | ||
| | | false | ||
| | 4 | ||
| test | ||
test | ||
--- | ||
--- | ||
assert((a as? String)?.length == 5) | ||
| | | | | ||
| | | false | ||
| | null | ||
| null | ||
1 | ||
--- | ||
--- | ||
requireNotNull(a as? String) { "" } | ||
| | | ||
| null | ||
1 | ||
--- |
43 changes: 43 additions & 0 deletions
43
plugins/power-assert/testData/codegen/cast/SafeCast.fir.kt.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
fun box(): String { | ||
return test1(a = "test").plus(other = test1(a = 1)).plus(other = test2(a = 1)) | ||
} | ||
|
||
fun test1(a: Any): String { | ||
return expectThrowableMessage(block = local fun <anonymous>() { | ||
{ // BLOCK | ||
val tmp_0: Any = a | ||
val tmp_1: String? = tmp_0 as? String | ||
val tmp_2: Int? = { // BLOCK | ||
val tmp_3: String? = tmp_1 | ||
when { | ||
EQEQ(arg0 = tmp_3, arg1 = null) -> null | ||
else -> tmp_3.<get-length>() | ||
} | ||
} | ||
val tmp_4: Boolean = EQEQ(arg0 = tmp_2, arg1 = 5) | ||
assert(value = tmp_4, lazyMessage = local fun <anonymous>(): String { | ||
return "\nassert((a as? String)?.length == 5)\n | | | |" + "\n | | | " + tmp_4 + "\n | | " + tmp_2 + "\n | " + tmp_1 + "\n " + tmp_0 + "\n" | ||
} | ||
) | ||
} | ||
} | ||
) | ||
} | ||
|
||
fun test2(a: Any): String { | ||
return expectThrowableMessage(block = local fun <anonymous>() { | ||
{ // BLOCK | ||
val tmp_5: Any = a | ||
val tmp_6: String? = tmp_5 as? String | ||
requireNotNull<String>(value = tmp_6, lazyMessage = local fun <anonymous>(): String { | ||
return local fun <anonymous>(): Any { | ||
return "" | ||
} | ||
.invoke() + "\nrequireNotNull(a as? String) { \"\" }\n | |" + "\n | " + tmp_6 + "\n " + tmp_5 + "\n" | ||
} | ||
) | ||
} /*~> Unit */ | ||
} | ||
) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// FUNCTION: kotlin.assert | ||
// FUNCTION: kotlin.requireNotNull | ||
// DUMP_KT_IR | ||
|
||
fun box(): String { | ||
return test1("test") + | ||
test1(1) + | ||
test2(1) | ||
} | ||
|
||
fun test1(a: Any) = expectThrowableMessage { | ||
assert((a as? String)?.length == 5) | ||
} | ||
|
||
fun test2(a: Any) = expectThrowableMessage { | ||
requireNotNull(a as? String) { "" } | ||
} |
43 changes: 43 additions & 0 deletions
43
plugins/power-assert/testData/codegen/cast/SafeCast.kt.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
fun box(): String { | ||
return test1(a = "test").plus(other = test1(a = 1)).plus(other = test2(a = 1)) | ||
} | ||
|
||
fun test1(a: Any): String { | ||
return expectThrowableMessage(block = local fun <anonymous>() { | ||
{ // BLOCK | ||
val tmp_0: Any = a | ||
val tmp_1: String? = tmp_0 as? String | ||
val tmp_2: Int? = { // BLOCK | ||
val tmp_3: String? = tmp_1 | ||
when { | ||
EQEQ(arg0 = tmp_3, arg1 = null) -> null | ||
else -> tmp_3.<get-length>() | ||
} | ||
} | ||
val tmp_4: Boolean = EQEQ(arg0 = tmp_2, arg1 = 5) | ||
assert(value = tmp_4, lazyMessage = local fun <anonymous>(): String { | ||
return "\nassert((a as? String)?.length == 5)\n | | | |" + "\n | | | " + tmp_4 + "\n | | " + tmp_2 + "\n | " + tmp_1 + "\n " + tmp_0 + "\n" | ||
} | ||
) | ||
} | ||
} | ||
) | ||
} | ||
|
||
fun test2(a: Any): String { | ||
return expectThrowableMessage(block = local fun <anonymous>() { | ||
{ // BLOCK | ||
val tmp_5: Any = a | ||
val tmp_6: String? = tmp_5 as? String | ||
requireNotNull<String>(value = tmp_6, lazyMessage = local fun <anonymous>(): String { | ||
return local fun <anonymous>(): Any { | ||
return "" | ||
} | ||
.invoke() + "\nrequireNotNull(a as? String) { \"\" }\n | |" + "\n | " + tmp_6 + "\n " + tmp_5 + "\n" | ||
} | ||
) | ||
} /*~> Unit */ | ||
} | ||
) | ||
} | ||
|
6 changes: 6 additions & 0 deletions
6
.../jetbrains/kotlin/powerassert/FirLightTreeBlackBoxCodegenTestForPowerAssertGenerated.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
...ts-gen/org/jetbrains/kotlin/powerassert/IrBlackBoxCodegenTestForPowerAssertGenerated.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.