From 829102b3e3e62019fcafbfca8d3d60342a53e903 Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Thu, 5 Sep 2019 14:35:29 +0300 Subject: [PATCH] FIR: Fix exception in inference Default SimpleTypeMarker::withNullability from ConeTypeContext doesn't take into account inference-related type kinds like type variables These cases are properly handled in org.jetbrains.kotlin.fir.resolve.ResolveUtilsKt#withNullability Original exception trace: at org.jetbrains.kotlin.fir.types.ConeTypeContext$DefaultImpls.withNullability(ConeTypeContext.kt:142) at org.jetbrains.kotlin.fir.resolve.calls.ConeInferenceContext$DefaultImpls.withNullability(ConeInferenceContext.kt) at org.jetbrains.kotlin.fir.resolve.transformers.FirBodyResolveTransformerKt$inferenceComponents$1.withNullability(FirBodyResolveTransformer.kt:943) at org.jetbrains.kotlin.fir.resolve.calls.ConeInferenceContext$DefaultImpls.makeSimpleTypeDefinitelyNotNullOrNotNull(ConeInferenceContext.kt:195) at org.jetbrains.kotlin.fir.resolve.transformers.FirBodyResolveTransformerKt$inferenceComponents$1.makeSimpleTypeDefinitelyNotNullOrNotNull(FirBodyResolveTransformer.kt:943) at org.jetbrains.kotlin.resolve.calls.inference.model.NewConstraintSystemImpl.makeSimpleTypeDefinitelyNotNullOrNotNull(NewConstraintSystemImpl.kt) at org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintInjector$TypeCheckerContext.makeSimpleTypeDefinitelyNotNullOrNotNull(ConstraintInjector.kt) at org.jetbrains.kotlin.resolve.calls.inference.components.AbstractTypeCheckerContextForConstraintSystem.simplifyLowerConstraint(AbstractTypeCheckerContextForConstraintSystem.kt:163) at org.jetbrains.kotlin.resolve.calls.inference.components.AbstractTypeCheckerContextForConstraintSystem.internalAddSubtypeConstraint(AbstractTypeCheckerContextForConstraintSystem.kt:71) at org.jetbrains.kotlin.resolve.calls.inference.components.AbstractTypeCheckerContextForConstraintSystem.addSubtypeConstraint(AbstractTypeCheckerContextForConstraintSystem.kt:50) at org.jetbrains.kotlin.types.AbstractTypeChecker.completeIsSubTypeOf(AbstractTypeChecker.kt:193) at org.jetbrains.kotlin.types.AbstractTypeChecker.isSubtypeOf(AbstractTypeChecker.kt:164) at org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintInjector$TypeCheckerContext.runIsSubtypeOf(ConstraintInjector.kt:153) at org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintInjector.addSubTypeConstraintAndIncorporateIt(ConstraintInjector.kt:77) at org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintInjector.addInitialSubtypeConstraint(ConstraintInjector.kt:55) at org.jetbrains.kotlin.resolve.calls.inference.model.NewConstraintSystemImpl.addSubtypeConstraint(NewConstraintSystemImpl.kt:84) at org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilderKt$addSubtypeConstraintIfCompatible$1.invoke(ConstraintSystemBuilder.kt:61) at org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilderKt$addSubtypeConstraintIfCompatible$1.invoke(ConstraintSystemBuilder.kt) at org.jetbrains.kotlin.resolve.calls.inference.model.NewConstraintSystemImpl.runTransaction(NewConstraintSystemImpl.kt:130) at org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilderKt.addSubtypeConstraintIfCompatible(ConstraintSystemBuilder.kt:60) at org.jetbrains.kotlin.fir.resolve.calls.ArgumentsKt.checkApplicabilityForArgumentType(Arguments.kt:170) at org.jetbrains.kotlin.fir.resolve.calls.ArgumentsKt.resolvePlainArgumentType(Arguments.kt:158) at org.jetbrains.kotlin.fir.resolve.calls.ArgumentsKt.resolveSubCallArgument(Arguments.kt:123) at org.jetbrains.kotlin.fir.resolve.calls.ArgumentsKt.resolveArgumentExpression(Arguments.kt:42) at org.jetbrains.kotlin.fir.resolve.calls.ArgumentsKt.resolveArgument(Arguments.kt:196) at org.jetbrains.kotlin.fir.resolve.calls.CheckArguments.check(ResolverParts.kt:145) at org.jetbrains.kotlin.fir.resolve.calls.CheckArguments$check$1.invokeSuspend(ResolverParts.kt) --- .../kotlin/fir/types/ConeTypeContext.kt | 15 +------------ .../j+k/complexFlexibleInference.kt | 21 +++++++++++++++++++ .../j+k/complexFlexibleInference.txt | 4 ++++ .../fir/FirDiagnosticsTestGenerated.java | 5 +++++ 4 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 compiler/fir/resolve/testData/diagnostics/j+k/complexFlexibleInference.kt create mode 100644 compiler/fir/resolve/testData/diagnostics/j+k/complexFlexibleInference.txt diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt index 6be033fddea..ce96102d769 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt @@ -127,20 +127,7 @@ interface ConeTypeContext : TypeSystemContext, TypeSystemOptimizationContext, Ty override fun SimpleTypeMarker.withNullability(nullable: Boolean): SimpleTypeMarker { require(this is ConeKotlinType) - if (nullability.isNullable == nullable) return this - return when (this) { - is ConeCapturedType -> ConeCapturedType(captureStatus, lowerType, ConeNullability.create(nullable), constructor) - is ConeTypeParameterType -> ConeTypeParameterTypeImpl(lookupTag, nullable) - is ConeClassErrorType -> this - is ConeClassType -> ConeClassTypeImpl(lookupTag, typeArguments, nullable) - is ConeAbbreviatedType -> ConeAbbreviatedTypeImpl( - lookupTag, - typeArguments, - nullable - ) - is ConeIntersectionType -> this.withNullability(ConeNullability.create(nullable)) - else -> error("!") - } + return withNullability(ConeNullability.create(nullable)) } override fun SimpleTypeMarker.typeConstructor(): TypeConstructorMarker { diff --git a/compiler/fir/resolve/testData/diagnostics/j+k/complexFlexibleInference.kt b/compiler/fir/resolve/testData/diagnostics/j+k/complexFlexibleInference.kt new file mode 100644 index 00000000000..8d4f99ccd3a --- /dev/null +++ b/compiler/fir/resolve/testData/diagnostics/j+k/complexFlexibleInference.kt @@ -0,0 +1,21 @@ +// FILE: BindingContext.java +public interface BindingContext { + @org.jetbrains.annotations.Nullable + V get(ReadOnlySlice slice, K key); +} + +// FILE: ReadOnlySlice.java +public interface ReadOnlySlice {} + +// FILE: Slices.java + +public class Slices { + public static ReadOnlySlice X = null; + public static ReadOnlySlice Y = null; +} + +// FILE: main.kt + +fun bar(bindingContext: BindingContext) { + bindingContext[Slices.X, bindingContext[Slices.Y, 1]] +} diff --git a/compiler/fir/resolve/testData/diagnostics/j+k/complexFlexibleInference.txt b/compiler/fir/resolve/testData/diagnostics/j+k/complexFlexibleInference.txt new file mode 100644 index 00000000000..66ee2e62e45 --- /dev/null +++ b/compiler/fir/resolve/testData/diagnostics/j+k/complexFlexibleInference.txt @@ -0,0 +1,4 @@ +FILE: main.kt + public final fun bar(bindingContext: R|BindingContext|): R|kotlin/Unit| { + R|/bindingContext|.R|/BindingContext.get|!|, R|ft!|>(Q|Slices|.R|/Slices.X|, R|/bindingContext|.R|/BindingContext.get|!|, R|ft!|>(Q|Slices|.R|/Slices.Y|, Int(1))) + } diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java index 21ba746e28e..cac777cc9d5 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java @@ -41,6 +41,11 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/fir/resolve/testData/diagnostics/j+k"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); } + @TestMetadata("complexFlexibleInference.kt") + public void testComplexFlexibleInference() throws Exception { + runTest("compiler/fir/resolve/testData/diagnostics/j+k/complexFlexibleInference.kt"); + } + @TestMetadata("KJKComplexHierarchy.kt") public void testKJKComplexHierarchy() throws Exception { runTest("compiler/fir/resolve/testData/diagnostics/j+k/KJKComplexHierarchy.kt");