KAPT+IR: support IR error types

#KT-49682
This commit is contained in:
Mikhael Bogdanov
2022-05-27 13:39:57 +02:00
committed by Space
parent 41d6f0dca4
commit 162ca4ac2b
21 changed files with 290 additions and 25 deletions
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.types
import org.jetbrains.kotlin.codegen.AsmUtil
import org.jetbrains.kotlin.codegen.signature.AsmTypeFactory
import org.jetbrains.kotlin.load.kotlin.JvmDescriptorTypeWriter
import org.jetbrains.kotlin.load.kotlin.NON_EXISTENT_CLASS_NAME
import org.jetbrains.kotlin.load.kotlin.TypeMappingMode
import org.jetbrains.kotlin.load.kotlin.mapBuiltInType
import org.jetbrains.kotlin.resolve.jvm.AsmTypes
@@ -56,6 +57,12 @@ object AbstractTypeMapper {
mode: TypeMappingMode = TypeMappingMode.DEFAULT,
sw: Writer? = null
): Type {
if (type.isError()) {
val jvmType = Type.getObjectType(NON_EXISTENT_CLASS_NAME)
with(context) { sw?.writeGenericType(type, jvmType, mode) }
return jvmType
}
val typeConstructor = type.typeConstructor()
if (type is SimpleTypeMarker) {
@@ -216,7 +216,7 @@ abstract class AnnotationCodegen(
genCompileTimeValue(getAnnotationArgumentJvmName(annotationClass, param.name), value, annotationVisitor)
else if (param.defaultValue != null)
continue // Default value will be supplied by JVM at runtime.
else
else if (context.state.classBuilderMode.generateBodies) //skip error for KAPT
error("No value for annotation parameter $param")
}
}
@@ -89,7 +89,7 @@ val IrType.erasedUpperBound: IrClass
is IrClassSymbol -> classifier.owner
is IrTypeParameterSymbol -> classifier.owner.erasedUpperBound
is IrScriptSymbol -> classifier.owner.targetClass!!.owner
else -> error(render())
else -> if (this is IrErrorType) symbol.owner else error(render())
}
/**
@@ -132,6 +132,11 @@ class IrTypeMapper(private val context: JvmBackendContext) : KotlinTypeMapperBas
): Type = AbstractTypeMapper.mapType(this, type, mode, sw)
override fun JvmSignatureWriter.writeGenericType(type: KotlinTypeMarker, asmType: Type, mode: TypeMappingMode) {
if (type is IrErrorType) {
writeAsmType(asmType)
return
}
if (type !is IrSimpleType) return
if (skipGenericSignature() || hasNothingInNonContravariantPosition(type) || type.arguments.isEmpty() || type.isRawTypeImpl()) {
writeAsmType(asmType)
@@ -31,6 +31,7 @@ interface IrDeclarationOrigin {
object FILE_CLASS : IrDeclarationOriginImpl("FILE_CLASS")
object SYNTHETIC_FILE_CLASS : IrDeclarationOriginImpl("SYNTHETIC_FILE_CLASS", isSynthetic = true)
object JVM_MULTIFILE_CLASS : IrDeclarationOriginImpl("JVM_MULTIFILE_CLASS")
object ERROR_CLASS : IrDeclarationOriginImpl("ERROR_CLASS")
object SCRIPT_CLASS : IrDeclarationOriginImpl("SCRIPT_CLASS")
object SCRIPT_STATEMENT : IrDeclarationOriginImpl("SCRIPT_STATEMENT")
@@ -1138,6 +1138,7 @@ fun IrType.toIrBasedKotlinType(): KotlinType = when (this) {
it
}
}
is IrErrorType -> kotlinType ?: error("Can't find KotlinType in IrErrorType: " + (this as IrType).render())
else ->
throw AssertionError("Unexpected type: $this = ${this.render()}")
}
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.ir.types
import org.jetbrains.kotlin.ir.declarations.IrAnnotationContainer
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeAliasSymbol
import org.jetbrains.kotlin.ir.types.impl.IrTypeBase
@@ -28,7 +29,10 @@ abstract class IrType : KotlinTypeMarker, IrAnnotationContainer {
abstract override fun hashCode(): Int
}
abstract class IrErrorType(kotlinType: KotlinType?) : IrTypeBase(kotlinType)
abstract class IrErrorType(kotlinType: KotlinType?, private val errorClassStubSymbol: IrClassSymbol) : IrTypeBase(kotlinType), SimpleTypeMarker {
val symbol: IrClassSymbol
get() = errorClassStubSymbol
}
abstract class IrDynamicType(kotlinType: KotlinType?) : IrTypeBase(kotlinType), DynamicTypeMarker
@@ -86,6 +86,7 @@ interface IrTypeSystemContext : TypeSystemContext, TypeSystemCommonSuperTypesCon
override fun SimpleTypeMarker.typeConstructor(): TypeConstructorMarker = when (this) {
is IrCapturedType -> constructor
is IrSimpleType -> classifier
is IrErrorType -> symbol
else -> error("Unknown type constructor")
}
@@ -5,11 +5,26 @@
package org.jetbrains.kotlin.ir.types.impl
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrTypeParameter
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.ir.IrFileEntry
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrClassImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrValueParameterImpl
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.ir.symbols.IrFileSymbol
import org.jetbrains.kotlin.ir.symbols.IrValueParameterSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrClassSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.SpecialNames
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.model.CaptureStatus
@@ -25,12 +40,46 @@ class IrErrorTypeImpl(
kotlinType: KotlinType?,
override val annotations: List<IrConstructorCall>,
override val variance: Variance,
) : IrErrorType(kotlinType) {
) : IrErrorType(kotlinType, IrErrorClassImpl.symbol) {
override fun equals(other: Any?): Boolean = other is IrErrorTypeImpl
override fun hashCode(): Int = IrErrorTypeImpl::class.java.hashCode()
}
object IrErrorClassImpl : IrClassImpl(
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.ERROR_CLASS, IrClassSymbolImpl(),
Name.special("<error>"), ClassKind.CLASS, DescriptorVisibilities.DEFAULT_VISIBILITY, Modality.FINAL
) {
override var parent: IrDeclarationParent
get() = object: IrFile() {
override val startOffset: Int
get() = TODO("Not yet implemented")
override val endOffset: Int
get() = TODO("Not yet implemented")
override var annotations: List<IrConstructorCall>
get() = TODO("Not yet implemented")
set(_) {}
override val declarations: MutableList<IrDeclaration>
get() = TODO("Not yet implemented")
override val symbol: IrFileSymbol
get() = TODO("Not yet implemented")
override val module: IrModuleFragment
get() = TODO("Not yet implemented")
override val fileEntry: IrFileEntry
get() = TODO("Not yet implemented")
override var metadata: MetadataSource?
get() = TODO("Not yet implemented")
set(_) {}
@ObsoleteDescriptorBasedAPI
override val packageFragmentDescriptor: PackageFragmentDescriptor
get() = TODO("Not yet implemented")
override val fqName: FqName
get() = FqName.ROOT
}
set(_) = TODO()
}
class IrDynamicTypeImpl(
kotlinType: KotlinType?,
override val annotations: List<IrConstructorCall>,
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.ir.types.impl.IrErrorClassImpl
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.SpecialNames
@@ -95,7 +95,11 @@ abstract class TypeTranslator(
when {
approximatedType.isError ->
return IrErrorTypeImpl(approximatedType, translateTypeAnnotations(approximatedType), variance)
return IrErrorTypeImpl(
approximatedType,
translateTypeAnnotations(approximatedType),
variance,
)
approximatedType.isDynamic() ->
return IrDynamicTypeImpl(approximatedType, translateTypeAnnotations(approximatedType), variance)
supportDefinitelyNotNullTypes && approximatedType is DefinitelyNotNullType ->
@@ -1009,13 +1009,14 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati
parameters: JavacList<JCVariableDecl>,
valueParametersFromDescriptor: List<ValueParameterDescriptor>
): Pair<SignatureParser.MethodGenericSignature, JCExpression?> {
val psiElement = kaptContext.origins[method]?.element
val genericSignature = signatureParser.parseMethodSignature(
method.signature, parameters, exceptionTypes, jcReturnType,
nonErrorParameterTypeProvider = { index, lazyType ->
if (descriptor is PropertySetterDescriptor && valueParametersFromDescriptor.size == 1 && index == 0) {
getNonErrorType(descriptor.correspondingProperty.returnType, METHOD_PARAMETER_TYPE,
ktTypeProvider = {
val setterOrigin = (kaptContext.origins[method]?.element as? KtCallableDeclaration)
val setterOrigin = (psiElement as? KtCallableDeclaration)
?.takeIf { it !is KtFunction }
setterOrigin?.typeReference
@@ -1023,7 +1024,11 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati
ifNonError = { lazyType() })
} else if (descriptor is FunctionDescriptor && valueParametersFromDescriptor.size == parameters.size) {
val parameterDescriptor = valueParametersFromDescriptor[index]
val sourceElement = kaptContext.origins[method]?.element as? KtFunction
val sourceElement = when {
psiElement is KtFunction -> psiElement
descriptor is ConstructorDescriptor && descriptor.isPrimary -> (psiElement as? KtClassOrObject)?.primaryConstructor
else -> null
}
getNonErrorType(
parameterDescriptor.type, METHOD_PARAMETER_TYPE,
@@ -1047,11 +1052,11 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati
val returnType = getNonErrorType(
descriptor.returnType, RETURN_TYPE,
ktTypeProvider = {
when (val element = kaptContext.origins[method]?.element) {
is KtFunction -> element.typeReference
is KtProperty -> if (descriptor is PropertyGetterDescriptor) element.typeReference else null
is KtPropertyAccessor -> if (descriptor is PropertyGetterDescriptor) element.property.typeReference else null
is KtParameter -> if (descriptor is PropertyGetterDescriptor) element.typeReference else null
when (psiElement) {
is KtFunction -> psiElement.typeReference
is KtProperty -> if (descriptor is PropertyGetterDescriptor) psiElement.typeReference else null
is KtPropertyAccessor -> if (descriptor is PropertyGetterDescriptor) psiElement.property.typeReference else null
is KtParameter -> if (descriptor is PropertyGetterDescriptor) psiElement.typeReference else null
else -> null
}
},
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// CORRECT_ERROR_TYPES
@file:Suppress("UNRESOLVED_REFERENCE", "ANNOTATION_ARGUMENT_MUST_BE_CONST", "NON_CONST_VAL_USED_IN_CONSTANT_EXPRESSION")
@@ -28,6 +27,6 @@ class ErrorInDeclarations {
annotation class Anno(val a: KClass<Any>)
// EXPECTED_ERROR(kotlin:11:1) cannot find symbol
// EXPECTED_ERROR(kotlin:6:1) cannot find symbol
// EXPECTED_ERROR(kotlin:12:1) cannot find symbol
// EXPECTED_ERROR(kotlin:7:1) cannot find symbol
// EXPECTED_ERROR(kotlin:13:1) cannot find symbol
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// CORRECT_ERROR_TYPES
// NO_VALIDATION
// WITH_STDLIB
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// CORRECT_ERROR_TYPES
// FILE: a.kt
@@ -53,4 +52,4 @@ interface TestC {
fun e(): LibFooBar
}
// EXPECTED_ERROR(kotlin:17:5) cannot find symbol
// EXPECTED_ERROR(kotlin:16:5) cannot find symbol
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// CORRECT_ERROR_TYPES
// FILE: te/st/a/JavaClass
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// CORRECT_ERROR_TYPES
// JAVAC_OPTION -Xmaxerrs=1
@@ -12,4 +11,4 @@ class Test {
// There are two errors (unresolved identifier ABC, BCD) actually.
// But we specified the max error count, so the error output is limited.
// EXPECTED_ERROR(kotlin:8:5) cannot find symbol
// EXPECTED_ERROR(kotlin:7:5) cannot find symbol
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// CORRECT_ERROR_TYPES
// NON_EXISTENT_CLASS
// NO_VALIDATION
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// NON_EXISTENT_CLASS
// NO_VALIDATION
@@ -0,0 +1,129 @@
import java.lang.System;
@kotlin.Suppress(names = {"UNRESOLVED_REFERENCE"})
@kotlin.Metadata()
public final class NonExistentClassWIthoutCorrectionKt {
}
////////////////////
import java.lang.System;
@kotlin.Metadata()
public final class NonExistentType {
@org.jetbrains.annotations.Nullable()
private static final error.NonExistentClass a = null;
@org.jetbrains.annotations.Nullable()
private static final java.util.List<error.NonExistentClass> b = null;
@org.jetbrains.annotations.NotNull()
private static final kotlin.jvm.functions.Function1<error.NonExistentClass, kotlin.Unit> c = null;
@org.jetbrains.annotations.Nullable()
private static final error.NonExistentClass d = null;
public static java.lang.String string2;
public static error.NonExistentClass coocoo;
public static error.NonExistentClass coocoo2;
public static error.NonExistentClass coocoo21;
public static error.NonExistentClass coocoo3;
public static error.NonExistentClass coocoo31;
@org.jetbrains.annotations.NotNull()
public static final NonExistentType INSTANCE = null;
private NonExistentType() {
super();
}
@org.jetbrains.annotations.Nullable()
public final error.NonExistentClass getA() {
return null;
}
@org.jetbrains.annotations.Nullable()
public final java.util.List<error.NonExistentClass> getB() {
return null;
}
@org.jetbrains.annotations.NotNull()
public final kotlin.jvm.functions.Function1<error.NonExistentClass, kotlin.Unit> getC() {
return null;
}
@org.jetbrains.annotations.Nullable()
public final error.NonExistentClass getD() {
return null;
}
@org.jetbrains.annotations.NotNull()
public final java.lang.String getString2() {
return null;
}
public final void setString2(@org.jetbrains.annotations.NotNull()
java.lang.String p0) {
}
@org.jetbrains.annotations.NotNull()
public final error.NonExistentClass getCoocoo() {
return null;
}
public final void setCoocoo(@org.jetbrains.annotations.NotNull()
error.NonExistentClass p0) {
}
@org.jetbrains.annotations.NotNull()
public final error.NonExistentClass getCoocoo2() {
return null;
}
public final void setCoocoo2(@org.jetbrains.annotations.NotNull()
error.NonExistentClass p0) {
}
@org.jetbrains.annotations.NotNull()
public final error.NonExistentClass getCoocoo21() {
return null;
}
public final void setCoocoo21(@org.jetbrains.annotations.NotNull()
error.NonExistentClass p0) {
}
@org.jetbrains.annotations.NotNull()
public final error.NonExistentClass getCoocoo3() {
return null;
}
public final void setCoocoo3(@org.jetbrains.annotations.NotNull()
error.NonExistentClass p0) {
}
@org.jetbrains.annotations.NotNull()
public final error.NonExistentClass getCoocoo31() {
return null;
}
public final void setCoocoo31(@org.jetbrains.annotations.NotNull()
error.NonExistentClass p0) {
}
@org.jetbrains.annotations.NotNull()
public final error.NonExistentClass a(@org.jetbrains.annotations.NotNull()
error.NonExistentClass a, @org.jetbrains.annotations.NotNull()
java.lang.String s) {
return null;
}
@org.jetbrains.annotations.NotNull()
public final error.NonExistentClass b(@org.jetbrains.annotations.NotNull()
java.lang.String s) {
return null;
}
}
////////////////////
package error;
public final class NonExistentClass {
}
@@ -0,0 +1,65 @@
import java.lang.System;
@kotlin.Suppress(names = {"UNRESOLVED_REFERENCE"})
@kotlin.Metadata()
public final class NonExistentType {
@org.jetbrains.annotations.Nullable()
private static final ABCDEF a = null;
@org.jetbrains.annotations.Nullable()
private static final java.util.List<ABCDEF> b = null;
@org.jetbrains.annotations.NotNull()
private static final Function1<ABCDEF, kotlin.Unit> c = null;
@org.jetbrains.annotations.Nullable()
private static final ABCDEF<java.lang.String, Function1<java.util.List<ABCDEF>, kotlin.Unit>> d = null;
@org.jetbrains.annotations.NotNull()
public static final NonExistentType INSTANCE = null;
private NonExistentType() {
super();
}
@org.jetbrains.annotations.Nullable()
public final ABCDEF getA() {
return null;
}
@org.jetbrains.annotations.Nullable()
public final java.util.List<ABCDEF> getB() {
return null;
}
@org.jetbrains.annotations.NotNull()
public final Function1<ABCDEF, kotlin.Unit> getC() {
return null;
}
@org.jetbrains.annotations.Nullable()
public final ABCDEF<java.lang.String, Function1<java.util.List<ABCDEF>, kotlin.Unit>> getD() {
return null;
}
@org.jetbrains.annotations.NotNull()
public final Foo getFoo() {
return null;
}
@org.jetbrains.annotations.NotNull()
public final ABCDEF a(@org.jetbrains.annotations.NotNull()
ABCDEF a, @org.jetbrains.annotations.NotNull()
java.lang.String s) {
return null;
}
@org.jetbrains.annotations.NotNull()
public final ABCDEF b(@org.jetbrains.annotations.NotNull()
java.lang.String s) {
return null;
}
}
////////////////////
package error;
public final class NonExistentClass {
}