[FIR IDE] Ensure resolve to CONTRACTS phase in ReturnTypeCalculator
If we want to analyse some function's call, we need to know about its contracts, otherwise resolving the following code would be broken. Computing return type of function is a prerequisite to using it in any sensible way, so it's the best place to resolve it to CONTRACTS KT-50733
This commit is contained in:
+10
@@ -1092,6 +1092,16 @@ public class LazyBodyIsNotTouchedTilContractsPhaseTestGenerated extends Abstract
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/analysis-tests/testData/resolve/contracts"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@TestMetadata("contractFromOtherModule.kt")
|
||||
public void testContractFromOtherModule() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/contracts/contractFromOtherModule.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("contractFromOtherModule_samePackage.kt")
|
||||
public void testContractFromOtherModule_samePackage() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/contracts/contractFromOtherModule_samePackage.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("genericContract.kt")
|
||||
public void testGenericContract() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/contracts/genericContract.kt");
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
Module: lib
|
||||
FILE: module_lib_contractFromOtherModule.kt
|
||||
package lib
|
||||
|
||||
public final fun requireIsTrue(value: R|kotlin/Boolean|): R|kotlin/Unit|
|
||||
[R|Contract description]
|
||||
<
|
||||
Returns(WILDCARD) -> value
|
||||
>
|
||||
{
|
||||
when () {
|
||||
R|<local>/value|.R|kotlin/Boolean.not|() -> {
|
||||
throw R|java/lang/IllegalArgumentException.IllegalArgumentException|()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Module: main
|
||||
FILE: module_main_contractFromOtherModule.kt
|
||||
package main
|
||||
|
||||
public final fun test(s: R|kotlin/Any|): R|kotlin/Unit| {
|
||||
R|lib/requireIsTrue|((R|<local>/s| is R|kotlin/String|))
|
||||
R|<local>/s|.R|kotlin/String.length|
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
// SKIP_JAVAC
|
||||
// This directive is needed to skip this test in LazyBodyIsNotTouchedTilContractsPhaseTestGenerated,
|
||||
// because it fails to parse module structure of multimodule test
|
||||
|
||||
// WITH_STDLIB
|
||||
|
||||
// MODULE: lib
|
||||
package lib
|
||||
|
||||
import kotlin.contracts.*
|
||||
|
||||
fun requireIsTrue(value: Boolean) contract [
|
||||
returns() implies value
|
||||
] {
|
||||
if (!value) throw IllegalArgumentException()
|
||||
}
|
||||
|
||||
// MODULE: main(lib)
|
||||
package main
|
||||
|
||||
import lib.requireIsTrue
|
||||
|
||||
fun test(s: Any) {
|
||||
requireIsTrue(s is String)
|
||||
s.length
|
||||
}
|
||||
Vendored
+25
@@ -0,0 +1,25 @@
|
||||
Module: lib
|
||||
FILE: module_lib_contractFromOtherModule_samePackage.kt
|
||||
package main
|
||||
|
||||
public final fun requireIsTrue(value: R|kotlin/Boolean|): R|kotlin/Unit|
|
||||
[R|Contract description]
|
||||
<
|
||||
Returns(WILDCARD) -> value
|
||||
>
|
||||
{
|
||||
when () {
|
||||
R|<local>/value|.R|kotlin/Boolean.not|() -> {
|
||||
throw R|java/lang/IllegalArgumentException.IllegalArgumentException|()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Module: main
|
||||
FILE: module_main_contractFromOtherModule_samePackage.kt
|
||||
package main
|
||||
|
||||
public final fun test(s: R|kotlin/Any|): R|kotlin/Unit| {
|
||||
R|main/requireIsTrue|((R|<local>/s| is R|kotlin/String|))
|
||||
R|<local>/s|.R|kotlin/String.length|
|
||||
}
|
||||
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
// SKIP_JAVAC
|
||||
// This directive is needed to skip this test in LazyBodyIsNotTouchedTilContractsPhaseTestGenerated,
|
||||
// because it fails to parse module structure of multimodule test
|
||||
|
||||
// WITH_STDLIB
|
||||
|
||||
// MODULE: lib
|
||||
package main
|
||||
|
||||
import kotlin.contracts.*
|
||||
|
||||
fun requireIsTrue(value: Boolean) contract [
|
||||
returns() implies value
|
||||
] {
|
||||
if (!value) throw IllegalArgumentException()
|
||||
}
|
||||
|
||||
// MODULE: main(lib)
|
||||
package main
|
||||
|
||||
fun test(s: Any) {
|
||||
requireIsTrue(s is String)
|
||||
s.length
|
||||
}
|
||||
+12
@@ -1257,6 +1257,18 @@ public class FirDiagnosticTestGenerated extends AbstractFirDiagnosticTest {
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/analysis-tests/testData/resolve/contracts"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("contractFromOtherModule.kt")
|
||||
public void testContractFromOtherModule() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/contracts/contractFromOtherModule.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("contractFromOtherModule_samePackage.kt")
|
||||
public void testContractFromOtherModule_samePackage() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/contracts/contractFromOtherModule_samePackage.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("genericContract.kt")
|
||||
public void testGenericContract() throws Exception {
|
||||
|
||||
+12
@@ -1257,6 +1257,18 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/analysis-tests/testData/resolve/contracts"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("contractFromOtherModule.kt")
|
||||
public void testContractFromOtherModule() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/contracts/contractFromOtherModule.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("contractFromOtherModule_samePackage.kt")
|
||||
public void testContractFromOtherModule_samePackage() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/contracts/contractFromOtherModule_samePackage.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("genericContract.kt")
|
||||
public void testGenericContract() throws Exception {
|
||||
|
||||
+18
@@ -9,6 +9,7 @@ import org.jetbrains.kotlin.fir.*
|
||||
import org.jetbrains.kotlin.fir.declarations.*
|
||||
import org.jetbrains.kotlin.fir.declarations.synthetic.FirSyntheticProperty
|
||||
import org.jetbrains.kotlin.fir.declarations.utils.hasExplicitBackingField
|
||||
import org.jetbrains.kotlin.fir.declarations.utils.isLocal
|
||||
import org.jetbrains.kotlin.fir.diagnostics.ConeSimpleDiagnostic
|
||||
import org.jetbrains.kotlin.fir.diagnostics.DiagnosticKind
|
||||
import org.jetbrains.kotlin.fir.resolve.FirRegularTowerDataContexts
|
||||
@@ -22,6 +23,7 @@ import org.jetbrains.kotlin.fir.resolve.transformers.TransformImplicitType
|
||||
import org.jetbrains.kotlin.fir.resolve.transformers.contracts.runContractResolveForLocalClass
|
||||
import org.jetbrains.kotlin.fir.scopes.FakeOverrideTypeCalculator
|
||||
import org.jetbrains.kotlin.fir.scopes.fakeOverrideSubstitution
|
||||
import org.jetbrains.kotlin.fir.symbols.ensureResolved
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirSyntheticPropertySymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol
|
||||
import org.jetbrains.kotlin.fir.types.FirImplicitTypeRef
|
||||
@@ -225,6 +227,8 @@ private class ReturnTypeCalculatorWithJump(
|
||||
)
|
||||
}
|
||||
|
||||
resolvedToContractsIfNecessary(declaration)
|
||||
|
||||
val returnTypeRef = declaration.returnTypeRef
|
||||
if (returnTypeRef is FirResolvedTypeRef) return returnTypeRef
|
||||
|
||||
@@ -257,6 +261,20 @@ private class ReturnTypeCalculatorWithJump(
|
||||
}
|
||||
}
|
||||
|
||||
private fun resolvedToContractsIfNecessary(declaration: FirCallableDeclaration) {
|
||||
if (declaration.resolvePhase >= FirResolvePhase.CONTRACTS) return
|
||||
|
||||
val canHaveContracts = when {
|
||||
declaration is FirProperty && !declaration.isLocal -> true
|
||||
declaration is FirSimpleFunction && !declaration.isLocal -> true
|
||||
else -> false
|
||||
}
|
||||
|
||||
if (canHaveContracts) {
|
||||
declaration.ensureResolved(FirResolvePhase.CONTRACTS)
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(PrivateForInline::class)
|
||||
private fun computeReturnTypeRef(declaration: FirCallableDeclaration): FirResolvedTypeRef {
|
||||
// To properly transform and resolve declaration's type, we need to use its module's session
|
||||
|
||||
Reference in New Issue
Block a user