⁠
llamalad7: Feature/method signature intersection (#2647 )
* Refactor: Rework expected MethodSignatures.
- ParameterGroups are gone, with every signature now effectively containing a required leading group and an optional vararg trailing group. The prior flexibility made working them easy to get wrong, for example in the matching logic which would have been incorrect for leading vararg groups.
- Inject previously contained 2 optional groups (the target's params and the captured locals) but this was in fact incorrect, since the captured params are required if we want to capture any locals, so it is now (better) represented as 2 separate signature options.
- The distinction between `WARN_IF_ABSENT` and `ERROR_IF_ABSENT` is entirely removed. It was effectively unused since it only applied to captured Inject locals, which are varargs and therefore never "absent" (a separate inspection handles unused LocalCapture).
- ModifyArgs is in fact all-or-nothing wrt capturing the target parameters, and this is now reflected.
* Fix: Disallow non-int int-like types in @ModifyConstant.
Not sure why these are there, Mixin doesn't allow them.
* Fix: Disallow `nullValue` in `@ModifyConstant`.
It does not work and cannot ever work.
* stash
* stash
* finish general intersection
* cleanup
* stash
* stash
* fix general intersection
* stash
* fix tests
* return type only suggestions
* prioritise existing return type in modifier suggestions
* restore BeforeConstant's behaviour of ensuring only 1 discriminator is specified, and supporting wildcard matches
* Fix: Support `.class` constants in `@ModifyConstant`.
* Switch `@ModifyConstant` to a general signature type.
* int coercion kinda fixed ish
* fix int coercion properly
* cleanup
* proper int-like logic
* cleanup
* some docs and fixes
* fix return type solving
* Cache walked supertypes and improve handling of Object as a supertype.
* Cleanup
* Cleanup
* Small fixes
* Small fixes
* Fix: Suggest "ci" for coerced callba...
* Refactor: Rework expected MethodSignatures.
- ParameterGroups are gone, with every signature now effectively containing a required leading group and an optional vararg trailing group. The prior flexibility made working them easy to get wrong, for example in the matching logic which would have been incorrect for leading vararg groups.
- Inject previously contained 2 optional groups (the target's params and the captured locals) but this was in fact incorrect, since the captured params are required if we want to capture any locals, so it is now (better) represented as 2 separate signature options.
- The distinction between `WARN_IF_ABSENT` and `ERROR_IF_ABSENT` is entirely removed. It was effectively unused since it only applied to captured Inject locals, which are varargs and therefore never "absent" (a separate inspection handles unused LocalCapture).
- ModifyArgs is in fact all-or-nothing wrt capturing the target parameters, and this is now reflected.
* Fix: Disallow non-int int-like types in @ModifyConstant.
Not sure why these are there, Mixin doesn't allow them.
* Fix: Disallow `nullValue` in `@ModifyConstant`.
It does not work and cannot ever work.
* stash
* stash
* finish general intersection
* cleanup
* stash
* stash
* fix general intersection
* stash
* fix tests
* return type only suggestions
* prioritise existing return type in modifier suggestions
* restore BeforeConstant's behaviour of ensuring only 1 discriminator is specified, and supporting wildcard matches
* Fix: Support `.class` constants in `@ModifyConstant`.
* Switch `@ModifyConstant` to a general signature type.
* int coercion kinda fixed ish
* fix int coercion properly
* cleanup
* proper int-like logic
* cleanup
* some docs and fixes
* fix return type solving
* Cache walked supertypes and improve handling of Object as a supertype.
* Cleanup
* Cleanup
* Small fixes
* Small fixes
* Fix: Suggest "ci" for coerced callba...
- /*
- * Minecraft Development for IntelliJ
- *
- * https://mcdev.io/
- *
- * Copyright (C) 2026 minecraft-dev
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published
- * by the Free Software Foundation, version 3.0 only.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
-
- package com.demonwav.mcdev.platform.mixin.inspection.injector
-
- import com.demonwav.mcdev.platform.mixin.handlers.InjectorAnnotationHandler
- import com.demonwav.mcdev.platform.mixin.handlers.MixinAnnotationHandler
- import com.demonwav.mcdev.platform.mixin.handlers.injectionPoint.CollectVisitor
- import com.demonwav.mcdev.platform.mixin.inspection.MixinInspection
- import com.demonwav.mcdev.platform.mixin.util.LocalInfo
- import com.demonwav.mcdev.platform.mixin.util.MethodTargetMember
- import com.demonwav.mcdev.platform.mixin.util.MixinConstants
- import com.demonwav.mcdev.platform.mixin.util.MixinTargetMember
- import com.demonwav.mcdev.platform.mixin.util.hasNamedLocalVariables
- import com.demonwav.mcdev.platform.mixin.util.isMixinExtrasSugar
- import com.demonwav.mcdev.util.findModule
- import com.intellij.codeInsight.intention.LowPriorityAction
- import com.intellij.codeInspection.LocalQuickFix
- import com.intellij.codeInspection.ProblemDescriptor
- import com.intellij.codeInspection.ProblemsHolder
- import com.intellij.codeInspection.options.OptPane
- import com.intellij.modcommand.ModCommand
- import com.intellij.modcommand.ModCommandQuickFix
- import com.intellij.openapi.module.Module
- import com.intellij.openapi.project.Project
- import com.intellij.psi.JavaElementVisitor
- import com.intellij.psi.PsiAnnotation
- import com.intellij.psi.PsiMethod
- import com.intellij.psi.PsiParameter
- import com.siyeh.ig.fixes.RenameFix
-
- class MixinParameterNameInspection : MixinInspection() {
- @JvmField
- var reportForMainSignature = true
- @JvmField
- var reportForLocal = true
-
- override fun getStaticDescription() = "Reports when a parameter name in a Mixin does not match the target class"
-
- override fun getOptionsPane() = OptPane.pane(
- OptPane.checkbox("reportForMainSignature", "Report for main signature"),
- OptPane.checkbox("reportForLocal", "Report for @Local parameters")
- )
-
- override fun buildVisitor(holder: ProblemsHolder) = object : JavaElementVisitor() {
- override fun visitMethod(method: PsiMethod) {
- val module = method.findModule() ?: return
- val parameters = method.parameterList.parameters
- val parametersWithoutSugar = parameters.dropLastWhile { it.isMixinExtrasSugar }.toTypedArray()
-
- val validNames = arrayOfNulls<MutableSet<String>>(parameters.size)
-
- for (annotation in method.annotations) {
- val handler = MixinAnnotationHandler.forMixinAnnotation(annotation, holder.project)
- as? InjectorAnnotationHandler ?: continue
- for (target in MixinAnnotationHandler.resolveTarget(annotation)) {
- if (!collectValidNames(validNames, module, method, parameters, parametersWithoutSugar, annotation, handler, target)) {
- return
- }
- }
- }
-
- for (index in validNames.indices) {
- val names = validNames[index] ?: continue
- if (names.isEmpty()) {
- continue
- }
-
- if (parameters[index].name !in names) {
- val fixes = mutableListOf<LocalQuickFix>()
-
- for (name in names) {
- fixes += RenameFix(name, false, false)
- }
-
- fixes += if (index < parametersWithoutSugar.size) {
- DontReportForMainSignatureFix()
- } else {
- DontReportForLocalFix()
- }
-
- holder.registerProblem(
- parameters[index].nameIdentifier ?: parameters[index],
- "Parameter name does not match name ${names.joinToString(" / ") { "'$it'" } } in target class",
- *fixes.toTypedArray()
- )
- }
- }
- }
- }
-
- private fun collectValidNames(
- validNames: Array<MutableSet<String>?>,
- module: Module, method: PsiMethod,
- parameters: Array<PsiParameter>,
- parametersWithoutSugar: Array<PsiParameter>,
- annotation: PsiAnnotation,
- handler: InjectorAnnotationHandler,
- target: MixinTargetMember
- ): Boolean {
- if (target !is MethodTargetMember) {
- return true
- }
-
- if (!method.hasNamedLocalVariables(target.classAndMethod.clazz.name.replace('/', '.'))) {
- return true
- }
-
- val validNamesForThisTarget = arrayOfNulls<MutableSet<String>>(parameters.size)
-
- if (reportForMainSignature) {
- val expectedSignatures =
- handler.expectedMethodSignature(annotation, target.classAndMethod.clazz, target.classAndMethod.method)
- ?: return false
- var anyValidSignatures = false
-
- for ((expectedParams, _) in expectedSignatures) {
- if (InvalidInjectorMethodSignatureInspection.Util.checkParameters(
- method.parameterList,
- expectedParams,
- handler.allowCoerce
- ) != InvalidInjectorMethodSignatureInspection.CheckResult.OK
- ) {
- continue
- }
-
- anyValidSignatures = true
-
- checkExpectedSignatureForKnownNames(
- validNamesForThisTarget,
- handler,
- expectedParams,
- parametersWithoutSugar
- )
- }
-
- if (!anyValidSignatures) {
- return false
- }
- }
-
- if (reportForLocal) {
- for (pos in parametersWithoutSugar.size until parameters.size) {
- if (!checkSugarForKnownNames(
- validNamesForThisTarget,
- module,
- handler,
- annotation,
- target,
- parameters[pos],
- pos
- )
- ) {
- return false
- }
- }
- }
-
- for (index in validNamesForThisTarget.indices) {
- val names = validNamesForThisTarget[index] ?: continue
- if (validNames[index] == null) {
- validNames[index] = names
- } else {
- validNames[index]!!.retainAll(names)
- }
- }
-
- return true
- }
-
- private fun checkExpectedSignatureForKnownNames(
- validNamesForThisTarget: Array<MutableSet<String>?>,
- handler: InjectorAnnotationHandler,
- expectedParams: List<ParameterGroup>,
- parametersWithoutSugar: Array<PsiParameter>
- ) {
- if (parametersWithoutSugar.isEmpty()) {
- return
- }
-
- var pos = 0
- for (group in expectedParams) {
- if (!group.match(parametersWithoutSugar, pos, handler.allowCoerce)) {
- continue
- }
- for (expectedParam in group.parameters) {
- if (expectedParam.knownName && expectedParam.name != null) {
- if (validNamesForThisTarget[pos] == null) {
- validNamesForThisTarget[pos] = mutableSetOf(expectedParam.name)
- } else {
- validNamesForThisTarget[pos]!!.add(expectedParam.name)
- }
- }
- pos++
- if (pos >= parametersWithoutSugar.size) {
- return
- }
- }
- }
- }
-
- private fun checkSugarForKnownNames(
- validNamesForThisTarget: Array<MutableSet<String>?>,
- module: Module,
- handler: InjectorAnnotationHandler,
- annotation: PsiAnnotation,
- target: MethodTargetMember,
- parameter: PsiParameter,
- pos: Int
- ): Boolean {
- val localAnnotation = parameter.getAnnotation(MixinConstants.MixinExtras.LOCAL) ?: return true
- val localInfo = LocalInfo.fromAnnotation(parameter.type, localAnnotation)
-
- for (insn in handler.resolveInstructions(
- annotation,
- target.classAndMethod.clazz,
- target.classAndMethod.method
- )) {
- val matchedLocal = localInfo.matchLocals(
- module,
- target.classAndMethod.clazz,
- target.classAndMethod.method,
- insn.insn, CollectVisitor.Mode.RESOLUTION
- )?.singleOrNull() ?: return false
- if (matchedLocal.isNamed) {
- if (validNamesForThisTarget[pos] == null) {
- validNamesForThisTarget[pos] = mutableSetOf(matchedLocal.name)
- } else {
- validNamesForThisTarget[pos]!!.add(matchedLocal.name)
- }
- }
- }
-
- return true
- }
-
- private inner class DontReportForMainSignatureFix : ModCommandQuickFix(), LowPriorityAction {
- override fun getFamilyName() = "Don't report for main signature"
-
- override fun perform(project: Project, descriptor: ProblemDescriptor): ModCommand {
- return ModCommand.updateInspectionOption(descriptor.psiElement, this@MixinParameterNameInspection) {
- it.reportForMainSignature = false
- }
- }
- }
-
- private inner class DontReportForLocalFix : ModCommandQuickFix(), LowPriorityAction {
- override fun getFamilyName() = "Don't report for @Local parameters"
-
- override fun perform(project: Project, descriptor: ProblemDescriptor): ModCommand {
- return ModCommand.updateInspectionOption(descriptor.psiElement, this@MixinParameterNameInspection) {
- it.reportForLocal = false
- }
- }
- }
- }
- /*
- * Minecraft Development for IntelliJ
- *
- * https://mcdev.io/
- *
- * Copyright (C) 2026 minecraft-dev
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published
- * by the Free Software Foundation, version 3.0 only.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
-
- package com.demonwav.mcdev.platform.mixin.inspection.injector
-
- import com.demonwav.mcdev.platform.mixin.handlers.InjectorAnnotationHandler
- import com.demonwav.mcdev.platform.mixin.handlers.InsnInjectorAnnotationHandler
- import com.demonwav.mcdev.platform.mixin.handlers.MixinAnnotationHandler
- import com.demonwav.mcdev.platform.mixin.handlers.injectionPoint.CollectVisitor
- import com.demonwav.mcdev.platform.mixin.inspection.MixinInspection
- import com.demonwav.mcdev.platform.mixin.util.LocalInfo
- import com.demonwav.mcdev.platform.mixin.util.MethodTargetMember
- import com.demonwav.mcdev.platform.mixin.util.MixinConstants
- import com.demonwav.mcdev.platform.mixin.util.MixinTargetMember
- import com.demonwav.mcdev.platform.mixin.util.hasNamedLocalVariables
- import com.demonwav.mcdev.platform.mixin.util.isMixinExtrasSugar
- import com.demonwav.mcdev.util.Parameter
- import com.demonwav.mcdev.util.findModule
- import com.demonwav.mcdev.util.ifNullOrEmpty
- import com.intellij.codeInsight.intention.LowPriorityAction
- import com.intellij.codeInspection.LocalQuickFix
- import com.intellij.codeInspection.ProblemDescriptor
- import com.intellij.codeInspection.ProblemsHolder
- import com.intellij.codeInspection.options.OptPane
- import com.intellij.modcommand.ModCommand
- import com.intellij.modcommand.ModCommandQuickFix
- import com.intellij.openapi.module.Module
- import com.intellij.openapi.project.Project
- import com.intellij.psi.JavaElementVisitor
- import com.intellij.psi.PsiAnnotation
- import com.intellij.psi.PsiMethod
- import com.intellij.psi.PsiParameter
- import com.siyeh.ig.fixes.RenameFix
-
- class MixinParameterNameInspection : MixinInspection() {
- @JvmField
- var reportForMainSignature = true
- @JvmField
- var reportForLocal = true
-
- override fun getStaticDescription() = "Reports when a parameter name in a Mixin does not match the target class"
-
- override fun getOptionsPane() = OptPane.pane(
- OptPane.checkbox("reportForMainSignature", "Report for main signature"),
- OptPane.checkbox("reportForLocal", "Report for @Local parameters")
- )
-
- override fun buildVisitor(holder: ProblemsHolder) = object : JavaElementVisitor() {
- override fun visitMethod(method: PsiMethod) {
- val module = method.findModule() ?: return
- val parameters = method.parameterList.parameters
- val parametersWithoutSugar = parameters.dropLastWhile { it.isMixinExtrasSugar }
-
- val validNames = arrayOfNulls<MutableSet<String>>(parameters.size)
-
- for (annotation in method.annotations) {
- val handler = MixinAnnotationHandler.forMixinAnnotation(annotation, holder.project)
- as? InjectorAnnotationHandler ?: continue
- for (target in MixinAnnotationHandler.resolveTarget(annotation)) {
- if (!collectValidNames(validNames, module, method, parameters, parametersWithoutSugar, annotation, handler, target)) {
- return
- }
- }
- }
-
- for (index in validNames.indices) {
- val names = validNames[index].ifNullOrEmpty { continue }
-
- if (parameters[index].name !in names) {
- val fixes = mutableListOf<LocalQuickFix>()
-
- for (name in names) {
- fixes += RenameFix(name, false, false)
- }
-
- fixes += if (index < parametersWithoutSugar.size) {
- DontReportForMainSignatureFix()
- } else {
- DontReportForLocalFix()
- }
-
- holder.registerProblem(
- parameters[index].nameIdentifier ?: parameters[index],
- "Parameter name does not match name ${names.joinToString(" / ") { "'$it'" } } in target class",
- *fixes.toTypedArray()
- )
- }
- }
- }
- }
-
- private fun collectValidNames(
- validNames: Array<MutableSet<String>?>,
- module: Module, method: PsiMethod,
- parameters: Array<PsiParameter>,
- parametersWithoutSugar: List<PsiParameter>,
- annotation: PsiAnnotation,
- handler: InjectorAnnotationHandler,
- target: MixinTargetMember
- ): Boolean {
- if (target !is MethodTargetMember) {
- return true
- }
-
- if (!method.hasNamedLocalVariables(target.classAndMethod.clazz.name.replace('/', '.'))) {
- return true
- }
-
- if (reportForMainSignature) {
- val expectedSignatures = handler.expectedMethodSignatures(annotation, listOf(target.classAndMethod))
-
- for (expected in expectedSignatures) {
- val signatures = when (expected) {
- ExpectedSignatures.Unknown -> continue
- ExpectedSignatures.Invalid -> return false
- is ExpectedSignatures.Valid -> expected.expected
- }
- val validNamesForThisTarget = Array<MutableSet<String>?>(parameters.size) { mutableSetOf() }
- var anyValidSignatures = false
-
- for (expectedSignature in signatures.options) {
- if (!expectedSignature.matches(method)) {
- continue
- }
- anyValidSignatures = true
-
- checkExpectedSignatureForKnownNames(
- validNamesForThisTarget,
- expectedSignature.requiredParams + expectedSignature.trailingParams,
- parametersWithoutSugar,
- )
- }
-
- if (!anyValidSignatures) {
- return false
- }
-
- for ((pos, names) in validNamesForThisTarget.withIndex()) {
- val suggestions = names ?: mutableSetOf()
- val existingNames = validNames[pos]
- if (existingNames == null) {
- validNames[pos] = suggestions
- } else {
- existingNames.retainAll(suggestions)
- }
- }
- }
- }
-
- if (reportForLocal) {
- for (pos in parametersWithoutSugar.size until parameters.size) {
- if (!checkSugarForKnownNames(
- validNames,
- module,
- handler,
- annotation,
- target,
- parameters[pos],
- pos
- )
- ) {
- return false
- }
- }
- }
-
- return true
- }
-
- private fun checkExpectedSignatureForKnownNames(
- validNamesForThisTarget: Array<MutableSet<String>?>,
- expectedParams: List<Parameter>,
- parametersWithoutSugar: List<PsiParameter>
- ) {
- if (parametersWithoutSugar.isEmpty()) {
- return
- }
-
- for (pos in parametersWithoutSugar.indices) {
- val expectedParam = expectedParams[pos]
- if (!expectedParam.knownName || expectedParam.name == null) {
- validNamesForThisTarget[pos] = null
- } else {
- validNamesForThisTarget[pos]?.add(expectedParam.name)
- }
- }
- }
-
- private fun checkSugarForKnownNames(
- validNames: Array<MutableSet<String>?>,
- module: Module,
- handler: InjectorAnnotationHandler,
- annotation: PsiAnnotation,
- target: MethodTargetMember,
- parameter: PsiParameter,
- pos: Int
- ): Boolean {
- if (handler !is InsnInjectorAnnotationHandler) {
- // Can't have locals
- return true
- }
- val localAnnotation = parameter.getAnnotation(MixinConstants.MixinExtras.LOCAL) ?: return true
- val localInfo = LocalInfo.fromAnnotation(parameter.type, localAnnotation)
-
- for (insn in handler.resolveInstructions(
- annotation,
- target.classAndMethod.clazz,
- target.classAndMethod.method
- )) {
- val matchedLocal = localInfo.matchLocals(
- module,
- target.classAndMethod.clazz,
- target.classAndMethod.method,
- insn.insn, CollectVisitor.Mode.RESOLUTION
- )?.singleOrNull() ?: return false
- val suggestions = if (matchedLocal.isNamed) mutableSetOf(matchedLocal.name) else mutableSetOf()
-
- val existingNames = validNames[pos]
- if (existingNames == null) {
- validNames[pos] = suggestions
- } else {
- existingNames.retainAll(suggestions)
- }
- }
-
- return true
- }
-
- private inner class DontReportForMainSignatureFix : ModCommandQuickFix(), LowPriorityAction {
- override fun getFamilyName() = "Don't report for main signature"
-
- override fun perform(project: Project, descriptor: ProblemDescriptor): ModCommand {
- return ModCommand.updateInspectionOption(descriptor.psiElement, this@MixinParameterNameInspection) {
- it.reportForMainSignature = false
- }
- }
- }
-
- private inner class DontReportForLocalFix : ModCommandQuickFix(), LowPriorityAction {
- override fun getFamilyName() = "Don't report for @Local parameters"
-
- override fun perform(project: Project, descriptor: ProblemDescriptor): ModCommand {
- return ModCommand.updateInspectionOption(descriptor.psiElement, this@MixinParameterNameInspection) {
- it.reportForLocal = false
- }
- }
- }
- }