HaxeFoundation / haxe

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

static @:to for non-multiType abstracts #8580

Closed nadako closed 5 years ago

nadako commented 5 years ago

This compiles, while it probably should not:

abstract A(Int) {
    @:to public static function toString(value:Int):String {
        return "hello";
    }
}

class Main {
    static function main() {
        var a:A = null;
        trace(a.toString()); // wat
    }
}

Looks like there are some missing checks for @:impl and friends (or they are done at the wrong time).

Maybe static @:to methods shouldn't be allowed at all for non-@:multiType abstracts?

nadako commented 5 years ago

Not a regression

RealyUniqueName commented 5 years ago

It's explicitly implemented here: https://github.com/HaxeFoundation/haxe/blob/bdddb588cbb99e2bb0428a79e6a6aef614715225/src/typing/matcher.ml#L244-L248 I didn't find any clue to why it is done. Disabling it doesn't break any tests. @Simn ?

nadako commented 5 years ago

are you sure it's about this issue and not #8579? :)

RealyUniqueName commented 5 years ago

Oh, right :)

back2dos commented 5 years ago

There's been some kind of "selective methods" or whatever they're termed. IIRC it was even in the manual at some point in time, but I can't find it anymore.

For @:to casts, it allows you to specialize on type parameter: http://try-haxe.mrcdk.com/#c0c9D

I guess it would look less weird if the argument were the abstract instead of the underlying type.

Simn commented 5 years ago

Checklist for any change regarding @:multiType:

  1. Does it break Map?
  2. If not, change it.
  3. Make a mental note that one day we'll implement Map differently.
RealyUniqueName commented 5 years ago

Static @:to is actually intended to work with non-multitype abstracts to allow specialization of @:to methods based on type parameters: #1419 https://github.com/HaxeFoundation/haxe/blob/c60166a8272726b42fdc9f7858b3337ed8151f6e/tests/unit/src/unit/MyAbstract.hx#L107-L115