From 932eed2c142e72fdc5592cf048ff74cc65e413ce Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Tue, 9 Mar 2021 12:45:31 +0300 Subject: [PATCH] [FIR] Fix determining nullability of arguments during enhancement --- ...TouchedTilContractsPhaseTestGenerated.java | 5 +++ .../typeMismatchOnExpectedJavaMap.fir.txt | 4 +++ .../typeMismatchOnExpectedJavaMap.kt | 26 +++++++++++++++ .../runners/FirDiagnosticTestGenerated.java | 6 ++++ ...DiagnosticsWithLightTreeTestGenerated.java | 6 ++++ .../enhancement/EnhancementSignatureParts.kt | 2 +- .../fir/java/enhancement/javaTypeUtils.kt | 33 +++++++++++++++++-- 7 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 compiler/fir/analysis-tests/testData/resolve/arguments/typeMismatchOnExpectedJavaMap.fir.txt create mode 100644 compiler/fir/analysis-tests/testData/resolve/arguments/typeMismatchOnExpectedJavaMap.kt diff --git a/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java b/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java index 5bc0c11ab54..50d7f4b52a7 100644 --- a/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java +++ b/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java @@ -601,6 +601,11 @@ public class LazyBodyIsNotTouchedTilContractsPhaseTestGenerated extends Abstract runTest("compiler/fir/analysis-tests/testData/resolve/arguments/tryInLambda.kt"); } + @TestMetadata("typeMismatchOnExpectedJavaMap.kt") + public void testTypeMismatchOnExpectedJavaMap() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/arguments/typeMismatchOnExpectedJavaMap.kt"); + } + @TestMetadata("untouchedReturnInIf.kt") public void testUntouchedReturnInIf() throws Exception { runTest("compiler/fir/analysis-tests/testData/resolve/arguments/untouchedReturnInIf.kt"); diff --git a/compiler/fir/analysis-tests/testData/resolve/arguments/typeMismatchOnExpectedJavaMap.fir.txt b/compiler/fir/analysis-tests/testData/resolve/arguments/typeMismatchOnExpectedJavaMap.fir.txt new file mode 100644 index 00000000000..56ced3756e0 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/arguments/typeMismatchOnExpectedJavaMap.fir.txt @@ -0,0 +1,4 @@ +FILE: main.kt + public final fun test(executor: R|BuildActionExecuter|, modelType: R|java/lang/Class|, env: R|kotlin/collections/Map|): R|kotlin/Unit| { + lval model: R|ft!>, BuildActionExecuter!>?>!| = R|/executor|.R|SubstitutionOverride!>, BuildActionExecuter!>?>!|>|(R|/env|) + } diff --git a/compiler/fir/analysis-tests/testData/resolve/arguments/typeMismatchOnExpectedJavaMap.kt b/compiler/fir/analysis-tests/testData/resolve/arguments/typeMismatchOnExpectedJavaMap.kt new file mode 100644 index 00000000000..dc7e4cef2ba --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/arguments/typeMismatchOnExpectedJavaMap.kt @@ -0,0 +1,26 @@ +// FULL_JDK + +// FILE: LongRunningOperation.java +import java.util.Map; +import org.jetbrains.annotations.Nullable; + +public interface LongRunningOperation { + LongRunningOperation setEnvironmentVariables(@Nullable Map envVariables); +} + +// FILE: ConfigurableLauncher.java +import java.util.Map; + +public interface ConfigurableLauncher> extends LongRunningOperation { + @Override + T setEnvironmentVariables(Map envVariables); +} + +// FILE: BuildActionExecuter.java +public interface BuildActionExecuter extends ConfigurableLauncher> {} + +// FILE: main.kt + +fun test(executor: BuildActionExecuter, modelType: Class, env: Map) { + val model = executor.setEnvironmentVariables(env) +} diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java index 6ae8fcb5344..c7b0e097d72 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java @@ -707,6 +707,12 @@ public class FirDiagnosticTestGenerated extends AbstractFirDiagnosticTest { runTest("compiler/fir/analysis-tests/testData/resolve/arguments/tryInLambda.kt"); } + @Test + @TestMetadata("typeMismatchOnExpectedJavaMap.kt") + public void testTypeMismatchOnExpectedJavaMap() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/arguments/typeMismatchOnExpectedJavaMap.kt"); + } + @Test @TestMetadata("untouchedReturnInIf.kt") public void testUntouchedReturnInIf() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java index 6dd609c2037..d79063baea4 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java @@ -711,6 +711,12 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos runTest("compiler/fir/analysis-tests/testData/resolve/arguments/tryInLambda.kt"); } + @Test + @TestMetadata("typeMismatchOnExpectedJavaMap.kt") + public void testTypeMismatchOnExpectedJavaMap() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/arguments/typeMismatchOnExpectedJavaMap.kt"); + } + @Test @TestMetadata("untouchedReturnInIf.kt") public void testUntouchedReturnInIf() throws Exception { diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/EnhancementSignatureParts.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/EnhancementSignatureParts.kt index 9493af4e75f..099fba4f5c6 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/EnhancementSignatureParts.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/EnhancementSignatureParts.kt @@ -248,7 +248,7 @@ internal class EnhancementSignatureParts( val superQualifiers = fromSupertypes.map { it.extractQualifiers(session) } val mutabilityFromSupertypes = superQualifiers.mapNotNull { it.mutability }.toSet() val nullabilityFromSupertypes = superQualifiers.mapNotNull { it.nullability }.toSet() - val nullabilityFromSupertypesWithWarning = fromOverridden + val nullabilityFromSupertypesWithWarning = fromSupertypes .mapNotNull { it.extractQualifiers(session).nullability } .toSet() diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/javaTypeUtils.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/javaTypeUtils.kt index f11796b6578..cf98bb42d3a 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/javaTypeUtils.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/javaTypeUtils.kt @@ -28,6 +28,8 @@ import org.jetbrains.kotlin.fir.symbols.impl.ConeClassLikeLookupTagImpl import org.jetbrains.kotlin.fir.typeContext import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef +import org.jetbrains.kotlin.fir.types.builder.buildStarProjection +import org.jetbrains.kotlin.fir.types.builder.buildTypeProjectionWithVariance import org.jetbrains.kotlin.fir.types.jvm.FirJavaTypeRef import org.jetbrains.kotlin.load.java.JavaDefaultQualifiers import org.jetbrains.kotlin.load.java.structure.JavaClassifierType @@ -38,6 +40,7 @@ import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.types.AbstractStrictEqualityTypeChecker import org.jetbrains.kotlin.types.ConstantValueKind +import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.utils.extractRadix internal class IndexedJavaTypeQualifiers(private val data: Array) { @@ -253,8 +256,34 @@ internal data class TypeAndDefaultQualifiers( val defaultQualifiers: JavaDefaultQualifiers? ) -internal fun FirTypeRef.typeArguments(): List = - (this as? FirUserTypeRef)?.qualifier?.lastOrNull()?.typeArgumentList?.typeArguments.orEmpty() +internal fun FirTypeRef.typeArguments(): List = when (this) { + is FirUserTypeRef -> qualifier.lastOrNull()?.typeArgumentList?.typeArguments.orEmpty() + is FirResolvedTypeRef -> type.typeArguments.map { + when (it) { + is ConeStarProjection -> buildStarProjection {} + else -> { + val kind = it.kind + val type = when (it) { + is ConeKotlinTypeProjection -> it.type + is ConeKotlinType -> it + else -> error("Should not be here") + } + buildTypeProjectionWithVariance { + variance = when (kind) { + ProjectionKind.IN -> Variance.IN_VARIANCE + ProjectionKind.OUT -> Variance.OUT_VARIANCE + ProjectionKind.INVARIANT -> Variance.INVARIANT + else -> error("Should not be here") + } + typeRef = buildResolvedTypeRef { + this.type = type + } + } + } + } + } + else -> emptyList() +} internal fun JavaType.typeArguments(): List = (this as? JavaClassifierType)?.typeArguments.orEmpty()