Check constness of 'apiVersionIsAtLeast' arguments
This commit is contained in:
+64
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.jvm.checkers
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.config.LanguageFeature
|
||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
|
||||
import org.jetbrains.kotlin.psi.KtPsiUtil
|
||||
import org.jetbrains.kotlin.resolve.calls.checkers.CallChecker
|
||||
import org.jetbrains.kotlin.resolve.calls.checkers.CallCheckerContext
|
||||
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm
|
||||
import org.jetbrains.kotlin.resolve.jvm.getCompileTimeConstant
|
||||
|
||||
object ApiVersionIsAtLeastArgumentsChecker : CallChecker {
|
||||
override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) {
|
||||
if (!isApiVersionIsAtLeast(resolvedCall.resultingDescriptor)) return
|
||||
|
||||
val bindingContext = context.trace.bindingContext
|
||||
val shouldInlineConstVals = context.languageVersionSettings.supportsFeature(LanguageFeature.InlineConstVals)
|
||||
|
||||
for ((_, resolvedValueArgument) in resolvedCall.valueArguments) {
|
||||
for (valueArgument in resolvedValueArgument.arguments) {
|
||||
val ktExpression = KtPsiUtil.deparenthesize(valueArgument.getArgumentExpression() ?: continue) ?: continue
|
||||
|
||||
val constant = getCompileTimeConstant(ktExpression, bindingContext, false, shouldInlineConstVals)
|
||||
if (constant == null) {
|
||||
context.trace.report(ErrorsJvm.API_VERSION_IS_AT_LEAST_ARGUMENT_SHOULD_BE_CONSTANT.on(ktExpression))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun isApiVersionIsAtLeast(descriptor: CallableDescriptor): Boolean {
|
||||
val functionDescriptor = descriptor as? FunctionDescriptor ?: return false
|
||||
|
||||
if (functionDescriptor.name.asString() != "apiVersionIsAtLeast") return false
|
||||
|
||||
val returnType = functionDescriptor.returnType ?: return false
|
||||
if (!KotlinBuiltIns.isBoolean(returnType)) return false
|
||||
|
||||
if (!functionDescriptor.valueParameters.all { KotlinBuiltIns.isInt(it.type) }) return false
|
||||
|
||||
val containingPackage = functionDescriptor.containingDeclaration as? PackageFragmentDescriptor ?: return false
|
||||
return containingPackage.fqName.asString() == "kotlin.internal"
|
||||
}
|
||||
}
|
||||
+2
@@ -123,6 +123,8 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension {
|
||||
MAP.put(JAVA_MODULE_DOES_NOT_DEPEND_ON_MODULE, "Symbol is declared in module ''{0}'' which current module does not depend on", STRING);
|
||||
MAP.put(JAVA_MODULE_DOES_NOT_READ_UNNAMED_MODULE, "Symbol is declared in unnamed module which is not read by current module");
|
||||
MAP.put(JAVA_MODULE_DOES_NOT_EXPORT_PACKAGE, "Symbol is declared in module ''{0}'' which does not export package ''{1}''", STRING, STRING);
|
||||
|
||||
MAP.put(API_VERSION_IS_AT_LEAST_ARGUMENT_SHOULD_BE_CONSTANT, "'apiVersionIsAtLeast' argument should be a constant expression");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -105,6 +105,8 @@ public interface ErrorsJvm {
|
||||
DiagnosticFactory0<PsiElement> JAVA_MODULE_DOES_NOT_READ_UNNAMED_MODULE = DiagnosticFactory0.create(ERROR);
|
||||
DiagnosticFactory2<PsiElement, String, String> JAVA_MODULE_DOES_NOT_EXPORT_PACKAGE = DiagnosticFactory2.create(ERROR);
|
||||
|
||||
DiagnosticFactory0<KtExpression> API_VERSION_IS_AT_LEAST_ARGUMENT_SHOULD_BE_CONSTANT = DiagnosticFactory0.create(WARNING);
|
||||
|
||||
enum NullabilityInformationSource {
|
||||
KOTLIN {
|
||||
@NotNull
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.jvm
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.VariableDescriptor
|
||||
import org.jetbrains.kotlin.load.java.descriptors.JavaPropertyDescriptor
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
import org.jetbrains.kotlin.psi.KtExpression
|
||||
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
|
||||
import org.jetbrains.kotlin.psi.KtVisitorVoid
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.constants.ConstantValue
|
||||
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
|
||||
|
||||
fun getCompileTimeConstant(
|
||||
expression: KtExpression,
|
||||
bindingContext: BindingContext,
|
||||
takeUpConstValsAsConst: Boolean,
|
||||
shouldInlineConstVals: Boolean
|
||||
): ConstantValue<*>? {
|
||||
val compileTimeValue = ConstantExpressionEvaluator.getConstant(expression, bindingContext)
|
||||
if (compileTimeValue == null || compileTimeValue.usesNonConstValAsConstant) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (!shouldInlineConstVals && !takeUpConstValsAsConst && compileTimeValue.usesVariableAsConstant) {
|
||||
val constantChecker = ConstantsChecker(bindingContext)
|
||||
expression.accept(constantChecker)
|
||||
if (constantChecker.containsNonInlinedVals) return null
|
||||
}
|
||||
|
||||
val expectedType = bindingContext.getType(expression) ?: return null
|
||||
|
||||
return compileTimeValue.toConstantValue(expectedType)
|
||||
}
|
||||
|
||||
|
||||
private class ConstantsChecker(private val bindingContext: BindingContext) : KtVisitorVoid() {
|
||||
var containsNonInlinedVals = false
|
||||
|
||||
override fun visitSimpleNameExpression(expression: KtSimpleNameExpression) {
|
||||
val resolvedCall = expression.getResolvedCall(bindingContext)
|
||||
if (resolvedCall != null) {
|
||||
val callableDescriptor = resolvedCall.resultingDescriptor
|
||||
if (callableDescriptor is PropertyDescriptor && isInlinedJavaConstProperty(callableDescriptor as VariableDescriptor)) {
|
||||
containsNonInlinedVals = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitKtElement(element: KtElement) {
|
||||
if (!containsNonInlinedVals) {
|
||||
element.acceptChildren(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun isInlinedJavaConstProperty(descriptor: VariableDescriptor): Boolean =
|
||||
(descriptor as? JavaPropertyDescriptor)?.isConst ?: false
|
||||
+2
-1
@@ -54,7 +54,8 @@ object JvmPlatformConfigurator : PlatformConfigurator(
|
||||
SuperCallWithDefaultArgumentsChecker(),
|
||||
ProtectedSyntheticExtensionCallChecker,
|
||||
ReifiedTypeParameterSubstitutionChecker(),
|
||||
RuntimeAssertionsOnExtensionReceiverCallChecker
|
||||
RuntimeAssertionsOnExtensionReceiverCallChecker,
|
||||
ApiVersionIsAtLeastArgumentsChecker
|
||||
),
|
||||
|
||||
additionalTypeCheckers = listOf(
|
||||
|
||||
Reference in New Issue
Block a user