[lua] Remove unnecessary do...end wrappers around tail returns#12601
[lua] Remove unnecessary do...end wrappers around tail returns#12601Simn merged 2 commits intoHaxeFoundation:developmentfrom
Conversation
|
This was mostly a cosmetic issue, and I put it off because it would require refactoring how I render function blocks... everywhere. However, the changes here aren't really so complex, the code winds up being much simpler, so I think it's a good change to make even if the only real benefit is cosmetic. |
|
This looks good! It would give some nice cleanup in #12600.
Could we also deal with these? There are still a lot of cases where it is not needed, for example something like: final tmp = 10;
if (tmp == 10) {
return;
}local tmp = 10;
if (tmp == 10) then
do return end;
end; |
|
I added a mutable pcall flag to check the status for that situation. It made the change even simpler, how does it look on your end? |
Looks much cleaner! :) function(v)
local _hx_1_result_status, _hx_1_result_value = _G.pcall(_G.load, [[return function(v)
if ((v <= 2147483647) and (v >= -2147483648)) then
if (v > 0) then
return _G.math.floor(v);
else
return _G.math.ceil(v);
end;
end;
if ((v ~= v) or not ((v > -_G.math.huge) and (v < _G.math.huge))) then
return nil;
end;
local v = v;
if (v > 2251798999999999) then
v = v * 2;
end;
return (v & 0x7FFFFFFF) - (v & 0x80000000);
end]]);
local nativeOperators;
if (_hx_1_result_status and (_hx_1_result_value ~= nil)) then
local fn = _hx_1_result_value;
nativeOperators = fn();
else
nativeOperators = nil;
end;
local clampImpl = (function()
local _hx_2
if (nativeOperators ~= nil) then
_hx_2 = nativeOperators; elseif (_hx_bit_raw == nil) then
_hx_2 = function(v)
if ((v <= 2147483647) and (v >= -2147483648)) then
if (v > 0) then
return _G.math.floor(v);
else
return _G.math.ceil(v);
end;
end;
if ((v ~= v) or not ((v > -_G.math.huge) and (v < _G.math.huge))) then
return nil;
end;
local v = v;
v = (v % 4294967296);
if (v >= 2147483648) then
v = v - 4294967296;
end;
return v;
end; else
_hx_2 = function(v)
if ((v <= 2147483647) and (v >= -2147483648)) then
if (v > 0) then
return _G.math.floor(v);
else
return _G.math.ceil(v);
end;
end;
if ((v ~= v) or not ((v > -_G.math.huge) and (v < _G.math.huge))) then
return nil;
end;
local v = v;
if (v > 2251798999999999) then
v = v * 2;
end;
local band = _hx_bit_raw.band;
return band(v, 2147483647) - _G.math.abs(band(v, 2147483648));
end; end
return _hx_2
end )();
__lua_Boot.clampInt32 = clampImpl;
return clampImpl(v);
endI don't really understand the mutable pcall case, perhaps we could add a test for this to demonstrate it and make sure it doesn't regress? (if there isn't one already) Apart from that this looks good, but it might be good to keep #7350 open (right now it's marked to autoclose) as there are some other generation issues mentioned there unrelated to |
|
This needs conflict resolution. |
|
Yeah good idea. |
b07df7a to
6eff185
Compare
When a return is the last statement in a function body, it doesn't need to be wrapped in do...end. Add a wrap parameter to gen_return and pass false at tail position in all four function body handlers.
Track pcall context with in_pcall flag so do...end is only used where Lua's grammar requires it (inside pcall bodies with sentinel returns).
6eff185 to
fa72152
Compare
Could we still add this test ? Also could we reopen the issue at #7350, this only addresses part of it. |
Summary
do return endwrappers; other codegen issues in that issue remain)returnis the last statement in a function body, thedo...endwrapper is unnecessary. This adds awrapparameter togen_returnand a sharedgen_func_blockhelper that emits barereturnat tail position in all four function body handlers.return.do return endsince the compiler appendsreturn _hx_pcall_defaultafter user code.Test plan
make haxebuilds successfullyreturnat function tail positions in generated outputdo return endstill used for returns inside try-catch (pcall)do...endwrappers (5,897 → 1,198 in test output)