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

Loops array initialization + fluent functions. Incorrect code generated. #23

Open
ze0nni opened this issue Dec 10, 2023 · 2 comments
Open
Labels
bug Something isn't working

Comments

@ze0nni
Copy link

ze0nni commented Dec 10, 2023

Me again but with a real bug

final n = Std.int(Mathf.random(3) + 1);
final cells = [for (i in 0...n) "A"]
        .concat([for (i in 0...n) "B"])
        .concat([for (i in 0...n) "C"]);
Lib.trace(cells);
var _n = (random(3) + 1 | 0);
var __g = []; 
repeat (_n) array_push(_cells, "A");
var __g = [];
repeat (_n) array_push(__g, "B");
var _cells1 = gml_internal_ArrayImpl.concat(_cells, __g);
var _cells = [];
repeat (_n) array_push(__g, "C");
show_debug_message(gml_internal_ArrayImpl.concat(_cells1, __g));

var _cells = []; Declared after first using
var __g = []; Declared two times

Looks like order of declare variables inverted.

@YellowAfterlife
Copy link
Member

YellowAfterlife commented Dec 10, 2023

Technically the generated structure is a bit of a mess - you can see it if you build it for JS,

class Test {
	static function main() {
		final n = Std.int(Std.random(3) + 1);
		final cells = [for (i in 0...n) "A"]
						.concat([for (i in 0...n) "B"])
						.concat([for (i in 0...n) "C"]);
		trace(cells);
	}
}
Output
var Test = function() { };
Test.main = function() {
	var n = Std.random(3) + 1 | 0;
	var _g = [];
	var _g1 = 0;
	var _g2 = n;
	while(_g1 < _g2) {
		var i = _g1++;
		_g.push("A");
	}
	var cells = _g;
	var _g = [];
	var _g1 = 0;
	var _g2 = n;
	while(_g1 < _g2) {
		var i = _g1++;
		_g.push("B");
	}
	var cells1 = cells.concat(_g);
	var _g = [];
	var _g1 = 0;
	var _g2 = n;
	while(_g1 < _g2) {
		var i = _g1++;
		_g.push("C");
	}
	var cells = cells1.concat(_g);
	console.log("src/Test.hx:13:",cells);
};

I have a little optimizer that removes one-off variables, but it struggles with this piece since the compiler re-uses and re-declares the variables. You can disable it using -D sf_no_opt_auto_var

@ze0nni
Copy link
Author

ze0nni commented Dec 11, 2023

Oh wow! This is a problem haxe js target.
I just will avoid fluent functions calls.
Thanks!

@YellowAfterlife YellowAfterlife added the bug Something isn't working label Dec 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants