Report error on calls to obsolete inline suspend functions

They do not contain markers for suspend calls in them

See the changes introduced for fixing KT-16603
This commit is contained in:
Denis Zharkov
2017-06-15 14:56:17 +03:00
parent 6441c9d913
commit cb95986c36
12 changed files with 115 additions and 29 deletions
@@ -35,7 +35,6 @@ import org.jetbrains.kotlin.codegen.signature.AsmTypeFactory;
import org.jetbrains.kotlin.codegen.signature.BothSignatureWriter;
import org.jetbrains.kotlin.codegen.signature.JvmSignatureWriter;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.IrBuiltinsPackageFragmentDescriptor;
import org.jetbrains.kotlin.descriptors.impl.LocalVariableAccessorDescriptor;
import org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor;
import org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor;
@@ -70,7 +69,6 @@ import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterKind;
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature;
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature;
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedCallableMemberDescriptor;
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedClassDescriptor;
import org.jetbrains.kotlin.types.*;
import org.jetbrains.kotlin.util.OperatorNameConventions;
import org.jetbrains.org.objectweb.asm.Type;
@@ -1161,29 +1159,12 @@ public class KotlinTypeMapper {
}
private void checkOwnerCompatibility(@NotNull FunctionDescriptor descriptor) {
if (!(descriptor instanceof DeserializedCallableMemberDescriptor)) return;
KotlinJvmBinaryClass ownerClass = KotlinJvmBinaryClassUtilKt.getContainingKotlinJvmBinaryClass(descriptor);
if (ownerClass == null) return;
KotlinJvmBinaryClass ownerClass = null;
DeclarationDescriptor container = descriptor.getContainingDeclaration();
if (container instanceof DeserializedClassDescriptor) {
SourceElement source = ((DeserializedClassDescriptor) container).getSource();
if (source instanceof KotlinJvmBinarySourceElement) {
ownerClass = ((KotlinJvmBinarySourceElement) source).getBinaryClass();
}
}
else if (container instanceof LazyJavaPackageFragment) {
SourceElement source = ((LazyJavaPackageFragment) container).getSource();
if (source instanceof KotlinJvmBinaryPackageSourceElement) {
ownerClass = ((KotlinJvmBinaryPackageSourceElement) source).getRepresentativeBinaryClass();
}
}
if (ownerClass != null) {
JvmBytecodeBinaryVersion version = ownerClass.getClassHeader().getBytecodeVersion();
if (!version.isCompatible()) {
incompatibleClassTracker.record(ownerClass);
}
JvmBytecodeBinaryVersion version = ownerClass.getClassHeader().getBytecodeVersion();
if (!version.isCompatible()) {
incompatibleClassTracker.record(ownerClass);
}
}
@@ -0,0 +1,37 @@
/*
* 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.load.kotlin
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.load.java.lazy.descriptors.LazyJavaPackageFragment
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedCallableMemberDescriptor
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedClassDescriptor
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
fun CallableDescriptor.getContainingKotlinJvmBinaryClass(): KotlinJvmBinaryClass? {
if (this !is DeserializedCallableMemberDescriptor) return null
val container = containingDeclaration
return when (container) {
is DeserializedClassDescriptor ->
container.source.safeAs<KotlinJvmBinarySourceElement>()?.binaryClass
is LazyJavaPackageFragment ->
container.source.safeAs<KotlinJvmBinaryPackageSourceElement>()?.getRepresentativeBinaryClass()
else -> null
}
}
@@ -0,0 +1,38 @@
/*
* 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.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.load.kotlin.getContainingKotlinJvmBinaryClass
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
object ObsoleteABIForInlineSuspendFunctionCallChecker : CallChecker {
override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) {
val candidateDescriptor = resolvedCall.candidateDescriptor as? FunctionDescriptor ?: return
if (!candidateDescriptor.isSuspend || !candidateDescriptor.isInline) return
val jvmBytecodeVersion = candidateDescriptor.getContainingKotlinJvmBinaryClass()?.classHeader?.bytecodeVersion ?: return
if (!jvmBytecodeVersion.isAtLeast(1, 0, 2)) {
context.trace.report(ErrorsJvm.OBSOLETE_SUSPEND_INLINE_FUNCTIONS_ABI.on(reportOn))
}
}
}
@@ -112,6 +112,7 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension {
MAP.put(INTERFACE_STATIC_METHOD_CALL_FROM_JAVA6_TARGET, "Calls to static methods in Java interfaces are deprecated 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(ErrorsJvm.OBSOLETE_SUSPEND_INLINE_FUNCTIONS_ABI, "Cannot inline suspend function built with compiler version less than 1.1.4/1.2-M1");
}
@NotNull
@@ -93,6 +93,7 @@ public interface ErrorsJvm {
DiagnosticFactory0<PsiElement> INTERFACE_STATIC_METHOD_CALL_FROM_JAVA6_TARGET = DiagnosticFactory0.create(WARNING);
DiagnosticFactory2<PsiElement, String, String> INLINE_FROM_HIGHER_PLATFORM = DiagnosticFactory2.create(ERROR);
DiagnosticFactory0<PsiElement> OBSOLETE_SUSPEND_INLINE_FUNCTIONS_ABI = DiagnosticFactory0.create(ERROR);
@SuppressWarnings("UnusedDeclaration")
Object _initializer = new Object() {
@@ -52,7 +52,8 @@ object JvmPlatformConfigurator : PlatformConfigurator(
UnsupportedSyntheticCallableReferenceChecker(),
SuperCallWithDefaultArgumentsChecker(),
ProtectedSyntheticExtensionCallChecker,
ReifiedTypeParameterSubstitutionChecker()
ReifiedTypeParameterSubstitutionChecker(),
ObsoleteABIForInlineSuspendFunctionCallChecker
),
additionalTypeCheckers = listOf(
@@ -0,0 +1,4 @@
package library
inline suspend fun foo() {}
suspend fun bar() {}
@@ -0,0 +1,6 @@
import library.*
suspend fun test() {
foo()
bar()
}
@@ -0,0 +1,4 @@
compiler/testData/bytecodeVersion/obsoleteInlineSuspend/B.kt:4:5: error: cannot inline suspend function built with compiler version less than 1.1.4/1.2-M1
foo()
^
COMPILATION_ERROR
@@ -22,7 +22,6 @@ import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
import org.jetbrains.kotlin.jvm.compiler.LoadDescriptorUtil
import org.jetbrains.kotlin.load.java.JvmAnnotationNames
import org.jetbrains.kotlin.load.java.JvmBytecodeBinaryVersion
import org.jetbrains.kotlin.load.kotlin.JvmMetadataVersion
import org.jetbrains.kotlin.test.KotlinTestUtils
import org.jetbrains.kotlin.test.testFramework.KtUsefulTestCase
import org.jetbrains.org.objectweb.asm.*
@@ -31,7 +30,7 @@ import java.io.File
class WrongBytecodeVersionTest : KtUsefulTestCase() {
private val incompatibleVersion = JvmBytecodeBinaryVersion(42, 0, 0).toArray()
private fun doTest(relativeDirectory: String) {
private fun doTest(relativeDirectory: String, version: IntArray = incompatibleVersion) {
val directory = KotlinTestUtils.getTestDataPathBase() + relativeDirectory
val librarySource = File(directory, "A.kt")
val usageSource = File(directory, "B.kt")
@@ -43,7 +42,7 @@ class WrongBytecodeVersionTest : KtUsefulTestCase() {
for (classFile in File(tmpdir, "library").listFiles { file -> file.extension == JavaClassFileType.INSTANCE.defaultExtension }) {
classFile.writeBytes(transformMetadataInClassFile(classFile.readBytes()) { name, _ ->
if (name == JvmAnnotationNames.BYTECODE_VERSION_FIELD_NAME) incompatibleVersion else null
if (name == JvmAnnotationNames.BYTECODE_VERSION_FIELD_NAME) version else null
})
}
@@ -65,6 +64,10 @@ class WrongBytecodeVersionTest : KtUsefulTestCase() {
doTest("/bytecodeVersion/simple")
}
fun testObsoleteInlineSuspend() {
doTest("/bytecodeVersion/obsoleteInlineSuspend", intArrayOf(1, 0, 1))
}
companion object {
fun transformMetadataInClassFile(bytes: ByteArray, transform: (fieldName: String, value: Any?) -> Any?): ByteArray {
val writer = ClassWriter(0)
@@ -27,7 +27,7 @@ class JvmBytecodeBinaryVersion(vararg numbers: Int) : BinaryVersion(*numbers) {
companion object {
@JvmField
val INSTANCE = JvmBytecodeBinaryVersion(1, 0, 1)
val INSTANCE = JvmBytecodeBinaryVersion(1, 0, 2)
@JvmField
val INVALID_VERSION = JvmBytecodeBinaryVersion()
@@ -46,6 +46,16 @@ abstract class BinaryVersion(vararg val numbers: Int) {
else major == ourVersion.major && minor <= ourVersion.minor
}
fun isAtLeast(major: Int, minor: Int, patch: Int): Boolean {
if (this.major > major) return true
if (this.major < major) return false
if (this.minor > minor) return true
if (this.minor < minor) return false
return this.patch >= patch
}
override fun toString(): String {
val versions = toArray().takeWhile { it != UNKNOWN }
return if (versions.isEmpty()) "unknown" else versions.joinToString(".")