HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.03k stars 648 forks source link

Abstract multitype value that is generic does not generate correctly in 4.3.4 #11676

Closed Oxdeception closed 2 weeks ago

Oxdeception commented 1 month ago

Online example

Minimal Example:

class Main {
    static function main() {
        var comp = new AComponent([1,2,3]);
        trace(comp.doSomething());
    }
}

interface Component<T> {
    function doSomething():T;
}

@:forward
@:multiType
abstract AComponent<T>(Component<T>) {
    public function new(value:T);

    @:to public static inline function toInt(t:Component<Int>, value:Int):IntComponent {
        return new IntComponent(value);
    }

    @:to public static inline function toIntArray(t:Component<Array<Int>>, value:Array<Int>):ArrayComponent<Int> {
        return new ArrayComponent(value);
    }
}

@:generic
@:remove
class ArrayComponent<T> implements Component<Array<T>> {
    final value:Array<T>;

    public function new(value:Array<T>) {
        this.value = value;
        var x = [];
        for (i in 0...value.length) {
            var y = new AComponent(this.value[i]).doSomething();
            x.push(y);
        }
    }

    public function doSomething():Array<T> {
        return this.value;
    }
}

class IntComponent implements Component<Int> {
    final value:Int;

    public function new(value:Int) {
        this.value = value;
    }

    public function doSomething():Int {
        return value;
    }
}

Under 4.3.4 this causes a Not_found error when generating C++, CPPIA, and JVM. Neko, SWF, Python, HashLink, and JavaScript will compile and hit runtime errors.

The example will work correctly with when using 4.3.3, with the exception of HashLink

Ensuring that the AComponent is assigned to a variable before using it apparently resolves the issue Online example

kLabz commented 1 month ago

Additional notes regarding hl:

| Haxe version | Analyzer off                                     | Analyzer on                                      |
| ------------ | ------------------------------------------------ | ------------------------------------------------ |
| 4.3.4        | ok                                               | segfault (`IntComponent.doSomething`)            |
| 4.3.3        | segfault (`$ArrayComponent_Int.__constructor__`) | segfault (`$ArrayComponent_Int.__constructor__`) |

Eval:

[0] Instance constructor not found: _Test.AComponent_Impl_
kLabz commented 1 month ago

Caused by https://github.com/HaxeFoundation/haxe/commit/b00bca0e767169e8ad6cffff6e951d7af7721e16

Other error on hl is unrelated (edit: hmm, maybe not really unrelated), will need to investigate

kLabz commented 1 month ago

@Simn do you have any idea what could be done here without breaking #11526? :/

Simn commented 1 month ago

Likely somewhere in handle_abstract_casts, but I'll have to take a closer look to provide more useful information.