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

[jvm] Bad type on operand stack when spreading an array for Rest<T> generic arg #11907

Open
PXshadow opened this issue Jan 2, 2025 · 1 comment
Assignees

Comments

@PXshadow
Copy link
Contributor

PXshadow commented Jan 2, 2025

function main():Void {
    t(...[10]);
}

function t<T>(x:haxe.Rest<T>) {}

openjdk 21.0.2
Haxe nightly 5.0.0-alpha.1+0620b1b

@Simn Simn self-assigned this Jan 3, 2025
@Simn
Copy link
Member

Simn commented Jan 3, 2025

I don't think an open Rest<T> can work on this target. Rest arguments are based on native arrays, which are invariant. There is no type in the run-time that would accept both int[] and java.lang.Object[], so declaring one in Haxe is always going to cause problems.

You can produce a similar error with Vector directly:

import haxe.ds.Vector;

function main():Void {
	var a = Vector.fromArrayCopy([10]);
	t(a); // Exception in thread "main" java.lang.ClassCastException: class [I cannot be cast to class [Ljava.lang.Object; ([I and [Ljava.lang.Object; are in module java.base of loader 'bootstrap')
}

function t<T>(x:Vector<T>) {
	trace(x);
}

The problem with catching this during typing is that the compiler doesn't have a mechanism to disallow something like a generic Vector<T>, and I'm not sure how that would best be expressed either.

This can be worked around by making t inline, because then we can work with the concrete type at the call-site.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants