Null exception when casting with inheritance of depth 2+

Open
#602 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
c
Domain
compilers

Research direction

Run the Haxe example in Main.hx on HashLink and compare its output with other targets. Use the cast from C to A and the Date.hx:0 stack location as starting points. Done means the example calls C.work() without the null exception or incorrect source location.

Written by the indexing model from the issue text.

Description

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.

Dominant language
C
Stars
911
Forks
207
Avg merge
1d 21h
Merged PRs (30d)
13

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from HaxeFoundation/hashlink

All issues in HaxeFoundation/hashlink

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.