haxe icon indicating copy to clipboard operation
haxe copied to clipboard

Compiling to HashLink doesn't raise an error when removing Null typing

Open Marika-0 opened this issue 3 years ago • 1 comments

When transforming an array with std.Array.map(Std.parseInt) and assigning the result to a variable typed as Array<Int>, compiling to HashLink doesn't raise an error.

Example code:

class Main {
	static function main():Void {
		final string = "0 1";

		final stringArray = string.split(" ");
		trace(stringArray);

		final nulledArray:Array<Null<Int>> = stringArray.map(Std.parseInt);  // Line 8
		trace(nulledArray);

		final pureIntArray:Array<Int> = stringArray.map(Std.parseInt);       // Line 11
		trace(pureIntArray);
	}
}

This successfully compiles to HashLink, but the assignment on Line 11 causes the following error:

$ haxe -m Main --hl run.hl && hl run.hl
Main.hx:6: [0,1]
Main.hx:9: [0,1]
Uncaught exception: Can't cast hl.types.ArrayObj to hl.types.ArrayBytes_Int
Called from $Main.main(Main.hx:11)
Called from fun$381(?:1)

The assignment on Line 8 is fine, since the variable is typed as Array<Null<Int>>, and the return type of Std.parseInt is Null<Int>.

I would expect compilation to fail due to Line 11, since HashLink is a static target, and I'm trying to assign a potentiall null value to an integer...

Reproduced with Haxe version 4.3.0-rc.1+2cbe1d0 and Hashlink version 1.11.0.

Marika-0 avatar Mar 23 '22 02:03 Marika-0

class Main {
	static function main():Void {
		var arrayNullInt:Array<Null<Int>> = [0, 1];
		var arrayInt:Array<Int> = [0, 1];
		arrayNullInt = arrayInt; // oh no
		trace(arrayNullInt);
	}
}

Oh hey @ncannasse, it's your favorite issue! What was the outcome here? I kind of had this filed as "resolved" in my head.

Simn avatar Mar 23 '22 06:03 Simn