From 982982c63027e416bd78ca40708c7238e7eed26b Mon Sep 17 00:00:00 2001 From: Jonas Herzig Date: Thu, 11 Nov 2021 16:52:27 +0100 Subject: Fix remapping of qualified inner class reference in Kotlin code When we used to remap `a.pkg.A.Inner` we would apply both mappings (the one for the inner class and the one for the outer class) at the same time, resulting in `b.pkg.B.Inner.Inner`. The direct cause being that we only checked conflicts for the range of the respective expression (which is just `A` and `Inner` for Kotlin, and therefore not conflicting) instead of the parent as we should have. With that fixed, it now also becomes apparent that we need to apply mappings for dot qualified expressions back to front (otherwise the outer class takes priority), so that is the second thing this commit change. --- .../kotlin/com/replaymod/gradle/remap/PsiMapper.kt | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'src/main/kotlin') diff --git a/src/main/kotlin/com/replaymod/gradle/remap/PsiMapper.kt b/src/main/kotlin/com/replaymod/gradle/remap/PsiMapper.kt index 07d7d1d..f7b50ba 100644 --- a/src/main/kotlin/com/replaymod/gradle/remap/PsiMapper.kt +++ b/src/main/kotlin/com/replaymod/gradle/remap/PsiMapper.kt @@ -219,7 +219,9 @@ internal class PsiMapper( } val parent: PsiElement? = expr.parent if ((parent is KtUserType || parent is KtQualifiedExpression) && parent.text == name) { - replace(parent, mapped) + if (valid(parent)) { + replace(parent, mapped) + } return } // FIXME this incorrectly filters things like "Packet" and doesn't filter same-name type aliases @@ -549,6 +551,24 @@ internal class PsiMapper( return super.visitNamedFunction(function, data) } + override fun visitDotQualifiedExpression(expression: KtDotQualifiedExpression, data: Void?): Void? { + // Dot qualified expressions such as "a.pkg.A.Inner" we want to remap back to front because the + // latter parts are more specific. + // I.e. we start with the inner class, and only if there is no mapping for that, do we try to remap + // the outer class. + expression.selectorExpression?.accept(this) + expression.receiverExpression.accept(this) + return null + } + + override fun visitUserType(type: KtUserType, data: Void?): Void? { + // Same as visitDotQualifiedExpression but for typealias declarations + type.referenceExpression?.accept(this) + type.qualifier?.accept(this) + type.typeArgumentList?.accept(this) + return null + } + override fun visitReferenceExpression(expression: KtReferenceExpression, data: Void?): Void? { if (valid(expression)) { val target = bindingContext[BindingContext.REFERENCE_TARGET, expression] -- cgit