[KAPT] Fix field type correction for delegates (#4107)

#KT-37586 Fixes
This commit is contained in:
Andrey Zinovyev
2021-02-14 10:38:38 +03:00
committed by GitHub
parent bf9fa4c9da
commit 4a0437a507
5 changed files with 77 additions and 4 deletions
@@ -56,6 +56,7 @@ import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DelegatingBindingTrace
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
import org.jetbrains.kotlin.resolve.calls.model.DefaultValueArgument
import org.jetbrains.kotlin.resolve.calls.model.ResolvedValueArgument
import org.jetbrains.kotlin.resolve.constants.*
@@ -704,20 +705,31 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati
return null
}
fun typeFromAsm() = signatureParser.parseFieldSignature(field.signature, treeMaker.Type(type))
// Enum type must be an identifier (Javac requirement)
val typeExpression = if (isEnum(field.access))
val typeExpression = if (isEnum(field.access)) {
treeMaker.SimpleName(treeMaker.getQualifiedName(type).substringAfterLast('.'))
else
} else if (descriptor is PropertyDescriptor && descriptor.isDelegated) {
getNonErrorType(
(descriptor as? CallableDescriptor)?.returnType, RETURN_TYPE,
(origin.element as? KtProperty)?.delegateExpression?.getType(kaptContext.bindingContext),
RETURN_TYPE,
ktTypeProvider = { null },
ifNonError = ::typeFromAsm
)
} else {
getNonErrorType(
(descriptor as? CallableDescriptor)?.returnType,
RETURN_TYPE,
ktTypeProvider = {
val fieldOrigin = (kaptContext.origins[field]?.element as? KtCallableDeclaration)
?.takeIf { it !is KtFunction }
fieldOrigin?.typeReference
},
ifNonError = { signatureParser.parseFieldSignature(field.signature, treeMaker.Type(type)) }
ifNonError = ::typeFromAsm
)
}
lineMappings.registerField(containingClass, field)
@@ -119,6 +119,11 @@ public class ClassFileToSourceStubConverterTestGenerated extends AbstractClassFi
runTest("plugins/kapt3/kapt3-compiler/testData/converter/defaultParameterValueOn.kt");
}
@TestMetadata("delegateCorrectErrorTypes.kt")
public void testDelegateCorrectErrorTypes() throws Exception {
runTest("plugins/kapt3/kapt3-compiler/testData/converter/delegateCorrectErrorTypes.kt");
}
@TestMetadata("deprecated.kt")
public void testDeprecated() throws Exception {
runTest("plugins/kapt3/kapt3-compiler/testData/converter/deprecated.kt");
@@ -120,6 +120,11 @@ public class IrClassFileToSourceStubConverterTestGenerated extends AbstractIrCla
runTest("plugins/kapt3/kapt3-compiler/testData/converter/defaultParameterValueOn.kt");
}
@TestMetadata("delegateCorrectErrorTypes.kt")
public void testDelegateCorrectErrorTypes() throws Exception {
runTest("plugins/kapt3/kapt3-compiler/testData/converter/delegateCorrectErrorTypes.kt");
}
@TestMetadata("deprecated.kt")
public void testDeprecated() throws Exception {
runTest("plugins/kapt3/kapt3-compiler/testData/converter/deprecated.kt");
@@ -0,0 +1,13 @@
// CORRECT_ERROR_TYPES
@file:Suppress("UNRESOLVED_REFERENCE")
package test
class Delegate {
operator fun getValue(thisRef: Any, property: KProperty<*>): Any {return Any()}
}
class Bar(delegate: Delegate) {
private val unknown: Unknown by delegate
}
@@ -0,0 +1,38 @@
package test;
import java.lang.System;
@kotlin.Metadata()
public final class Bar {
private final test.Delegate unknown$delegate = null;
public Bar(@org.jetbrains.annotations.NotNull()
test.Delegate delegate) {
super();
}
private final Unknown getUnknown() {
return null;
}
}
////////////////////
package test;
import java.lang.System;
@kotlin.Metadata()
public final class Delegate {
public Delegate() {
super();
}
@org.jetbrains.annotations.NotNull()
public final java.lang.Object getValue(@org.jetbrains.annotations.NotNull()
java.lang.Object thisRef, @org.jetbrains.annotations.NotNull()
KProperty<?> property) {
return null;
}
}