Do not use DefaultBuiltIns in contracts
Default built-ins represent built-ins loaded from the compiler jar via class loader, and they may not be equivalent to the built-ins present in the standard library that is used in compilation dependencies, in case the compiler and stdlib versions do not match. Use built-ins from the given module instead. This commit deals with more-or-less obvious usages of DefaultBuiltIns; next commits refactor the ESConstant values and related code to support injected built-ins
This commit is contained in:
@@ -16,8 +16,9 @@
|
||||
|
||||
package org.jetbrains.kotlin.contracts
|
||||
|
||||
import org.jetbrains.kotlin.builtins.DefaultBuiltIns
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.config.LanguageVersionSettings
|
||||
import org.jetbrains.kotlin.contracts.description.expressions.ConstantReference
|
||||
import org.jetbrains.kotlin.contracts.model.ESValue
|
||||
import org.jetbrains.kotlin.contracts.model.MutableContextInfo
|
||||
import org.jetbrains.kotlin.contracts.model.structure.ESConstant
|
||||
@@ -26,41 +27,47 @@ import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfoFactory
|
||||
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue
|
||||
import org.jetbrains.kotlin.resolve.calls.smartcasts.IdentifierInfo
|
||||
|
||||
fun MutableContextInfo.toDataFlowInfo(languageVersionSettings: LanguageVersionSettings): DataFlowInfo {
|
||||
fun MutableContextInfo.toDataFlowInfo(languageVersionSettings: LanguageVersionSettings, builtIns: KotlinBuiltIns): DataFlowInfo {
|
||||
var resultingDataFlowInfo = DataFlowInfoFactory.EMPTY
|
||||
|
||||
extractDataFlowStatements(equalValues) { leftDfv, rightValue ->
|
||||
val rightDfv = rightValue.toDataFlowValue()
|
||||
extractDataFlowStatements(equalValues, builtIns) { leftDfv, rightValue ->
|
||||
val rightDfv = rightValue.toDataFlowValue(builtIns)
|
||||
if (rightDfv != null) {
|
||||
resultingDataFlowInfo = resultingDataFlowInfo.equate(leftDfv, rightDfv, false, languageVersionSettings)
|
||||
}
|
||||
IntArray(42) { it }
|
||||
}
|
||||
|
||||
extractDataFlowStatements(notEqualValues) { leftDfv, rightValue ->
|
||||
val rightDfv = rightValue.toDataFlowValue()
|
||||
extractDataFlowStatements(notEqualValues, builtIns) { leftDfv, rightValue ->
|
||||
val rightDfv = rightValue.toDataFlowValue(builtIns)
|
||||
if (rightDfv != null) {
|
||||
resultingDataFlowInfo = resultingDataFlowInfo.disequate(leftDfv, rightDfv, languageVersionSettings)
|
||||
}
|
||||
}
|
||||
|
||||
extractDataFlowStatements(subtypes) { leftDfv, type ->
|
||||
extractDataFlowStatements(subtypes, builtIns) { leftDfv, type ->
|
||||
resultingDataFlowInfo = resultingDataFlowInfo.establishSubtyping(leftDfv, type, languageVersionSettings)
|
||||
}
|
||||
|
||||
return resultingDataFlowInfo
|
||||
}
|
||||
|
||||
inline private fun <D> extractDataFlowStatements(dictionary: Map<ESValue, Set<D>>, callback: (DataFlowValue, D) -> Unit) {
|
||||
private inline fun <D> extractDataFlowStatements(
|
||||
dictionary: Map<ESValue, Set<D>>,
|
||||
builtIns: KotlinBuiltIns,
|
||||
callback: (DataFlowValue, D) -> Unit
|
||||
) {
|
||||
for ((key, setOfValues) in dictionary) {
|
||||
val leftDfv = key.toDataFlowValue() ?: continue
|
||||
val leftDfv = key.toDataFlowValue(builtIns) ?: continue
|
||||
setOfValues.forEach { callback(leftDfv, it) }
|
||||
}
|
||||
}
|
||||
|
||||
private fun ESValue.toDataFlowValue(): DataFlowValue? = when (this) {
|
||||
private fun ESValue.toDataFlowValue(builtIns: KotlinBuiltIns): DataFlowValue? = when (this) {
|
||||
is ESDataFlowValue -> dataFlowValue
|
||||
ESConstant.NULL -> DataFlowValue.nullValue(DefaultBuiltIns.Instance)
|
||||
is ESConstant -> DataFlowValue(IdentifierInfo.NO, type)
|
||||
is ESConstant -> when (constantReference) {
|
||||
ConstantReference.NULL -> DataFlowValue.nullValue(builtIns)
|
||||
else -> DataFlowValue(IdentifierInfo.NO, type)
|
||||
}
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,15 +18,11 @@ package org.jetbrains.kotlin.contracts
|
||||
|
||||
import org.jetbrains.kotlin.config.LanguageFeature
|
||||
import org.jetbrains.kotlin.config.LanguageVersionSettings
|
||||
import org.jetbrains.kotlin.contracts.model.structure.ESCalls
|
||||
import org.jetbrains.kotlin.contracts.model.structure.ESReturns
|
||||
import org.jetbrains.kotlin.contracts.model.functors.EqualsFunctor
|
||||
import org.jetbrains.kotlin.contracts.model.structure.ESConstant
|
||||
import org.jetbrains.kotlin.contracts.model.structure.UNKNOWN_COMPUTATION
|
||||
import org.jetbrains.kotlin.contracts.model.structure.lift
|
||||
import org.jetbrains.kotlin.contracts.model.ESEffect
|
||||
import org.jetbrains.kotlin.contracts.model.Computation
|
||||
import org.jetbrains.kotlin.contracts.model.ESEffect
|
||||
import org.jetbrains.kotlin.contracts.model.MutableContextInfo
|
||||
import org.jetbrains.kotlin.contracts.model.functors.EqualsFunctor
|
||||
import org.jetbrains.kotlin.contracts.model.structure.*
|
||||
import org.jetbrains.kotlin.contracts.model.visitors.InfoCollector
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.psi.KtCallExpression
|
||||
@@ -54,7 +50,7 @@ class EffectSystem(val languageVersionSettings: LanguageVersionSettings, val dat
|
||||
|
||||
val resultContextInfo = getContextInfoWhen(ESReturns(ESConstant.WILDCARD), callExpression, bindingTrace, moduleDescriptor)
|
||||
|
||||
return resultContextInfo.toDataFlowInfo(languageVersionSettings)
|
||||
return resultContextInfo.toDataFlowInfo(languageVersionSettings, moduleDescriptor.builtIns)
|
||||
}
|
||||
|
||||
fun getDataFlowInfoWhenEquals(
|
||||
@@ -73,12 +69,13 @@ class EffectSystem(val languageVersionSettings: LanguageVersionSettings, val dat
|
||||
|
||||
val effects = EqualsFunctor(false).invokeWithArguments(leftComputation, rightComputation)
|
||||
|
||||
val builtIns = moduleDescriptor.builtIns
|
||||
val equalsContextInfo = InfoCollector(ESReturns(true.lift())).collectFromSchema(effects)
|
||||
val notEqualsContextInfo = InfoCollector(ESReturns(false.lift())).collectFromSchema(effects)
|
||||
|
||||
return ConditionalDataFlowInfo(
|
||||
equalsContextInfo.toDataFlowInfo(languageVersionSettings),
|
||||
notEqualsContextInfo.toDataFlowInfo(languageVersionSettings)
|
||||
equalsContextInfo.toDataFlowInfo(languageVersionSettings, builtIns),
|
||||
notEqualsContextInfo.toDataFlowInfo(languageVersionSettings, builtIns)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -107,7 +104,7 @@ class EffectSystem(val languageVersionSettings: LanguageVersionSettings, val dat
|
||||
if (condition == null) return DataFlowInfo.EMPTY
|
||||
|
||||
return getContextInfoWhen(ESReturns(value.lift()), condition, bindingTrace, moduleDescriptor)
|
||||
.toDataFlowInfo(languageVersionSettings)
|
||||
.toDataFlowInfo(languageVersionSettings, moduleDescriptor.builtIns)
|
||||
}
|
||||
|
||||
private fun getContextInfoWhen(
|
||||
@@ -124,4 +121,4 @@ class EffectSystem(val languageVersionSettings: LanguageVersionSettings, val dat
|
||||
val computation = EffectsExtractingVisitor(trace, moduleDescriptor, dataFlowValueFactory).extractOrGetCached(expression)
|
||||
return if (computation == UNKNOWN_COMPUTATION) null else computation
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,9 +16,8 @@
|
||||
|
||||
package org.jetbrains.kotlin.contracts
|
||||
|
||||
import org.jetbrains.kotlin.builtins.DefaultBuiltIns
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.contracts.description.ContractProviderKey
|
||||
import org.jetbrains.kotlin.contracts.interpretation.ContractInterpretationDispatcher
|
||||
import org.jetbrains.kotlin.contracts.model.Computation
|
||||
import org.jetbrains.kotlin.contracts.model.ConditionalEffect
|
||||
import org.jetbrains.kotlin.contracts.model.ESEffect
|
||||
@@ -54,6 +53,8 @@ class EffectsExtractingVisitor(
|
||||
private val moduleDescriptor: ModuleDescriptor,
|
||||
private val dataFlowValueFactory: DataFlowValueFactory
|
||||
) : KtVisitor<Computation, Unit>() {
|
||||
private val builtIns: KotlinBuiltIns = moduleDescriptor.builtIns
|
||||
|
||||
fun extractOrGetCached(element: KtElement): Computation {
|
||||
trace[BindingContext.EXPRESSION_EFFECTS, element]?.let { return it }
|
||||
return element.accept(this, Unit).also { trace.record(BindingContext.EXPRESSION_EFFECTS, element, it) }
|
||||
@@ -68,7 +69,7 @@ class EffectsExtractingVisitor(
|
||||
val descriptor = resolvedCall.resultingDescriptor
|
||||
return when {
|
||||
descriptor.isEqualsDescriptor() -> CallComputation(
|
||||
DefaultBuiltIns.Instance.booleanType,
|
||||
builtIns.booleanType,
|
||||
EqualsFunctor(false).invokeWithArguments(arguments)
|
||||
)
|
||||
descriptor is ValueDescriptor -> ESDataFlowValue(
|
||||
@@ -112,7 +113,7 @@ class EffectsExtractingVisitor(
|
||||
val rightType: KotlinType = trace[BindingContext.TYPE, expression.typeReference] ?: return UNKNOWN_COMPUTATION
|
||||
val arg = extractOrGetCached(expression.leftHandSide)
|
||||
return CallComputation(
|
||||
DefaultBuiltIns.Instance.booleanType,
|
||||
builtIns.booleanType,
|
||||
IsFunctor(rightType, expression.isNegated).invokeWithArguments(listOf(arg))
|
||||
)
|
||||
}
|
||||
@@ -138,10 +139,10 @@ class EffectsExtractingVisitor(
|
||||
val args = listOf(left, right)
|
||||
|
||||
return when (expression.operationToken) {
|
||||
KtTokens.EXCLEQ -> CallComputation(DefaultBuiltIns.Instance.booleanType, EqualsFunctor(true).invokeWithArguments(args))
|
||||
KtTokens.EQEQ -> CallComputation(DefaultBuiltIns.Instance.booleanType, EqualsFunctor(false).invokeWithArguments(args))
|
||||
KtTokens.ANDAND -> CallComputation(DefaultBuiltIns.Instance.booleanType, AndFunctor().invokeWithArguments(args))
|
||||
KtTokens.OROR -> CallComputation(DefaultBuiltIns.Instance.booleanType, OrFunctor().invokeWithArguments(args))
|
||||
KtTokens.EXCLEQ -> CallComputation(builtIns.booleanType, EqualsFunctor(true).invokeWithArguments(args))
|
||||
KtTokens.EQEQ -> CallComputation(builtIns.booleanType, EqualsFunctor(false).invokeWithArguments(args))
|
||||
KtTokens.ANDAND -> CallComputation(builtIns.booleanType, AndFunctor().invokeWithArguments(args))
|
||||
KtTokens.OROR -> CallComputation(builtIns.booleanType, OrFunctor().invokeWithArguments(args))
|
||||
else -> UNKNOWN_COMPUTATION
|
||||
}
|
||||
}
|
||||
@@ -149,7 +150,7 @@ class EffectsExtractingVisitor(
|
||||
override fun visitUnaryExpression(expression: KtUnaryExpression, data: Unit): Computation {
|
||||
val arg = extractOrGetCached(expression.baseExpression ?: return UNKNOWN_COMPUTATION)
|
||||
return when (expression.operationToken) {
|
||||
KtTokens.EXCL -> CallComputation(DefaultBuiltIns.Instance.booleanType, NotFunctor().invokeWithArguments(arg))
|
||||
KtTokens.EXCL -> CallComputation(builtIns.booleanType, NotFunctor().invokeWithArguments(arg))
|
||||
else -> UNKNOWN_COMPUTATION
|
||||
}
|
||||
}
|
||||
@@ -170,7 +171,7 @@ class EffectsExtractingVisitor(
|
||||
|
||||
private fun FunctionDescriptor.getFunctor(): Functor? {
|
||||
val contractDescription = getUserData(ContractProviderKey)?.getContractDescription() ?: return null
|
||||
return contractDescription.functor
|
||||
return contractDescription.getFunctor(moduleDescriptor)
|
||||
}
|
||||
|
||||
private fun ResolvedCall<*>.isCallWithUnsupportedReceiver(): Boolean =
|
||||
|
||||
Reference in New Issue
Block a user