HaxeFoundation / haxe

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

Getter + increment haxe.Int64 variable compile error #11605

Open yuxiaomao opened 3 months ago

yuxiaomao commented 3 months ago

Recently, the following example does not compile, with message "haxe.Int64 should be Int". However, we should be able to increment an haxe.Int64 value. Without getter or without increment, the code compile correctly.

static var uid(get,default) : haxe.Int64;
static var __uid : haxe.Int64 = 0;
static inline function get_uid():haxe.Int64 return __uid;
static var uid2 : haxe.Int64 = 0;

static public function main() {
    uid++; // Error "haxe.Int64 should be Int"
    uid += 1; // Ok
    uid2++; // Ok
}

The first bad commit is https://github.com/HaxeFoundation/haxe/commit/a57c74c770e986f6bbd40c350af6e1bae65abf0b which is also related to the issue about setter and increment https://github.com/HaxeFoundation/haxe/issues/11577

kLabz commented 3 months ago

Same issue there:

    static inline function allocUID() : UID {
        return (SEQ << (#if hxbit64 64 #else 32 #end - SEQ_BITS)) | (++UID);
    }

With hxbit.UID should be Int on ++UID. Reverting a57c74c fixes that.

Simn commented 3 months ago

I have no intuition for how accessor + operator overload should interact here. Should this behave the same way as the += case or what?

yuxiaomao commented 3 months ago

The case of @kLabz is exactly why I open this issue. I'm replacing it locally to continue my dev, with

UID += 1;
return (SEQ << (#if hxbit64 64 #else 32 #end - SEQ_BITS)) | (UID);
ncannasse commented 3 months ago

@Simn if we have a + operator, we should be able to use += and prefix/postfix ++ , by generating the following code:

x += 1; // x = X.add(x,1)
++x; // same
x++;  // { var tmp = x; x = X.add(x,1); tmp; }
Simn commented 3 months ago

Ok hang on, the specification is really not clear to me. We can have three different kinds of operator overload:

  1. @:op(A + B)
  2. @:op(A += B)
  3. @:op(++A)

If I'm understanding what you're saying correctly, the following should be true:

  1. An expression a + b only checks for @:op(A + B)
  2. An expression a += b checks for @:op(A += B), and if that's not applicable it checks for @:op(A + B)
  3. An expression ++a checks for @:op(++A), and if that's not applicable it checks for @:op(A += B), and if that's not applicable it checks for @:op(A + B)

Is that correct? Note that I'm not even talking about getters/setters because it seems like the problem here starts even earlier.

yuxiaomao commented 3 months ago

I thought that @:op(A++) is defined for haxe.Int64 which will performs an add1 (seems quite natural for me as that's an Int), I must have misunderstood something :'( If I recall correctly, @ncannasse said that ++ on String should not be valid (but there is a + on String), so I do not think ++ should automatically performs a +1