diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/state/KotlinTypeMapper.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/state/KotlinTypeMapper.java index 7a7d49563cd..e010c2683d5 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/state/KotlinTypeMapper.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/state/KotlinTypeMapper.java @@ -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); } } diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/kotlinJvmBinaryClassUtil.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/kotlinJvmBinaryClassUtil.kt new file mode 100644 index 00000000000..570759064dd --- /dev/null +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/kotlinJvmBinaryClassUtil.kt @@ -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()?.binaryClass + is LazyJavaPackageFragment -> + container.source.safeAs()?.getRepresentativeBinaryClass() + else -> null + } +} diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/ObsoleteABIForInlineSuspendFunctionCallChecker.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/ObsoleteABIForInlineSuspendFunctionCallChecker.kt new file mode 100644 index 00000000000..6001abda9b1 --- /dev/null +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/ObsoleteABIForInlineSuspendFunctionCallChecker.kt @@ -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)) + } + } +} diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/DefaultErrorMessagesJvm.java b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/DefaultErrorMessagesJvm.java index d6f3fdb4223..5ce975a3ab9 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/DefaultErrorMessagesJvm.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/DefaultErrorMessagesJvm.java @@ -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 diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/ErrorsJvm.java b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/ErrorsJvm.java index 94064abf18e..cc15cc7100e 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/ErrorsJvm.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/ErrorsJvm.java @@ -93,6 +93,7 @@ public interface ErrorsJvm { DiagnosticFactory0 INTERFACE_STATIC_METHOD_CALL_FROM_JAVA6_TARGET = DiagnosticFactory0.create(WARNING); DiagnosticFactory2 INLINE_FROM_HIGHER_PLATFORM = DiagnosticFactory2.create(ERROR); + DiagnosticFactory0 OBSOLETE_SUSPEND_INLINE_FUNCTIONS_ABI = DiagnosticFactory0.create(ERROR); @SuppressWarnings("UnusedDeclaration") Object _initializer = new Object() { diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt index 245046d0d00..0b607aef48b 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt @@ -52,7 +52,8 @@ object JvmPlatformConfigurator : PlatformConfigurator( UnsupportedSyntheticCallableReferenceChecker(), SuperCallWithDefaultArgumentsChecker(), ProtectedSyntheticExtensionCallChecker, - ReifiedTypeParameterSubstitutionChecker() + ReifiedTypeParameterSubstitutionChecker(), + ObsoleteABIForInlineSuspendFunctionCallChecker ), additionalTypeCheckers = listOf( diff --git a/compiler/testData/bytecodeVersion/obsoleteInlineSuspend/A.kt b/compiler/testData/bytecodeVersion/obsoleteInlineSuspend/A.kt new file mode 100644 index 00000000000..56b90c2015d --- /dev/null +++ b/compiler/testData/bytecodeVersion/obsoleteInlineSuspend/A.kt @@ -0,0 +1,4 @@ +package library + +inline suspend fun foo() {} +suspend fun bar() {} diff --git a/compiler/testData/bytecodeVersion/obsoleteInlineSuspend/B.kt b/compiler/testData/bytecodeVersion/obsoleteInlineSuspend/B.kt new file mode 100644 index 00000000000..74ef9b1aad0 --- /dev/null +++ b/compiler/testData/bytecodeVersion/obsoleteInlineSuspend/B.kt @@ -0,0 +1,6 @@ +import library.* + +suspend fun test() { + foo() + bar() +} diff --git a/compiler/testData/bytecodeVersion/obsoleteInlineSuspend/output.txt b/compiler/testData/bytecodeVersion/obsoleteInlineSuspend/output.txt new file mode 100644 index 00000000000..ee9f65b96a0 --- /dev/null +++ b/compiler/testData/bytecodeVersion/obsoleteInlineSuspend/output.txt @@ -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 diff --git a/compiler/tests/org/jetbrains/kotlin/cli/WrongBytecodeVersionTest.kt b/compiler/tests/org/jetbrains/kotlin/cli/WrongBytecodeVersionTest.kt index 2ce63466b6b..946a8e0e81f 100644 --- a/compiler/tests/org/jetbrains/kotlin/cli/WrongBytecodeVersionTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/cli/WrongBytecodeVersionTest.kt @@ -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) diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/JvmBytecodeBinaryVersion.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/JvmBytecodeBinaryVersion.kt index 91a52550441..fc5b5bcc4fd 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/JvmBytecodeBinaryVersion.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/JvmBytecodeBinaryVersion.kt @@ -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() diff --git a/core/util.runtime/src/org/jetbrains/kotlin/serialization/deserialization/BinaryVersion.kt b/core/util.runtime/src/org/jetbrains/kotlin/serialization/deserialization/BinaryVersion.kt index 4020b8a1b27..5e83392a7a5 100644 --- a/core/util.runtime/src/org/jetbrains/kotlin/serialization/deserialization/BinaryVersion.kt +++ b/core/util.runtime/src/org/jetbrains/kotlin/serialization/deserialization/BinaryVersion.kt @@ -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(".")