Report diagnostic on inline parameter usage inside defaul value for another inline parameter

This commit is contained in:
Mikhael Bogdanov
2017-05-17 15:34:43 +02:00
parent 0d1ede40c1
commit 42074f143d
8 changed files with 211 additions and 34 deletions
@@ -927,6 +927,7 @@ public interface Errors {
DiagnosticFactory0<KtAnnotationEntry> NON_INTERNAL_PUBLISHED_API = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<PsiElement, CallableDescriptor> PROTECTED_CALL_FROM_PUBLIC_INLINE = DiagnosticFactory1.create(WARNING);
DiagnosticFactory2<KtElement, KtExpression, DeclarationDescriptor> INVALID_DEFAULT_FUNCTIONAL_PARAMETER_FOR_INLINE = DiagnosticFactory2.create(ERROR);
DiagnosticFactory2<KtElement, KtExpression, DeclarationDescriptor> NOT_SUPPORTED_USAGE_OF_INLINE_PARAMETER_IN_DEFAULT_INLINE_ONE = DiagnosticFactory2.create(ERROR);
DiagnosticFactory0<PsiElement> NON_LOCAL_SUSPENSION_POINT = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<PsiElement, CallableDescriptor> ILLEGAL_SUSPEND_FUNCTION_CALL = DiagnosticFactory1.create(ERROR);
@@ -837,6 +837,7 @@ public class DefaultErrorMessages {
MAP.put(NON_INTERNAL_PUBLISHED_API, "@PublishedApi annotation is only applicable for internal declaration");
MAP.put(PROTECTED_CALL_FROM_PUBLIC_INLINE, "Protected function call from public-API inline function is deprecated", NAME);
MAP.put(INVALID_DEFAULT_FUNCTIONAL_PARAMETER_FOR_INLINE, "Invalid default value for inline parameter: ''{0}''. Only lambdas, anonymous functions, and callable references are supported", ELEMENT_TEXT, SHORT_NAMES_IN_TYPES);
MAP.put(NOT_SUPPORTED_USAGE_OF_INLINE_PARAMETER_IN_DEFAULT_INLINE_ONE, "Usage of inline parameter ''{0}'' in default value for another inline parameter is not supported", ELEMENT_TEXT, SHORT_NAMES_IN_TYPES);
//Inline non locals
MAP.put(NON_LOCAL_RETURN_NOT_ALLOWED, "Can''t inline ''{0}'' here: it may contain non-local returns. Add ''crossinline'' modifier to parameter declaration ''{0}''", ELEMENT_TEXT);
MAP.put(INLINE_CALL_CYCLE, "The ''{0}'' invocation is a part of inline cycle", NAME);
@@ -17,48 +17,49 @@
package org.jetbrains.kotlin.resolve.calls.checkers
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.builtins.*
import org.jetbrains.kotlin.builtins.isFunctionType
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.diagnostics.Errors.*
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.calls.callUtil.*
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.DefaultValueArgument
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall
import org.jetbrains.kotlin.resolve.descriptorUtil.*
import org.jetbrains.kotlin.resolve.descriptorUtil.isEffectivelyPrivateApi
import org.jetbrains.kotlin.resolve.descriptorUtil.isInsidePrivateClass
import org.jetbrains.kotlin.resolve.inline.InlineUtil
import org.jetbrains.kotlin.resolve.inline.InlineUtil.allowsNonLocalReturns
import org.jetbrains.kotlin.resolve.inline.InlineUtil.checkNonLocalReturnUsage
import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver
import org.jetbrains.kotlin.resolve.scopes.receivers.ExtensionReceiver
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
import org.jetbrains.kotlin.resolve.source.KotlinSourceElement
import org.jetbrains.kotlin.util.OperatorNameConventions
import java.util.LinkedHashSet
import org.jetbrains.kotlin.diagnostics.Errors.NON_LOCAL_RETURN_NOT_ALLOWED
import org.jetbrains.kotlin.diagnostics.Errors.USAGE_IS_NOT_INLINABLE
import org.jetbrains.kotlin.resolve.inline.InlineUtil.allowsNonLocalReturns
import org.jetbrains.kotlin.resolve.inline.InlineUtil.checkNonLocalReturnUsage
import kotlin.properties.Delegates
internal class InlineChecker(private val descriptor: FunctionDescriptor) : CallChecker {
private val inlinableParameters = LinkedHashSet<CallableDescriptor>()
private val inlineFunEffectiveVisibility: EffectiveVisibility
private val isEffectivelyPrivateApiFunction: Boolean
init {
assert(InlineUtil.isInline(descriptor)) { "This extension should be created only for inline functions: " + descriptor }
this.inlineFunEffectiveVisibility = descriptor.effectiveVisibility(descriptor.visibility, true)
this.isEffectivelyPrivateApiFunction = descriptor.isEffectivelyPrivateApi
for (param in descriptor.valueParameters) {
if (isInlinableParameter(param)) {
inlinableParameters.add(param)
}
}
assert(InlineUtil.isInline(descriptor)) { "This extension should be created only for inline functions: $descriptor" }
}
private val inlineFunEffectiveVisibility = descriptor.effectiveVisibility(descriptor.visibility, true)
private val isEffectivelyPrivateApiFunction = descriptor.isEffectivelyPrivateApi
private val inlinableParameters = descriptor.valueParameters.filter { isInlinableParameter(it) }
private val inlinableKtParameters = inlinableParameters.mapNotNull { (it.source as? KotlinSourceElement)?.psi }
private var supportDefaultValueInline by Delegates.notNull<Boolean>()
override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) {
val expression = resolvedCall.call.calleeExpression ?: return
supportDefaultValueInline = context.languageVersionSettings.supportsFeature(LanguageFeature.InlineDefaultFunctionalParameters)
//checking that only invoke or inlinable extension called on function parameter
val targetDescriptor = resolvedCall.resultingDescriptor
@@ -66,8 +67,9 @@ internal class InlineChecker(private val descriptor: FunctionDescriptor) : CallC
checkCallWithReceiver(context, targetDescriptor, resolvedCall.extensionReceiver, expression)
if (inlinableParameters.contains(targetDescriptor)) {
if (!isInsideCall(expression)) {
context.trace.report(USAGE_IS_NOT_INLINABLE.on(expression, expression, descriptor))
when {
!checkNotInDefaultParameter(context, targetDescriptor, expression) -> { /*error*/ }
!isInsideCall(expression) -> context.trace.report(USAGE_IS_NOT_INLINABLE.on(expression, expression, descriptor))
}
}
@@ -83,6 +85,15 @@ internal class InlineChecker(private val descriptor: FunctionDescriptor) : CallC
checkRecursion(context, targetDescriptor, expression)
}
private fun checkNotInDefaultParameter( context: CallCheckerContext , targetDescriptor: CallableDescriptor, expression: KtExpression) =
!supportDefaultValueInline || expression.getParentOfType<KtParameter>(true)?.let {
val allow = it !in inlinableKtParameters
if (!allow) {
context.trace.report(NOT_SUPPORTED_USAGE_OF_INLINE_PARAMETER_IN_DEFAULT_INLINE_ONE.on(expression, expression, descriptor))
}
allow
} ?: true
private fun isInsideCall(expression: KtExpression): Boolean {
val parent = KtPsiUtil.getParentCallIfPresent(expression)
if (parent is KtBinaryExpression) {
@@ -118,16 +129,18 @@ internal class InlineChecker(private val descriptor: FunctionDescriptor) : CallC
val argumentCallee = getCalleeDescriptor(context, argumentExpression, false)
if (argumentCallee != null && inlinableParameters.contains(argumentCallee)) {
if (InlineUtil.isInline(targetDescriptor) && isInlinableParameter(targetParameterDescriptor)) {
if (allowsNonLocalReturns(argumentCallee) && !allowsNonLocalReturns(targetParameterDescriptor)) {
context.trace.report(NON_LOCAL_RETURN_NOT_ALLOWED.on(argumentExpression, argumentExpression))
}
else {
checkNonLocalReturn(context, argumentCallee, argumentExpression)
}
}
else {
context.trace.report(USAGE_IS_NOT_INLINABLE.on(argumentExpression, argumentExpression, descriptor))
when {
!checkNotInDefaultParameter(context, argumentCallee, argumentExpression) -> { /*error*/ }
InlineUtil.isInline(targetDescriptor) && isInlinableParameter(targetParameterDescriptor) ->
if (allowsNonLocalReturns(argumentCallee) && !allowsNonLocalReturns(targetParameterDescriptor)) {
context.trace.report(NON_LOCAL_RETURN_NOT_ALLOWED.on(argumentExpression, argumentExpression))
}
else {
checkNonLocalReturn(context, argumentCallee, argumentExpression)
}
else -> context.trace.report(USAGE_IS_NOT_INLINABLE.on(argumentExpression, argumentExpression, descriptor))
}
}
}