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

Null exception when casting with inheritance of depth 2+ #602

Open
cardgdev opened this issue May 22, 2023 · 1 comment
Open

Null exception when casting with inheritance of depth 2+ #602

cardgdev opened this issue May 22, 2023 · 1 comment

Comments

@cardgdev
Copy link

cardgdev commented May 22, 2023

Casting a subclass of a subclass to their base class will cause a strange "null" exception at line 0 of a random external/standard library.

abstract class A {
	public abstract function work():Void;
}

abstract class B extends A {

	public abstract function work(): Void;
}

class C extends B {
	public function new() {}

	public function work():Void {
		trace("doing");
	}
}

class Main {

    static function main(){
        var c = new C();
        var a: A = cast c;
        a.work();
    }
}

Result:

Called from A.work(C:\HaxeToolkit\haxe\std/hl/_std/Date.hx:0)
Called from $Main.main(Main.hx:24)
Called from .init(?:1)

As you can see it strangely fails at "Date.hx:0". Casting to Dynamic instead can be used as a workaround.

The above code snippet works in other targets.

@ncannasse
Copy link
Member

ncannasse commented May 22, 2023

This comes from haxe compiler (genhl) that does not detect that the abstract "work" method is overriden in subclasses, and thus creates a static call to A.work() instead of dynamic call for the specific instance.

Here's ASM dump:

		.21    @0 new 0
		.21    @1 call 1, C.new(0)
		.23    @2 call 1, A.work(0)
		.24    @3 ret 1

The temporary solution is to either define the work() method in A (not abstract method) or maybe to override it in a class D extends C, but I don't think that will be enough.

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