Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calls to static @Invoker methods with dummy body cause "unreachable code" warnings #2284

Open
TheRealWormbo opened this issue Apr 20, 2024 · 6 comments
Labels
platform: mixins status: accepted This is a high-priority feature or a reproduced bug type: bug

Comments

@TheRealWormbo
Copy link

Minecraft Development for IntelliJ plugin version

2024.1-1.7.4

IntelliJ version

2024.1

Operating System

Linux Mint 21.1

Target platform

Mixins

Description of the bug

Mixins that define an @Invoker for a static target method are required to provide a body for the mixin method, but these methods are usually implemented to always throw an exception, for example:

@Mixin(CriteriaTriggers.class)
public interface CriteriaTriggersAccessor {
	@Invoker("register")
	static <T extends CriterionTrigger<?>> T botania_register(T thing) {
		throw new IllegalStateException();
	}
}

The target method (net.minecraft.advancements.CriteriaTriggers#register) usually completes without throwing an exception. However, code using that invoker method causes following lines to be reported as "unreachable code" by IntelliJ:

public class BotaniaCriteriaTriggers {
	public static void init() {
		CriteriaTriggersAccessor.botania_register(AlfheimPortalTrigger.INSTANCE);
		CriteriaTriggersAccessor.botania_register(CorporeaRequestTrigger.INSTANCE); // unreachable code from here
		CriteriaTriggersAccessor.botania_register(GaiaGuardianNoArmorTrigger.INSTANCE);
		CriteriaTriggersAccessor.botania_register(RelicBindTrigger.INSTANCE);
		CriteriaTriggersAccessor.botania_register(UseItemSuccessTrigger.INSTANCE);
		CriteriaTriggersAccessor.botania_register(ManaBlasterTrigger.INSTANCE);
		CriteriaTriggersAccessor.botania_register(LokiPlaceTrigger.INSTANCE);
		CriteriaTriggersAccessor.botania_register(AlfheimPortalBreadTrigger.INSTANCE);
	}
}

(Code from Botania repository: https://github.com/VazkiiMods/Botania classes vazkii.botania.mixin.CriteriaTriggersAccessor and vazkii.botania.common.advancements.BotaniaCriteriaTriggers from the Xplat module, targeting Minecraft 1.20.1)

@RedNesto RedNesto added status: accepted This is a high-priority feature or a reproduced bug platform: mixins and removed status: unverified Needs triage labels Apr 20, 2024
@isXander
Copy link

2024.1 also creates this problem with regular accessor casts for me.

@Earthcomputer
Copy link
Member

IntelliJ is too smart for its own good.

@valoeghese
Copy link

valoeghese commented Apr 30, 2024

I get this issue with casts and instanceof conditions in mixins too
(And yes I'm aware good code practice here would be using accessors instead of casting this to Player)

image

image

Thanks intellij 😍

@Earthcomputer
Copy link
Member

Unfortunately this is not easy to fix. IntelliJ's dataflow inspections are hardcoded with no extension points for plugins. There is no way to tell it that actually no, this expression won't throw, and prevent it from highlighting the subsequent code as unused.

So far the inspection has only highlighted the casts themselves, so we were able to check for a mixin cast and suppress the highlight. However this is hard to tell the difference between actually unreachable code and bogus unreachable code without unreliable heuristics.

@valoeghese
Copy link

Perhaps a pull request adding the required extension points could be made to intellij community edition

@Earthcomputer
Copy link
Member

Note that JetBrains will be disabling this inspection by default as it is causing problems with other frameworks too: https://youtrack.jetbrains.com/issue/IDEA-352283

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform: mixins status: accepted This is a high-priority feature or a reproduced bug type: bug
Projects
None yet
Development

No branches or pull requests

5 participants