HaxeFoundation / HaxeFoundation/haxe
[cpp] Broken generation when mixing erased type parameters with native templates
- Dominant language
- Haxe
- Stars
- 6.9k
- Forks
- 715
- Avg merge
- 2d 2h
- Merged PRs (30d)
- 11
Description
```haxe
class Main {
static function main() {
var array = [""];
set(cpp.Pointer.ofArray(array), "foo");
}
static function set(a:cpp.Pointer, v:T) {
a.ref = v;
}
}
```
```
Error: ./src/Main.cpp: In static member function ‘static void Main_obj::main()’:
./src/Main.cpp:34:41: error: cannot convert ‘Pointer’ to ‘Pointer’
34 | HXDLIN( 4) ::Main_obj::set(tmp,HX_("foo",06,c7,4d,00));
| ^~~
| |
| Pointer
In file included from ./src/Main.cpp:5:
include/Main.h:55:62: note: initializing argument 1 of ‘static void Main_obj::set(cpp::Pointer, Dynamic)’
55 | static void set(::cpp::Pointer< ::Dynamic > a, ::Dynamic v);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
```
The problem is that T is erased, but cpp.Pointer is a native template which must be initialised with something. Haxe decides that `cpp.Pointer` becomes `::cpp::Pointer<::Dynamic>`. This is fine internally, but as soon as it crosses the boundary between unparameterised and parameterised code, it breaks.
This is actually identical to a ancient flash issue: #1210. flash.Vector is similar to cpp templates, because it is a "real generic" and you cannot just use Dynamic if you don't know the type or you run into issues.
Workaround is to use `@:generic` or `extern inline`, but this should be caught by haxe rather than causing a cpp error. Maybe on cpp it should be disallowed to use non-generic type parameters to instantiate extern type parameters?
Contributor guide
Research direction
Start with the Haxe example and inspect the generated src/Main.cpp and include/Main.h to reproduce the invalid Pointer to Pointer boundary. Compare the behavior with the related flash issue #1210; done means this case is rejected or handled before producing a C++ compilation error, with a regression test covering it.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100