Add new metadata flag for class files compiled with FIR

Report a separate error when class files compiled with FIR are in
dependencies, in addition to the one for class files compiled with FE
1.0 + JVM IR.

 #KT-43592
This commit is contained in:
Alexander Udalov
2020-12-07 21:26:23 +01:00
parent cbd90c3af5
commit 3f517d7e8d
16 changed files with 74 additions and 37 deletions
@@ -190,6 +190,14 @@ class AnalyzerWithCompilerReport(
)
}
if (diagnostics.any { it.factory == Errors.FIR_COMPILED_CLASS }) {
messageCollector.report(
ERROR,
"Classes compiled by the new Kotlin compiler frontend were found in dependencies. " +
"Remove them from the classpath or use '-Xallow-jvm-ir-dependencies' to suppress errors"
)
}
return hasErrors
}
@@ -118,7 +118,7 @@ public class JVMConfigurationKeys {
CompilerConfigurationKey.create("Paths to .klib libraries");
public static final CompilerConfigurationKey<JvmAbiStability> ABI_STABILITY =
CompilerConfigurationKey.create("ABI stability of class files produced by the JVM IR backend");
CompilerConfigurationKey.create("ABI stability of class files produced by JVM IR and/or FIR");
public static final CompilerConfigurationKey<Boolean> DO_NOT_CLEAR_BINDING_CONTEXT =
CompilerConfigurationKey.create("When using the IR backend, do not clear BindingContext between psi2ir and lowerings");
@@ -9,9 +9,11 @@ import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.backend.jvm.JvmBackendExtension
import org.jetbrains.kotlin.backend.jvm.codegen.MetadataSerializer
import org.jetbrains.kotlin.codegen.serialization.JvmSerializationBindings
import org.jetbrains.kotlin.config.JvmAbiStability
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.backend.Fir2IrComponents
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.load.java.JvmAnnotationNames
import org.jetbrains.org.objectweb.asm.Type
class FirJvmBackendExtension(private val session: FirSession, private val components: Fir2IrComponents) : JvmBackendExtension {
@@ -24,4 +26,9 @@ class FirJvmBackendExtension(private val session: FirSession, private val compon
): MetadataSerializer {
return FirMetadataSerializer(session, context, klass, bindings, components, parentSerializer)
}
override fun generateMetadataExtraFlags(abiStability: JvmAbiStability?): Int =
JvmAnnotationNames.METADATA_JVM_IR_FLAG or
JvmAnnotationNames.METADATA_FIR_FLAG or
(if (abiStability == JvmAbiStability.STABLE) JvmAnnotationNames.METADATA_JVM_IR_STABLE_ABI_FLAG else 0)
}
@@ -119,6 +119,7 @@ public interface Errors {
DiagnosticFactory1<PsiElement, String> MISSING_SCRIPT_PROVIDED_PROPERTY_CLASS = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, String> PRE_RELEASE_CLASS = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, String> IR_COMPILED_CLASS = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, String> FIR_COMPILED_CLASS = DiagnosticFactory1.create(ERROR);
DiagnosticFactory2<PsiElement, String, IncompatibleVersionErrorData<?>> INCOMPATIBLE_CLASS = DiagnosticFactory2.create(ERROR);
//Elements with "INVISIBLE_REFERENCE" error are marked as unresolved, unlike elements with "INVISIBLE_MEMBER" error
@@ -402,6 +402,7 @@ public class DefaultErrorMessages {
MAP.put(MISSING_SCRIPT_PROVIDED_PROPERTY_CLASS, "Cannot access script provided property class ''{0}''. Check your module classpath for missing or conflicting dependencies", TO_STRING);
MAP.put(PRE_RELEASE_CLASS, "{0} is compiled by a pre-release version of Kotlin and cannot be loaded by this version of the compiler", TO_STRING);
MAP.put(IR_COMPILED_CLASS, "{0} is compiled by a new Kotlin compiler backend and cannot be loaded by the old compiler", TO_STRING);
MAP.put(FIR_COMPILED_CLASS, "{0} is compiled by the new Kotlin compiler frontend and cannot be loaded by the old compiler", TO_STRING);
MAP.put(INCOMPATIBLE_CLASS,
"{0} was compiled with an incompatible version of Kotlin. {1}",
TO_STRING,
@@ -59,6 +59,9 @@ object MissingDependencyClassChecker : CallChecker {
if (source.isPreReleaseInvisible) {
return PRE_RELEASE_CLASS.on(reportOn, source.presentableString)
}
if (source.abiStability == DeserializedContainerAbiStability.FIR_UNSTABLE) {
return FIR_COMPILED_CLASS.on(reportOn, source.presentableString)
}
if (source.abiStability == DeserializedContainerAbiStability.IR_UNSTABLE) {
return IR_COMPILED_CLASS.on(reportOn, source.presentableString)
}
@@ -8,7 +8,9 @@ package org.jetbrains.kotlin.backend.jvm
import org.jetbrains.kotlin.backend.jvm.codegen.DescriptorMetadataSerializer
import org.jetbrains.kotlin.backend.jvm.codegen.MetadataSerializer
import org.jetbrains.kotlin.codegen.serialization.JvmSerializationBindings
import org.jetbrains.kotlin.config.JvmAbiStability
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.load.java.JvmAnnotationNames
import org.jetbrains.org.objectweb.asm.Type
interface JvmBackendExtension {
@@ -16,6 +18,8 @@ interface JvmBackendExtension {
context: JvmBackendContext, klass: IrClass, type: Type, bindings: JvmSerializationBindings, parentSerializer: MetadataSerializer?
): MetadataSerializer
fun generateMetadataExtraFlags(abiStability: JvmAbiStability?): Int
object Default : JvmBackendExtension {
override fun createSerializer(
context: JvmBackendContext,
@@ -26,5 +30,9 @@ interface JvmBackendExtension {
): MetadataSerializer {
return DescriptorMetadataSerializer(context, klass, type, bindings, parentSerializer)
}
override fun generateMetadataExtraFlags(abiStability: JvmAbiStability?): Int =
JvmAnnotationNames.METADATA_JVM_IR_FLAG or
(if (abiStability == JvmAbiStability.STABLE) JvmAnnotationNames.METADATA_JVM_IR_STABLE_ABI_FLAG else 0)
}
}
@@ -13,7 +13,6 @@ import org.jetbrains.kotlin.backend.jvm.lower.hasAssertionsDisabledField
import org.jetbrains.kotlin.codegen.DescriptorAsmUtil
import org.jetbrains.kotlin.codegen.inline.*
import org.jetbrains.kotlin.codegen.writeKotlinMetadata
import org.jetbrains.kotlin.config.JvmAbiStability
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
@@ -197,10 +196,7 @@ class ClassCodegen private constructor(
private fun generateKotlinMetadataAnnotation() {
// TODO: if `-Xmultifile-parts-inherit` is enabled, write the corresponding flag for parts and facades to [Metadata.extraInt].
var extraFlags = JvmAnnotationNames.METADATA_JVM_IR_FLAG
if (state.abiStability != JvmAbiStability.UNSTABLE) {
extraFlags += JvmAnnotationNames.METADATA_JVM_IR_STABLE_ABI_FLAG
}
val extraFlags = context.backendExtension.generateMetadataExtraFlags(state.abiStability)
val facadeClassName = context.multifileFacadeForPart[irClass.attributeOwnerId]
val metadata = irClass.metadata
@@ -13,7 +13,6 @@ import org.jetbrains.kotlin.backend.jvm.lower.hasAssertionsDisabledField
import org.jetbrains.kotlin.codegen.DescriptorAsmUtil
import org.jetbrains.kotlin.codegen.inline.*
import org.jetbrains.kotlin.codegen.writeKotlinMetadata
import org.jetbrains.kotlin.config.JvmAbiStability
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
@@ -197,10 +196,7 @@ class ClassCodegen private constructor(
private fun generateKotlinMetadataAnnotation() {
// TODO: if `-Xmultifile-parts-inherit` is enabled, write the corresponding flag for parts and facades to [Metadata.extraInt].
var extraFlags = JvmAnnotationNames.METADATA_JVM_IR_FLAG
if (state.abiStability != JvmAbiStability.UNSTABLE) {
extraFlags += JvmAnnotationNames.METADATA_JVM_IR_STABLE_ABI_FLAG
}
val extraFlags = context.backendExtension.generateMetadataExtraFlags(state.abiStability)
val facadeClassName = context.multifileFacadeForPart[irClass.attributeOwnerId]
val metadata = irClass.metadata
@@ -46,6 +46,7 @@ public final class JvmAnnotationNames {
public static final int METADATA_STRICT_VERSION_SEMANTICS_FLAG = 1 << 3;
public static final int METADATA_JVM_IR_FLAG = 1 << 4;
public static final int METADATA_JVM_IR_STABLE_ABI_FLAG = 1 << 5;
public static final int METADATA_FIR_FLAG = 1 << 6;
public static final Name DEFAULT_ANNOTATION_MEMBER_NAME = Name.identifier("value");
@@ -27,6 +27,9 @@ enum class DeserializedContainerAbiStability {
// Either the container is stable, or this compiler is configured to ignore ABI stability of dependencies.
STABLE,
// The container is unstable because it is compiled with FIR, and this compiler is _not_ configured to ignore that.
FIR_UNSTABLE,
// The container is unstable because it is compiled with unstable JVM IR backend, and this compiler is _not_ configured to ignore that.
IR_UNSTABLE,
}
@@ -101,6 +101,7 @@ class DeserializedDescriptorResolver {
private val KotlinJvmBinaryClass.abiStability: DeserializedContainerAbiStability
get() = when {
components.configuration.allowUnstableDependencies -> DeserializedContainerAbiStability.STABLE
classHeader.isUnstableFirBinary -> DeserializedContainerAbiStability.FIR_UNSTABLE
classHeader.isUnstableJvmIrBinary -> DeserializedContainerAbiStability.IR_UNSTABLE
else -> DeserializedContainerAbiStability.STABLE
}
@@ -5,7 +5,7 @@
package org.jetbrains.kotlin.load.kotlin.header
import org.jetbrains.kotlin.load.java.JvmAnnotationNames
import org.jetbrains.kotlin.load.java.JvmAnnotationNames.*
import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader.MultifileClassKind.DELEGATING
import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader.MultifileClassKind.INHERITING
import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmBytecodeBinaryVersion
@@ -54,21 +54,25 @@ class KotlinClassHeader(
@Suppress("unused")
val multifileClassKind: MultifileClassKind?
get() = if (kind == Kind.MULTIFILE_CLASS || kind == Kind.MULTIFILE_CLASS_PART) {
if ((extraInt and JvmAnnotationNames.METADATA_MULTIFILE_PARTS_INHERIT_FLAG) != 0)
if (extraInt.has(METADATA_MULTIFILE_PARTS_INHERIT_FLAG))
INHERITING
else
DELEGATING
} else null
val isUnstableJvmIrBinary: Boolean
get() = (extraInt and JvmAnnotationNames.METADATA_JVM_IR_FLAG) != 0 &&
(extraInt and JvmAnnotationNames.METADATA_JVM_IR_STABLE_ABI_FLAG == 0)
get() = extraInt.has(METADATA_JVM_IR_FLAG) && !extraInt.has(METADATA_JVM_IR_STABLE_ABI_FLAG)
val isUnstableFirBinary: Boolean
get() = extraInt.has(METADATA_FIR_FLAG) && !extraInt.has(METADATA_JVM_IR_STABLE_ABI_FLAG)
val isPreRelease: Boolean
get() = (extraInt and JvmAnnotationNames.METADATA_PRE_RELEASE_FLAG) != 0
get() = extraInt.has(METADATA_PRE_RELEASE_FLAG)
val isScript: Boolean
get() = (extraInt and JvmAnnotationNames.METADATA_SCRIPT_FLAG) != 0
get() = extraInt.has(METADATA_SCRIPT_FLAG)
override fun toString() = "$kind version=$metadataVersion"
private fun Int.has(flag: Int): Boolean = (this and flag) != 0
}
@@ -192,7 +192,7 @@ private class ElementAnnotator(
val factory = diagnostic.factory
// hack till the root cause #KT-21246 is fixed
if (isIrCompileClassDiagnosticForModulesWithEnabledIR(diagnostic)) return
if (isUnstableAbiClassDiagnosticForModulesWithEnabledUnstableAbi(diagnostic)) return
assert(diagnostics.all { it.psiElement == element && it.factory == factory })
@@ -272,16 +272,18 @@ private class ElementAnnotator(
data.processDiagnostics(holder, diagnostics, fixesMap)
}
private fun isIrCompileClassDiagnosticForModulesWithEnabledIR(diagnostic: Diagnostic): Boolean {
if (diagnostic.factory != Errors.IR_COMPILED_CLASS) return false
private fun isUnstableAbiClassDiagnosticForModulesWithEnabledUnstableAbi(diagnostic: Diagnostic): Boolean {
val setting = when (diagnostic.factory) {
Errors.IR_COMPILED_CLASS -> K2JVMCompilerArguments::useIR
Errors.FIR_COMPILED_CLASS -> K2JVMCompilerArguments::useFir
else -> return false
}
val module = element.module ?: return false
val moduleFacetSettings = KotlinFacetSettingsProvider.getInstance(element.project)?.getSettings(module) ?: return false
return moduleFacetSettings.isCompilerSettingPresent(K2JVMCompilerArguments::useIR)
|| moduleFacetSettings.isCompilerSettingPresent(K2JVMCompilerArguments::allowJvmIrDependencies)
return moduleFacetSettings.isCompilerSettingPresent(setting)
}
companion object {
val LOG = Logger.getInstance(ElementAnnotator::class.java)
}
}
@@ -22,7 +22,6 @@ import com.intellij.debugger.engine.evaluation.EvaluateException
import com.intellij.debugger.engine.evaluation.EvaluateExceptionUtil
import com.intellij.debugger.engine.evaluation.EvaluationContextImpl
import com.intellij.debugger.engine.evaluation.expression.*
import com.intellij.lang.java.JavaLanguage
import com.intellij.openapi.diagnostic.Attachment
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.progress.ProcessCanceledException
@@ -32,7 +31,6 @@ import com.intellij.testFramework.runInEdtAndWait
import com.sun.jdi.*
import com.sun.jdi.Value
import org.jetbrains.eval4j.*
import org.jetbrains.eval4j.Value as Eval4JValue
import org.jetbrains.eval4j.jdi.JDIEval
import org.jetbrains.eval4j.jdi.asJdiValue
import org.jetbrains.eval4j.jdi.asValue
@@ -47,28 +45,32 @@ import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.idea.core.util.attachmentByPsiFile
import org.jetbrains.kotlin.idea.core.util.mergeAttachments
import org.jetbrains.kotlin.idea.core.util.runInReadActionWithWriteActionPriorityWithPCE
import org.jetbrains.kotlin.idea.debugger.*
import org.jetbrains.kotlin.idea.debugger.DebuggerUtils
import org.jetbrains.kotlin.idea.debugger.evaluate.EvaluationStatus.EvaluationContextLanguage
import org.jetbrains.kotlin.idea.debugger.evaluate.KotlinDebuggerCaches.Companion.compileCodeFragmentCacheAware
import org.jetbrains.kotlin.idea.debugger.evaluate.classLoading.GENERATED_CLASS_NAME
import org.jetbrains.kotlin.idea.debugger.evaluate.classLoading.GENERATED_FUNCTION_NAME
import org.jetbrains.kotlin.idea.debugger.evaluate.compilation.*
import org.jetbrains.kotlin.idea.debugger.evaluate.compilingEvaluator.ClassLoadingResult
import org.jetbrains.kotlin.idea.debugger.evaluate.compilingEvaluator.loadClassesSafely
import org.jetbrains.kotlin.idea.debugger.evaluate.variables.EvaluatorValueConverter
import org.jetbrains.kotlin.idea.debugger.evaluate.variables.VariableFinder
import org.jetbrains.kotlin.idea.debugger.safeLocation
import org.jetbrains.kotlin.idea.debugger.safeMethod
import org.jetbrains.kotlin.idea.debugger.safeVisibleVariableByName
import org.jetbrains.kotlin.idea.resolve.ResolutionFacade
import org.jetbrains.kotlin.idea.util.application.executeWriteCommand
import org.jetbrains.kotlin.idea.util.application.runReadAction
import org.jetbrains.kotlin.platform.jvm.JvmPlatforms
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.AnalyzingUtils
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.isInlineClassType
import org.jetbrains.kotlin.resolve.jvm.AsmTypes
import org.jetbrains.org.objectweb.asm.*
import org.jetbrains.org.objectweb.asm.ClassReader
import org.jetbrains.org.objectweb.asm.tree.ClassNode
import org.jetbrains.kotlin.idea.debugger.evaluate.EvaluationStatus.EvaluationContextLanguage
import org.jetbrains.kotlin.idea.debugger.evaluate.compilingEvaluator.ClassLoadingResult
import org.jetbrains.kotlin.idea.resolve.ResolutionFacade
import org.jetbrains.kotlin.platform.jvm.JvmPlatforms
import java.util.*
import org.jetbrains.eval4j.Value as Eval4JValue
internal val LOG = Logger.getInstance(KotlinEvaluator::class.java)
@@ -446,7 +448,10 @@ class KotlinEvaluator(val codeFragment: KtCodeFragment, private val sourcePositi
companion object {
private val IGNORED_DIAGNOSTICS: Set<DiagnosticFactory<*>> = Errors.INVISIBLE_REFERENCE_DIAGNOSTICS +
setOf(Errors.EXPERIMENTAL_API_USAGE_ERROR, Errors.MISSING_DEPENDENCY_SUPERCLASS, Errors.IR_COMPILED_CLASS)
setOf(
Errors.EXPERIMENTAL_API_USAGE_ERROR, Errors.MISSING_DEPENDENCY_SUPERCLASS, Errors.IR_COMPILED_CLASS,
Errors.FIR_COMPILED_CLASS
)
private val DEFAULT_METHOD_MARKERS = listOf(AsmTypes.OBJECT_TYPE, AsmTypes.DEFAULT_CONSTRUCTOR_MARKER)
@@ -66,11 +66,12 @@ public annotation class Metadata(
* * 0 - this is a multi-file class facade or part, compiled with `-Xmultifile-parts-inherit`.
* * 1 - this class file is compiled by a pre-release version of Kotlin and is not visible to release versions.
* * 2 - this class file is a compiled Kotlin script source file (.kts).
* * 3 - the metadata of this class file is not supposed to be read by the compiler, whose major.minor version is less than
* the major.minor version of this metadata ([metadataVersion]).
* * 4 - this class file is compiled with the new Kotlin compiler backend introduced in Kotlin 1.4.
* * 5 - if the class file is compiled with the new Kotlin compiler backend, the metadata has been verified by the author and
* no metadata incompatibility diagnostic should be reported at the call site.
* * 3 - "strict metadata version semantics". The metadata of this class file is not supposed to be read by the compiler,
* whose major.minor version is less than the major.minor version of this metadata ([metadataVersion]).
* * 4 - this class file is compiled with the new Kotlin compiler backend (JVM IR) introduced in Kotlin 1.4.
* * 5 - this class file has stable metadata and ABI. This is used only for class files compiled with JVM IR (see flag #4) or FIR (#6),
* and prevents metadata incompatibility diagnostics from being reported where the class is used.
* * 6 - this class file is compiled with the new Kotlin compiler frontend (FIR).
*/
@SinceKotlin("1.1")
@get:JvmName("xi")