Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cs] Generated casts are not consistent sometimes #8664

Closed
RealyUniqueName opened this issue Aug 19, 2019 · 1 comment
Closed

[cs] Generated casts are not consistent sometimes #8664

RealyUniqueName opened this issue Aug 19, 2019 · 1 comment
Assignees
Milestone

Comments

@RealyUniqueName
Copy link
Member

RealyUniqueName commented Aug 19, 2019

import cs.NativeArray;

class Main {
	public static function main() {
		var a:Arr<String> = Arr.alloc(10);
		var a2 = a.map(function(v) return Std.parseInt(v));
	}
}

class Arr<T> {
	public var length(default, null):Int;

	var __a:NativeArray<T>;

	public inline static function alloc<Y>(size:Int):Arr<Y> {
		return new Arr(new NativeArray(size));
	}

	function new(native:NativeArray<T>) {
		this.length = native.Length;
		this.__a = native;
	}

	public inline function map<S>(f:T->S):Arr<S> {
		var ret = alloc(length);
		for (i in 0...length)
			ret.__unsafe_set(i, f(__unsafe_get(i)));
		return ret;
	}

	inline function __unsafe_get(idx:Int):T {
		return __a[idx];
	}

	inline function __unsafe_set(idx:Int, val:T):T {
		return __a[idx] = val;
	}
}
$ haxe -main Main -cs bin
haxelib run hxcs hxcs_build.txt --haxe-version 4000 --feature-level 1
Note: dmcs is deprecated, please use mcs instead!
src/Main.cs(38,44): error CS0030: Cannot convert type `object[]' to `haxe.lang.Null<int>[]'
Compilation failed: 1 error(s), 0 warnings

Generated code for main function:

public static void main() {
  global::Array<string> a = new global::Array<string>(new string[]{"1", "2"});
  // vvv Here everything is object[] vvv
  global::Array<object> ret = new global::Array<object>(((object[]) (new object[a.length]) ));   {
    int _g = 0;
    int _g1 = a.length;
    while (( _g < _g1 )) {
      int i = _g++;
      {
        global::haxe.lang.Null<int> val = global::Std.parseInt(((string) (a.__a[i]) ));
        // vvv Here it's Nill<int>[] (this is line 38) vvv
        ((global::haxe.lang.Null<int>[]) (ret.__a) )[i] = val; 
      }

    }

  }

  global::Array<object> a2 = ret;
}

Both emphasized lines have the same types in AST. But generated types in cs are different.

@RealyUniqueName RealyUniqueName added this to the Release 4.0 milestone Aug 19, 2019
@RealyUniqueName RealyUniqueName self-assigned this Aug 19, 2019
@RealyUniqueName
Copy link
Member Author

Was blocking #5749

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant