diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUtil.kt index 1240e91c0f1..578943d85b6 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUtil.kt @@ -27,16 +27,10 @@ import org.jetbrains.kotlin.resolve.constants.ErrorValue // If you wish to add another JVM-related annotation and has/find utility methods, please proceed to jvmAnnotationUtil.kt val JVM_STATIC_ANNOTATION_FQ_NAME = FqName("kotlin.jvm.JvmStatic") -private val IMPLICIT_INTEGER_COERCION_ANNOTATION_FQ_NAME = FqName("kotlin.internal.ImplicitIntegerCoercion") - fun DeclarationDescriptor.hasJvmStaticAnnotation(): Boolean { return annotations.findAnnotation(JVM_STATIC_ANNOTATION_FQ_NAME) != null } -fun DeclarationDescriptor.hasImplicitIntegerCoercionAnnotation(): Boolean { - return annotations.findAnnotation(IMPLICIT_INTEGER_COERCION_ANNOTATION_FQ_NAME) != null -} - fun AnnotationDescriptor.argumentValue(parameterName: String): ConstantValue<*>? { return allValueArguments[Name.identifier(parameterName)].takeUnless { it is ErrorValue } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/ImplicitIntegerCoercion.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/ImplicitIntegerCoercion.kt new file mode 100644 index 00000000000..272cbf51bc0 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/ImplicitIntegerCoercion.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.resolve + +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.descriptors.ModuleDescriptor +import org.jetbrains.kotlin.descriptors.ParameterDescriptor +import org.jetbrains.kotlin.descriptors.VariableDescriptor +import org.jetbrains.kotlin.name.FqName + +object ImplicitIntegerCoercion { + + val MODULE_CAPABILITY = ModuleDescriptor.Capability("ImplicitIntegerCoercion") + + fun isEnabledForParameter(descriptor: ParameterDescriptor): Boolean = isEnabledFor(descriptor) + + fun isEnabledForConstVal(descriptor: VariableDescriptor): Boolean = isEnabledFor(descriptor) + + private fun isEnabledFor(descriptor: DeclarationDescriptor): Boolean = + descriptor.hasImplicitIntegerCoercionAnnotation() || + DescriptorUtils.getContainingModuleOrNull(descriptor) + ?.getCapability(ImplicitIntegerCoercion.MODULE_CAPABILITY) == true + + private val IMPLICIT_INTEGER_COERCION_ANNOTATION_FQ_NAME = FqName("kotlin.internal.ImplicitIntegerCoercion") + + private fun DeclarationDescriptor.hasImplicitIntegerCoercionAnnotation(): Boolean { + return annotations.findAnnotation(IMPLICIT_INTEGER_COERCION_ANNOTATION_FQ_NAME) != null + } +} \ No newline at end of file diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallCompleter.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallCompleter.kt index f77eeea9351..f3ba97846b6 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallCompleter.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallCompleter.kt @@ -15,7 +15,6 @@ import org.jetbrains.kotlin.diagnostics.Errors import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.resolve.* import org.jetbrains.kotlin.resolve.BindingContext.CONSTRAINT_SYSTEM_COMPLETER -import org.jetbrains.kotlin.resolve.annotations.hasImplicitIntegerCoercionAnnotation import org.jetbrains.kotlin.resolve.calls.callResolverUtil.ResolveArgumentsMode.RESOLVE_FUNCTION_ARGUMENTS import org.jetbrains.kotlin.resolve.calls.callResolverUtil.getEffectiveExpectedType import org.jetbrains.kotlin.resolve.calls.callResolverUtil.isInvokeCallOnVariable @@ -345,7 +344,7 @@ class CallCompleter( updatedType = argumentTypeResolver.updateResultArgumentTypeIfNotDenotable(context, expression) ?: updatedType } - if (parameter?.hasImplicitIntegerCoercionAnnotation() == true) { + if (parameter != null && ImplicitIntegerCoercion.isEnabledForParameter(parameter)) { val argumentCompileTimeValue = context.trace[BindingContext.COMPILE_TIME_VALUE, deparenthesized] if (argumentCompileTimeValue != null && argumentCompileTimeValue.parameters.isConvertableConstVal) { val generalNumberType = createTypeForConvertableConstant(argumentCompileTimeValue) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt index e87fb045f3c..1f18656f0d7 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt @@ -24,7 +24,6 @@ import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType import org.jetbrains.kotlin.resolve.* import org.jetbrains.kotlin.resolve.BindingContext.COLLECTION_LITERAL_CALL -import org.jetbrains.kotlin.resolve.annotations.hasImplicitIntegerCoercionAnnotation import org.jetbrains.kotlin.resolve.calls.callResolverUtil.getEffectiveExpectedType import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall @@ -749,7 +748,7 @@ private class ConstantExpressionEvaluatorVisitor( val isConvertableConstVal = callableDescriptor.isConst && - callableDescriptor.hasImplicitIntegerCoercionAnnotation() && + ImplicitIntegerCoercion.isEnabledForConstVal(callableDescriptor) && callableDescriptor.compileTimeInitializer is IntValue return callableDescriptor.compileTimeInitializer?.wrap(