Suboptimal JS code with full DCE

34 views
Skip to first unread message

Jozef Chúťka

unread,
Sep 19, 2016, 4:48:01 AM9/19/16
to Haxe
I noted this issue while running closure complier for haxe generated code:
class Test {
   
static var a(default, set):Int = 0;
   
   
static function main() {
        a
--;
   
}
   
   
static function set_a(value:Int):Int {
       
return a = value;
   
}
}

would produce ( http://try.haxe.org/#f002d ) :
(function (console) { "use strict";
var Test = function() { };
Test.main = function() {
 
var _g = Test;
 
var _g1 = _g.a;
 _g
.set_a(_g1 - 1);
 _g1
;
};
Test.set_a = function(value) {
 
return Test.a = value;
};
Test.a = 0;
Test.main();
})(typeof console != "undefined" ? console : {log:function(){}});

google closure complains:
JSC_USELESS_CODE: Suspicious code. This code lacks side-effects. Is there a bug? at line 7 character 1
 _g1
;
 
^

The problem is the extra line with _g1; , this will get removed with analyzer but I thought the compiler itself should not produce it anyway.

Simon Krajewski

unread,
Sep 19, 2016, 4:53:46 AM9/19/16
to haxe...@googlegroups.com
Yes, this actually gave us trouble with our own diagnostics checker as well: https://github.com/HaxeFoundation/haxe/issues/5581

I closed this issue after ignoring non-user variables, but now that I think about it again it might indeed be prudent to not generate the value in the first place. This requires keeping track of whether or not we expect a value when typing the unop.

Simon
Reply all
Reply to author
Forward
0 new messages