Effects: add diagnostic tests on functions from stdlib

==========
Introduction of EffectSystem: 18/18
This commit is contained in:
Dmitry Savvinov
2017-10-03 17:06:44 +03:00
parent fb03656e99
commit d4d7946e6a
12 changed files with 320 additions and 0 deletions
@@ -0,0 +1,28 @@
// !LANGUAGE: +ReturnsEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
fun testCheckSmartcast(x: Any?) {
check(x is String)
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
fun testCheckUnreachableCode() {
check(false)
// Can't be reported without notion of 'iff'
println("Can't get here!")
}
fun testCheckWithMessage(x: Any?) {
check(x is String) { "x is not String!" }
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
fun testCheckWithFailingMessage(x: Any?) {
check(x is String) { throw kotlin.IllegalStateException("What a strange idea") }
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
fun tesCheckNotNullWithMessage(x: Int?) {
checkNotNull(x) { "x is null!"}
<!DEBUG_INFO_SMARTCAST!>x<!>.inc()
}
@@ -0,0 +1,7 @@
package
public fun tesCheckNotNullWithMessage(/*0*/ x: kotlin.Int?): kotlin.Unit
public fun testCheckSmartcast(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun testCheckUnreachableCode(): kotlin.Unit
public fun testCheckWithFailingMessage(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun testCheckWithMessage(/*0*/ x: kotlin.Any?): kotlin.Unit
@@ -0,0 +1,110 @@
// !LANGUAGE: +CallsInPlaceEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
fun testRunWithUnitReturn() {
val x: Int
run { x = 42 }
println(x)
}
fun testRunWithReturnValue() {
val x: Int
val y = run {
x = 42
"hello"
}
println(x)
println(y)
}
fun testRunWithCoercionToUnit() {
val <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>x<!>: Int
run {
x = 42
"hello"
}
}
fun testRunWithReceiver(x: Int) {
val s: String
x.run {
s = this.toString()
}
println(s)
}
fun testWith(x: Int) {
val s: String
with(x) {
s = toString()
}
println(s)
}
fun testApply(x: Int) {
val y: Int
val z: Int = x.apply { y = 42 }
println(y)
println(z)
}
fun testAlso(x: Int) {
val y: Int
x.also { y = it + 1 }
println(y)
}
fun testLet(x: Int) {
val z: Int
val y: String = x.let {
z = 42
(it + 1).toString()
}
println(z)
println(y)
}
fun testTakeIf(x: Int?) {
val y: Int
x.takeIf {
y = 42
it != null
}
println(y)
}
fun testTakeUnless(x: Int?) {
val y: Int
x.takeIf {
y = 42
it != null
}
println(y)
}
fun testRepeatOnVal(x: Int) {
val y: Int
repeat(x) {
// reassignment instead of captured val initalization
<!VAL_REASSIGNMENT!>y<!> = 42
}
println(<!UNINITIALIZED_VARIABLE!>y<!>)
}
fun testRepeatOnVar(x: Int) {
var y: Int
repeat(x) {
// no reassignment reported
y = 42
}
// but here we still unsure if 'y' was initialized
println(<!UNINITIALIZED_VARIABLE!>y<!>)
}
fun testRepeatOnInitializedVar(x: Int) {
var y: Int = 24
repeat(x) {
y = 42
}
println(y)
}
@@ -0,0 +1,15 @@
package
public fun testAlso(/*0*/ x: kotlin.Int): kotlin.Unit
public fun testApply(/*0*/ x: kotlin.Int): kotlin.Unit
public fun testLet(/*0*/ x: kotlin.Int): kotlin.Unit
public fun testRepeatOnInitializedVar(/*0*/ x: kotlin.Int): kotlin.Unit
public fun testRepeatOnVal(/*0*/ x: kotlin.Int): kotlin.Unit
public fun testRepeatOnVar(/*0*/ x: kotlin.Int): kotlin.Unit
public fun testRunWithCoercionToUnit(): kotlin.Unit
public fun testRunWithReceiver(/*0*/ x: kotlin.Int): kotlin.Unit
public fun testRunWithReturnValue(): kotlin.Unit
public fun testRunWithUnitReturn(): kotlin.Unit
public fun testTakeIf(/*0*/ x: kotlin.Int?): kotlin.Unit
public fun testTakeUnless(/*0*/ x: kotlin.Int?): kotlin.Unit
public fun testWith(/*0*/ x: kotlin.Int): kotlin.Unit
@@ -0,0 +1,20 @@
// !LANGUAGE: +ReturnsEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
fun testIsNullOrBlank(x: String?) {
if (x.isNullOrBlank()) {
x<!UNSAFE_CALL!>.<!>length
}
else {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
}
fun testIsNotNullOrBlank(x: String?) {
if (!x.isNullOrBlank()) {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
x<!UNSAFE_CALL!>.<!>length
}
@@ -0,0 +1,4 @@
package
public fun testIsNotNullOrBlank(/*0*/ x: kotlin.String?): kotlin.Unit
public fun testIsNullOrBlank(/*0*/ x: kotlin.String?): kotlin.Unit
@@ -0,0 +1,20 @@
// !LANGUAGE: +ReturnsEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
fun testIsNullOrEmpty(x: String?) {
if (x.isNullOrEmpty()) {
x<!UNSAFE_CALL!>.<!>length
}
else {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
}
fun testIsNotNullOrEmpty(x: String?) {
if (!x.isNullOrEmpty()) {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
x<!UNSAFE_CALL!>.<!>length
}
@@ -0,0 +1,4 @@
package
public fun testIsNotNullOrEmpty(/*0*/ x: kotlin.String?): kotlin.Unit
public fun testIsNullOrEmpty(/*0*/ x: kotlin.String?): kotlin.Unit
@@ -0,0 +1,27 @@
// !LANGUAGE: +ReturnsEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
fun testRequireSmartcast(x: Any?) {
require(x is String)
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
fun testRequireUnreachableCode() {
require(false)
println("Can't get here!")
}
fun testRequireWithMessage(x: Any?) {
require(x is String) { "x is not String!" }
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
fun testRequireWithFailingMessage(x: Any?) {
require(x is String) { throw kotlin.IllegalStateException("What a strange idea") }
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
fun tesRequireNotNullWithMessage(x: Int?) {
requireNotNull(x) { "x is null!"}
<!DEBUG_INFO_SMARTCAST!>x<!>.inc()
}
@@ -0,0 +1,7 @@
package
public fun tesRequireNotNullWithMessage(/*0*/ x: kotlin.Int?): kotlin.Unit
public fun testRequireSmartcast(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun testRequireUnreachableCode(): kotlin.Unit
public fun testRequireWithFailingMessage(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun testRequireWithMessage(/*0*/ x: kotlin.Any?): kotlin.Unit