Extract effect from lambda argument if it is in parentheses
Otherwise, contracts on the parameter have no effect. #KT-42044 Fixed #KT-26229 Fixed
This commit is contained in:
Generated
+5
@@ -5324,6 +5324,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
|
||||
runTest("compiler/testData/codegen/box/contracts/exception.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldInConstructorParens.kt")
|
||||
public void testFieldInConstructorParens() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/contracts/fieldInConstructorParens.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldReadInConstructor.kt")
|
||||
public void testFieldReadInConstructor() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/contracts/fieldReadInConstructor.kt");
|
||||
|
||||
@@ -242,7 +242,10 @@ class EffectsExtractingVisitor(
|
||||
private fun ValueArgument.toComputation(): Computation? {
|
||||
return when (this) {
|
||||
is KtLambdaArgument -> getLambdaExpression()?.let { ESLambda(it) }
|
||||
is KtValueArgument -> getArgumentExpression()?.let { extractOrGetCached(it) }
|
||||
is KtValueArgument -> getArgumentExpression()?.let {
|
||||
if (it is KtLambdaExpression) ESLambda(it)
|
||||
else extractOrGetCached(it)
|
||||
}
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
|
||||
// IGNORE_BACKEND: NATIVE
|
||||
// WITH_RUNTIME
|
||||
// KJS_WITH_FULL_RUNTIME
|
||||
|
||||
import kotlin.contracts.*
|
||||
|
||||
class Smth {
|
||||
val whatever: Int
|
||||
|
||||
init {
|
||||
calculate({ whatever = it })
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalContracts::class)
|
||||
private inline fun calculate(block: (Int) -> Unit) {
|
||||
contract {
|
||||
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
block(42)
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val smth = Smth()
|
||||
return if (smth.whatever == 42) "OK" else "FAIL ${smth.whatever}"
|
||||
}
|
||||
+5
-12
@@ -12,15 +12,16 @@
|
||||
|
||||
// TESTCASE NUMBER: 1
|
||||
fun case_1() {
|
||||
val value_1: Int
|
||||
funWithExactlyOnceCallsInPlace({ <!CAPTURED_VAL_INITIALIZATION!>value_1<!> = 10 })
|
||||
var value_1: Int
|
||||
val l = { value_1 = 10 }
|
||||
funWithAtLeastOnceCallsInPlace(l)
|
||||
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
|
||||
}
|
||||
|
||||
// TESTCASE NUMBER: 2
|
||||
fun case_2() {
|
||||
var value_1: Int
|
||||
val l = { value_1 = 10 }
|
||||
val l = fun () { value_1 = 10 }
|
||||
funWithAtLeastOnceCallsInPlace(l)
|
||||
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
|
||||
}
|
||||
@@ -28,20 +29,12 @@ fun case_2() {
|
||||
// TESTCASE NUMBER: 3
|
||||
fun case_3() {
|
||||
var value_1: Int
|
||||
val l = fun () { value_1 = 10 }
|
||||
funWithAtLeastOnceCallsInPlace(l)
|
||||
funWithAtLeastOnceCallsInPlace(fun () { value_1 = 10 })
|
||||
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
|
||||
}
|
||||
|
||||
// TESTCASE NUMBER: 4
|
||||
fun case_4() {
|
||||
var value_1: Int
|
||||
funWithAtLeastOnceCallsInPlace(fun () { value_1 = 10 })
|
||||
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
|
||||
}
|
||||
|
||||
// TESTCASE NUMBER: 5
|
||||
fun case_5() {
|
||||
val value_1: Int
|
||||
val o = object {
|
||||
fun l() { <!CAPTURED_VAL_INITIALIZATION!>value_1<!> = 10 }
|
||||
|
||||
-16
@@ -1,16 +0,0 @@
|
||||
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
|
||||
// SKIP_TXT
|
||||
|
||||
// TESTCASE NUMBER: 1
|
||||
fun case_1() {
|
||||
val value_1: Int
|
||||
funWithExactlyOnceCallsInPlace({ value_1 = 11 })
|
||||
value_1.inc()
|
||||
}
|
||||
|
||||
// TESTCASE NUMBER: 2
|
||||
fun case_2() {
|
||||
var value_1: Int
|
||||
funWithAtLeastOnceCallsInPlace({ value_1 = 11 })
|
||||
value_1.inc()
|
||||
}
|
||||
-27
@@ -1,27 +0,0 @@
|
||||
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
|
||||
// SKIP_TXT
|
||||
|
||||
/*
|
||||
* KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE)
|
||||
*
|
||||
* SECTIONS: contracts, analysis, controlFlow, initialization
|
||||
* NUMBER: 6
|
||||
* DESCRIPTION: Check the lack of CallsInPlace effect on the lambda in the parentheses.
|
||||
* UNEXPECTED BEHAVIOUR
|
||||
* ISSUES: KT-26229
|
||||
* HELPERS: contractFunctions
|
||||
*/
|
||||
|
||||
// TESTCASE NUMBER: 1
|
||||
fun case_1() {
|
||||
val value_1: Int
|
||||
funWithExactlyOnceCallsInPlace({ <!CAPTURED_VAL_INITIALIZATION!>value_1<!> = 11 })
|
||||
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
|
||||
}
|
||||
|
||||
// TESTCASE NUMBER: 2
|
||||
fun case_2() {
|
||||
var value_1: Int
|
||||
funWithAtLeastOnceCallsInPlace({ value_1 = 11 })
|
||||
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
|
||||
}
|
||||
+8
-10
@@ -5,9 +5,7 @@
|
||||
*
|
||||
* SECTIONS: contracts, analysis, controlFlow, initialization
|
||||
* NUMBER: 6
|
||||
* DESCRIPTION: Check the lack of CallsInPlace effect on the lambda in the parentheses.
|
||||
* UNEXPECTED BEHAVIOUR
|
||||
* ISSUES: KT-26229
|
||||
* DESCRIPTION: Check the presence of CallsInPlace effect on the lambda in the parentheses.
|
||||
* HELPERS: contractFunctions
|
||||
*/
|
||||
|
||||
@@ -37,15 +35,15 @@ import contracts.*
|
||||
// TESTCASE NUMBER: 1
|
||||
fun case_1() {
|
||||
val value_1: Int
|
||||
funWithExactlyOnceCallsInPlace({ <!CAPTURED_VAL_INITIALIZATION!>value_1<!> = 11 })
|
||||
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
|
||||
funWithExactlyOnceCallsInPlace({ value_1 = 11 })
|
||||
value_1.inc()
|
||||
}
|
||||
|
||||
// TESTCASE NUMBER: 2
|
||||
fun case_2() {
|
||||
var value_1: Int
|
||||
funWithAtLeastOnceCallsInPlace({ value_1 = 11 })
|
||||
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
|
||||
value_1.inc()
|
||||
}
|
||||
|
||||
// TESTCASE NUMBER: 3
|
||||
@@ -53,8 +51,8 @@ fun case_3() {
|
||||
val value_1: Int
|
||||
var value_2: Int
|
||||
val value_3: Int
|
||||
contracts.case_3({ <!CAPTURED_VAL_INITIALIZATION!>value_1<!> = 1 }, { value_2 = 2 }, { <!CAPTURED_VAL_INITIALIZATION!>value_3<!> = 3 })
|
||||
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>value_2<!>.inc()
|
||||
<!UNINITIALIZED_VARIABLE!>value_3<!>.inc()
|
||||
contracts.case_3({ value_1 = 1 }, { value_2 = 2 }, { value_3 = 3 })
|
||||
value_1.inc()
|
||||
value_2.inc()
|
||||
value_3.inc()
|
||||
}
|
||||
|
||||
Generated
-5
@@ -6527,11 +6527,6 @@ public class DiagnosticsTestSpecGenerated extends AbstractDiagnosticsTestSpec {
|
||||
runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/5.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("6.kt")
|
||||
public void test6() throws Exception {
|
||||
runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/6.kt");
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInNeg() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
|
||||
}
|
||||
|
||||
Generated
-5
@@ -6527,11 +6527,6 @@ public class FirDiagnosticsTestSpecGenerated extends AbstractFirDiagnosticsTestS
|
||||
runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/5.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("6.kt")
|
||||
public void test6() throws Exception {
|
||||
runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/6.kt");
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInNeg() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
|
||||
}
|
||||
|
||||
+5
@@ -5354,6 +5354,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
runTest("compiler/testData/codegen/box/contracts/exception.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldInConstructorParens.kt")
|
||||
public void testFieldInConstructorParens() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/contracts/fieldInConstructorParens.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldReadInConstructor.kt")
|
||||
public void testFieldReadInConstructor() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/contracts/fieldReadInConstructor.kt");
|
||||
|
||||
+5
@@ -5354,6 +5354,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
|
||||
runTest("compiler/testData/codegen/box/contracts/exception.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldInConstructorParens.kt")
|
||||
public void testFieldInConstructorParens() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/contracts/fieldInConstructorParens.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldReadInConstructor.kt")
|
||||
public void testFieldReadInConstructor() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/contracts/fieldReadInConstructor.kt");
|
||||
|
||||
+5
@@ -5324,6 +5324,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
|
||||
runTest("compiler/testData/codegen/box/contracts/exception.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldInConstructorParens.kt")
|
||||
public void testFieldInConstructorParens() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/contracts/fieldInConstructorParens.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldReadInConstructor.kt")
|
||||
public void testFieldReadInConstructor() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/contracts/fieldReadInConstructor.kt");
|
||||
|
||||
Generated
+5
@@ -4239,6 +4239,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
|
||||
runTest("compiler/testData/codegen/box/contracts/exception.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldInConstructorParens.kt")
|
||||
public void testFieldInConstructorParens() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/contracts/fieldInConstructorParens.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldReadInConstructor.kt")
|
||||
public void testFieldReadInConstructor() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/contracts/fieldReadInConstructor.kt");
|
||||
|
||||
Generated
+5
@@ -4239,6 +4239,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
|
||||
runTest("compiler/testData/codegen/box/contracts/exception.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldInConstructorParens.kt")
|
||||
public void testFieldInConstructorParens() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/contracts/fieldInConstructorParens.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldReadInConstructor.kt")
|
||||
public void testFieldReadInConstructor() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/contracts/fieldReadInConstructor.kt");
|
||||
|
||||
+5
@@ -4239,6 +4239,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
|
||||
runTest("compiler/testData/codegen/box/contracts/exception.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldInConstructorParens.kt")
|
||||
public void testFieldInConstructorParens() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/contracts/fieldInConstructorParens.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldReadInConstructor.kt")
|
||||
public void testFieldReadInConstructor() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/contracts/fieldReadInConstructor.kt");
|
||||
|
||||
Reference in New Issue
Block a user