Skip to content

Commit 787a8bb

Browse files
committed
Revert "[NI] Disable capturing/approximation type in TypeSubstitutor with enabled NI"
This reverts commit 7c4101e. #KT-31866 Fixed #KT-31868 Fixed #EA-125401 Fixed #KT-25290 Open
1 parent f48640f commit 787a8bb

File tree

79 files changed

+197
-887
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+197
-887
lines changed

compiler/cli/cli-common/src/org/jetbrains/kotlin/analyzer/common/CommonResolverForModuleFactory.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import org.jetbrains.kotlin.resolve.lazy.declarations.DeclarationProviderFactory
4545
import org.jetbrains.kotlin.resolve.lazy.declarations.DeclarationProviderFactoryService
4646
import org.jetbrains.kotlin.serialization.deserialization.MetadataPackageFragmentProvider
4747
import org.jetbrains.kotlin.serialization.deserialization.MetadataPartProvider
48-
import org.jetbrains.kotlin.types.SubstitutingScopeProviderImpl
4948

5049
class CommonAnalysisParameters(
5150
val metadataPartProviderFactory: (ModuleContent<*>) -> MetadataPartProvider
@@ -171,7 +170,6 @@ private fun createContainerToResolveCommonCode(
171170

172171
configureCommonSpecificComponents()
173172
useInstance(metadataPartProvider)
174-
useImpl<SubstitutingScopeProviderImpl>()
175173

176174
val metadataFinderFactory = ServiceManager.getService(
177175
moduleContext.project,

compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java

Lines changed: 0 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/frontend.java/src/org/jetbrains/kotlin/frontend/java/di/injection.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import org.jetbrains.kotlin.resolve.jvm.JavaDescriptorResolver
4646
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatformAnalyzerServices
4747
import org.jetbrains.kotlin.resolve.lazy.KotlinCodeAnalyzer
4848
import org.jetbrains.kotlin.resolve.lazy.declarations.DeclarationProviderFactory
49-
import org.jetbrains.kotlin.types.SubstitutingScopeProviderImpl
5049

5150
fun createContainerForLazyResolveWithJava(
5251
jvmPlatform: TargetPlatform,
@@ -128,7 +127,6 @@ fun StorageComponentContainer.configureJavaSpecificComponents(
128127
)
129128

130129
useImpl<FilesByFacadeFqNameIndexer>()
131-
useImpl<SubstitutingScopeProviderImpl>()
132130
}
133131

134132
fun ComponentProvider.initJvmBuiltInsForTopDownAnalysis() {

compiler/frontend/src/org/jetbrains/kotlin/descriptors/impl/LocalVariableDescriptor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ public void setOutType(KotlinType outType) {
8181
@NotNull
8282
@Override
8383
public LocalVariableDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
84-
return this;
84+
if (substitutor.isEmpty()) return this;
85+
throw new UnsupportedOperationException(); // TODO
8586
}
8687

8788
@Override

compiler/frontend/src/org/jetbrains/kotlin/diagnostics/diagnosticUtils.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import org.jetbrains.kotlin.resolve.calls.inference.wrapWithCapturingSubstitutio
3838
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
3939
import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall
4040
import org.jetbrains.kotlin.types.KotlinType
41-
import org.jetbrains.kotlin.types.SubstitutingScopeProvider
4241
import org.jetbrains.kotlin.types.TypeConstructorSubstitution
4342
import org.jetbrains.kotlin.types.TypeUtils
4443
import org.jetbrains.kotlin.types.typeUtil.isAnyOrNullableAny
@@ -78,9 +77,7 @@ fun ResolutionContext<*>.reportTypeMismatchDueToTypeProjection(
7877
TypeConstructorSubstitution
7978
.create(receiverType)
8079
.wrapWithCapturingSubstitution(needApproximation = false)
81-
.buildSubstitutor().apply {
82-
setSubstitutingScopeProvider(SubstitutingScopeProvider.DEFAULT)
83-
}.let { callableDescriptor.substitute(it) } ?: return false
80+
.buildSubstitutor().let { callableDescriptor.substitute(it) } ?: return false
8481

8582
val nonApproximatedExpectedType = correspondingNotApproximatedTypeByDescriptor(substitutedDescriptor) ?: return false
8683
if (!TypeUtils.contains(nonApproximatedExpectedType) { it.isCaptured() }) return false

compiler/frontend/src/org/jetbrains/kotlin/frontend/di/injection.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import org.jetbrains.kotlin.resolve.calls.tower.KotlinResolutionStatelessCallbac
3434
import org.jetbrains.kotlin.resolve.checkers.ExperimentalUsageChecker
3535
import org.jetbrains.kotlin.resolve.lazy.*
3636
import org.jetbrains.kotlin.resolve.lazy.declarations.DeclarationProviderFactory
37-
import org.jetbrains.kotlin.types.SubstitutingScopeProviderImpl
3837
import org.jetbrains.kotlin.types.expressions.DeclarationScopeProviderForLocalClassifierAnalyzer
3938
import org.jetbrains.kotlin.types.expressions.LocalClassDescriptorHolder
4039
import org.jetbrains.kotlin.types.expressions.LocalLazyDeclarationResolver
@@ -118,7 +117,6 @@ fun createContainerForBodyResolve(
118117
useImpl<AnnotationResolverImpl>()
119118

120119
useImpl<BodyResolver>()
121-
useImpl<SubstitutingScopeProviderImpl>()
122120
}
123121

124122
fun createContainerForLazyBodyResolve(
@@ -138,7 +136,6 @@ fun createContainerForLazyBodyResolve(
138136
useImpl<AnnotationResolverImpl>()
139137
useImpl<LazyTopDownAnalyzer>()
140138
useImpl<BasicAbsentDescriptorHandler>()
141-
useImpl<SubstitutingScopeProviderImpl>()
142139
}
143140

144141
fun createContainerForLazyLocalClassifierAnalyzer(
@@ -169,7 +166,6 @@ fun createContainerForLazyLocalClassifierAnalyzer(
169166
useImpl<DeclarationScopeProviderForLocalClassifierAnalyzer>()
170167
useImpl<LocalLazyDeclarationResolver>()
171168

172-
useImpl<SubstitutingScopeProviderImpl>()
173169
useInstance(statementFilter)
174170
}
175171

@@ -187,7 +183,6 @@ fun createContainerForLazyResolve(
187183
configureStandardResolveComponents()
188184

189185
useInstance(declarationProviderFactory)
190-
useImpl<SubstitutingScopeProviderImpl>()
191186

192187
targetEnvironment.configure(this)
193188

compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -911,8 +911,7 @@ private PropertyDescriptor resolveAsPropertyDescriptor(
911911
container instanceof ClassDescriptor && ((ClassDescriptor) container).isExpect(),
912912
modifierList != null && PsiUtilsKt.hasActualModifier(modifierList),
913913
modifierList != null && modifierList.hasModifier(KtTokens.EXTERNAL_KEYWORD),
914-
propertyInfo.getHasDelegate(),
915-
new SubstitutingScopeProviderImpl(languageVersionSettings)
914+
propertyInfo.getHasDelegate()
916915
);
917916

918917
List<TypeParameterDescriptorImpl> typeParameterDescriptors;

compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt

Lines changed: 21 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ package org.jetbrains.kotlin.resolve.calls.tower
88
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
99
import org.jetbrains.kotlin.descriptors.*
1010
import org.jetbrains.kotlin.descriptors.annotations.Annotations
11-
import org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl
12-
import org.jetbrains.kotlin.descriptors.impl.PropertySetterDescriptorImpl
1311
import org.jetbrains.kotlin.diagnostics.Diagnostic
1412
import org.jetbrains.kotlin.diagnostics.Errors
1513
import org.jetbrains.kotlin.psi.*
@@ -25,13 +23,12 @@ import org.jetbrains.kotlin.resolve.calls.components.AdditionalDiagnosticReporte
2523
import org.jetbrains.kotlin.resolve.calls.components.isVararg
2624
import org.jetbrains.kotlin.resolve.calls.context.BasicCallResolutionContext
2725
import org.jetbrains.kotlin.resolve.calls.context.CallPosition
28-
import org.jetbrains.kotlin.resolve.calls.inference.approximateCapturedTypes
2926
import org.jetbrains.kotlin.resolve.calls.inference.buildResultingSubstitutor
3027
import org.jetbrains.kotlin.resolve.calls.inference.components.FreshVariableNewTypeSubstitutor
3128
import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstitutor
3229
import org.jetbrains.kotlin.resolve.calls.inference.model.*
3330
import org.jetbrains.kotlin.resolve.calls.inference.substitute
34-
import org.jetbrains.kotlin.resolve.calls.inference.substituteAndApproximateIntegerLiteralTypes
31+
import org.jetbrains.kotlin.resolve.calls.inference.substituteAndApproximateCapturedTypes
3532
import org.jetbrains.kotlin.resolve.calls.model.*
3633
import org.jetbrains.kotlin.resolve.calls.resolvedCallUtil.makeNullableTypeIfSafeReceiver
3734
import org.jetbrains.kotlin.resolve.calls.results.ResolutionStatus
@@ -41,13 +38,14 @@ import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind
4138
import org.jetbrains.kotlin.resolve.calls.tasks.TracingStrategy
4239
import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant
4340
import org.jetbrains.kotlin.resolve.constants.IntegerLiteralTypeConstructor
41+
import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstant
4442
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
4543
import org.jetbrains.kotlin.resolve.deprecation.DeprecationResolver
46-
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
4744
import org.jetbrains.kotlin.resolve.scopes.receivers.CastImplicitClassReceiver
4845
import org.jetbrains.kotlin.resolve.scopes.receivers.ImplicitClassReceiver
4946
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
5047
import org.jetbrains.kotlin.types.*
48+
import org.jetbrains.kotlin.types.checker.NewCapturedType
5149
import org.jetbrains.kotlin.types.expressions.DataFlowAnalyzer
5250
import org.jetbrains.kotlin.types.expressions.DoubleColonExpressionResolver
5351
import org.jetbrains.kotlin.types.expressions.ExpressionTypingServices
@@ -295,14 +293,15 @@ class KotlinToResolvedCallTransformer(
295293
// todo external argument
296294

297295
val argumentExpression = valueArgument.getArgumentExpression() ?: continue
298-
updateRecordedType(argumentExpression, parameter, newContext)
296+
updateRecordedType(argumentExpression, parameter, newContext, resolvedCall.isReallySuccess())
299297
}
300298
}
301299

302300
fun updateRecordedType(
303301
expression: KtExpression,
304302
parameter: ValueParameterDescriptor?,
305-
context: BasicCallResolutionContext
303+
context: BasicCallResolutionContext,
304+
reportErrorForTypeMismatch: Boolean
306305
): KotlinType? {
307306
val deparenthesized = expression.let {
308307
KtPsiUtil.getLastElementDeparenthesized(it, context.statementFilter)
@@ -319,6 +318,9 @@ class KotlinToResolvedCallTransformer(
319318
updatedType = argumentTypeResolver.updateResultArgumentTypeIfNotDenotable(context, deparenthesized) ?: updatedType
320319
}
321320

321+
322+
var reportErrorDuringTypeCheck = reportErrorForTypeMismatch
323+
322324
if (parameter != null && ImplicitIntegerCoercion.isEnabledForParameter(parameter)) {
323325
val argumentCompileTimeValue = context.trace[BindingContext.COMPILE_TIME_VALUE, deparenthesized]
324326
if (argumentCompileTimeValue != null && argumentCompileTimeValue.parameters.isConvertableConstVal) {
@@ -327,14 +329,15 @@ class KotlinToResolvedCallTransformer(
327329
updatedType = argumentTypeResolver.updateResultArgumentTypeIfNotDenotable(
328330
context.trace, context.statementFilter, context.expectedType, generalNumberType, expression
329331
)
332+
reportErrorDuringTypeCheck = true
330333
}
331334

332335
}
333336
}
334337

335338
updatedType = updateRecordedTypeForArgument(updatedType, recordedType, expression, context)
336339

337-
dataFlowAnalyzer.checkType(updatedType, deparenthesized, context, false)
340+
dataFlowAnalyzer.checkType(updatedType, deparenthesized, context, reportErrorDuringTypeCheck)
338341

339342
return updatedType
340343
}
@@ -687,44 +690,32 @@ class NewResolvedCallImpl<D : CallableDescriptor>(
687690
@Suppress("UNCHECKED_CAST")
688691
resultingDescriptor = run {
689692
val candidateDescriptor = resolvedCallAtom.candidateDescriptor
690-
val containsIntegerLiteralTypes = resolvedCallAtom.candidateDescriptor.returnType?.contains {
691-
it.constructor is IntegerLiteralTypeConstructor
692-
} ?: false
693+
val containsCapturedTypes = resolvedCallAtom.candidateDescriptor.returnType?.contains { it is NewCapturedType } ?: false
694+
val containsIntegerLiteralTypes = resolvedCallAtom.candidateDescriptor.returnType?.contains { it.constructor is IntegerLiteralTypeConstructor } ?: false
693695

694696
when {
695697
candidateDescriptor is FunctionDescriptor ||
696-
(candidateDescriptor is PropertyDescriptor && candidateDescriptor.typeParameters.isNotEmpty() || containsIntegerLiteralTypes) ->
698+
(candidateDescriptor is PropertyDescriptor && (candidateDescriptor.typeParameters.isNotEmpty() || containsCapturedTypes || containsIntegerLiteralTypes)) ->
697699
// this code is very suspicious. Now it is very useful for BE, because they cannot do nothing with captured types,
698700
// but it seems like temporary solution.
699-
candidateDescriptor.substituteAndApproximateIntegerLiteralTypes(resolvedCallAtom.substitutor).let {
700-
if (substitutor != null) {
701-
it.substitute(substitutor)
702-
} else {
703-
it
704-
}
705-
}
706-
701+
candidateDescriptor.substitute(resolvedCallAtom.substitutor).substituteAndApproximateCapturedTypes(
702+
substitutor ?: FreshVariableNewTypeSubstitutor.Empty
703+
)
707704
else ->
708705
candidateDescriptor
709706
}
710707
} as D
711708

712709
typeArguments = resolvedCallAtom.substitutor.freshVariables.map {
713-
(substitutor ?: FreshVariableNewTypeSubstitutor.Empty).safeSubstitute(it.defaultType)
710+
val substituted = (substitutor ?: FreshVariableNewTypeSubstitutor.Empty).safeSubstitute(it.defaultType)
711+
TypeApproximator(substituted.constructor.builtIns)
712+
.approximateToSuperType(substituted, TypeApproximatorConfiguration.IntegerLiteralsTypesApproximation)
713+
?: substituted
714714
}
715715

716716
calculateExpectedTypeForSamConvertedArgumentMap(substitutor)
717717
}
718718

719-
fun approximateCapturedTypesAndHackSetters() {
720-
val approximator = TypeApproximator(resultingDescriptor.builtIns)
721-
resultingDescriptor = resultingDescriptor.hackSettersAccordingToCapturedOutTypes()
722-
resultingDescriptor = resultingDescriptor.approximateCapturedTypes()
723-
typeArguments = typeArguments.map {
724-
approximator.approximateToSuperType(it, TypeApproximatorConfiguration.CapturedAndIntegerLiteralsTypesApproximation) ?: it
725-
}
726-
}
727-
728719
fun getExpectedTypeForSamConvertedArgument(valueArgument: ValueArgument): UnwrappedType? =
729720
expedtedTypeForSamConvertedArgumentMap?.get(valueArgument)
730721

@@ -817,41 +808,3 @@ fun NewResolvedCallImpl<*>.hasInferredReturnType(): Boolean {
817808
val returnType = this.resultingDescriptor.returnType ?: return false
818809
return !returnType.contains { ErrorUtils.isUninferredParameter(it) }
819810
}
820-
821-
fun ResolvedCall<*>.approximateCapturedTypesAndHackSetters() {
822-
when (this) {
823-
is NewResolvedCallImpl<*> -> approximateCapturedTypesAndHackSetters()
824-
is NewVariableAsFunctionResolvedCallImpl -> {
825-
functionCall.approximateCapturedTypesAndHackSetters()
826-
variableCall.approximateCapturedTypesAndHackSetters()
827-
}
828-
else -> throw UnsupportedOperationException("Illegal resolved call: $this")
829-
}
830-
}
831-
832-
fun <D : CallableDescriptor> D.hackSettersAccordingToCapturedOutTypes(): D {
833-
return when (this) {
834-
is PropertyDescriptorImpl -> hackSettersAccordingToCapturedOutTypes() as D
835-
else -> this
836-
}
837-
}
838-
839-
private fun PropertyDescriptorImpl.hackSettersAccordingToCapturedOutTypes(): PropertyDescriptor {
840-
val setter = setter ?: return this
841-
val valueParameter = setter.valueParameters.first()
842-
val inputType = valueParameter.type
843-
val approximatedType = TypeApproximator(builtIns).approximateToSubType(
844-
inputType.unwrap(),
845-
TypeApproximatorConfiguration.CapturedAndIntegerLiteralsTypesApproximation
846-
) ?: return this
847-
848-
val newProperty = newCopyBuilder().setOriginal(original).build() as PropertyDescriptorImpl
849-
850-
val newSetter = with(setter) {
851-
PropertySetterDescriptorImpl(newProperty, annotations, modality, visibility, isDefault, isExternal, isInline, kind, original, source)
852-
}
853-
newSetter.initialize(PropertySetterDescriptorImpl.createSetterParameter(newSetter, approximatedType, setter.annotations))
854-
newProperty.initialize(getter, newSetter, backingField, delegateField)
855-
newProperty.isSetterProjectedOut = true
856-
return newProperty
857-
}

compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,6 @@ class PSICallResolver(
382382

383383
return cache.getOrPut(implicitReceiver) {
384384
context.transformToReceiverWithSmartCastInfo(implicitReceiver.value)
385-
.prepareReceiverRegardingCaptureTypes()
386385
}
387386
}
388387
}

compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ class ResolvedAtomCompleter(
110110

111111
kotlinToResolvedCallTransformer.reportDiagnostics(topLevelCallContext, topLevelTrace, resolvedCall, diagnostics)
112112

113-
resolvedCall.approximateCapturedTypesAndHackSetters()
114113
return resolvedCall
115114
}
116115

@@ -156,7 +155,9 @@ class ResolvedAtomCompleter(
156155
.replaceBindingTrace(topLevelTrace)
157156

158157
val argumentExpression = resultValueArgument.valueArgument.getArgumentExpression() ?: continue
159-
kotlinToResolvedCallTransformer.updateRecordedType(argumentExpression, parameter = null, context = newContext)
158+
kotlinToResolvedCallTransformer.updateRecordedType(
159+
argumentExpression, parameter = null, context = newContext, reportErrorForTypeMismatch = true
160+
)
160161
}
161162
}
162163

compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/LazyClassContext.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ import org.jetbrains.kotlin.resolve.*
2424
import org.jetbrains.kotlin.resolve.extensions.SyntheticResolveExtension
2525
import org.jetbrains.kotlin.resolve.lazy.declarations.DeclarationProviderFactory
2626
import org.jetbrains.kotlin.storage.StorageManager
27-
import org.jetbrains.kotlin.types.SubstitutingScopeProvider
2827
import org.jetbrains.kotlin.types.WrappedTypeFactory
2928

3029
interface LazyClassContext {
3130
val declarationScopeProvider: DeclarationScopeProvider
32-
val substitutingScopeProvider: SubstitutingScopeProvider
3331

3432
val storageManager: StorageManager
3533
val trace: BindingTrace

0 commit comments

Comments
 (0)