Skip to content

Commit 81203ad

Browse files
committed
fix Fix inlining
If a local variable shadows an outer one, inlining using bare blocks breaks currently
1 parent 73d34b3 commit 81203ad

2 files changed

Lines changed: 26 additions & 6 deletions

File tree

test/minifier/test_simplify_expressions.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ def test_has_return(self):
8181
"""
8282
self.compare_code(code, expected)
8383

84-
def test_allowed(self):
84+
def _test_allowed(self):
85+
# disabled due to test_shadowing
8586
code = """
8687
function test(a, b)
8788
foo = a + b
@@ -108,7 +109,8 @@ def test_empty_function(self):
108109
expected = "; ; ;"
109110
self.compare_code(code, expected)
110111

111-
def test_correct_names(self):
112+
def _test_correct_names(self):
113+
# disabled due to test_shadowing
112114
code = """
113115
function test(a, b)
114116
foo = a.a + b
@@ -140,6 +142,26 @@ def test_not_a_function(self):
140142
"""
141143
self.compare_code(code, code)
142144

145+
def test_shadowing(self):
146+
# This is the reason why inlining using
147+
code = """
148+
function test(b)
149+
local a = b + 1
150+
print(a, b)
151+
end
152+
a = 1
153+
test(a)
154+
"""
155+
expected = """
156+
; -- residual function
157+
a = 1
158+
(function (b)
159+
local a = b + 1
160+
print(a, b)
161+
end)(a)
162+
"""
163+
self.compare_code(code, expected)
164+
143165

144166
class TestUnOp(BaseClass):
145167
def test_not(self):

tumfl/minifier/simplify_expressions.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
from logging import warning
43
from typing import Optional
54

65
from tumfl.AST import (
@@ -133,9 +132,6 @@ def visit_FunctionCall(self, node: FunctionCall) -> None:
133132
definition,
134133
(FunctionDefinition, LocalFunctionDefinition, ExpFunctionDefinition),
135134
):
136-
warning(
137-
f'Function call to "{node.function!s}" is not a function definition'
138-
)
139135
return
140136
if (
141137
all(
@@ -152,7 +148,9 @@ def visit_FunctionCall(self, node: FunctionCall) -> None:
152148
and all(isinstance(arg, Name) for arg in node.arguments)
153149
and all(isinstance(arg, Name) for arg in definition.parameters)
154150
and not self.has_return(definition.body)
151+
and False
155152
):
153+
# See test_shadowing why this is disabled for now, may be reenabled in the future
156154
# Inlining is quite conservative, inlining only functions that have only name arguments,
157155
# and no return statements (and for obvious reasons, only if there is only 1 callee)
158156
replacements: dict[Name, Name] = {}

0 commit comments

Comments
 (0)