Effects: add diagnostic tests on contracts

- Make AbstractDiagnosticsTest dump function contracts
- Add diagnostics tests on parsing contracts
- Add diagnostics tests on smartcats in presence of functions with
contracts
- Add diagnostics tests on initialization and flow in presence of
in-place called lambdas

==========
Introduction of EffectSystem: 16/18
This commit is contained in:
Dmitry Savvinov
2017-10-03 15:02:51 +03:00
parent f487525a1d
commit 4434db4d69
120 changed files with 4179 additions and 1 deletions
@@ -0,0 +1,79 @@
// !LANGUAGE: +ReturnsEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.internal.contracts.*
fun trueWhenString(x: Any?): Boolean {
contract {
returns(true) implies (x is String)
}
return x is String
}
fun trueWhenInt(x: Any?): Boolean {
contract {
returns(true) implies (x is Int)
}
return x is Int
}
fun falseWhenString(x: Any?): Boolean {
contract {
returns(false) implies (x is String)
}
return x !is String
}
fun falseWhenInt(x: Any?): Boolean {
contract {
returns(false) implies (x is Int)
}
return x !is Int
}
// ==== Actual tests ====
fun truetrue(x: Any?) {
if (trueWhenString(x) && trueWhenInt(x)) {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
<!DEBUG_INFO_SMARTCAST!>x<!>.inc()
}
x.<!UNRESOLVED_REFERENCE!>length<!>
x.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>inc<!>()
}
fun truefalse(x: Any?) {
if (trueWhenString(x) && falseWhenInt(x)) {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
x.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>inc<!>()
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
x.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>inc<!>()
}
}
fun falsetrue(x: Any?) {
if (falseWhenString(x) && trueWhenInt(x)) {
x.<!UNRESOLVED_REFERENCE!>length<!>
<!DEBUG_INFO_SMARTCAST!>x<!>.inc()
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
x.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>inc<!>()
}
}
fun falsefalse(x: Any?) {
if (falseWhenString(x) && falseWhenInt(x)) {
x.<!UNRESOLVED_REFERENCE!>length<!>
x.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>inc<!>()
}
else {
// Note that we can't argue that we have any of smartcasts here,
// because we don't know which one of both arguments was 'false' to bring us here
x.<!UNRESOLVED_REFERENCE!>length<!>
x.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>inc<!>()
}
}
@@ -0,0 +1,18 @@
package
public fun falseWhenInt(/*0*/ x: kotlin.Any?): kotlin.Boolean
Returns(FALSE) -> x is Int
public fun falseWhenString(/*0*/ x: kotlin.Any?): kotlin.Boolean
Returns(FALSE) -> x is String
public fun falsefalse(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun falsetrue(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun trueWhenInt(/*0*/ x: kotlin.Any?): kotlin.Boolean
Returns(TRUE) -> x is Int
public fun trueWhenString(/*0*/ x: kotlin.Any?): kotlin.Boolean
Returns(TRUE) -> x is String
public fun truefalse(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun truetrue(/*0*/ x: kotlin.Any?): kotlin.Unit
@@ -0,0 +1,59 @@
// !LANGUAGE: +ReturnsEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.internal.contracts.*
fun trueWhenString(x: Any?): Boolean {
contract {
returns(true) implies (x is String)
}
return x is String
}
fun falseWhenString(x: Any?): Boolean {
contract {
returns(false) implies (x is String)
}
return x !is String
}
fun annotatedTrueAndTrue(x: Any?) {
if (trueWhenString(x) && true) {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun annotatedTrueAndFalse(x: Any?) {
if (trueWhenString(x) && false) {
// Unreachable
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun annotatedFalseAndTrue(x: Any?) {
if (falseWhenString(x) && true) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
else {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
}
fun annotatedFalseAndFalse(x: Any?) {
if (falseWhenString(x) && false) {
// Unreachable
x.<!UNRESOLVED_REFERENCE!>length<!>
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
@@ -0,0 +1,11 @@
package
public fun annotatedFalseAndFalse(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun annotatedFalseAndTrue(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun annotatedTrueAndFalse(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun annotatedTrueAndTrue(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun falseWhenString(/*0*/ x: kotlin.Any?): kotlin.Boolean
Returns(FALSE) -> x is String
public fun trueWhenString(/*0*/ x: kotlin.Any?): kotlin.Boolean
Returns(TRUE) -> x is String
@@ -0,0 +1,59 @@
// !LANGUAGE: +ReturnsEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.internal.contracts.*
fun trueWhenString(x: Any?): Boolean {
contract {
returns(true) implies (x is String)
}
return x is String
}
fun falseWhenString(x: Any?): Boolean {
contract {
returns(false) implies (x is String)
}
return x !is String
}
fun unknownFunction(x: Any?) = x == 42
fun annotatedTrue(x: Any?) {
if (trueWhenString(x) && unknownFunction(x)) {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun annotatedFalse(x: Any?) {
if (falseWhenString(x) && unknownFunction(x)) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun annotatedTrueWithVariable(x: Any?, b: Boolean) {
if (trueWhenString(x) && b) {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun annotatedFalseWithVariable(x: Any?, b: Boolean) {
if (falseWhenString(x) && b) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
@@ -0,0 +1,13 @@
package
public fun annotatedFalse(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun annotatedFalseWithVariable(/*0*/ x: kotlin.Any?, /*1*/ b: kotlin.Boolean): kotlin.Unit
public fun annotatedTrue(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun annotatedTrueWithVariable(/*0*/ x: kotlin.Any?, /*1*/ b: kotlin.Boolean): kotlin.Unit
public fun falseWhenString(/*0*/ x: kotlin.Any?): kotlin.Boolean
Returns(FALSE) -> x is String
public fun trueWhenString(/*0*/ x: kotlin.Any?): kotlin.Boolean
Returns(TRUE) -> x is String
public fun unknownFunction(/*0*/ x: kotlin.Any?): kotlin.Boolean
@@ -0,0 +1,45 @@
// !LANGUAGE: +ReturnsEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.internal.contracts.*
fun myEqualsNull(x: Int?): Boolean {
contract {
returns(false) implies (x != null)
}
return x == null
}
fun myEqualsNotNull(x: Int?): Boolean {
contract {
returns(true) implies (x != null)
}
return x != null
}
fun testBasicEquals(x: Int?) {
x<!UNSAFE_CALL!>.<!>inc()
if (myEqualsNull(x)) {
x<!UNSAFE_CALL!>.<!>inc()
}
else {
<!DEBUG_INFO_SMARTCAST!>x<!>.inc()
}
x<!UNSAFE_CALL!>.<!>inc()
}
fun testBasicNotEquals(x: Int?) {
x<!UNSAFE_CALL!>.<!>inc()
if (myEqualsNotNull(x)) {
<!DEBUG_INFO_SMARTCAST!>x<!>.inc()
}
else {
x<!UNSAFE_CALL!>.<!>inc()
}
x<!UNSAFE_CALL!>.<!>inc()
}
@@ -0,0 +1,10 @@
package
public fun myEqualsNotNull(/*0*/ x: kotlin.Int?): kotlin.Boolean
Returns(TRUE) -> x != null
public fun myEqualsNull(/*0*/ x: kotlin.Int?): kotlin.Boolean
Returns(FALSE) -> x != null
public fun testBasicEquals(/*0*/ x: kotlin.Int?): kotlin.Unit
public fun testBasicNotEquals(/*0*/ x: kotlin.Int?): kotlin.Unit
@@ -0,0 +1,68 @@
// !LANGUAGE: +ReturnsEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.internal.contracts.*
fun safeIsString(x: Any?): Boolean? {
contract {
returns(true) implies (x is String)
}
return x?.let { it is String }
}
fun equalsTrue(x: Any?) {
if (safeIsString(x) == true) {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun equalsFalse(x: Any?) {
if (safeIsString(x) == false) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun equalsNull(x: Any?) {
if (safeIsString(x) == null) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun notEqualsTrue(x: Any?) {
if (safeIsString(x) != true) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
else {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
}
fun notEqualsFalse(x: Any?) {
if (safeIsString(x) != false) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun notEqualsNull(x: Any?) {
if (safeIsString(x) != null) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
@@ -0,0 +1,10 @@
package
public fun equalsFalse(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun equalsNull(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun equalsTrue(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun notEqualsFalse(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun notEqualsNull(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun notEqualsTrue(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun safeIsString(/*0*/ x: kotlin.Any?): kotlin.Boolean?
Returns(TRUE) -> x is String
@@ -0,0 +1,59 @@
// !LANGUAGE: +ReturnsEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.internal.contracts.*
fun isString(x: Any?): Boolean {
contract {
returns(true) implies (x is String)
}
return x is String
}
fun notIsString(x: Any?): Boolean {
contract {
returns(false) implies (x is String)
}
return x !is String
}
fun testSimple(x: Any?) {
x.<!UNRESOLVED_REFERENCE!>length<!>
if (isString(x)) {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun testSpilling(x: Any?) {
x.<!UNRESOLVED_REFERENCE!>length<!>
if (isString(x)) <!DEBUG_INFO_SMARTCAST!>x<!>.length
x.<!UNRESOLVED_REFERENCE!>length<!>
}
fun testInversion(x: Any?) {
if (notIsString(x)) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
else {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
}
fun testInversionSpilling(x: Any?) {
x.<!UNRESOLVED_REFERENCE!>length<!>
if (notIsString(x)) else <!DEBUG_INFO_SMARTCAST!>x<!>.length
x.<!UNRESOLVED_REFERENCE!>length<!>
}
@@ -0,0 +1,12 @@
package
public fun isString(/*0*/ x: kotlin.Any?): kotlin.Boolean
Returns(TRUE) -> x is String
public fun notIsString(/*0*/ x: kotlin.Any?): kotlin.Boolean
Returns(FALSE) -> x is String
public fun testInversion(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun testInversionSpilling(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun testSimple(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun testSpilling(/*0*/ x: kotlin.Any?): kotlin.Unit
@@ -0,0 +1,76 @@
// !LANGUAGE: +ReturnsEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.internal.contracts.*
fun trueWhenString(x: Any?): Boolean {
contract {
returns(true) implies (x is String)
}
return x is String
}
fun trueWhenInt(x: Any?): Boolean {
contract {
returns(true) implies (x is Int)
}
return x is Int
}
fun falseWhenString(x: Any?): Boolean {
contract {
returns(false) implies (x is String)
}
return x !is String
}
fun falseWhenInt(x: Any?): Boolean {
contract {
returns(false) implies (x is Int)
}
return x !is Int
}
fun truetrue(x: Any?) {
if (trueWhenString(x) || trueWhenInt(x)) {
x.<!UNRESOLVED_REFERENCE!>length<!>
x.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>inc<!>()
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
x.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>inc<!>()
}
}
fun truefalse(x: Any?) {
if (trueWhenString(x) || falseWhenInt(x)) {
x.<!UNRESOLVED_REFERENCE!>length<!>
x.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>inc<!>()
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
<!DEBUG_INFO_SMARTCAST!>x<!>.inc()
}
}
fun falsetrue(x: Any?) {
if (falseWhenString(x) || trueWhenInt(x)) {
x.<!UNRESOLVED_REFERENCE!>length<!>
x.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>inc<!>()
}
else {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
x.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>inc<!>()
}
}
fun falsefalse(x: Any?) {
if (falseWhenString(x) || falseWhenInt(x)) {
x.<!UNRESOLVED_REFERENCE!>length<!>
x.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>inc<!>()
}
else {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
<!DEBUG_INFO_SMARTCAST!>x<!>.inc()
}
}
@@ -0,0 +1,18 @@
package
public fun falseWhenInt(/*0*/ x: kotlin.Any?): kotlin.Boolean
Returns(FALSE) -> x is Int
public fun falseWhenString(/*0*/ x: kotlin.Any?): kotlin.Boolean
Returns(FALSE) -> x is String
public fun falsefalse(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun falsetrue(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun trueWhenInt(/*0*/ x: kotlin.Any?): kotlin.Boolean
Returns(TRUE) -> x is Int
public fun trueWhenString(/*0*/ x: kotlin.Any?): kotlin.Boolean
Returns(TRUE) -> x is String
public fun truefalse(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun truetrue(/*0*/ x: kotlin.Any?): kotlin.Unit
@@ -0,0 +1,56 @@
// !LANGUAGE: +ReturnsEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.internal.contracts.*
fun trueWhenString(x: Any?): Boolean {
contract {
returns(true) implies (x is String)
}
return x is String
}
fun falseWhenString(x: Any?): Boolean {
contract {
returns(false) implies (x is String)
}
return x !is String
}
fun annotatedTrueOrTrue(x: Any?) {
if (trueWhenString(x) || true) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
else {
// Unreachable
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun annotatedTrueOrFalse(x: Any?) {
if (trueWhenString(x) || false) {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun annotatedFalseOrTrue(x: Any?) {
if (falseWhenString(x) || true) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
else {
// Unreachable
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
}
fun annotatedFalseOrFalse(x: Any?) {
if (falseWhenString(x) || false) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
else {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
}
@@ -0,0 +1,11 @@
package
public fun annotatedFalseOrFalse(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun annotatedFalseOrTrue(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun annotatedTrueOrFalse(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun annotatedTrueOrTrue(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun falseWhenString(/*0*/ x: kotlin.Any?): kotlin.Boolean
Returns(FALSE) -> x is String
public fun trueWhenString(/*0*/ x: kotlin.Any?): kotlin.Boolean
Returns(TRUE) -> x is String
@@ -0,0 +1,59 @@
// !LANGUAGE: +ReturnsEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.internal.contracts.*
fun trueWhenString(x: Any?): Boolean {
contract {
returns(true) implies (x is String)
}
return x is String
}
fun falseWhenString(x: Any?): Boolean {
contract {
returns(false) implies (x is String)
}
return x !is String
}
fun unknownFunction(x: Any?) = x == 42
fun annotatedTrue(x: Any?) {
if (trueWhenString(x) || unknownFunction(x)) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun annotatedFalse(x: Any?) {
if (falseWhenString(x) || unknownFunction(x)) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
else {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
}
fun annotatedTrueWithVariable(x: Any?, b: Boolean) {
if (trueWhenString(x) || b) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun annotatedFalseWithVariable(x: Any?, b: Boolean) {
if (falseWhenString(x) || b) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
else {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
}
@@ -0,0 +1,13 @@
package
public fun annotatedFalse(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun annotatedFalseWithVariable(/*0*/ x: kotlin.Any?, /*1*/ b: kotlin.Boolean): kotlin.Unit
public fun annotatedTrue(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun annotatedTrueWithVariable(/*0*/ x: kotlin.Any?, /*1*/ b: kotlin.Boolean): kotlin.Unit
public fun falseWhenString(/*0*/ x: kotlin.Any?): kotlin.Boolean
Returns(FALSE) -> x is String
public fun trueWhenString(/*0*/ x: kotlin.Any?): kotlin.Boolean
Returns(TRUE) -> x is String
public fun unknownFunction(/*0*/ x: kotlin.Any?): kotlin.Boolean