[NI] Analyse lambda in factory pattern resolution in independent context

This commit is contained in:
Dmitriy Novozhilov
2020-04-24 13:14:12 +03:00
parent a0c7bece93
commit e7869bd9d4
10 changed files with 144 additions and 6 deletions
@@ -1800,6 +1800,11 @@ public class FirOldFrontendDiagnosticsTestWithStdlibGenerated extends AbstractFi
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/testsWithStdLib/factoryPattern"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
}
@TestMetadata("independentResolutionInLambda.kt")
public void testIndependentResolutionInLambda() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/factoryPattern/independentResolutionInLambda.kt");
}
@TestMetadata("overloadByLambdaReturnType_disabled.kt")
public void testOverloadByLambdaReturnType_disabled() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/factoryPattern/overloadByLambdaReturnType_disabled.kt");
@@ -95,7 +95,8 @@ class KotlinResolutionCallbacksImpl(
parameters: List<UnwrappedType>,
expectedReturnType: UnwrappedType?,
annotations: Annotations,
stubsForPostponedVariables: Map<NewTypeVariable, StubType>
stubsForPostponedVariables: Map<NewTypeVariable, StubType>,
shouldRunInIndependentContext: Boolean
): ReturnArgumentsAnalysisResult {
val psiCallArgument = lambdaArgument.psiCallArgument as PSIFunctionKotlinCallArgument
val outerCallContext = psiCallArgument.outerCallContext
@@ -137,7 +138,7 @@ class KotlinResolutionCallbacksImpl(
val lambdaInfo = LambdaInfo(
expectedReturnType ?: TypeUtils.NO_EXPECTED_TYPE,
if (expectedReturnType == null) ContextDependency.DEPENDENT else ContextDependency.INDEPENDENT
if (expectedReturnType != null || shouldRunInIndependentContext) ContextDependency.INDEPENDENT else ContextDependency.DEPENDENT
)
val builtIns = outerCallContext.scope.ownerDescriptor.builtIns
@@ -72,7 +72,8 @@ interface KotlinResolutionCallbacks {
parameters: List<UnwrappedType>,
expectedReturnType: UnwrappedType?, // null means, that return type is not proper i.e. it depends on some type variables
annotations: Annotations,
stubsForPostponedVariables: Map<NewTypeVariable, StubType>
stubsForPostponedVariables: Map<NewTypeVariable, StubType>,
shouldRunInIndependentContext: Boolean = false
): ReturnArgumentsAnalysisResult
fun bindStubResolvedCallForCandidate(candidate: ResolvedCallAtom)
@@ -115,7 +115,8 @@ class KotlinCallCompleter(
firstCandidate.getSystem().asPostponedArgumentsAnalyzerContext(),
resolutionCallbacks,
firstAtom,
diagnosticHolderForLambda
diagnosticHolderForLambda,
shouldRunInIndependentContext = true
)
lambdas.getValue(firstCandidate).setAnalyzedResults(results.returnArgumentsInfo, listOf(firstAtom))
@@ -70,7 +70,8 @@ class PostponedArgumentsAnalyzer(
c: Context,
resolutionCallbacks: KotlinResolutionCallbacks,
lambda: ResolvedLambdaAtom,
diagnosticHolder: KotlinDiagnosticsHolder
diagnosticHolder: KotlinDiagnosticsHolder,
shouldRunInIndependentContext: Boolean = false
): ReturnArgumentsAnalysisResult {
val stubsForPostponedVariables = c.bindingStubsForPostponedVariables()
val currentSubstitutor = c.buildCurrentSubstitutor(stubsForPostponedVariables.mapKeys { it.key.freshTypeConstructor(c) })
@@ -128,7 +129,8 @@ class PostponedArgumentsAnalyzer(
parameters,
expectedTypeForReturnArguments,
convertedAnnotations ?: Annotations.EMPTY,
stubsForPostponedVariables.cast()
stubsForPostponedVariables.cast(),
shouldRunInIndependentContext
)
val (returnArgumentsInfo, inferenceSession, inferedReturnType, hasInapplicableCallForBuilderInference) =
returnArgumentsAnalysisResult
@@ -0,0 +1,41 @@
// !LANGUAGE: +NewInference +FactoryPatternResolution
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNUSED_EXPRESSION
// ISSUE: KT-11265
// FILE: FactoryPattern.kt
package annotations
annotation class FactoryPattern
// FILE: main.kt
import annotations.FactoryPattern
@kotlin.jvm.JvmName("myFlatMapIterable")
@FactoryPattern
fun <T, R> Sequence<T>.myFlatMap(transform: (T) -> Iterable<R>): Sequence<R> {
TODO()
}
fun <T, R> Sequence<T>.myFlatMap(transform: (T) -> Sequence<R>): Sequence<R> {
TODO()
}
interface A {
val supertypes: Collection<B>
}
interface B {
val descriptors: Sequence<C>?
}
interface C
fun <K : Any> elvis(x: K?, y: K): K = y
fun test(a: A) {
a.supertypes.asSequence().<!AMBIGUITY!>myFlatMap<!> {
<!INAPPLICABLE_CANDIDATE!>elvis<!>(<!UNRESOLVED_REFERENCE!>it<!>.<!UNRESOLVED_REFERENCE!>descriptors<!>, sequenceOf())
}
}
@@ -0,0 +1,41 @@
// !LANGUAGE: +NewInference +FactoryPatternResolution
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNUSED_EXPRESSION
// ISSUE: KT-11265
// FILE: FactoryPattern.kt
package annotations
annotation class FactoryPattern
// FILE: main.kt
import annotations.FactoryPattern
@kotlin.jvm.JvmName("myFlatMapIterable")
@FactoryPattern
fun <T, R> Sequence<T>.myFlatMap(transform: (T) -> Iterable<R>): Sequence<R> {
TODO()
}
fun <T, R> Sequence<T>.myFlatMap(transform: (T) -> Sequence<R>): Sequence<R> {
TODO()
}
interface A {
val supertypes: Collection<B>
}
interface B {
val descriptors: Sequence<C>?
}
interface C
fun <K : Any> elvis(x: K?, y: K): K = y
fun test(a: A) {
a.supertypes.asSequence().myFlatMap {
elvis(it.descriptors, sequenceOf())
}
}
@@ -0,0 +1,36 @@
package
public fun </*0*/ K : kotlin.Any> elvis(/*0*/ x: K?, /*1*/ y: K): K
public fun test(/*0*/ a: A): kotlin.Unit
@kotlin.jvm.JvmName(name = "myFlatMapIterable") @annotations.FactoryPattern public fun </*0*/ T, /*1*/ R> kotlin.sequences.Sequence<T>.myFlatMap(/*0*/ transform: (T) -> kotlin.collections.Iterable<R>): kotlin.sequences.Sequence<R>
public fun </*0*/ T, /*1*/ R> kotlin.sequences.Sequence<T>.myFlatMap(/*0*/ transform: (T) -> kotlin.sequences.Sequence<R>): kotlin.sequences.Sequence<R>
public interface A {
public abstract val supertypes: kotlin.collections.Collection<B>
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface B {
public abstract val descriptors: kotlin.sequences.Sequence<C>?
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface C {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
package annotations {
public final annotation class FactoryPattern : kotlin.Annotation {
public constructor FactoryPattern()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
@@ -2815,6 +2815,11 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/testsWithStdLib/factoryPattern"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
}
@TestMetadata("independentResolutionInLambda.kt")
public void testIndependentResolutionInLambda() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/factoryPattern/independentResolutionInLambda.kt");
}
@TestMetadata("overloadByLambdaReturnType_disabled.kt")
public void testOverloadByLambdaReturnType_disabled() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/factoryPattern/overloadByLambdaReturnType_disabled.kt");
@@ -2815,6 +2815,11 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/testsWithStdLib/factoryPattern"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
}
@TestMetadata("independentResolutionInLambda.kt")
public void testIndependentResolutionInLambda() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/factoryPattern/independentResolutionInLambda.kt");
}
@TestMetadata("overloadByLambdaReturnType_disabled.kt")
public void testOverloadByLambdaReturnType_disabled() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/factoryPattern/overloadByLambdaReturnType_disabled.kt");