haxe icon indicating copy to clipboard operation
haxe copied to clipboard

Setter not triggered with abstract

Open ncannasse opened this issue 5 months ago • 1 comments

In some cases it seems doing a set inside an abstract does not trigger the value setter.

enum E {
   A;
}
class Main {
    public static var flags(default,set) : haxe.EnumFlags<E> = new haxe.EnumFlags();
    static function set_flags(v) {
        throw "Not reached";
        return flags = v;
    }
    static function main() {
        flags.set(A);
        trace("Done");
    }
}

ncannasse avatar Jun 10 '25 18:06 ncannasse

This seems a little complicated because we're basically turning a read access (flags) into a write access by inlining a call that writes to this. The @:debug.inline output is this:

Inline set:
        Args:
                v<6003> = [Field:E]
                        [TypeExpr E:Enum<E>]
                        [FEnum:E]
                                E
                                A
                this<6002> = [Field:haxe.EnumFlags<E>]
                        [TypeExpr Main:Class<Main>]
                        [FStatic:haxe.EnumFlags<E>]
                                Main
                                flags:haxe.EnumFlags<E>
                _this<6001> = [TypeExpr haxe._EnumFlags.EnumFlags_Impl_:Abstract<haxe.EnumFlags>]
        Expr: [Block:Int]
                [Binop:Int]
                        [Local this(5976):Int:Int]
                        |=
                        [Binop:Int]
                                [Const:Int] 1
                                <<
                                [Field:Int]
                                        [Local v(5977):haxe.EnumFlags.T:haxe.EnumFlags.T]
                                        [FDynamic:Int] _hx_index
        Result: [Block:Void]
                [Binop:Int]
                        [Field:haxe.EnumFlags<E>]
                                [TypeExpr Main:Class<Main>]
                                [FStatic:haxe.EnumFlags<E>]
                                        Main
                                        flags:haxe.EnumFlags<E>
                        |=
                        [Binop:Int]
                                [Const:Int] 1
                                <<
                                [Field:Int]
                                        [Field:E]
                                                [TypeExpr E:Enum<E>]
                                                [FEnum:E]
                                                        E
                                                        A
                                        [FDynamic:Int] _hx_index

So this would require completely rewriting the |= assignment as if we were just typing it. Our entire operator handling is mostly based on expr, not texpr, which makes this a new case.

I'm not saying it's impossible but it looks like a bit of a project.

Simn avatar Jun 11 '25 06:06 Simn