未验证 提交 4a0437a5 编写于 作者: A Andrey Zinovyev 提交者: GitHub

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

#KT-37586 Fixes
上级 bf9fa4c9
......@@ -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");
......
// 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
}
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;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册