HaxeFoundation / HaxeFoundation/haxe

[hl] __compare method in @:struct class breaks

Open
#10,075 3 comments 0 reactions 1 assignee Claimed by @ncannasse View on GitHub
bug platform-hl
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

So given the sample code:
```hx
@:struct
@:structInit
@:publicFields
class Thing {
var a: Int;
var b: Int;

@:keep
function __compare(other: Dynamic): Int {
return switch Std.downcast(other, Thing) {
case null: -1;
case thing: switch a - thing.a {
case 0: b - thing.b;
case c: c;
}
}
}
}

class Main {
static function main() {
var thing1: Thing = {a: 1, b: 2};
var thing2: Thing = {a: 1, b: 2};
trace(thing1 == thing2); // <--- Fails here
}
}
```
It'll fail with this error message (from hashlink):
```
SIGNAL 11
hl.BaseType.check(/usr/share/haxe/std/hl/BaseType.hx:33)
Thing.__compare(Main.hx:10)
$Main.main(Main.hx:23)
fun$399(?:1)

Segmentation fault (core dumped)
Error: Command failed with error 139
```
After looking at the bytecode dump, it looks like `__compare` isn't actually generated as a method, but rather a "binding":
```
$Thing no global
extends hl.Class
0 fields
0 methods
1 bindings
@4 fun@27 <----- this should be in the `methods` section!
```
For reference, [this](https://github.com/HaxeFoundation/hashlink/blob/master/src/std/obj.c#L252-L269) is where HashLink looks for the `__compare` method.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.