Skip to content

Commit

Permalink
insert this. automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed Jan 3, 2024
1 parent 15e2568 commit 50e3af4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/main/kotlin/com/replaymod/gradle/remap/PsiMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.jetbrains.kotlin.com.intellij.psi.impl.compiled.ClsClassImpl
import org.jetbrains.kotlin.com.intellij.psi.impl.java.stubs.JavaStubElementTypes
import org.jetbrains.kotlin.com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.com.intellij.psi.util.ClassUtil
import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.js.resolve.diagnostics.findPsi
Expand Down Expand Up @@ -153,16 +154,32 @@ internal class PsiMapper(
ambiguousImportStatics[ref]!!.add(mapped ?: fieldName)
}
if (mapped == null || mapped == fieldName) return
replaceIdentifier(expr, mapped)

if (expr is PsiJavaCodeReferenceElement
&& !expr.isQualified // qualified access is fine
&& !isSwitchCase(expr) // referencing constants in case statements is fine
) {
error(expr, "Implicit member reference to remapped field \"$fieldName\". " +
"This can cause issues if the remapped reference becomes shadowed by a local variable and is therefore forbidden. " +
"Use \"this.$fieldName\" instead.")
// check if actually ambiguous (new name is shadowed by local variable)
var parent = expr.parent
while (parent != null && parent !is PsiCodeBlock) {
parent = parent.parent
}
if (parent != null) {
val scope = PsiTreeUtil.collectElementsOfType(parent, PsiVariable::class.java)
for (variable in scope) {
if (variable.name == mapped) {
replaceIdentifier(expr, "this.")
}
}
} else {
error(
expr, "Implicit member reference to remapped field \"$fieldName\". " +
"This can cause issues if the remapped reference becomes shadowed by a local variable and is therefore forbidden. " +
"Use \"this.$fieldName\" instead."
)
}
}
replaceIdentifier(expr, mapped)
}

private fun map(expr: PsiElement, method: PsiMethod) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,37 @@ class TestMixinShadow {
}
""".trimIndent()
}

@Test
fun `automatically add this(dot) for conflicts with local variables`() {
TestData.remap("""
@org.spongepowered.asm.mixin.Mixin(a.pkg.A.class)
abstract class MixinA {
@org.spongepowered.asm.mixin.Shadow
private a.pkg.A a;
@org.spongepowered.asm.mixin.Shadow
private int aField;
private void test() {
a = null;
int bField = 0;
aField = 1;
bField = 1;
}
}
""".trimIndent()) shouldBe """
@org.spongepowered.asm.mixin.Mixin(b.pkg.B.class)
abstract class MixinA {
@org.spongepowered.asm.mixin.Shadow
private b.pkg.B b;
@org.spongepowered.asm.mixin.Shadow
private int bField;
private void test() {
b = null;
int bField = 0;
this.bField = 1;
bField = 1;
}
}
""".trimIndent()
}
}

0 comments on commit 50e3af4

Please sign in to comment.