HaxeFoundation / HaxeFoundation/haxe

`unify_min` ignores `@:to` and `@:from` fields

Open
#8,479 2 comments 0 reactions 0 assignees View on GitHub
bug feature-type-inference
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

Sample from https://github.com/HaxeFoundation/haxe/issues/5279#issuecomment-222106177
```haxe
class Main {
public static function main() {
function test() //:Int (works if explicitly hint return type as Int)
return if (true) getInt() else getAbstract();
}

static function getInt():Int return 1;
static function getAbstract():MyAbstract return "1";
}
abstract MyAbstract(String) from String to String {
@:to
public inline function toInt():Int return Std.parseInt(this);
}
```
Lines, which make the difference depending on return type hint:
https://github.com/HaxeFoundation/haxe/blob/e24798614b516420614b92163e649643fedb642e/src/typing/typer.ml#L2213-L2221
The same happens for `try...catch` and `switch` expressions.
I tried to change `Type.unify` to take into account `@:from`/`@:to`, but that breaks other things
Changing `unify_min` to handle `@:to/@:from` is also not enough, because (unlike `Abstract.cast_or_unify`) it does not return cast expressions.
I think we need to change `unify_min` and `unify_min_raise` to return expressions in addition to deduced type, but that will affect various other places in the compiler (e.g. array and map declaration)

Contributor guide

Open the contributing guide

Research direction

Start with src/typing/typer.ml at the mentioned lines and trace unify_min, unify_min_raise, Type.unify, and Abstract.cast_or_unify. Use the Haxe sample as the reproduction; done means inferred return types handle @:to/@:from conversions consistently in the sample, try...catch, and switch expressions.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.