HaxeFoundation / HaxeFoundation/haxe

Passing constant parameter when calling inline function ignores type

Open
#9,170 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

I reported this in the Kha repo Kode/Kha/issues/1179, but it was suggested I report the issue here instead.

The problem is that when you call an inline function with a constant parameter, Haxe will copy the constant directly when inlining, which creates a problem when the syntax of the constant doesn't represent the intended type on a target:

```haxe
import kha.Color;
import kha.FastFloat;

class Main {

static inline function test(x:FastFloat):FastFloat{
return Color.White.A * x; //Color.White.A is a FastFloat
}

static function main(){
var a = test(1.0);
trace(a);
}

}
```

Results in the following generated Java code:

```
float a = ( ( (( ((int) (-1) ) >>> 24 )) * ((float) (0.00392156862745098) ) ) * 1.0 );
```

Note that `FastFloat` is a typedef to `Single` in Java, so the above generated code is illegal without an explicit cast. In C++ it would compile, but the resulting implicit cast could be a potential problem that won't be easy to catch from the Haxe side.

A workaround in the above example would be to change the call to `test((1.0:FastFloat));`

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.