HaxeFoundation / HaxeFoundation/haxe

Catching multiple distinct enums catches with the wrong catch

Open
#12,151 13 comments 0 reactions 1 assignee Claimed by @kLabz View on GitHub
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

If you have a try{} from which multiple different enums might be thrown the first catch for any enum will catch whatever enum is thrown. This happens on cpp. HL gets this right and the correct catch is hit. I believe this is related to Std.isOfType on cpp not correctly distinguishing the different enums. In the attached example changing the order of the three enum catches Error, FirstErrorEnum, and SecondErrorEnum will determine which catches the Error.Custom("EOF").

```Main.hx
import haxe.ValueException;
import haxe.io.Eof;
import haxe.io.Error;

enum FirstErrorEnum {
ERROR1;
}

enum SecondErrorEnum {
ERROR2;
}

class Main {
static function handle_error(e:Dynamic) {
switch (e) {
// First enum in the list will match Error on CPP.
case Std.isOfType(e, FirstErrorEnum) => true:
trace('handle FirstErrorEnum: ${Type.typeof(e)}');
case Std.isOfType(e, SecondErrorEnum) => true:
trace('handle SecondErrorEnum: ${Type.typeof(e)}');
case Std.isOfType(_, Eof) => true:
trace('handle Eof: ${Type.typeof(e)}');
case Std.isOfType(e, Error) => true:
trace('handle Error ${Type.typeof(e)}');
case Std.isOfType(e, ValueException) => true:
trace('handle ValueException: $e');
default:
trace('handle default: $e');
}
}

static function f() {
throw Error.Custom("EOF");
}

static function main() {
try {
f();
} catch (e:Eof) {
// Eof is a class and is correctly skipped
trace('catch Eof ${Type.typeof(e)}');
handle_error(e);
} catch (e:ValueException) {
trace('catch ValueException $e');
handle_error(e);
} catch (e:FirstErrorEnum) {
trace('catch FirstErrorEnum ${Type.typeof(e)}');
handle_error(e);
} catch (e:Error) {
trace('catch Error ${Type.typeof(e)}');
handle_error(e);
} catch (e:SecondErrorEnum) {
// First enum in the list will match Error on CPP.
trace('catch SecondErrorEnum ${Type.typeof(e)}');
handle_error(e);
}
}
}
```

build-cpp.hxml

```
-cp src
--main Main
--cpp export/cpp
-D real-position
```

build-hl.hxml

```
-cp src
--main Main
--hl export/hl/Main.hl
-D real-position
```

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.