JVM: remove diagnostics related to JVM target 1.6

This commit is contained in:
Alexander Udalov
2023-05-12 10:21:19 +02:00
committed by Space Team
parent e4e1bcefbd
commit 75197d1b86
16 changed files with 25 additions and 260 deletions
@@ -30,32 +30,22 @@ import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtSuperExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.DescriptorUtils.*
import org.jetbrains.kotlin.resolve.DescriptorUtils.isInterface
import org.jetbrains.kotlin.resolve.DescriptorUtils.unwrapFakeOverride
import org.jetbrains.kotlin.resolve.LanguageVersionSettingsProvider
import org.jetbrains.kotlin.resolve.calls.util.getSuperCallExpression
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.*
class InterfaceDefaultMethodCallChecker(val jvmTarget: JvmTarget, private val project: Project) : CallChecker {
import org.jetbrains.kotlin.resolve.calls.util.getSuperCallExpression
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm.INTERFACE_CANT_CALL_DEFAULT_METHOD_VIA_SUPER
class InterfaceDefaultMethodCallChecker(val jvmTarget: JvmTarget, project: Project) : CallChecker {
private val ideService = LanguageVersionSettingsProvider.getInstance(project)
override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) {
val supportDefaults = jvmTarget >= JvmTarget.JVM_1_8
val descriptor = resolvedCall.resultingDescriptor as? CallableMemberDescriptor ?: return
if (descriptor is JavaPropertyDescriptor) return
if (!supportDefaults &&
isStaticDeclaration(descriptor) &&
isInterface(descriptor.containingDeclaration) &&
descriptor is JavaCallableMemberDescriptor) {
context.trace.report(INTERFACE_STATIC_METHOD_CALL_FROM_JAVA6_TARGET.on(context.languageVersionSettings, reportOn))
}
val superCallExpression = getSuperCallExpression(resolvedCall.call) ?: return
if (!isInterface(descriptor.original.containingDeclaration)) return
@@ -70,17 +60,13 @@ class InterfaceDefaultMethodCallChecker(val jvmTarget: JvmTarget, private val pr
val bindingContext = context.trace.bindingContext
val thisForSuperCall = getSuperCallLabelTarget(bindingContext, superCallExpression)
if (thisForSuperCall != null && DescriptorUtils.isInterface(thisForSuperCall)) {
if (thisForSuperCall != null && isInterface(thisForSuperCall)) {
val declarationWithCall = findInterfaceMember(thisForSuperCall, superCallExpression, bindingContext)
if (declarationWithCall?.isCompiledToJvmDefaultWithProperMode(ideService, jvmDefaultMode) == false) {
context.trace.report(INTERFACE_CANT_CALL_DEFAULT_METHOD_VIA_SUPER.on(reportOn))
return
}
}
if (!supportDefaults) {
context.trace.report(DEFAULT_METHOD_CALL_FROM_JAVA6_TARGET.on(context.languageVersionSettings, reportOn))
}
}
}
@@ -109,10 +109,7 @@ class JvmDefaultChecker(private val jvmTarget: JvmTarget, project: Project) : De
): Boolean {
descriptor.annotations.findAnnotation(JVM_DEFAULT_NO_COMPATIBILITY_FQ_NAME)?.let { annotationDescriptor ->
val reportOn = DescriptorToSourceUtils.getSourceFromAnnotation(annotationDescriptor) ?: declaration
if (jvmTarget == JvmTarget.JVM_1_6) {
context.trace.report(ErrorsJvm.JVM_DEFAULT_IN_JVM6_TARGET.on(reportOn, "JvmDefaultWithoutCompatibility"))
return true
} else if (!jvmDefaultMode.isEnabled) {
if (!jvmDefaultMode.isEnabled) {
context.trace.report(ErrorsJvm.JVM_DEFAULT_IN_DECLARATION.on(reportOn, "JvmDefaultWithoutCompatibility"))
return true
}
@@ -120,10 +117,7 @@ class JvmDefaultChecker(private val jvmTarget: JvmTarget, project: Project) : De
descriptor.annotations.findAnnotation(JVM_DEFAULT_WITH_COMPATIBILITY_FQ_NAME)?.let { annotationDescriptor ->
val reportOn = DescriptorToSourceUtils.getSourceFromAnnotation(annotationDescriptor) ?: declaration
if (jvmTarget == JvmTarget.JVM_1_6) {
context.trace.report(ErrorsJvm.JVM_DEFAULT_IN_JVM6_TARGET.on(reportOn, "JvmDefaultWithCompatibility"))
return true
} else if (jvmDefaultMode != JvmDefaultMode.ALL_INCOMPATIBLE) {
if (jvmDefaultMode != JvmDefaultMode.ALL_INCOMPATIBLE) {
context.trace.report(ErrorsJvm.JVM_DEFAULT_WITH_COMPATIBILITY_IN_DECLARATION.on(reportOn))
return true
} else if (!isInterface(descriptor)) {
@@ -132,21 +132,15 @@ class RepeatableAnnotationChecker(
&& isRepeatableAnnotation(classDescriptor)
&& classDescriptor.getAnnotationRetention() != KotlinRetention.SOURCE
) {
when {
jvmTarget == JvmTarget.JVM_1_6 -> {
trace.report(ErrorsJvm.REPEATED_ANNOTATION_TARGET6.on(entry))
}
languageVersionSettings.supportsFeature(LanguageFeature.RepeatableAnnotations) -> {
// It's not allowed to have both a repeated annotation (applied more than once) and its container
// on the same element. See https://docs.oracle.com/javase/specs/jls/se16/html/jls-9.html#jls-9.7.5.
val explicitContainer = resolveContainerAnnotation(classDescriptor)
if (explicitContainer != null && annotations.any { it.descriptor.fqName == explicitContainer }) {
trace.report(ErrorsJvm.REPEATED_ANNOTATION_WITH_CONTAINER.on(entry, fqName, explicitContainer))
}
}
else -> {
trace.report(ErrorsJvm.NON_SOURCE_REPEATED_ANNOTATION.on(entry))
if (languageVersionSettings.supportsFeature(LanguageFeature.RepeatableAnnotations)) {
// It's not allowed to have both a repeated annotation (applied more than once) and its container
// on the same element. See https://docs.oracle.com/javase/specs/jls/se16/html/jls-9.html#jls-9.7.5.
val explicitContainer = resolveContainerAnnotation(classDescriptor)
if (explicitContainer != null && annotations.any { it.descriptor.fqName == explicitContainer }) {
trace.report(ErrorsJvm.REPEATED_ANNOTATION_WITH_CONTAINER.on(entry, fqName, explicitContainer))
}
} else {
trace.report(ErrorsJvm.NON_SOURCE_REPEATED_ANNOTATION.on(entry))
}
}
@@ -17,7 +17,6 @@
package org.jetbrains.kotlin.resolve.jvm.checkers
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.config.JvmTarget
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.*
@@ -25,7 +24,6 @@ import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.diagnostics.DiagnosticSink
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.JvmNames.VOLATILE_ANNOTATION_FQ_NAME
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
@@ -58,9 +56,7 @@ class LocalFunInlineChecker : DeclarationChecker {
}
}
class JvmStaticChecker(jvmTarget: JvmTarget, languageVersionSettings: LanguageVersionSettings) : DeclarationChecker {
private val isLessJVM18 = jvmTarget.majorVersion < JvmTarget.JVM_1_8.majorVersion
class JvmStaticChecker(languageVersionSettings: LanguageVersionSettings) : DeclarationChecker {
private val supportJvmStaticInInterface = languageVersionSettings.supportsFeature(LanguageFeature.JvmStaticInInterface)
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
@@ -91,9 +87,6 @@ class JvmStaticChecker(jvmTarget: JvmTarget, languageVersionSettings: LanguageVe
descriptor is DeclarationDescriptorWithVisibility
) {
checkForInterface(descriptor, diagnosticHolder, declaration)
if (isLessJVM18) {
diagnosticHolder.report(ErrorsJvm.JVM_STATIC_IN_INTERFACE_1_6.on(declaration))
}
} else {
diagnosticHolder.report(
(if (supportJvmStaticInInterface) ErrorsJvm.JVM_STATIC_NOT_IN_OBJECT_OR_COMPANION
@@ -42,7 +42,6 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension {
MAP.put(JVM_STATIC_NOT_IN_OBJECT_OR_CLASS_COMPANION, "Only members in named objects and companion objects of classes can be annotated with '@JvmStatic'");
MAP.put(JVM_STATIC_NOT_IN_OBJECT_OR_COMPANION, "Only members in named objects and companion objects can be annotated with '@JvmStatic'");
MAP.put(JVM_STATIC_ON_NON_PUBLIC_MEMBER, "Only public members in interface companion objects can be annotated with '@JvmStatic'");
MAP.put(JVM_STATIC_IN_INTERFACE_1_6, "'@JvmStatic' annotation in interface supported only with JVM target 1.8 and above. Recompile with '-jvm-target 1.8'\"");
MAP.put(JVM_STATIC_ON_CONST_OR_JVM_FIELD, "'@JvmStatic' annotation is useless for const or '@JvmField' properties");
MAP.put(JVM_STATIC_ON_EXTERNAL_IN_INTERFACE, "'@JvmStatic' annotation cannot be used on 'external' members of interface companions");
MAP.put(OVERRIDE_CANNOT_BE_STATIC, "Override member cannot be '@JvmStatic' in object");
@@ -69,7 +68,6 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension {
MAP.put(POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION, "Only named arguments are available for Java annotations");
MAP.put(DEPRECATED_JAVA_ANNOTATION, "This annotation is deprecated in Kotlin. Use ''@{0}'' instead", TO_STRING);
MAP.put(NON_SOURCE_REPEATED_ANNOTATION, "Repeatable annotations with non-SOURCE retention are only supported starting from Kotlin 1.6");
MAP.put(REPEATED_ANNOTATION_TARGET6, "Repeatable annotations with non-SOURCE retention are not supported with JVM target 1.6. Use -jvm-target 1.8");
MAP.put(REPEATED_ANNOTATION_WITH_CONTAINER, "Repeated annotation ''@{0}'' cannot be used on a declaration which is annotated with its container annotation ''@{1}''", TO_STRING, TO_STRING);
MAP.put(ANNOTATION_IS_NOT_APPLICABLE_TO_MULTIFILE_CLASSES, "Annotation ''@{0}'' is not applicable to the multi-file classes", TO_STRING);
@@ -153,9 +151,6 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension {
"Please make explicit overrides (abstract or concrete) for the following non-abstract members of ''{1}'': {2}",
NAME, NAME, STRING);
MAP.put(DEFAULT_METHOD_CALL_FROM_JAVA6_TARGET, "Super calls to Java default methods are prohibited in JVM target 1.6. Recompile with '-jvm-target 1.8'");
MAP.put(INTERFACE_STATIC_METHOD_CALL_FROM_JAVA6_TARGET, "Calls to static methods in Java interfaces are prohibited in JVM target 1.6. Recompile with '-jvm-target 1.8'");
MAP.put(INLINE_FROM_HIGHER_PLATFORM, "Cannot inline bytecode built with {0} into bytecode that is being built with {1}. Please specify proper ''-jvm-target'' option", STRING, STRING);
MAP.put(INLINE_FROM_HIGHER_PLATFORM_WARNING, "Cannot inline bytecode built with {0} into bytecode that is being built with {1}. Please specify proper ''-jvm-target'' option", STRING, STRING);
@@ -168,7 +163,6 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension {
MAP.put(ASSIGNMENT_TO_ARRAY_LOOP_VARIABLE, "Assignment to a for-in-array loop range variable. Behavior may change in Kotlin 1.3. " +
"See https://youtrack.jetbrains.com/issue/KT-21354 for more details");
MAP.put(JVM_DEFAULT_IN_JVM6_TARGET,"''@{0}'' is only supported since JVM target 1.8. Recompile with ''-jvm-target 1.8''", STRING);
MAP.put(JVM_DEFAULT_IN_DECLARATION, "Usage of ''@{0}'' is only allowed with -Xjvm-default option", STRING);
MAP.put(JVM_DEFAULT_WITH_COMPATIBILITY_IN_DECLARATION, "Usage of '@JvmDefaultWithCompatibility' is only allowed with '-Xjvm-default=all' option");
MAP.put(JVM_DEFAULT_WITH_COMPATIBILITY_NOT_ON_INTERFACE, "'@JvmDefaultWithCompatibility' annotation is only allowed on interfaces");
@@ -30,7 +30,6 @@ public interface ErrorsJvm {
DiagnosticFactory0<KtDeclaration> JVM_STATIC_NOT_IN_OBJECT_OR_CLASS_COMPANION = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE);
DiagnosticFactory0<KtDeclaration> JVM_STATIC_NOT_IN_OBJECT_OR_COMPANION = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE);
DiagnosticFactory0<KtDeclaration> JVM_STATIC_ON_NON_PUBLIC_MEMBER = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE);
DiagnosticFactory0<KtDeclaration> JVM_STATIC_IN_INTERFACE_1_6 = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE);
DiagnosticFactory0<KtDeclaration> JVM_STATIC_ON_CONST_OR_JVM_FIELD = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE);
DiagnosticFactory0<KtDeclaration> JVM_STATIC_ON_EXTERNAL_IN_INTERFACE = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE);
@@ -72,7 +71,6 @@ public interface ErrorsJvm {
DiagnosticFactory1<KtAnnotationEntry, FqName> DEPRECATED_JAVA_ANNOTATION = DiagnosticFactory1.create(WARNING);
DiagnosticFactory0<KtAnnotationEntry> NON_SOURCE_REPEATED_ANNOTATION = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtAnnotationEntry> REPEATED_ANNOTATION_TARGET6 = DiagnosticFactory0.create(ERROR);
DiagnosticFactory2<KtAnnotationEntry, FqName, FqName> REPEATED_ANNOTATION_WITH_CONTAINER = DiagnosticFactory2.create(ERROR);
DiagnosticFactoryForDeprecation2<KtAnnotationEntry, FqName, FqName> REPEATABLE_CONTAINER_MUST_HAVE_VALUE_ARRAY =
@@ -130,7 +128,6 @@ public interface ErrorsJvm {
DiagnosticFactory0<KtExpression> ASSIGNMENT_TO_ARRAY_LOOP_VARIABLE = DiagnosticFactory0.create(WARNING);
DiagnosticFactory1<PsiElement, String> JVM_DEFAULT_IN_JVM6_TARGET = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<KtElement, String> JVM_DEFAULT_IN_DECLARATION = DiagnosticFactory1.create(ERROR, DECLARATION_SIGNATURE_OR_DEFAULT);
DiagnosticFactory0<KtElement> JVM_DEFAULT_WITH_COMPATIBILITY_IN_DECLARATION = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtElement> JVM_DEFAULT_WITH_COMPATIBILITY_NOT_ON_INTERFACE = DiagnosticFactory0.create(ERROR);