[Misc] Make ArgumentTypeResolver::isSubtypeOfForArgumentType not static

Later, it will use type-refinement component that'll be injected there
This commit is contained in:
Denis Zharkov
2019-04-24 12:21:42 +03:00
committed by Dmitry Savvinov
parent 529763b9bd
commit 7ba3f7e599
3 changed files with 15 additions and 11 deletions
@@ -55,7 +55,6 @@ import static org.jetbrains.kotlin.types.TypeUtils.NO_EXPECTED_TYPE;
public class ArgumentTypeResolver { public class ArgumentTypeResolver {
@NotNull private final TypeResolver typeResolver; @NotNull private final TypeResolver typeResolver;
@NotNull private final DoubleColonExpressionResolver doubleColonExpressionResolver;
@NotNull private final KotlinBuiltIns builtIns; @NotNull private final KotlinBuiltIns builtIns;
@NotNull private final ReflectionTypes reflectionTypes; @NotNull private final ReflectionTypes reflectionTypes;
@NotNull private final ConstantExpressionEvaluator constantExpressionEvaluator; @NotNull private final ConstantExpressionEvaluator constantExpressionEvaluator;
@@ -63,10 +62,10 @@ public class ArgumentTypeResolver {
@NotNull private final ModuleDescriptor moduleDescriptor; @NotNull private final ModuleDescriptor moduleDescriptor;
private ExpressionTypingServices expressionTypingServices; private ExpressionTypingServices expressionTypingServices;
private DoubleColonExpressionResolver doubleColonExpressionResolver;
public ArgumentTypeResolver( public ArgumentTypeResolver(
@NotNull TypeResolver typeResolver, @NotNull TypeResolver typeResolver,
@NotNull DoubleColonExpressionResolver doubleColonExpressionResolver,
@NotNull KotlinBuiltIns builtIns, @NotNull KotlinBuiltIns builtIns,
@NotNull ReflectionTypes reflectionTypes, @NotNull ReflectionTypes reflectionTypes,
@NotNull ConstantExpressionEvaluator constantExpressionEvaluator, @NotNull ConstantExpressionEvaluator constantExpressionEvaluator,
@@ -74,7 +73,6 @@ public class ArgumentTypeResolver {
@NotNull ModuleDescriptor moduleDescriptor @NotNull ModuleDescriptor moduleDescriptor
) { ) {
this.typeResolver = typeResolver; this.typeResolver = typeResolver;
this.doubleColonExpressionResolver = doubleColonExpressionResolver;
this.builtIns = builtIns; this.builtIns = builtIns;
this.reflectionTypes = reflectionTypes; this.reflectionTypes = reflectionTypes;
this.constantExpressionEvaluator = constantExpressionEvaluator; this.constantExpressionEvaluator = constantExpressionEvaluator;
@@ -88,7 +86,12 @@ public class ArgumentTypeResolver {
this.expressionTypingServices = expressionTypingServices; this.expressionTypingServices = expressionTypingServices;
} }
public static boolean isSubtypeOfForArgumentType( @Inject
public void setDoubleColonExpressionResolver(@NotNull DoubleColonExpressionResolver doubleColonExpressionResolver) {
this.doubleColonExpressionResolver = doubleColonExpressionResolver;
}
public boolean isSubtypeOfForArgumentType(
@NotNull KotlinType actualType, @NotNull KotlinType actualType,
@NotNull KotlinType expectedType @NotNull KotlinType expectedType
) { ) {
@@ -99,6 +102,7 @@ public class ArgumentTypeResolver {
return KotlinTypeChecker.DEFAULT.isSubtypeOf(actualType, expectedType); return KotlinTypeChecker.DEFAULT.isSubtypeOf(actualType, expectedType);
} }
public void checkTypesWithNoCallee( public void checkTypesWithNoCallee(
@NotNull CallResolutionContext<?> context @NotNull CallResolutionContext<?> context
) { ) {
@@ -362,7 +362,7 @@ class CandidateResolver(
if (type == null || (type.isError && !type.isFunctionPlaceholder)) { if (type == null || (type.isError && !type.isFunctionPlaceholder)) {
matchStatus = ArgumentMatchStatus.ARGUMENT_HAS_NO_TYPE matchStatus = ArgumentMatchStatus.ARGUMENT_HAS_NO_TYPE
} else if (!noExpectedType(expectedType)) { } else if (!noExpectedType(expectedType)) {
if (!ArgumentTypeResolver.isSubtypeOfForArgumentType(type, expectedType)) { if (!argumentTypeResolver.isSubtypeOfForArgumentType(type, expectedType)) {
val smartCast = smartCastValueArgumentTypeIfPossible(expression, newContext.expectedType, type, newContext) val smartCast = smartCastValueArgumentTypeIfPossible(expression, newContext.expectedType, type, newContext)
if (smartCast == null) { if (smartCast == null) {
resultStatus = tryNotNullableArgument(type, expectedType) ?: OTHER_ERROR resultStatus = tryNotNullableArgument(type, expectedType) ?: OTHER_ERROR
@@ -410,7 +410,7 @@ class CandidateResolver(
if (!argumentType.isMarkedNullable || parameterType.isMarkedNullable) return null if (!argumentType.isMarkedNullable || parameterType.isMarkedNullable) return null
val notNullableArgumentType = argumentType.makeNotNullable() val notNullableArgumentType = argumentType.makeNotNullable()
val isApplicable = ArgumentTypeResolver.isSubtypeOfForArgumentType(notNullableArgumentType, parameterType) val isApplicable = argumentTypeResolver.isSubtypeOfForArgumentType(notNullableArgumentType, parameterType)
return if (isApplicable) NULLABLE_ARGUMENT_TYPE_MISMATCH else null return if (isApplicable) NULLABLE_ARGUMENT_TYPE_MISMATCH else null
} }
@@ -37,7 +37,7 @@ import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.typeUtil.expandIntersectionTypeIfNecessary import org.jetbrains.kotlin.types.typeUtil.expandIntersectionTypeIfNecessary
import java.util.* import java.util.*
class SmartCastManager { class SmartCastManager(private val argumentTypeResolver: ArgumentTypeResolver) {
fun getSmartCastVariants( fun getSmartCastVariants(
receiverToCast: ReceiverValue, receiverToCast: ReceiverValue,
@@ -110,10 +110,10 @@ class SmartCastManager {
context: ResolutionContext<*> context: ResolutionContext<*>
): ReceiverSmartCastResult? = ): ReceiverSmartCastResult? =
when { when {
ArgumentTypeResolver.isSubtypeOfForArgumentType(receiverArgument.type, receiverParameterType) -> argumentTypeResolver.isSubtypeOfForArgumentType(receiverArgument.type, receiverParameterType) ->
ReceiverSmartCastResult.OK ReceiverSmartCastResult.OK
getSmartCastVariantsExcludingReceiver(context, receiverArgument).any { getSmartCastVariantsExcludingReceiver(context, receiverArgument).any {
ArgumentTypeResolver.isSubtypeOfForArgumentType(it, receiverParameterType) argumentTypeResolver.isSubtypeOfForArgumentType(it, receiverParameterType)
} -> } ->
ReceiverSmartCastResult.SMARTCAST_NEEDED_OR_NOT_NULL_EXPECTED ReceiverSmartCastResult.SMARTCAST_NEEDED_OR_NOT_NULL_EXPECTED
else -> null else -> null
@@ -135,7 +135,7 @@ class SmartCastManager {
listOf(expectedType) listOf(expectedType)
for (possibleType in c.dataFlowInfo.getCollectedTypes(dataFlowValue, c.languageVersionSettings)) { for (possibleType in c.dataFlowInfo.getCollectedTypes(dataFlowValue, c.languageVersionSettings)) {
if (expectedTypes.any { ArgumentTypeResolver.isSubtypeOfForArgumentType(possibleType, it) } && if (expectedTypes.any { argumentTypeResolver.isSubtypeOfForArgumentType(possibleType, it) } &&
(additionalPredicate == null || additionalPredicate(possibleType)) (additionalPredicate == null || additionalPredicate(possibleType))
) { ) {
if (expression != null) { if (expression != null) {
@@ -180,7 +180,7 @@ class SmartCastManager {
val immanentlyNotNull = !dataFlowValue.immanentNullability.canBeNull() val immanentlyNotNull = !dataFlowValue.immanentNullability.canBeNull()
val nullableExpectedType = TypeUtils.makeNullable(expectedType) val nullableExpectedType = TypeUtils.makeNullable(expectedType)
if (ArgumentTypeResolver.isSubtypeOfForArgumentType(dataFlowValue.type, nullableExpectedType) && if (argumentTypeResolver.isSubtypeOfForArgumentType(dataFlowValue.type, nullableExpectedType) &&
(additionalPredicate == null || additionalPredicate(dataFlowValue.type)) (additionalPredicate == null || additionalPredicate(dataFlowValue.type))
) { ) {
if (!immanentlyNotNull && expression != null) { if (!immanentlyNotNull && expression != null) {