mirror of
https://github.com/Sei-Lisa/LSL-PyOptimizer
synced 2025-07-01 15:48:21 +00:00
Allow returning void expressions where state changes are allowed
In the same places as state changes are allowed, i.e. in places where a parent of the AST node is a WHILE/DO/FOR or an IF without ELSE, it's allowed to use return statements with expressions that return void, e.g. llDie(), provided the function itself is declared as returning void. The construction, when found, is rewritten to '{<void expression>; return;}' because the optimizer is not designed to deal with these monsters. We've renamed the variable SuspiciousStSw to PruneBug, because it's used for both purposes now, though a better name might have been PruneBugPendingChecks, because these are only errors if the IF has an ELSE. We've also added the exception to raise as part of the data stored in the list. Per report by Tonaie Resident.
This commit is contained in:
parent
3cfbbb923c
commit
9d540798b4
5 changed files with 183 additions and 14 deletions
|
@ -1791,7 +1791,8 @@ list lazy_list_set(list L, integer i, list v)
|
|||
if AllowStSw is False:
|
||||
raise EParseCantChangeState(self)
|
||||
if AllowStSw is None:
|
||||
self.SuspiciousStSw.append(self.errorpos)
|
||||
self.PruneBug.append((self.errorpos,
|
||||
EParseCantChangeState))
|
||||
self.NextToken()
|
||||
if self.tok[0] not in ('DEFAULT', 'IDENT'):
|
||||
raise EParseSyntax(self)
|
||||
|
@ -1812,12 +1813,31 @@ list lazy_list_set(list L, integer i, list v)
|
|||
value = None
|
||||
else:
|
||||
savepos = self.errorpos
|
||||
saveAllowVoid = self.allowVoid
|
||||
# Needed due to another LSL bug, see regr/void-in-return.lsl
|
||||
self.allowVoid = True
|
||||
value = self.Parse_expression()
|
||||
self.allowVoid = saveAllowVoid
|
||||
self.expect(';')
|
||||
self.NextToken()
|
||||
if ReturnType is None and value is not None:
|
||||
self.errorpos = savepos
|
||||
raise EParseReturnShouldBeEmpty(self)
|
||||
# It follows the same rules as AllowStSw
|
||||
if AllowStSw is False:
|
||||
self.errorpos = savepos
|
||||
raise EParseReturnShouldBeEmpty(self)
|
||||
elif value.t is None:
|
||||
if AllowStSw is None:
|
||||
self.PruneBug.append((self.errorpos,
|
||||
EParseReturnShouldBeEmpty))
|
||||
self.PushScope()
|
||||
scope = self.scopeindex
|
||||
self.PopScope()
|
||||
return nr(nt='{}', t=None, scope=scope,
|
||||
ch=[nr(nt='EXPR', t=None, ch=[value]),
|
||||
nr(nt='RETURN', t=None)])
|
||||
else:
|
||||
self.errorpos = savepos
|
||||
raise EParseTypeMismatch(self)
|
||||
if ReturnType is not None and value is None:
|
||||
self.errorpos = savepos
|
||||
raise EParseReturnIsEmpty(self)
|
||||
|
@ -1835,22 +1855,22 @@ list lazy_list_set(list L, integer i, list v)
|
|||
ret.ch.append(self.Parse_expression())
|
||||
self.expect(')')
|
||||
self.NextToken()
|
||||
saveSuspiciousStSw = self.SuspiciousStSw
|
||||
self.SuspiciousStSw = []
|
||||
savePruneBug = self.PruneBug
|
||||
self.PruneBug = []
|
||||
ret.ch.append(self.Parse_statement(ReturnType, AllowStSw = None, InsideLoop = InsideLoop))
|
||||
if self.tok[0] == 'ELSE':
|
||||
if AllowStSw is False and self.SuspiciousStSw:
|
||||
self.errorpos = self.SuspiciousStSw[0]
|
||||
raise EParseCantChangeState(self)
|
||||
if AllowStSw is False and self.PruneBug:
|
||||
self.errorpos = self.PruneBug[0][0]
|
||||
raise self.PruneBug[0][1](self)
|
||||
LastIsReturn = getattr(ret.ch[1], 'LIR', False)
|
||||
self.NextToken()
|
||||
ret.ch.append(self.Parse_statement(ReturnType,
|
||||
AllowStSw = AllowStSw, InsideLoop = InsideLoop))
|
||||
if AllowStSw is None:
|
||||
saveSuspiciousStSw += self.SuspiciousStSw
|
||||
savePruneBug += self.PruneBug
|
||||
if LastIsReturn and getattr(ret.ch[2], 'LIR', False):
|
||||
ret.LIR = True
|
||||
self.SuspiciousStSw = saveSuspiciousStSw
|
||||
self.PruneBug = savePruneBug
|
||||
return ret
|
||||
|
||||
if tok0 == 'WHILE':
|
||||
|
@ -2915,8 +2935,10 @@ list lazy_list_set(list L, integer i, list v)
|
|||
# List of preprocessor #line directives.
|
||||
self.linedir = []
|
||||
|
||||
# List of positions with suspicious state change statements.
|
||||
self.SuspiciousStSw = []
|
||||
# List of tuples (position, exception) where suspicious state change
|
||||
# statements or returns with void expressions happen. These can only
|
||||
# be detected when the 'else' is found.
|
||||
self.PruneBug = []
|
||||
|
||||
# This is a small hack to prevent circular definitions in globals when
|
||||
# extended expressions are enabled. When false (default), forward
|
||||
|
|
15
run-tests.py
15
run-tests.py
|
@ -368,8 +368,19 @@ class UnitTestRegression(UnitTestCase):
|
|||
parser.parse('default{timer(){[llDie()];}}')
|
||||
parser.parse('default{timer(){llDie();}}')
|
||||
parser.parse('default{timer(){(llDie());}}')
|
||||
parser.parse('default{timer(){for(llDie();1;llDie());}}'
|
||||
, ('optimize',))
|
||||
parser.parse('default{timer(){for(llDie();1;llDie());}}',
|
||||
('optimize',))
|
||||
# 'return <void expr>' works in the same situations as state changes
|
||||
self.assertRaises(lslparse.EParseReturnShouldBeEmpty, parser.parse,
|
||||
'default{timer(){return llDie();}}')
|
||||
self.assertRaises(lslparse.EParseReturnShouldBeEmpty, parser.parse,
|
||||
'default{timer(){if(1)return llDie();else;}}')
|
||||
self.assertRaises(lslparse.EParseReturnShouldBeEmpty, parser.parse,
|
||||
'default{timer(){if(1);else return llDie();}}')
|
||||
self.assertRaises(lslparse.EParseReturnShouldBeEmpty, parser.parse,
|
||||
'default{timer(){return 1;}}')
|
||||
self.assertRaises(lslparse.EParseTypeMismatch, parser.parse,
|
||||
'default{timer(){if(1)return 1;}}')
|
||||
|
||||
class UnitTestCoverage(UnitTestCase):
|
||||
def test_coverage_misc(self):
|
||||
|
|
41
unit_tests/regression.suite/void-in-return.lsl
Normal file
41
unit_tests/regression.suite/void-in-return.lsl
Normal file
|
@ -0,0 +1,41 @@
|
|||
x(){
|
||||
// All these should work
|
||||
if (1)
|
||||
return llDie();
|
||||
while (1)
|
||||
return llDie();
|
||||
do
|
||||
return llDie();
|
||||
while (1);
|
||||
for (;1;)
|
||||
return llDie();
|
||||
if (1)
|
||||
{
|
||||
return llDie();
|
||||
if (1) return llDie(); else return llDie();
|
||||
return llDie();
|
||||
}
|
||||
}
|
||||
|
||||
default
|
||||
{
|
||||
state_entry()
|
||||
{
|
||||
// Similarly, all these should work
|
||||
if (1)
|
||||
return llDie();
|
||||
while (1)
|
||||
return llDie();
|
||||
do
|
||||
return llDie();
|
||||
while (1);
|
||||
for (;1;)
|
||||
return llDie();
|
||||
if (1)
|
||||
{
|
||||
return llDie();
|
||||
if (1) return llDie(); else return llDie();
|
||||
return llDie();
|
||||
}
|
||||
}
|
||||
}
|
94
unit_tests/regression.suite/void-in-return.out
Normal file
94
unit_tests/regression.suite/void-in-return.out
Normal file
|
@ -0,0 +1,94 @@
|
|||
x()
|
||||
{
|
||||
if (1)
|
||||
{
|
||||
llDie();
|
||||
return;
|
||||
}
|
||||
while (1)
|
||||
{
|
||||
llDie();
|
||||
return;
|
||||
}
|
||||
do
|
||||
{
|
||||
llDie();
|
||||
return;
|
||||
}
|
||||
while (1);
|
||||
for (; 1; )
|
||||
{
|
||||
llDie();
|
||||
return;
|
||||
}
|
||||
if (1)
|
||||
{
|
||||
{
|
||||
llDie();
|
||||
return;
|
||||
}
|
||||
if (1)
|
||||
{
|
||||
llDie();
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
llDie();
|
||||
return;
|
||||
}
|
||||
{
|
||||
llDie();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
default
|
||||
{
|
||||
state_entry()
|
||||
{
|
||||
if (1)
|
||||
{
|
||||
llDie();
|
||||
return;
|
||||
}
|
||||
while (1)
|
||||
{
|
||||
llDie();
|
||||
return;
|
||||
}
|
||||
do
|
||||
{
|
||||
llDie();
|
||||
return;
|
||||
}
|
||||
while (1);
|
||||
for (; 1; )
|
||||
{
|
||||
llDie();
|
||||
return;
|
||||
}
|
||||
if (1)
|
||||
{
|
||||
{
|
||||
llDie();
|
||||
return;
|
||||
}
|
||||
if (1)
|
||||
{
|
||||
llDie();
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
llDie();
|
||||
return;
|
||||
}
|
||||
{
|
||||
llDie();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
1
unit_tests/regression.suite/void-in-return.run
Normal file
1
unit_tests/regression.suite/void-in-return.run
Normal file
|
@ -0,0 +1 @@
|
|||
main.py - -O -optimize
|
Loading…
Add table
Add a link
Reference in a new issue