Intoduce and support DslMarker annotation
#KT-11551 In Progress
This commit is contained in:
@@ -644,6 +644,8 @@ public interface Errors {
|
||||
|
||||
DiagnosticFactory2<PsiElement, KtModifierKeywordToken, String> INAPPLICABLE_MODIFIER = DiagnosticFactory2.create(ERROR);
|
||||
|
||||
DiagnosticFactory1<PsiElement, CallableDescriptor> DSL_SCOPE_VIOLATION = DiagnosticFactory1.create(ERROR);
|
||||
|
||||
// Labels
|
||||
|
||||
DiagnosticFactory0<KtSimpleNameExpression> LABEL_NAME_CLASH = DiagnosticFactory0.create(WARNING);
|
||||
|
||||
+3
@@ -396,6 +396,9 @@ public class DefaultErrorMessages {
|
||||
|
||||
MAP.put(INAPPLICABLE_MODIFIER, "''{0}'' modifier is inapplicable. The reason is that {1}", TO_STRING, STRING);
|
||||
|
||||
MAP.put(DSL_SCOPE_VIOLATION, "''{0}'' can't be called in this context by implicit receiver. " +
|
||||
"Use the explicit one if necessary", COMPACT);
|
||||
|
||||
MAP.put(RETURN_IN_FUNCTION_WITH_EXPRESSION_BODY,
|
||||
"Returns are not allowed for functions with expression body. Use block body in '{...}'");
|
||||
MAP.put(NO_RETURN_IN_FUNCTION_WITH_BLOCK_BODY, "A 'return' expression required in a function with a block body ('{...}')");
|
||||
|
||||
@@ -72,7 +72,7 @@ private val DEFAULT_CALL_CHECKERS = listOf(
|
||||
CapturingInClosureChecker(), InlineCheckerWrapper(), ReifiedTypeParameterSubstitutionChecker(), SafeCallChecker(),
|
||||
DeprecatedCallChecker, CallReturnsArrayOfNothingChecker(), InfixCallChecker(), OperatorCallChecker(),
|
||||
ConstructorHeaderCallChecker, ProtectedConstructorCallChecker, ApiVersionCallChecker,
|
||||
CoroutineSuspendCallChecker, BuilderFunctionsCallChecker
|
||||
CoroutineSuspendCallChecker, BuilderFunctionsCallChecker, DslScopeViolationCallChecker
|
||||
)
|
||||
private val DEFAULT_TYPE_CHECKERS = emptyList<AdditionalTypeChecker>()
|
||||
private val DEFAULT_CLASSIFIER_USAGE_CHECKERS = listOf(DeprecatedClassifierUsageChecker(), ApiVersionClassifierUsageChecker)
|
||||
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.resolve.calls.checkers
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.calls.resolvedCallUtil.getImplicitReceivers
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.getAllSuperClassifiers
|
||||
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
|
||||
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
|
||||
import org.jetbrains.kotlin.resolve.scopes.utils.parentsWithSelf
|
||||
|
||||
object DslScopeViolationCallChecker : CallChecker {
|
||||
override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) {
|
||||
val callImplicitReceivers = resolvedCall.getImplicitReceivers()
|
||||
|
||||
for (callImplicitReceiver in callImplicitReceivers) {
|
||||
checkCallImplicitReceiver(callImplicitReceiver, resolvedCall, reportOn, context)
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkCallImplicitReceiver(
|
||||
callImplicitReceiver: ReceiverValue,
|
||||
resolvedCall: ResolvedCall<*>,
|
||||
reportOn: PsiElement,
|
||||
context: CallCheckerContext
|
||||
) {
|
||||
val receiversUntilOneFromTheCall =
|
||||
context.scope.parentsWithSelf
|
||||
.mapNotNull { (it as? LexicalScope)?.implicitReceiver?.value }
|
||||
.takeWhile { it != callImplicitReceiver }.toList()
|
||||
|
||||
if (receiversUntilOneFromTheCall.isEmpty()) return
|
||||
|
||||
val callDslMarkers = callImplicitReceiver.extractDslMarkerFqNames()
|
||||
if (callDslMarkers.isEmpty()) return
|
||||
|
||||
val closestAnotherReceiverWithSameDslMarker =
|
||||
receiversUntilOneFromTheCall.firstOrNull { receiver -> receiver.extractDslMarkerFqNames().any(callDslMarkers::contains) }
|
||||
|
||||
if (closestAnotherReceiverWithSameDslMarker != null) {
|
||||
// TODO: report receivers configuration (what's one is used and what's one is the closest)
|
||||
context.trace.report(Errors.DSL_SCOPE_VIOLATION.on(reportOn, resolvedCall.resultingDescriptor))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun ReceiverValue.extractDslMarkerFqNames(): Set<FqName> {
|
||||
val result = mutableSetOf<FqName>()
|
||||
|
||||
result.addAll(type.annotations.extractDslMarkerFqNames())
|
||||
|
||||
type.constructor.declarationDescriptor?.getAllSuperClassifiers()?.asIterable()
|
||||
?.flatMapTo(result) { it.annotations.extractDslMarkerFqNames() }
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
private fun Annotations.extractDslMarkerFqNames() =
|
||||
filter(AnnotationDescriptor::isDslMarker).map { it.type.constructor.declarationDescriptor!!.fqNameSafe }
|
||||
|
||||
private fun AnnotationDescriptor.isDslMarker(): Boolean {
|
||||
val classDescriptor = type.constructor.declarationDescriptor as? ClassDescriptor ?: return false
|
||||
return classDescriptor.annotations.hasAnnotation(DSL_MARKER_FQ_NAME)
|
||||
}
|
||||
|
||||
private val DSL_MARKER_FQ_NAME = FqName("kotlin.DslMarker")
|
||||
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.isSafeCall
|
||||
import org.jetbrains.kotlin.resolve.calls.context.CallResolutionContext
|
||||
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory
|
||||
import org.jetbrains.kotlin.resolve.calls.smartcasts.getReceiverValueWithSmartCast
|
||||
import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind
|
||||
@@ -82,6 +83,21 @@ fun ResolvedCall<*>.getImplicitReceiverValue(): ImplicitReceiver? {
|
||||
} as? ImplicitReceiver
|
||||
}
|
||||
|
||||
fun ResolvedCall<*>.getImplicitReceivers(): Collection<ReceiverValue> {
|
||||
if (this is VariableAsFunctionResolvedCall) {
|
||||
val receivers = variableCall.getImplicitReceivers() + functionCall.getImplicitReceivers()
|
||||
assert(receivers.size <= 3) { "There are ${receivers.size} for $this call" }
|
||||
return receivers
|
||||
}
|
||||
|
||||
return when (explicitReceiverKind) {
|
||||
ExplicitReceiverKind.NO_EXPLICIT_RECEIVER -> listOfNotNull(dispatchReceiver, extensionReceiver)
|
||||
ExplicitReceiverKind.DISPATCH_RECEIVER -> listOfNotNull(extensionReceiver)
|
||||
ExplicitReceiverKind.EXTENSION_RECEIVER -> listOfNotNull(dispatchReceiver)
|
||||
ExplicitReceiverKind.BOTH_RECEIVERS -> emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
private fun ResolvedCall<*>.hasSafeNullableReceiver(context: CallResolutionContext<*>): Boolean {
|
||||
if (!call.isSafeCall()) return false
|
||||
val receiverValue = getExplicitReceiverValue()?.let { DataFlowValueFactory.createDataFlowValue(it, context) }
|
||||
|
||||
Reference in New Issue
Block a user